Skip to content

Commit 77142ec

Browse files
committed
Refactoring Java tests: 1-5
1 parent 479e2d8 commit 77142ec

File tree

5 files changed

+14
-15
lines changed

5 files changed

+14
-15
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void testJavaBean() throws IOException, ClassNotFoundException {
5555
}
5656

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

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

112112
executor.shutdown();
113113
}
114114

115115
interface HelloService extends Remote {
116-
String sayHello() throws RemoteException;
116+
String getMessage() throws RemoteException;
117117
}
118118

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

124124
@Override
125-
public String sayHello() throws RemoteException {
125+
public String getMessage() throws RemoteException {
126126
return "Hello World!";
127127
}
128128
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ public void testJavaNamingAndDirectoryInterfaceAkaJNDILookup() throws NamingExce
2727
String bindingName = "hello";
2828
String bindingResult = "Hello World!";
2929

30-
Context context = new InitialContext();
31-
context.bind(bindingName, bindingResult);
32-
String lookupResult = (String) context.lookup(bindingName);
30+
Context ctx = new InitialContext();
31+
ctx.bind(bindingName, bindingResult);
32+
String lookupResult = (String) ctx.lookup(bindingName);
3333
Assertions.assertEquals(bindingResult, lookupResult);
3434

35-
context.unbind(bindingName);
35+
ctx.unbind(bindingName);
3636
Assertions.assertThrows(NamingException.class, () -> {
37-
context.lookup(bindingName);
37+
ctx.lookup(bindingName);
3838
});
3939
}
4040

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.nio.channels.SocketChannel;
1313
import java.nio.file.Files;
1414
import java.nio.file.Path;
15-
import java.nio.file.Paths;
1615
import java.util.concurrent.CountDownLatch;
1716
import java.util.concurrent.Executors;
1817
import java.util.logging.Logger;
@@ -35,7 +34,7 @@ public void testAssertionKeyWord() {
3534

3635
@Test
3736
public void testNewInputOutputAkaNIO() throws IOException {
38-
Path path = Paths.get("src", "test", "resources", "nio.txt");
37+
Path path = Path.of("src", "test", "resources", "nio.txt");
3938
byte[] fileBytes = Files.readAllBytes(path);
4039
String content = new String(fileBytes);
4140
Assertions.assertEquals("Hello NIO!", content);
@@ -44,12 +43,12 @@ public void testNewInputOutputAkaNIO() throws IOException {
4443
@Test
4544
public void testRegex() {
4645
Pattern pattern = Pattern.compile("<title>(.*)</title>", Pattern.DOTALL);
47-
Matcher matcher = pattern.matcher(getTestHtml());
46+
Matcher matcher = pattern.matcher(createTestHtml());
4847
Assertions.assertTrue(matcher.find());
4948
Assertions.assertEquals("Title", matcher.group(1));
5049
}
5150

52-
private String getTestHtml() {
51+
private String createTestHtml() {
5352
return """
5453
<!DOCTYPE html>
5554
<html lang="en">
@@ -89,6 +88,7 @@ public void testImageIO() throws IOException {
8988
BufferedImage image = ImageIO.read(new File("src/test/resources/imageio.png"));
9089
Assertions.assertTrue(ImageIO.write(image, "jpg", File.createTempFile("imageio", ".jpg")));
9190
}
91+
9292
@Test
9393
public void testServerClientSocketChannel() throws IOException, InterruptedException {
9494
int port = 8004;
@@ -124,5 +124,4 @@ private void handleConnectedClient(ServerSocket serverSocket) throws IOException
124124
}
125125
}
126126

127-
128127
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void testGenerics() {
3131
Box rawBox = new Box();
3232
rawBox.setContent("Hello").setContent(1);
3333
Assertions.assertThrows(ClassCastException.class, () -> {
34-
String rawBoxContent = (String) rawBox.getContent();
34+
String wrongTypeContent = (String) rawBox.getContent();
3535
});
3636
Integer rawBoxContent = (Integer) rawBox.getContent();
3737
Assertions.assertEquals(1, rawBoxContent);

0 commit comments

Comments
 (0)