|
31 | 31 | * @run testng RemovedJDKInternals |
32 | 32 | */ |
33 | 33 |
|
| 34 | +import java.io.IOException; |
34 | 35 | import java.nio.file.Path; |
35 | 36 | import java.nio.file.Paths; |
| 37 | +import java.util.Arrays; |
| 38 | +import java.util.LinkedHashMap; |
| 39 | +import java.util.List; |
36 | 40 | import java.util.Map; |
| 41 | +import java.util.regex.Matcher; |
| 42 | +import java.util.regex.Pattern; |
37 | 43 |
|
38 | 44 | import com.sun.tools.jdeps.DepsAnalyzer; |
39 | 45 | import com.sun.tools.jdeps.Graph; |
|
42 | 48 | import org.testng.annotations.Test; |
43 | 49 |
|
44 | 50 | import static org.testng.Assert.assertEquals; |
| 51 | +import static org.testng.Assert.assertFalse; |
45 | 52 | import static org.testng.Assert.assertTrue; |
46 | 53 |
|
47 | 54 | public class RemovedJDKInternals { |
@@ -115,6 +122,65 @@ public void runTest(String name, ModuleMetaData data) throws Exception { |
115 | 122 | } |
116 | 123 | } |
117 | 124 |
|
| 125 | + private static final List<String> REMOVED_APIS = List.of( |
| 126 | + "com.sun.image.codec.jpeg.JPEGCodec", |
| 127 | + "sun.misc.Service", |
| 128 | + "sun.misc.SoftCache", |
| 129 | + "sun.reflect.Reflection" |
| 130 | + ); |
| 131 | + private static final String REMOVED_INTERNAL_API = "JDK removed internal API"; |
| 132 | + |
| 133 | + |
| 134 | + @Test |
| 135 | + public void removedInternalJDKs() throws IOException { |
| 136 | + // verify the JDK removed internal API |
| 137 | + JdepsRunner summary = JdepsRunner.run("-summary", CLASSES_DIR.toString()); |
| 138 | + Arrays.stream(summary.output()).map(l -> l.split(" -> ")) |
| 139 | + .map(a -> a[1]).filter(n -> n.equals(REMOVED_INTERNAL_API)) |
| 140 | + .findFirst().orElseThrow(); |
| 141 | + |
| 142 | + JdepsRunner jdeps = JdepsRunner.run("-verbose:class", CLASSES_DIR.toString()); |
| 143 | + String output = jdeps.stdout.toString(); |
| 144 | + Map<String, String> result = findDeps(output); |
| 145 | + for (String cn : result.keySet()) { |
| 146 | + String name = result.get(cn); |
| 147 | + if (REMOVED_APIS.contains(cn)) { |
| 148 | + assertEquals(name, REMOVED_INTERNAL_API); |
| 149 | + } else if (cn.startsWith("sun.reflect")){ |
| 150 | + assertEquals(name, "JDK internal API (jdk.unsupported)"); |
| 151 | + } else { |
| 152 | + assertEquals(name, "java.base"); |
| 153 | + } |
| 154 | + } |
| 155 | + REMOVED_APIS.stream().map(result::containsKey).allMatch(b -> b); |
| 156 | + } |
| 157 | + |
| 158 | + // Pattern used to parse lines |
| 159 | + private static final Pattern linePattern = Pattern.compile(".*\r?\n"); |
| 160 | + private static final Pattern pattern = Pattern.compile("\\s+ -> (\\S+) +(.*)"); |
| 161 | + |
| 162 | + // Use the linePattern to break the given String into lines, applying |
| 163 | + // the pattern to each line to see if we have a match |
| 164 | + private static Map<String, String> findDeps(String out) { |
| 165 | + Map<String, String> result = new LinkedHashMap<>(); |
| 166 | + Matcher lm = linePattern.matcher(out); // Line matcher |
| 167 | + Matcher pm = null; // Pattern matcher |
| 168 | + int lines = 0; |
| 169 | + while (lm.find()) { |
| 170 | + lines++; |
| 171 | + CharSequence cs = lm.group(); // The current line |
| 172 | + if (pm == null) |
| 173 | + pm = pattern.matcher(cs); |
| 174 | + else |
| 175 | + pm.reset(cs); |
| 176 | + if (pm.find()) |
| 177 | + result.put(pm.group(1), pm.group(2).trim()); |
| 178 | + if (lm.end() == out.length()) |
| 179 | + break; |
| 180 | + } |
| 181 | + return result; |
| 182 | + } |
| 183 | + |
118 | 184 | private static final Map<String, String> REPLACEMENTS = Map.of( |
119 | 185 | "com.sun.image.codec.jpeg.JPEGCodec", "Use javax.imageio @since 1.4", |
120 | 186 | "sun.misc.Service", "Use java.util.ServiceLoader @since 1.6", |
|
0 commit comments