View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math4.core.jdkmath;
18  
19  import java.util.List;
20  import java.util.ArrayList;
21  import java.lang.reflect.Method;
22  import java.lang.reflect.Modifier;
23  import java.lang.reflect.Type;
24  
25  import org.junit.Test;
26  import org.junit.Assert;
27  
28  /**
29   * Tests for {@link JdkMath}.
30   */
31  public class JdkMathTest {
32      /** Separator. */
33      private static final String LINE_SEP = System.lineSeparator();
34  
35      /**
36       * Drop-in replacement of {@link Math} with {@link JdkMath} is only valid
37       * for Java 8.
38       * Test should be updated when {@code AccurateMath} implements functions
39       * added in Java 9+.
40       */
41      @Test
42      public void checkMissingMethods() {
43          final String runtimeVersion = System.getProperty("java.runtime.version");
44          final boolean doTest = runtimeVersion.matches("^1\\.8\\..*");
45          org.junit.Assume.assumeTrue(doTest);
46  
47          final List<String> notFound = compareClassMethods(StrictMath.class,
48                                                            JdkMath.class);
49          if (!notFound.isEmpty()) {
50              final StringBuilder sb = new StringBuilder();
51              sb.append("JdkMath is missing the following StrictMath methods:");
52              for (String m : notFound) {
53                  sb.append(LINE_SEP).append(m);
54              }
55              Assert.fail(sb.toString());
56          }
57      }
58  
59      /**
60       * @param class1 Reference implementation.
61       * @param class2 Alternate implementation.
62       * @return the methods defined in {@code class1} that are not in {@code class2}.
63       */
64      private List<String> compareClassMethods(Class<?> class1,
65                                               Class<?> class2) {
66          final List<String> notFound = new ArrayList<>();
67          for (Method method1 : class1.getDeclaredMethods()) {
68              if (Modifier.isPublic(method1.getModifiers())) {
69                  final Type[] params = method1.getGenericParameterTypes();
70                  try {
71                      class2.getDeclaredMethod(method1.getName(), (Class[]) params);
72                  } catch (NoSuchMethodException e) {
73                      notFound.add(method1.toString());
74                  }
75              }
76          }
77  
78          return notFound;
79      }
80  }