Skip to content

Commit 6a02e11

Browse files
committed
chore: add unit tests
1 parent 8e0ce51 commit 6a02e11

13 files changed

+1327
-178
lines changed

src/main/java/com/taosdata/jdbc/AbstractResultSet.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ public SQLWarning getWarnings() throws SQLException {
168168
public void clearWarnings() throws SQLException {
169169
if (isClosed())
170170
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED);
171-
172171
}
173172

174173
@Override
@@ -183,7 +182,7 @@ public Object getObject(String columnLabel) throws SQLException {
183182

184183
@Override
185184
public Reader getCharacterStream(int columnIndex) throws SQLException {
186-
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
185+
return notSupportedMethod();
187186
}
188187

189188
@Override
@@ -200,7 +199,8 @@ public BigDecimal getBigDecimal(String columnLabel) throws SQLException {
200199
public void setFetchDirection(int direction) throws SQLException {
201200
if (isClosed())
202201
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_RESULTSET_CLOSED);
203-
//nothing to do
202+
if (direction != ResultSet.FETCH_FORWARD)
203+
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_INVALID_VARIABLE);
204204
}
205205

206206
@Override

src/main/java/com/taosdata/jdbc/EmptyResultSet.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,6 @@ public boolean isLast() throws SQLException {
265265
return false;
266266
}
267267

268-
@Override
269-
public void beforeFirst() throws SQLException {
270-
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
271-
}
272-
273-
@Override
274-
public void afterLast() throws SQLException {
275-
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
276-
}
277-
278268
@Override
279269
public boolean first() throws SQLException {
280270
return false;
@@ -305,11 +295,6 @@ public boolean previous() throws SQLException {
305295
return false;
306296
}
307297

308-
@Override
309-
public void setFetchDirection(int direction) throws SQLException {
310-
throw TSDBError.createSQLException(TSDBErrorNumbers.ERROR_UNSUPPORTED_METHOD);
311-
}
312-
313298
@Override
314299
public int getFetchDirection() throws SQLException {
315300
return ResultSet.FETCH_FORWARD;

src/test/java/com/taosdata/jdbc/EmptyResultSetTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.taosdata.jdbc;
22

3+
import org.junit.Assert;
34
import org.junit.Before;
45
import org.junit.Test;
56

@@ -240,9 +241,10 @@ public void testPrevious() throws SQLException {
240241
assertFalse(emptyResultSet.previous());
241242
}
242243

243-
@Test(expected = SQLException.class)
244+
@Test
244245
public void testSetFetchDirection() throws SQLException {
245246
emptyResultSet.setFetchDirection(ResultSet.FETCH_FORWARD);
247+
Assert.assertEquals(ResultSet.FETCH_FORWARD, emptyResultSet.getFetchDirection());
246248
}
247249

248250
@Test
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.taosdata.jdbc.common;
2+
import org.junit.Test;
3+
4+
import java.util.Collections;
5+
import java.util.HashSet;
6+
import java.util.Set;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class ClusterTest {
11+
12+
@Test
13+
public void testConstructorWithVarargs() {
14+
Endpoint endpoint1 = new Endpoint("127.0.0.1", 8080, false);
15+
Endpoint endpoint2 = new Endpoint("192.168.1.1", 8081, false);
16+
17+
Cluster cluster = new Cluster(endpoint1, endpoint2);
18+
19+
Set<Endpoint> expectedEndpoints = new HashSet<>();
20+
Collections.addAll(expectedEndpoints, endpoint1, endpoint2);
21+
22+
assertEquals(expectedEndpoints, cluster.getEndpoints());
23+
}
24+
25+
@Test
26+
public void testConstructorWithSet() {
27+
Endpoint endpoint1 = new Endpoint("127.0.0.1", 8080, false);
28+
Endpoint endpoint2 = new Endpoint("192.168.1.1", 8081, false);
29+
30+
Set<Endpoint> endpoints = new HashSet<>();
31+
Collections.addAll(endpoints, endpoint1, endpoint2);
32+
33+
Cluster cluster = new Cluster(endpoints);
34+
35+
assertEquals(endpoints, cluster.getEndpoints());
36+
}
37+
38+
@Test
39+
public void testGetEndpoints() {
40+
Endpoint endpoint = new Endpoint("127.0.0.1", 8080, false);
41+
Cluster cluster = new Cluster(endpoint);
42+
43+
Set<Endpoint> endpoints = cluster.getEndpoints();
44+
45+
assertNotNull(endpoints);
46+
assertEquals(1, endpoints.size());
47+
assertTrue(endpoints.contains(endpoint));
48+
}
49+
50+
@Test
51+
public void testEqualsAndHashCode() {
52+
Endpoint endpoint1 = new Endpoint("127.0.0.1", 8080, false);
53+
Endpoint endpoint2 = new Endpoint("192.168.1.1", 8081, false);
54+
55+
Cluster cluster1 = new Cluster(endpoint1, endpoint2);
56+
Cluster cluster2 = new Cluster(endpoint2, endpoint1);
57+
58+
assertEquals(cluster1, cluster2);
59+
assertEquals(cluster1.hashCode(), cluster2.hashCode());
60+
}
61+
62+
@Test
63+
public void testNotEquals() {
64+
Endpoint endpoint1 = new Endpoint("127.0.0.1", 8080, false);
65+
Endpoint endpoint2 = new Endpoint("192.168.1.1", 8081, false);
66+
67+
Cluster cluster1 = new Cluster(endpoint1);
68+
Cluster cluster2 = new Cluster(endpoint2);
69+
70+
assertNotEquals(cluster1, cluster2);
71+
}
72+
73+
@Test
74+
public void testToString() {
75+
Endpoint endpoint = new Endpoint("127.0.0.1", 8080, false);
76+
Cluster cluster = new Cluster(endpoint);
77+
78+
String expected = "Cluster{endpoints=[" + endpoint.toString() + "]}";
79+
assertEquals(expected, cluster.toString());
80+
}
81+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.taosdata.jdbc.common;
2+
3+
import org.junit.After;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
import java.io.PrintWriter;
8+
import java.io.StringWriter;
9+
import java.sql.SQLException;
10+
11+
import static org.junit.Assert.*;
12+
import static org.mockito.Mockito.*;
13+
14+
public class ConsumerManagerTest {
15+
16+
private StringWriter logOutput;
17+
18+
@Before
19+
public void setUp() {
20+
logOutput = new StringWriter();
21+
ConsumerManager.setLogWriter(new PrintWriter(logOutput));
22+
}
23+
24+
@After
25+
public void tearDown() {
26+
ConsumerManager.setLogWriter(null);
27+
}
28+
29+
@Test
30+
public void testRegister() {
31+
ConsumerFactory mockFactory = mock(ConsumerFactory.class);
32+
ConsumerManager.register(mockFactory);
33+
34+
assertTrue(logOutput.toString().contains("registerConsumerFactory"));
35+
}
36+
37+
@Test(expected = NullPointerException.class)
38+
public void testRegister_NullFactory() {
39+
ConsumerManager.register(null);
40+
}
41+
42+
// @Test
43+
// public void testGetConsumer_Success() throws SQLException {
44+
// ConsumerFactory mockFactory = mock(ConsumerFactory.class);
45+
// when(mockFactory.acceptsType("testType")).thenReturn(true);
46+
// Consumer<?> mockConsumer = mock(Consumer.class);
47+
// when(mockFactory.getConsumer()).thenReturn(mockConsumer);
48+
//
49+
// ConsumerManager.register(mockFactory);
50+
// Consumer<?> consumer = ConsumerManager.getConsumer("testType");
51+
//
52+
// assertNotNull(consumer);
53+
// assertEquals(mockConsumer, consumer);
54+
// }
55+
56+
@Test(expected = SQLException.class)
57+
public void testGetConsumer_NoSuitableConsumer() throws SQLException {
58+
ConsumerFactory mockFactory = mock(ConsumerFactory.class);
59+
when(mockFactory.acceptsType("testType")).thenReturn(false);
60+
61+
ConsumerManager.register(mockFactory);
62+
ConsumerManager.getConsumer("testType");
63+
}
64+
65+
@Test
66+
public void testGetLogWriter() {
67+
PrintWriter writer = ConsumerManager.getLogWriter();
68+
assertNotNull(writer);
69+
}
70+
71+
@Test
72+
public void testSetLogWriter() {
73+
PrintWriter newWriter = new PrintWriter(new StringWriter());
74+
ConsumerManager.setLogWriter(newWriter);
75+
76+
assertEquals(newWriter, ConsumerManager.getLogWriter());
77+
}
78+
79+
@Test
80+
public void testPrintln() {
81+
ConsumerManager.println("Test message");
82+
assertTrue(logOutput.toString().contains("Test message"));
83+
}
84+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.taosdata.jdbc.common;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class EndpointTest {
8+
9+
@Test
10+
public void testConstructorAndGetters() {
11+
Endpoint endpoint = new Endpoint("127.0.0.1", 8080, false);
12+
13+
assertEquals("127.0.0.1", endpoint.getHost());
14+
assertEquals(8080, endpoint.getPort());
15+
assertFalse(endpoint.isIpv6());
16+
}
17+
18+
@Test
19+
public void testEquals_SameObject() {
20+
Endpoint endpoint = new Endpoint("127.0.0.1", 8080, false);
21+
22+
assertEquals(endpoint, endpoint);
23+
}
24+
25+
@Test
26+
public void testEquals_DifferentObjectSameValues() {
27+
Endpoint endpoint1 = new Endpoint("127.0.0.1", 8080, false);
28+
Endpoint endpoint2 = new Endpoint("127.0.0.1", 8080, false);
29+
30+
assertEquals(endpoint1, endpoint2);
31+
assertEquals(endpoint1.hashCode(), endpoint2.hashCode());
32+
}
33+
34+
@Test
35+
public void testEquals_DifferentValues() {
36+
Endpoint endpoint1 = new Endpoint("127.0.0.1", 8080, false);
37+
Endpoint endpoint2 = new Endpoint("192.168.1.1", 8081, true);
38+
39+
assertNotEquals(endpoint1, endpoint2);
40+
}
41+
42+
@Test
43+
public void testEquals_NullObject() {
44+
Endpoint endpoint = new Endpoint("127.0.0.1", 8080, false);
45+
46+
assertNotNull(endpoint);
47+
}
48+
49+
@Test
50+
public void testEquals_DifferentClass() {
51+
Endpoint endpoint = new Endpoint("127.0.0.1", 8080, false);
52+
53+
assertNotEquals("Some String", endpoint);
54+
}
55+
56+
@Test
57+
public void testHashCode() {
58+
Endpoint endpoint1 = new Endpoint("127.0.0.1", 8080, false);
59+
Endpoint endpoint2 = new Endpoint("127.0.0.1", 8080, false);
60+
61+
assertEquals(endpoint1.hashCode(), endpoint2.hashCode());
62+
}
63+
64+
@Test
65+
public void testToString_Ipv4() {
66+
Endpoint endpoint = new Endpoint("127.0.0.1", 8080, false);
67+
68+
assertEquals("127.0.0.1:8080", endpoint.toString());
69+
}
70+
71+
@Test
72+
public void testToString_Ipv6() {
73+
Endpoint endpoint = new Endpoint("::1", 8080, true);
74+
75+
assertEquals("[::1]:8080", endpoint.toString());
76+
}
77+
}

0 commit comments

Comments
 (0)