Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Refactoring Java tests: 1-5
  • Loading branch information
mperor committed Oct 27, 2025
commit 77142ec6770d47242f974feb9e2105282bae9dc9
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
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
9 changes: 4 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,7 +34,7 @@ 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);
Expand All @@ -44,12 +43,12 @@ public void testNewInputOutputAkaNIO() throws IOException {
@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 +88,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 +124,4 @@ private void handleConnectedClient(ServerSocket serverSocket) throws IOException
}
}


}
2 changes: 1 addition & 1 deletion 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