Skip to content

Commit bc66cba

Browse files
committed
Update test classes for Java 4-17
- Refactor Java 6 tests: improve dynamic compilation and add assertions for class accessibility - Adjust wait time in Java 5 concurrency test - Correct file path in Java 17 deserialization test - Remove redundant whitespace in Java 4 file read test - Refactor functional interface declaration in Java 8 tests
1 parent 77142ec commit bc66cba

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

JavaReleases/src/test/java/pl/mperor/lab/java/Java17.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public void testRmiActivationRemoved() {
146146

147147
@Test
148148
public void testDeserializationFilters() throws IOException, ClassNotFoundException {
149-
var file = new File("src/test/resources/bean");
149+
var file = new File("src/test/resources/bean.bin");
150150

151151
var basePackageFilter = ObjectInputFilter.Config.createFilter("java.base/*;!*");
152152
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {

JavaReleases/src/test/java/pl/mperor/lab/java/Java4.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void testNewInputOutputAkaNIO() throws IOException {
3737
Path path = Path.of("src", "test", "resources", "nio.txt");
3838
byte[] fileBytes = Files.readAllBytes(path);
3939
String content = new String(fileBytes);
40+
4041
Assertions.assertEquals("Hello NIO!", content);
4142
}
4243

JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void testScheduledExecutor() throws InterruptedException {
124124
latch.countDown();
125125
}, 0, 100, TimeUnit.MILLISECONDS);
126126

127-
boolean completed = latch.await(300, TimeUnit.MILLISECONDS);
127+
boolean completed = latch.await(200, TimeUnit.MILLISECONDS);
128128
Assertions.assertTrue(completed, "Task did not execute twice in time");
129129
Assertions.assertEquals(2, counter.get());
130130

JavaReleases/src/test/java/pl/mperor/lab/java/Java6.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
5+
import pl.mperor.lab.common.TestUtils;
56

67
import javax.script.ScriptEngine;
78
import javax.script.ScriptEngineManager;
@@ -11,6 +12,10 @@
1112
import java.awt.*;
1213
import java.io.File;
1314
import java.io.IOException;
15+
import java.lang.reflect.InvocationTargetException;
16+
import java.net.MalformedURLException;
17+
import java.net.URL;
18+
import java.net.URLClassLoader;
1419
import java.nio.file.Files;
1520

1621
/**
@@ -32,24 +37,36 @@ public void testScriptingLanguageSupport() throws ScriptException {
3237
}
3338

3439
@Test
35-
public void testJavaDynamicCompilation() throws IOException {
36-
File sourceFile = new File("./HelloWorld.java");
40+
public void testJavaDynamicCompilation() throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
41+
File sourceFile = new File("HelloWorld.java");
3742
String javaSourceCode = """
3843
public class HelloWorld {
39-
public static void main(String[] args) {
40-
System.out.println("Hello World!");
44+
public void sayHello() {
45+
System.out.print("Hello World!");
4146
}
4247
}
4348
""";
4449
Files.write(sourceFile.toPath(), javaSourceCode.getBytes());
4550

4651
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
4752
Assertions.assertNotNull(compiler, "JavaCompiler should not be null");
48-
4953
int compilationResult = compiler.run(null, null, null, sourceFile.getAbsolutePath());
5054
Assertions.assertEquals(0, compilationResult, "Compilation should succeed with result 0!");
55+
assertHelloWorldClassAccessible();
56+
5157
Assertions.assertTrue(sourceFile.delete(), "Source file should be deleted after compilation");
52-
Assertions.assertTrue(new File("./HelloWorld.class").delete(), "Now compilation result can be deleted!");
58+
Assertions.assertTrue(new File("HelloWorld.class").delete(), "Now compilation result can be deleted!");
59+
}
60+
61+
private void assertHelloWorldClassAccessible() throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
62+
URL[] urls = {new File("").toURI().toURL()};
63+
URLClassLoader loader = new URLClassLoader(urls);
64+
Class<?> clazz = loader.loadClass("HelloWorld");
65+
Object instance = clazz.getDeclaredConstructor().newInstance();
66+
var readableOut = TestUtils.setTempSystemOut();
67+
clazz.getDeclaredMethod("sayHello").invoke(instance);
68+
Assertions.assertEquals(readableOut.all(), "Hello World!");
69+
TestUtils.resetSystemOut();
5370
}
5471

5572
@Test

JavaReleases/src/test/java/pl/mperor/lab/java/Java8.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ public void testCustomFunctionalInterface() {
7272
assertFunctionalInterface(Testable.class);
7373
}
7474

75+
@FunctionalInterface
76+
public interface Testable {
77+
void test();
78+
}
79+
7580
private static void assertFunctionalInterface(Class<?> clazz) {
7681
if (!clazz.isInterface()) {
7782
Assertions.fail("Clazz is not an interface!");
@@ -86,11 +91,6 @@ private static void assertFunctionalInterface(Class<?> clazz) {
8691
"Functional interface should contain exactly one abstract method, but @FunctionalInterface is optional!");
8792
}
8893

89-
@FunctionalInterface
90-
public interface Testable {
91-
void test();
92-
}
93-
9494
@Test
9595
public void testDefaultAndStaticMethodsInInterface() throws NoSuchMethodException {
9696
Tester tester = () -> new Testable[]{

0 commit comments

Comments
 (0)