diff --git a/marklogic-client-api/src/main/java/com/marklogic/client/DatabaseClient.java b/marklogic-client-api/src/main/java/com/marklogic/client/DatabaseClient.java index 18fe271d1..25735575f 100644 --- a/marklogic-client-api/src/main/java/com/marklogic/client/DatabaseClient.java +++ b/marklogic-client-api/src/main/java/com/marklogic/client/DatabaseClient.java @@ -3,6 +3,7 @@ */ package com.marklogic.client; +import java.io.Closeable; import java.io.OutputStream; import java.io.Serializable; @@ -28,7 +29,7 @@ * A Database Client instantiates document and query managers and other objects * with shared access to a database. */ -public interface DatabaseClient { +public interface DatabaseClient extends Closeable { /** * Identifies whether the client connects directly to MarkLogic (the default) or * by means of a gateway such as a load balancer. @@ -241,4 +242,14 @@ static public interface ConnectionResult { String getDatabase(); SecurityContext getSecurityContext(); + + /** + * Overridden from the {@code Closeable} interface so that a user doesn't have to deal with a checked + * IOException. + * + * @since 7.1.0 + */ + default void close() { + release(); + } } diff --git a/marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientTest.java b/marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientTest.java index d78ea1740..03e652d7f 100644 --- a/marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientTest.java +++ b/marklogic-client-api/src/test/java/com/marklogic/client/test/DatabaseClientTest.java @@ -3,14 +3,11 @@ */ package com.marklogic.client.test; +import com.marklogic.client.DatabaseClient; import com.marklogic.client.DatabaseClient.ConnectionResult; import com.marklogic.client.admin.QueryOptionsManager; import com.marklogic.client.alerting.RuleManager; -import com.marklogic.client.document.BinaryDocumentManager; -import com.marklogic.client.document.GenericDocumentManager; -import com.marklogic.client.document.JSONDocumentManager; -import com.marklogic.client.document.TextDocumentManager; -import com.marklogic.client.document.XMLDocumentManager; +import com.marklogic.client.document.*; import com.marklogic.client.eval.ServerEvaluationCall; import com.marklogic.client.pojo.PojoRepository; import com.marklogic.client.query.QueryManager; @@ -19,105 +16,115 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class DatabaseClientTest { - @BeforeAll - public static void beforeClass() { - Common.connect(); - Common.connectRestAdmin(); - } - @AfterAll - public static void afterClass() { - } - - @Test - public void testNewDocument() { - GenericDocumentManager doc = Common.client.newDocumentManager(); - assertNotNull( doc); - } - - @Test - public void testNewBinaryDocument() { - BinaryDocumentManager doc = Common.client.newBinaryDocumentManager(); - assertNotNull( doc); - } - - @Test - public void testNewJSONDocument() { - JSONDocumentManager doc = Common.client.newJSONDocumentManager(); - assertNotNull( doc); - } - - @Test - public void testNewTextDocument() { - TextDocumentManager doc = Common.client.newTextDocumentManager(); - assertNotNull( doc); - } - - @Test - public void testNewXMLDocument() { - XMLDocumentManager doc = Common.client.newXMLDocumentManager(); - assertNotNull( doc); - } - - @Test - public void testNewLogger() { - RequestLogger logger = Common.client.newLogger(System.out); - assertNotNull( logger); - } - - @Test - public void testNewQueryManager() { - QueryManager mgr = Common.client.newQueryManager(); - assertNotNull( mgr); - } - - @Test - public void testNewRuleManager() { - RuleManager mgr = Common.client.newRuleManager(); - assertNotNull( mgr); - } - - @Test - public void testNewPojoRepository() { - PojoRepository mgr = Common.client.newPojoRepository(City.class, Integer.class); - assertNotNull( mgr); - } - - @Test - public void testNewServerEvaluationCall() { - ServerEvaluationCall mgr = Common.client.newServerEval(); - assertNotNull( mgr); - } - - @Test - public void testNewQueryOptionsManager() { - QueryOptionsManager mgr = Common.restAdminClient.newServerConfigManager().newQueryOptionsManager(); - assertNotNull( mgr); - } - - @Test - public void testGetClientImplementationObject() { - Object impl = Common.client.getClientImplementation(); - assertNotNull( impl); - assertTrue( impl instanceof okhttp3.OkHttpClient); - } - - @Test - public void testCheckConnectionWithValidUser() { - ConnectionResult connResult = Common.newClient().checkConnection(); - assertTrue(connResult.isConnected()); - } - - @Test - public void testCheckConnectionWithInvalidUser() { - ConnectionResult connResult = Common.newClientBuilder().withUsername("invalid").withPassword("invalid").build().checkConnection(); - assertFalse(connResult.isConnected()); - assertTrue(connResult.getStatusCode() == 401); - assertTrue(connResult.getErrorMessage().equalsIgnoreCase("Unauthorized")); - } +import static org.junit.jupiter.api.Assertions.*; + +class DatabaseClientTest { + + @BeforeAll + public static void beforeClass() { + Common.connect(); + Common.connectRestAdmin(); + } + + @AfterAll + public static void afterClass() { + } + + @Test + void tryWithResource() { + try (DatabaseClient client = Common.newClient()) { + ConnectionResult result = client.checkConnection(); + assertTrue(result.isConnected(), "This test is ensuring that a DatabaseClient, as of 7.1.0, can " + + "be used in a try-with-resources block without any error being thrown. We don't have a way of " + + "verifying that release() is actually called on the client though."); + } + } + + @Test + public void testNewDocument() { + GenericDocumentManager doc = Common.client.newDocumentManager(); + assertNotNull(doc); + } + + @Test + public void testNewBinaryDocument() { + BinaryDocumentManager doc = Common.client.newBinaryDocumentManager(); + assertNotNull(doc); + } + + @Test + public void testNewJSONDocument() { + JSONDocumentManager doc = Common.client.newJSONDocumentManager(); + assertNotNull(doc); + } + + @Test + public void testNewTextDocument() { + TextDocumentManager doc = Common.client.newTextDocumentManager(); + assertNotNull(doc); + } + + @Test + public void testNewXMLDocument() { + XMLDocumentManager doc = Common.client.newXMLDocumentManager(); + assertNotNull(doc); + } + + @Test + public void testNewLogger() { + RequestLogger logger = Common.client.newLogger(System.out); + assertNotNull(logger); + } + + @Test + public void testNewQueryManager() { + QueryManager mgr = Common.client.newQueryManager(); + assertNotNull(mgr); + } + + @Test + public void testNewRuleManager() { + RuleManager mgr = Common.client.newRuleManager(); + assertNotNull(mgr); + } + + @Test + public void testNewPojoRepository() { + PojoRepository mgr = Common.client.newPojoRepository(City.class, Integer.class); + assertNotNull(mgr); + } + + @Test + public void testNewServerEvaluationCall() { + ServerEvaluationCall mgr = Common.client.newServerEval(); + assertNotNull(mgr); + } + + @Test + public void testNewQueryOptionsManager() { + QueryOptionsManager mgr = Common.restAdminClient.newServerConfigManager().newQueryOptionsManager(); + assertNotNull(mgr); + } + + @Test + public void testGetClientImplementationObject() { + Object impl = Common.client.getClientImplementation(); + assertNotNull(impl); + assertTrue(impl instanceof okhttp3.OkHttpClient); + } + + @Test + public void testCheckConnectionWithValidUser() { + ConnectionResult connResult = Common.newClient().checkConnection(); + assertTrue(connResult.isConnected()); + } + + @Test + public void testCheckConnectionWithInvalidUser() { + ConnectionResult connResult = Common.newClientBuilder().withUsername("invalid").withPassword("invalid").build().checkConnection(); + assertFalse(connResult.isConnected()); + assertTrue(connResult.getStatusCode() == 401); + assertTrue(connResult.getErrorMessage().equalsIgnoreCase("Unauthorized")); + } }