/** * Build a URLClassLoader that only includes the null-parent-classloader artifact in its classpath, * along with the bootstrap class loader as its parent. * * @throws MalformedURLException * on failure to build the classpath URL */ private URLClassLoader buildLoader(TestReporter reporter) throws MalformedURLException { String testRootFolder = NullParentClassLoaderIT.class.getResource("/").getPath(); reporter.publishEntry("Test classes folder", testRootFolder); Path projectJar = Paths .get(testRootFolder) .getParent() .resolve("class-loading-1.0-SNAPSHOT.jar"); assumeTrue( projectJar.toFile().exists(), "Project JAR must exist as " + projectJar.toString() + " for test to be executed."); reporter.publishEntry("Project JAR", projectJar.toString()); URL path[] = { projectJar.toUri().toURL() }; // this is the parent that is required when running under Java 9: // ClassLoader parent = ClassLoader.getPlatformClassLoader(); ClassLoader parent = null; URLClassLoader loader = new URLClassLoader(path, parent); reporter.publishEntry("Class loader", loader.toString()); return loader; }
@ParameterizedTest(name = "loading {0}") @MethodSource(value = "classNames") public void loadJdkClass(String className, TestReporter reporter) throws ClassNotFoundException { TestClassLoader classLoader = new TestClassLoader(); try { Class c = classLoader.loadClass(className); reporter.publishEntry(className, "visible"); // the assertion is pretty useless, but if `c` would not be used, // dead code elimination might remove it assertThat(c.getName()).isEqualTo(className); } catch (ClassNotFoundException ex) { reporter.publishEntry(className, "not visible"); throw ex; } }
@Test void reportSeveralValues(TestReporter testReporter) { HashMap<String, String> values = new HashMap<>(); values.put("name", "john"); values.put("surname", "doe"); testReporter.publishEntry(values); }
/** * Attempt to load a class from the URLClassLoader that references a java.sql.* class. The * java.sql.* package is one of those not visible to the Java 9 bootstrap class loader that * was visible to the Java 8 bootstrap class loader. */ @Test public void loadSqlDateUsingNullParent(TestReporter reporter) throws Exception { URLClassLoader loader = buildLoader(reporter); Class<?> jsqlUserClass = loader.loadClass("wtf.java9.class_loading.JavaSqlUser"); reporter.publishEntry("Loaded class", jsqlUserClass.toString()); Object jsqlUser = jsqlUserClass.getConstructor().newInstance(); reporter.publishEntry("Created instance", jsqlUser.toString()); loader.close(); }
@Test void doesNotAddEmptyLines(TestReporter reporter) throws Exception { Lines transformation = transform(parse(INITIAL_XML)); reporter.publishEntry("Transformed XML", "\n" + transformation); assertThat(transformation.lineAt(2).trim()).isNotEmpty(); }
@Test // expected to pass on Java 9 void pushesRootNodeToUnindentedNewLine(TestReporter reporter) throws Exception { Lines transformation = transform(parse(INITIAL_XML)); reporter.publishEntry("Transformed XML", "\n" + transformation); assertThat(transformation.lineAt(0)).doesNotContain("<root"); assertThat(transformation.lineAt(1)).startsWith("<root"); }
@Test // expected to fail on Java 9 because it puts in new lines void doesNotAddEmptyLines(TestReporter reporter) throws Exception { Lines transformation = transform(parse(INITIAL_XML)); reporter.publishEntry("Transformed XML", "\n" + transformation); assertThat(transformation.lineAt(2).trim()).isNotEmpty(); }
@Test // expected to fail on Java 9 because it reformats existing lines void keepsIndentationOfUnchangedNodes(TestReporter reporter) throws Exception { Lines transformation = transform(parse(INITIAL_XML)); reporter.publishEntry("Transformed XML", "\n" + transformation); assertThat(transformation.lineWith("<node")).startsWith(" <node"); }
@Test // expected to pass on Java 9 because new nodes are always correctly indented void newNodesAreIndented(TestReporter reporter) throws Exception { Document document = parse(INITIAL_XML); setChildNode(document, "node", "inner", "inner node content"); Lines transformation = transform(document); reporter.publishEntry("Transformed XML", "\n" + transformation); assertThat(transformation.lineWith("<inner>")).isEqualTo(" <inner>inner node content</inner>"); }
@Test // expected to fail on Java 9 because it puts CDATA on its own line void cDataIsInline(TestReporter reporter) throws Exception { Document document = parse(INITIAL_XML); setCDataContent(document, "node", "cdata content"); Lines transformation = transform(document); reporter.publishEntry("Transformed XML", "\n" + transformation); assertThat(transformation.lineWith("CDATA")).endsWith("<node><![CDATA[cdata content]]></node>"); }
@Test void junitAndSpringMethodInjectionCombined(@Autowired Cat kittyCat, TestInfo testInfo, ApplicationContext context, TestReporter testReporter) { assertNotNull(testInfo, "TestInfo should have been injected by JUnit"); assertNotNull(testReporter, "TestReporter should have been injected by JUnit"); assertNotNull(context, "ApplicationContext should have been injected by Spring"); assertNotNull(kittyCat, "Cat should have been @Autowired by Spring"); }
@DataProvider static Object[][] loadFromExternalFile(TestInfo testInfo, TestReporter testReporter) { checkNotNull(testInfo, "'testInfo' is not set"); checkNotNull(testReporter, "'testReporter' is not set"); String testDataFile = testInfo.getTestMethod().get().getAnnotation(ExternalFile.class).value(); // Load the data from the external file here ... return new Object[][] { { testDataFile } }; }
@Test void reportSingleValue(TestReporter testReporter) { testReporter.publishEntry("key", "value"); }
@ParameterizedTest @ValueSource(strings = { "Hello", "JUnit" }) void withOtherParams(String word, TestInfo info, TestReporter reporter) { reporter.publishEntry(info.getDisplayName(), "Word: " + word); assertNotNull(word); }