Skip to content
This repository was archived by the owner on Jul 23, 2025. It is now read-only.

Commit 9ed4291

Browse files
committed
Reworked tests to use JUnit5
1 parent 0f17124 commit 9ed4291

17 files changed

+561
-567
lines changed

build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ dependencies {
2727
api 'com.marklogic:marklogic-client-api:5.5.0'
2828
implementation 'org.slf4j:slf4j-api:1.7.31'
2929

30-
testImplementation "junit:junit:4+"
31-
// Sticking with the latest 4.3.x for tests; including the latest 5.2.x release oddly resulted in the servlet package
32-
// being required
33-
testImplementation "org.springframework:spring-test:4.3.29.RELEASE"
34-
testImplementation "com.marklogic:ml-javaclient-util:4.3.0"
30+
testImplementation "com.marklogic:marklogic-junit5:1.1.0"
3531

3632
// Forcing Spring to use logback instead of commons-logging
3733
testImplementation "ch.qos.logback:logback-classic:1.2.4"
3834
testImplementation "org.slf4j:jcl-over-slf4j:1.7.31"
3935
testImplementation "org.slf4j:slf4j-api:1.7.31"
4036
}
4137

38+
test {
39+
useJUnitPlatform()
40+
}
41+
4242
task sourcesJar(type: Jar, dependsOn: classes) {
4343
classifier 'sources'
4444
from sourceSets.main.allSource

gradle.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ mavenCentralUrl=https://oss.sonatype.org/service/local/staging/deploy/maven2/
99
#signing.keyId=YourKeyId
1010
#signing.password=YourPublicKeyPassword
1111
#signing.secretKeyRingFile=PathToYourKeyRingFile
12+
13+
# Used for running tests; can be overridden in gradle-local.properties
14+
mlHost=localhost
15+
mlTestRestPort=8000
16+
mlUsername=admin
17+
mlPassword=admin

src/test/java/com/marklogic/client/ext/datamovement/AbstractDataMovementTest.java

Lines changed: 94 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -8,146 +8,111 @@
88
import com.marklogic.client.ext.DefaultConfiguredDatabaseClientFactory;
99
import com.marklogic.client.ext.batch.DataMovementBatchWriter;
1010
import com.marklogic.client.ext.datamovement.job.DeleteCollectionsJob;
11-
import com.marklogic.client.ext.spring.SpringDatabaseClientConfig;
1211
import com.marklogic.client.impl.DocumentWriteOperationImpl;
1312
import com.marklogic.client.io.DocumentMetadataHandle;
1413
import com.marklogic.client.io.Format;
1514
import com.marklogic.client.io.StringHandle;
16-
import org.junit.After;
17-
import org.junit.Assert;
18-
import org.junit.Before;
19-
import org.junit.runner.RunWith;
20-
import org.slf4j.Logger;
21-
import org.slf4j.LoggerFactory;
15+
import com.marklogic.junit5.spring.AbstractSpringMarkLogicTest;
16+
import org.junit.jupiter.api.AfterEach;
17+
import org.junit.jupiter.api.BeforeEach;
2218
import org.springframework.beans.factory.annotation.Autowired;
23-
import org.springframework.context.annotation.Bean;
24-
import org.springframework.context.annotation.Configuration;
25-
import org.springframework.context.annotation.Import;
26-
import org.springframework.context.annotation.PropertySource;
27-
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
28-
import org.springframework.test.context.ContextConfiguration;
29-
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3019

3120
import java.io.File;
3221
import java.io.IOException;
3322
import java.util.*;
3423
import java.util.zip.ZipEntry;
3524
import java.util.zip.ZipFile;
3625

37-
/**
38-
* Abstract class for making it easier to test DMSDK listeners and consumers.
39-
*/
40-
@RunWith(SpringJUnit4ClassRunner.class)
41-
@ContextConfiguration(classes = {TestConfig.class})
42-
public abstract class AbstractDataMovementTest extends Assert {
43-
44-
protected final static String COLLECTION = "data-movement-test";
45-
46-
protected final static String FIRST_URI = "/test/dmsdk-test-1.xml";
47-
protected final static String SECOND_URI = "/test/dmsdk-test-2.xml";
48-
49-
protected QueryBatcherTemplate queryBatcherTemplate;
50-
51-
protected Logger logger = LoggerFactory.getLogger(getClass());
52-
53-
@Autowired
54-
protected DatabaseClientConfig clientConfig;
55-
protected DatabaseClient client;
56-
57-
protected ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory();
58-
59-
protected DatabaseClient newClient() {
60-
client = configuredDatabaseClientFactory.newDatabaseClient(clientConfig);
61-
return client;
62-
}
63-
64-
@After
65-
public void releaseClientOnTearDown() {
66-
if (client != null) {
67-
try {
68-
client.release();
69-
} catch (Exception ex) {
70-
// That's fine, the test probably released it already
71-
}
72-
}
73-
}
74-
75-
@Before
76-
public void setupAbstractDataMovementTest() {
77-
queryBatcherTemplate = new QueryBatcherTemplate(newClient("Documents"));
78-
queryBatcherTemplate.setJobName("manage-collections-test");
79-
queryBatcherTemplate.setBatchSize(1);
80-
queryBatcherTemplate.setThreadCount(2);
81-
82-
queryBatcherTemplate.applyOnDocumentUris(new DeleteListener(), FIRST_URI, SECOND_URI);
83-
new DeleteCollectionsJob(COLLECTION).run(client);
84-
85-
writeDocuments(FIRST_URI, SECOND_URI);
86-
}
87-
88-
protected DatabaseClient newClient(String database) {
89-
String currentDatabase = clientConfig.getDatabase();
90-
clientConfig.setDatabase(database);
91-
client = configuredDatabaseClientFactory.newDatabaseClient(clientConfig);
92-
clientConfig.setDatabase(currentDatabase);
93-
return client;
94-
}
95-
96-
protected void writeDocuments(String... uris) {
97-
List<DocumentWriteOperation> list = new ArrayList<>();
98-
DocumentMetadataHandle metadata = new DocumentMetadataHandle();
99-
metadata.getCollections().add(COLLECTION);
100-
for (String uri : uris) {
101-
list.add(new DocumentWriteOperationImpl(DocumentWriteOperation.OperationType.DOCUMENT_WRITE, uri,
102-
metadata, new StringHandle("<test>" + uri + "</test>").withFormat(Format.XML)));
103-
}
104-
writeDocuments(list);
105-
}
106-
107-
protected void writeDocuments(List<DocumentWriteOperation> writeOperations) {
108-
DataMovementBatchWriter writer = new DataMovementBatchWriter(client);
109-
writer.initialize();
110-
writer.write(writeOperations);
111-
writer.waitForCompletion();
112-
}
113-
114-
protected void assertZipFileContainsEntryNames(File file, String... names) {
115-
Set<String> entryNames = new HashSet<>();
116-
try {
117-
ZipFile zipFile = new ZipFile(file);
118-
try {
119-
Enumeration<?> entries = zipFile.entries();
120-
while (entries.hasMoreElements()) {
121-
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
122-
entryNames.add(zipEntry.getName());
123-
}
124-
} finally {
125-
zipFile.close();
126-
}
127-
} catch (IOException ie) {
128-
throw new RuntimeException(ie);
129-
}
130-
131-
logger.info("Entry names: " + entryNames);
132-
for (String name : names) {
133-
assertTrue(entryNames.contains(name));
134-
}
135-
assertEquals(names.length, entryNames.size());
136-
}
26+
import static org.junit.jupiter.api.Assertions.assertEquals;
27+
import static org.junit.jupiter.api.Assertions.assertTrue;
28+
29+
public abstract class AbstractDataMovementTest extends AbstractSpringMarkLogicTest {
30+
31+
protected final static String COLLECTION = "data-movement-test";
32+
33+
protected final static String FIRST_URI = "/test/dmsdk-test-1.xml";
34+
protected final static String SECOND_URI = "/test/dmsdk-test-2.xml";
35+
36+
protected QueryBatcherTemplate queryBatcherTemplate;
37+
38+
@Autowired
39+
protected DatabaseClientConfig clientConfig;
40+
protected DatabaseClient client;
41+
42+
protected ConfiguredDatabaseClientFactory configuredDatabaseClientFactory = new DefaultConfiguredDatabaseClientFactory();
43+
44+
@AfterEach
45+
public void releaseClientOnTearDown() {
46+
if (client != null) {
47+
try {
48+
client.release();
49+
} catch (Exception ex) {
50+
// That's fine, the test probably released it already
51+
}
52+
}
53+
}
54+
55+
@BeforeEach
56+
public void setupAbstractDataMovementTest() {
57+
queryBatcherTemplate = new QueryBatcherTemplate(newClient("Documents"));
58+
queryBatcherTemplate.setJobName("manage-collections-test");
59+
queryBatcherTemplate.setBatchSize(1);
60+
queryBatcherTemplate.setThreadCount(2);
61+
62+
queryBatcherTemplate.applyOnDocumentUris(new DeleteListener(), FIRST_URI, SECOND_URI);
63+
new DeleteCollectionsJob(COLLECTION).run(client);
64+
65+
writeDocuments(FIRST_URI, SECOND_URI);
66+
}
67+
68+
protected DatabaseClient newClient(String database) {
69+
String currentDatabase = clientConfig.getDatabase();
70+
clientConfig.setDatabase(database);
71+
client = configuredDatabaseClientFactory.newDatabaseClient(clientConfig);
72+
clientConfig.setDatabase(currentDatabase);
73+
return client;
74+
}
75+
76+
protected void writeDocuments(String... uris) {
77+
List<DocumentWriteOperation> list = new ArrayList<>();
78+
DocumentMetadataHandle metadata = new DocumentMetadataHandle();
79+
metadata.getCollections().add(COLLECTION);
80+
for (String uri : uris) {
81+
list.add(new DocumentWriteOperationImpl(DocumentWriteOperation.OperationType.DOCUMENT_WRITE, uri,
82+
metadata, new StringHandle("<test>" + uri + "</test>").withFormat(Format.XML)));
83+
}
84+
writeDocuments(list);
85+
}
86+
87+
protected void writeDocuments(List<DocumentWriteOperation> writeOperations) {
88+
DataMovementBatchWriter writer = new DataMovementBatchWriter(client);
89+
writer.initialize();
90+
writer.write(writeOperations);
91+
writer.waitForCompletion();
92+
}
93+
94+
protected void assertZipFileContainsEntryNames(File file, String... names) {
95+
Set<String> entryNames = new HashSet<>();
96+
try {
97+
ZipFile zipFile = new ZipFile(file);
98+
try {
99+
Enumeration<?> entries = zipFile.entries();
100+
while (entries.hasMoreElements()) {
101+
ZipEntry zipEntry = (ZipEntry) entries.nextElement();
102+
entryNames.add(zipEntry.getName());
103+
}
104+
} finally {
105+
zipFile.close();
106+
}
107+
} catch (IOException ie) {
108+
throw new RuntimeException(ie);
109+
}
110+
111+
logger.info("Entry names: " + entryNames);
112+
for (String name : names) {
113+
assertTrue(entryNames.contains(name));
114+
}
115+
assertEquals(names.length, entryNames.size());
116+
}
137117

138118
}
139-
140-
@Configuration
141-
@Import(value = {SpringDatabaseClientConfig.class})
142-
@PropertySource("classpath:application.properties")
143-
class TestConfig {
144-
145-
/**
146-
* Ensures that placeholders are replaced with property values
147-
*/
148-
@Bean
149-
public static PropertySourcesPlaceholderConfigurer propertyPlaceHolderConfigurer() {
150-
return new PropertySourcesPlaceholderConfigurer();
151-
}
152-
}
153-
Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
package com.marklogic.client.ext.datamovement;
22

3-
import org.junit.Assert;
4-
import org.junit.Test;
3+
import org.junit.jupiter.api.Test;
54

6-
public class UrisQueryQueryBatcherBuilderTest extends Assert {
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
76

8-
private UrisQueryQueryBatcherBuilder builder = new UrisQueryQueryBatcherBuilder();
7+
public class UrisQueryQueryBatcherBuilderTest {
98

10-
@Test
11-
public void javascriptStartingWithFnSubsequence() {
12-
String query = "fn.subsequence(cts.uris(null, null, cts.collectionQuery('Testing')), 1, 3)";
13-
String newQuery = builder.wrapJavascriptIfAppropriate(query);
14-
assertEquals("The query should not be wrapped with cts.uris since it doesn't start with cts.", query, newQuery);
15-
}
9+
private UrisQueryQueryBatcherBuilder builder = new UrisQueryQueryBatcherBuilder();
1610

17-
@Test
18-
public void javascriptStartingWithCtsUris() {
19-
String query = "cts.uris(null, null, cts.collectionQuery('Testing'))";
20-
String newQuery = builder.wrapJavascriptIfAppropriate(query);
21-
assertEquals("The query should not be wrapped with cts.uris since it already starts with cts.uris", query, newQuery);
22-
}
11+
@Test
12+
public void javascriptStartingWithFnSubsequence() {
13+
String query = "fn.subsequence(cts.uris(null, null, cts.collectionQuery('Testing')), 1, 3)";
14+
String newQuery = builder.wrapJavascriptIfAppropriate(query);
15+
assertEquals(query, newQuery,
16+
"The query should not be wrapped with cts.uris since it doesn't start with cts.");
17+
}
2318

24-
@Test
25-
public void javascriptStartingWithCtsCollectionQuery() {
26-
String query = "cts.collectionQuery('Testing')";
27-
String newQuery = builder.wrapJavascriptIfAppropriate(query);
28-
assertEquals("The query should not be wrapped with cts.uris since it doesn't start with cts.uris",
29-
"cts.uris(\"\", null, cts.collectionQuery('Testing'))", newQuery);
30-
}
19+
@Test
20+
public void javascriptStartingWithCtsUris() {
21+
String query = "cts.uris(null, null, cts.collectionQuery('Testing'))";
22+
String newQuery = builder.wrapJavascriptIfAppropriate(query);
23+
assertEquals(query, newQuery,
24+
"The query should not be wrapped with cts.uris since it already starts with cts.uris");
25+
}
26+
27+
@Test
28+
public void javascriptStartingWithCtsCollectionQuery() {
29+
String query = "cts.collectionQuery('Testing')";
30+
String newQuery = builder.wrapJavascriptIfAppropriate(query);
31+
assertEquals("cts.uris(\"\", null, cts.collectionQuery('Testing'))", newQuery,
32+
"The query should not be wrapped with cts.uris since it doesn't start with cts.uris");
33+
}
3134
}

src/test/java/com/marklogic/client/ext/datamovement/job/DeleteCollectionsTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import com.marklogic.client.datamovement.DeleteListener;
44
import com.marklogic.client.ext.datamovement.AbstractDataMovementTest;
5-
import org.junit.Test;
5+
import org.junit.jupiter.api.Test;
66

77
import java.util.List;
88
import java.util.Properties;
99

10+
import static org.junit.jupiter.api.Assertions.*;
11+
1012
public class DeleteCollectionsTest extends AbstractDataMovementTest {
1113

1214
@Test
@@ -16,7 +18,7 @@ public void test() {
1618

1719
DeleteCollectionsJob job = new DeleteCollectionsJob();
1820
List<String> messages = job.configureJob(props);
19-
assertTrue("This job doesn't require where* properties to be set", messages.isEmpty());
21+
assertTrue(messages.isEmpty(), "This job doesn't require where* properties to be set");
2022

2123
assertNotNull(client.newDocumentManager().exists(FIRST_URI));
2224
assertNotNull(client.newDocumentManager().exists(SECOND_URI));
@@ -32,8 +34,8 @@ public void collectionsSetViaConstructor() {
3234
DeleteCollectionsJob job = new DeleteCollectionsJob(COLLECTION);
3335

3436
List<String> messages = job.configureJob(new Properties());
35-
assertTrue("This job doesn't require where* properties to be set, and 'collections' shouldn't be required " +
36-
"since the collections were specified via the constructor", messages.isEmpty());
37+
assertTrue(messages.isEmpty(), "This job doesn't require where* properties to be set, and 'collections' shouldn't be required " +
38+
"since the collections were specified via the constructor");
3739

3840
assertNotNull(client.newDocumentManager().exists(FIRST_URI));
3941
assertNotNull(client.newDocumentManager().exists(SECOND_URI));

0 commit comments

Comments
 (0)