Skip to content

Commit cc9c45c

Browse files
authored
Merge pull request #1731 from marklogic/feature/18458-try-client
MLE-18458 Added support for try-with-resource
2 parents 82b468f + 9709182 commit cc9c45c

File tree

2 files changed

+124
-106
lines changed

2 files changed

+124
-106
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/DatabaseClient.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
package com.marklogic.client;
55

6+
import java.io.Closeable;
67
import java.io.OutputStream;
78
import java.io.Serializable;
89

@@ -28,7 +29,7 @@
2829
* A Database Client instantiates document and query managers and other objects
2930
* with shared access to a database.
3031
*/
31-
public interface DatabaseClient {
32+
public interface DatabaseClient extends Closeable {
3233
/**
3334
* Identifies whether the client connects directly to MarkLogic (the default) or
3435
* by means of a gateway such as a load balancer.
@@ -241,4 +242,14 @@ static public interface ConnectionResult {
241242
String getDatabase();
242243

243244
SecurityContext getSecurityContext();
245+
246+
/**
247+
* Overridden from the {@code Closeable} interface so that a user doesn't have to deal with a checked
248+
* IOException.
249+
*
250+
* @since 7.1.0
251+
*/
252+
default void close() {
253+
release();
254+
}
244255
}

marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientTest.java

Lines changed: 112 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
*/
44
package com.marklogic.client.test;
55

6+
import com.marklogic.client.DatabaseClient;
67
import com.marklogic.client.DatabaseClient.ConnectionResult;
78
import com.marklogic.client.admin.QueryOptionsManager;
89
import com.marklogic.client.alerting.RuleManager;
9-
import com.marklogic.client.document.BinaryDocumentManager;
10-
import com.marklogic.client.document.GenericDocumentManager;
11-
import com.marklogic.client.document.JSONDocumentManager;
12-
import com.marklogic.client.document.TextDocumentManager;
13-
import com.marklogic.client.document.XMLDocumentManager;
10+
import com.marklogic.client.document.*;
1411
import com.marklogic.client.eval.ServerEvaluationCall;
1512
import com.marklogic.client.pojo.PojoRepository;
1613
import com.marklogic.client.query.QueryManager;
@@ -19,105 +16,115 @@
1916
import org.junit.jupiter.api.BeforeAll;
2017
import org.junit.jupiter.api.Test;
2118

22-
import static org.junit.jupiter.api.Assertions.assertFalse;
23-
import static org.junit.jupiter.api.Assertions.assertNotNull;
24-
import static org.junit.jupiter.api.Assertions.assertTrue;
25-
26-
public class DatabaseClientTest {
27-
@BeforeAll
28-
public static void beforeClass() {
29-
Common.connect();
30-
Common.connectRestAdmin();
31-
}
32-
@AfterAll
33-
public static void afterClass() {
34-
}
35-
36-
@Test
37-
public void testNewDocument() {
38-
GenericDocumentManager doc = Common.client.newDocumentManager();
39-
assertNotNull( doc);
40-
}
41-
42-
@Test
43-
public void testNewBinaryDocument() {
44-
BinaryDocumentManager doc = Common.client.newBinaryDocumentManager();
45-
assertNotNull( doc);
46-
}
47-
48-
@Test
49-
public void testNewJSONDocument() {
50-
JSONDocumentManager doc = Common.client.newJSONDocumentManager();
51-
assertNotNull( doc);
52-
}
53-
54-
@Test
55-
public void testNewTextDocument() {
56-
TextDocumentManager doc = Common.client.newTextDocumentManager();
57-
assertNotNull( doc);
58-
}
59-
60-
@Test
61-
public void testNewXMLDocument() {
62-
XMLDocumentManager doc = Common.client.newXMLDocumentManager();
63-
assertNotNull( doc);
64-
}
65-
66-
@Test
67-
public void testNewLogger() {
68-
RequestLogger logger = Common.client.newLogger(System.out);
69-
assertNotNull( logger);
70-
}
71-
72-
@Test
73-
public void testNewQueryManager() {
74-
QueryManager mgr = Common.client.newQueryManager();
75-
assertNotNull( mgr);
76-
}
77-
78-
@Test
79-
public void testNewRuleManager() {
80-
RuleManager mgr = Common.client.newRuleManager();
81-
assertNotNull( mgr);
82-
}
83-
84-
@Test
85-
public void testNewPojoRepository() {
86-
PojoRepository<City, Integer> mgr = Common.client.newPojoRepository(City.class, Integer.class);
87-
assertNotNull( mgr);
88-
}
89-
90-
@Test
91-
public void testNewServerEvaluationCall() {
92-
ServerEvaluationCall mgr = Common.client.newServerEval();
93-
assertNotNull( mgr);
94-
}
95-
96-
@Test
97-
public void testNewQueryOptionsManager() {
98-
QueryOptionsManager mgr = Common.restAdminClient.newServerConfigManager().newQueryOptionsManager();
99-
assertNotNull( mgr);
100-
}
101-
102-
@Test
103-
public void testGetClientImplementationObject() {
104-
Object impl = Common.client.getClientImplementation();
105-
assertNotNull( impl);
106-
assertTrue( impl instanceof okhttp3.OkHttpClient);
107-
}
108-
109-
@Test
110-
public void testCheckConnectionWithValidUser() {
111-
ConnectionResult connResult = Common.newClient().checkConnection();
112-
assertTrue(connResult.isConnected());
113-
}
114-
115-
@Test
116-
public void testCheckConnectionWithInvalidUser() {
117-
ConnectionResult connResult = Common.newClientBuilder().withUsername("invalid").withPassword("invalid").build().checkConnection();
118-
assertFalse(connResult.isConnected());
119-
assertTrue(connResult.getStatusCode() == 401);
120-
assertTrue(connResult.getErrorMessage().equalsIgnoreCase("Unauthorized"));
121-
}
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
class DatabaseClientTest {
22+
23+
@BeforeAll
24+
public static void beforeClass() {
25+
Common.connect();
26+
Common.connectRestAdmin();
27+
}
28+
29+
@AfterAll
30+
public static void afterClass() {
31+
}
32+
33+
@Test
34+
void tryWithResource() {
35+
try (DatabaseClient client = Common.newClient()) {
36+
ConnectionResult result = client.checkConnection();
37+
assertTrue(result.isConnected(), "This test is ensuring that a DatabaseClient, as of 7.1.0, can " +
38+
"be used in a try-with-resources block without any error being thrown. We don't have a way of " +
39+
"verifying that release() is actually called on the client though.");
40+
}
41+
}
42+
43+
@Test
44+
public void testNewDocument() {
45+
GenericDocumentManager doc = Common.client.newDocumentManager();
46+
assertNotNull(doc);
47+
}
48+
49+
@Test
50+
public void testNewBinaryDocument() {
51+
BinaryDocumentManager doc = Common.client.newBinaryDocumentManager();
52+
assertNotNull(doc);
53+
}
54+
55+
@Test
56+
public void testNewJSONDocument() {
57+
JSONDocumentManager doc = Common.client.newJSONDocumentManager();
58+
assertNotNull(doc);
59+
}
60+
61+
@Test
62+
public void testNewTextDocument() {
63+
TextDocumentManager doc = Common.client.newTextDocumentManager();
64+
assertNotNull(doc);
65+
}
66+
67+
@Test
68+
public void testNewXMLDocument() {
69+
XMLDocumentManager doc = Common.client.newXMLDocumentManager();
70+
assertNotNull(doc);
71+
}
72+
73+
@Test
74+
public void testNewLogger() {
75+
RequestLogger logger = Common.client.newLogger(System.out);
76+
assertNotNull(logger);
77+
}
78+
79+
@Test
80+
public void testNewQueryManager() {
81+
QueryManager mgr = Common.client.newQueryManager();
82+
assertNotNull(mgr);
83+
}
84+
85+
@Test
86+
public void testNewRuleManager() {
87+
RuleManager mgr = Common.client.newRuleManager();
88+
assertNotNull(mgr);
89+
}
90+
91+
@Test
92+
public void testNewPojoRepository() {
93+
PojoRepository<City, Integer> mgr = Common.client.newPojoRepository(City.class, Integer.class);
94+
assertNotNull(mgr);
95+
}
96+
97+
@Test
98+
public void testNewServerEvaluationCall() {
99+
ServerEvaluationCall mgr = Common.client.newServerEval();
100+
assertNotNull(mgr);
101+
}
102+
103+
@Test
104+
public void testNewQueryOptionsManager() {
105+
QueryOptionsManager mgr = Common.restAdminClient.newServerConfigManager().newQueryOptionsManager();
106+
assertNotNull(mgr);
107+
}
108+
109+
@Test
110+
public void testGetClientImplementationObject() {
111+
Object impl = Common.client.getClientImplementation();
112+
assertNotNull(impl);
113+
assertTrue(impl instanceof okhttp3.OkHttpClient);
114+
}
115+
116+
@Test
117+
public void testCheckConnectionWithValidUser() {
118+
ConnectionResult connResult = Common.newClient().checkConnection();
119+
assertTrue(connResult.isConnected());
120+
}
121+
122+
@Test
123+
public void testCheckConnectionWithInvalidUser() {
124+
ConnectionResult connResult = Common.newClientBuilder().withUsername("invalid").withPassword("invalid").build().checkConnection();
125+
assertFalse(connResult.isConnected());
126+
assertTrue(connResult.getStatusCode() == 401);
127+
assertTrue(connResult.getErrorMessage().equalsIgnoreCase("Unauthorized"));
128+
}
122129

123130
}

0 commit comments

Comments
 (0)