-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathServidorTest.java
More file actions
149 lines (129 loc) · 5.57 KB
/
ServidorTest.java
File metadata and controls
149 lines (129 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package br.unesp.sjrp.httpserver;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.mock;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
class ServidorTest {
@Test
void testMainClassExists() {
// This test simply verifies that the Servidor class exists and can be instantiated
// The main method is difficult to test directly due to its infinite loop and socket binding
assertDoesNotThrow(() -> {
Servidor servidor = new Servidor();
assertNotNull(servidor);
});
}
@Test
void testPortAvailability() {
// Test that we can create a ServerSocket on a different port
// This verifies the basic ServerSocket functionality used in main()
try (ServerSocket testSocket = new ServerSocket(0)) { // Use port 0 for automatic assignment
assertNotNull(testSocket);
assertTrue(testSocket.getLocalPort() > 0);
assertFalse(testSocket.isClosed());
} catch (IOException e) {
fail("Should be able to create ServerSocket: " + e.getMessage());
}
}
@Test
void testThreadPoolConcept() {
// Test that ExecutorService can be created (simulating what main() does)
java.util.concurrent.ExecutorService pool = null;
try {
pool = java.util.concurrent.Executors.newFixedThreadPool(20);
assertNotNull(pool);
assertFalse(pool.isShutdown());
} finally {
if (pool != null) {
pool.shutdown();
}
}
}
@Test
void testMainMethodExists() {
// Verify that the main method exists and can be called with null args
// We can't test the actual execution due to the infinite loop, but we can verify it exists
try {
// Get the main method
java.lang.reflect.Method mainMethod = Servidor.class.getMethod("main", String[].class);
assertNotNull(mainMethod);
// Verify it's public and static
assertTrue(java.lang.reflect.Modifier.isPublic(mainMethod.getModifiers()));
assertTrue(java.lang.reflect.Modifier.isStatic(mainMethod.getModifiers()));
} catch (NoSuchMethodException e) {
fail("main method should exist in Servidor class");
}
}
@Test
void testServerSocketCreationOnPort8000() {
// Test that we can verify port 8000 behavior
// We can't actually bind to 8000 since it might be in use, but we can test the concept
boolean canCreateSocket = false;
try (ServerSocket testSocket = new ServerSocket(0)) { // Use any available port
canCreateSocket = true;
// Verify basic socket properties that would be used in main()
assertNotNull(testSocket);
assertTrue(testSocket.getLocalPort() > 0);
assertFalse(testSocket.isClosed());
assertFalse(testSocket.isBound() == false); // Should be bound
} catch (IOException e) {
// If we can't create any socket, that's a problem
fail("Should be able to create ServerSocket: " + e.getMessage());
}
assertTrue(canCreateSocket);
}
@Test
void testDefaultConstructor() {
// Test that Servidor can be instantiated with default constructor
Servidor servidor = new Servidor();
assertNotNull(servidor);
// Verify the class itself
assertEquals("Servidor", servidor.getClass().getSimpleName());
assertEquals("br.unesp.sjrp.httpserver.Servidor", servidor.getClass().getName());
}
@Test
void testClassStructure() {
// Test class structure and properties
assertEquals("Servidor", Servidor.class.getSimpleName());
assertEquals("br.unesp.sjrp.httpserver.Servidor", Servidor.class.getName());
// Verify it's a public class
assertTrue(java.lang.reflect.Modifier.isPublic(Servidor.class.getModifiers()));
// Verify it's not abstract or interface
assertFalse(java.lang.reflect.Modifier.isAbstract(Servidor.class.getModifiers()));
assertFalse(Servidor.class.isInterface());
}
@Test
void testMultipleInstances() {
// Test that multiple Servidor instances can be created
Servidor servidor1 = new Servidor();
Servidor servidor2 = new Servidor();
assertNotNull(servidor1);
assertNotNull(servidor2);
assertNotSame(servidor1, servidor2);
}
@Test
void testObjectMethods() {
// Test basic Object methods
Servidor servidor = new Servidor();
assertNotNull(servidor.toString());
assertEquals(servidor.hashCode(), servidor.hashCode()); // Consistent
assertTrue(servidor.equals(servidor)); // Reflexive
assertFalse(servidor.equals(null));
assertFalse(servidor.equals("not a Servidor"));
}
@Test
void testThreadConexaoInstantiation() {
// Test that ThreadConexao can be instantiated (simulating what main() does)
try (ServerSocket testSocket = new ServerSocket(0)) {
// This tests the concept used in main() method
Socket clientSocket = mock(Socket.class);
ThreadConexao threadConexao = new ThreadConexao(clientSocket);
assertNotNull(threadConexao);
assertTrue(threadConexao instanceof Runnable);
} catch (IOException e) {
fail("Should be able to create test socket: " + e.getMessage());
}
}
}