Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java1.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void testJavaBean() throws IOException, ClassNotFoundException {
}

private void assertJavaBeanSerializationAndDeserialization(JavaBean bean) throws IOException, ClassNotFoundException {
var file = new File("src/test/resources/bean");
var file = new File("src/test/resources/bean.bin");
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file))) {
out.writeObject(bean);
}
Expand Down Expand Up @@ -107,13 +107,13 @@ public void testRemoteMethodInvocationAkaRMI() throws RemoteException, NotBoundE
HelloService stub = (HelloService) registry.lookup("HelloService");

// Call the remote method and verify the result
Assertions.assertEquals("Hello World!", stub.sayHello());
Assertions.assertEquals("Hello World!", stub.getMessage());

executor.shutdown();
}

interface HelloService extends Remote {
String sayHello() throws RemoteException;
String getMessage() throws RemoteException;
}

static class HelloServiceImpl extends UnicastRemoteObject implements HelloService {
Expand All @@ -122,7 +122,7 @@ protected HelloServiceImpl() throws RemoteException {
}

@Override
public String sayHello() throws RemoteException {
public String getMessage() throws RemoteException {
return "Hello World!";
}
}
Expand Down
2 changes: 1 addition & 1 deletion JavaReleases/src/test/java/pl/mperor/lab/java/Java17.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void testRmiActivationRemoved() {

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

var basePackageFilter = ObjectInputFilter.Config.createFilter("java.base/*;!*");
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(file))) {
Expand Down
10 changes: 5 additions & 5 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java3.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ public void testJavaNamingAndDirectoryInterfaceAkaJNDILookup() throws NamingExce
String bindingName = "hello";
String bindingResult = "Hello World!";

Context context = new InitialContext();
context.bind(bindingName, bindingResult);
String lookupResult = (String) context.lookup(bindingName);
Context ctx = new InitialContext();
ctx.bind(bindingName, bindingResult);
String lookupResult = (String) ctx.lookup(bindingName);
Assertions.assertEquals(bindingResult, lookupResult);

context.unbind(bindingName);
ctx.unbind(bindingName);
Assertions.assertThrows(NamingException.class, () -> {
context.lookup(bindingName);
ctx.lookup(bindingName);
});
}

Expand Down
10 changes: 5 additions & 5 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java4.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.nio.channels.SocketChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.logging.Logger;
Expand All @@ -35,21 +34,22 @@ public void testAssertionKeyWord() {

@Test
public void testNewInputOutputAkaNIO() throws IOException {
Path path = Paths.get("src", "test", "resources", "nio.txt");
Path path = Path.of("src", "test", "resources", "nio.txt");
byte[] fileBytes = Files.readAllBytes(path);
String content = new String(fileBytes);

Assertions.assertEquals("Hello NIO!", content);
}

@Test
public void testRegex() {
Pattern pattern = Pattern.compile("<title>(.*)</title>", Pattern.DOTALL);
Matcher matcher = pattern.matcher(getTestHtml());
Matcher matcher = pattern.matcher(createTestHtml());
Assertions.assertTrue(matcher.find());
Assertions.assertEquals("Title", matcher.group(1));
}

private String getTestHtml() {
private String createTestHtml() {
return """
<!DOCTYPE html>
<html lang="en">
Expand Down Expand Up @@ -89,6 +89,7 @@ public void testImageIO() throws IOException {
BufferedImage image = ImageIO.read(new File("src/test/resources/imageio.png"));
Assertions.assertTrue(ImageIO.write(image, "jpg", File.createTempFile("imageio", ".jpg")));
}

@Test
public void testServerClientSocketChannel() throws IOException, InterruptedException {
int port = 8004;
Expand Down Expand Up @@ -124,5 +125,4 @@ private void handleConnectedClient(ServerSocket serverSocket) throws IOException
}
}


}
4 changes: 2 additions & 2 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java5.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testGenerics() {
Box rawBox = new Box();
rawBox.setContent("Hello").setContent(1);
Assertions.assertThrows(ClassCastException.class, () -> {
String rawBoxContent = (String) rawBox.getContent();
String wrongTypeContent = (String) rawBox.getContent();
});
Integer rawBoxContent = (Integer) rawBox.getContent();
Assertions.assertEquals(1, rawBoxContent);
Expand Down Expand Up @@ -124,7 +124,7 @@ public void testScheduledExecutor() throws InterruptedException {
latch.countDown();
}, 0, 100, TimeUnit.MILLISECONDS);

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

Expand Down
29 changes: 23 additions & 6 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java6.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import pl.mperor.lab.common.TestUtils;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
Expand All @@ -11,6 +12,10 @@
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;

/**
Expand All @@ -32,24 +37,36 @@ public void testScriptingLanguageSupport() throws ScriptException {
}

@Test
public void testJavaDynamicCompilation() throws IOException {
File sourceFile = new File("./HelloWorld.java");
public void testJavaDynamicCompilation() throws IOException, ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException {
File sourceFile = new File("HelloWorld.java");
String javaSourceCode = """
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
public void sayHello() {
System.out.print("Hello World!");
}
}
""";
Files.write(sourceFile.toPath(), javaSourceCode.getBytes());

JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
Assertions.assertNotNull(compiler, "JavaCompiler should not be null");

int compilationResult = compiler.run(null, null, null, sourceFile.getAbsolutePath());
Assertions.assertEquals(0, compilationResult, "Compilation should succeed with result 0!");
assertHelloWorldClassAccessible();

Assertions.assertTrue(sourceFile.delete(), "Source file should be deleted after compilation");
Assertions.assertTrue(new File("./HelloWorld.class").delete(), "Now compilation result can be deleted!");
Assertions.assertTrue(new File("HelloWorld.class").delete(), "Now compilation result can be deleted!");
}

private void assertHelloWorldClassAccessible() throws MalformedURLException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
URL[] urls = {new File("").toURI().toURL()};
URLClassLoader loader = new URLClassLoader(urls);
Class<?> clazz = loader.loadClass("HelloWorld");
Object instance = clazz.getDeclaredConstructor().newInstance();
var readableOut = TestUtils.setTempSystemOut();
clazz.getDeclaredMethod("sayHello").invoke(instance);
Assertions.assertEquals(readableOut.all(), "Hello World!");
TestUtils.resetSystemOut();
}

@Test
Expand Down
10 changes: 5 additions & 5 deletions JavaReleases/src/test/java/pl/mperor/lab/java/Java8.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public void testCustomFunctionalInterface() {
assertFunctionalInterface(Testable.class);
}

@FunctionalInterface
public interface Testable {
void test();
}

private static void assertFunctionalInterface(Class<?> clazz) {
if (!clazz.isInterface()) {
Assertions.fail("Clazz is not an interface!");
Expand All @@ -86,11 +91,6 @@ private static void assertFunctionalInterface(Class<?> clazz) {
"Functional interface should contain exactly one abstract method, but @FunctionalInterface is optional!");
}

@FunctionalInterface
public interface Testable {
void test();
}

@Test
public void testDefaultAndStaticMethodsInInterface() throws NoSuchMethodException {
Tester tester = () -> new Testable[]{
Expand Down
Loading