Skip to content

Commit c45d0af

Browse files
committed
Add ConnectionStringTest subclass of AbstractConnectionStringTest
JAVA-3066
1 parent 83e1606 commit c45d0af

File tree

2 files changed

+87
-42
lines changed

2 files changed

+87
-42
lines changed

driver-core/src/test/unit/com/mongodb/AbstractConnectionStringTest.java

Lines changed: 9 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@
3434
import java.util.Map;
3535
import java.util.concurrent.TimeUnit;
3636

37-
// See https://github.com/mongodb/specifications/tree/master/source/connection-string/tests
3837
@RunWith(Parameterized.class)
39-
public class AbstractConnectionStringTest extends TestCase {
38+
public abstract class AbstractConnectionStringTest extends TestCase {
4039
private final String filename;
4140
private final String description;
4241
private final String input;
@@ -50,47 +49,15 @@ public AbstractConnectionStringTest(final String filename, final String descript
5049
this.definition = definition;
5150
}
5251

53-
@Test
54-
public void shouldPassAllOutcomes() {
55-
if (filename.equals("invalid-uris.json")) {
56-
testInvalidUris();
57-
} else if (filename.equals("valid-auth.json")) {
58-
testValidAuth();
59-
} else if (filename.equals("valid-db-with-dotted-name.json")) {
60-
testValidHostIdentifiers();
61-
testValidAuth();
62-
} else if (filename.equals("valid-host_identifiers.json")) {
63-
testValidHostIdentifiers();
64-
} else if (filename.equals("valid-options.json")) {
65-
testValidOptions();
66-
} else if (filename.equals("valid-unix_socket-absolute.json")) {
67-
testValidHostIdentifiers();
68-
} else if (filename.equals("valid-unix_socket-relative.json")) {
69-
testValidHostIdentifiers();
70-
} else if (filename.equals("valid-warnings.json")) {
71-
testValidHostIdentifiers();
72-
if (!definition.get("options").isNull()) {
73-
testValidOptions();
74-
}
75-
} else {
76-
throw new IllegalArgumentException("Unsupported file: " + filename);
77-
}
52+
protected String getFilename() {
53+
return filename;
7854
}
7955

80-
@Parameterized.Parameters(name = "{1}")
81-
public static Collection<Object[]> data() throws URISyntaxException, IOException {
82-
List<Object[]> data = new ArrayList<Object[]>();
83-
for (File file : JsonPoweredTestHelper.getTestFiles("/connection-string")) {
84-
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
85-
for (BsonValue test : testDocument.getArray("tests")) {
86-
data.add(new Object[]{file.getName(), test.asDocument().getString("description").getValue(),
87-
test.asDocument().getString("uri").getValue(), test.asDocument()});
88-
}
89-
}
90-
return data;
56+
protected BsonDocument getDefinition() {
57+
return definition;
9158
}
9259

93-
private void testInvalidUris() {
60+
protected void testInvalidUris() {
9461
Throwable expectedError = null;
9562

9663
try {
@@ -103,7 +70,7 @@ private void testInvalidUris() {
10370
expectedError instanceof IllegalArgumentException);
10471
}
10572

106-
private void testValidHostIdentifiers() {
73+
protected void testValidHostIdentifiers() {
10774
ConnectionString connectionString = null;
10875
try {
10976
connectionString = new ConnectionString(input);
@@ -114,7 +81,7 @@ private void testValidHostIdentifiers() {
11481
assertExpectedHosts(connectionString.getHosts());
11582
}
11683

117-
private void testValidOptions() {
84+
protected void testValidOptions() {
11885
ConnectionString connectionString = null;
11986

12087
try {
@@ -140,7 +107,7 @@ private void testValidOptions() {
140107
}
141108
}
142109

143-
private void testValidAuth() {
110+
protected void testValidAuth() {
144111
ConnectionString connectionString = null;
145112

146113
try {
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.mongodb;
18+
19+
import org.bson.BsonDocument;
20+
import org.bson.BsonValue;
21+
import org.junit.Test;
22+
import org.junit.runners.Parameterized;
23+
import util.JsonPoweredTestHelper;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.net.URISyntaxException;
28+
import java.util.ArrayList;
29+
import java.util.Collection;
30+
import java.util.List;
31+
32+
// See https://github.com/mongodb/specifications/tree/master/source/connection-string/tests
33+
public class ConnectionStringTest extends AbstractConnectionStringTest {
34+
public ConnectionStringTest(final String filename, final String description, final String input, final BsonDocument definition) {
35+
super(filename, description, input, definition);
36+
}
37+
38+
@Test
39+
public void shouldPassAllOutcomes() {
40+
if (getFilename().equals("invalid-uris.json")) {
41+
testInvalidUris();
42+
} else if (getFilename().equals("valid-auth.json")) {
43+
testValidAuth();
44+
} else if (getFilename().equals("valid-db-with-dotted-name.json")) {
45+
testValidHostIdentifiers();
46+
testValidAuth();
47+
} else if (getFilename().equals("valid-host_identifiers.json")) {
48+
testValidHostIdentifiers();
49+
} else if (getFilename().equals("valid-options.json")) {
50+
testValidOptions();
51+
} else if (getFilename().equals("valid-unix_socket-absolute.json")) {
52+
testValidHostIdentifiers();
53+
} else if (getFilename().equals("valid-unix_socket-relative.json")) {
54+
testValidHostIdentifiers();
55+
} else if (getFilename().equals("valid-warnings.json")) {
56+
testValidHostIdentifiers();
57+
if (!getDefinition().get("options").isNull()) {
58+
testValidOptions();
59+
}
60+
} else {
61+
throw new IllegalArgumentException("Unsupported file: " + getFilename());
62+
}
63+
}
64+
65+
66+
@Parameterized.Parameters(name = "{1}")
67+
public static Collection<Object[]> data() throws URISyntaxException, IOException {
68+
List<Object[]> data = new ArrayList<Object[]>();
69+
for (File file : JsonPoweredTestHelper.getTestFiles("/connection-string")) {
70+
BsonDocument testDocument = JsonPoweredTestHelper.getTestDocument(file);
71+
for (BsonValue test : testDocument.getArray("tests")) {
72+
data.add(new Object[]{file.getName(), test.asDocument().getString("description").getValue(),
73+
test.asDocument().getString("uri").getValue(), test.asDocument()});
74+
}
75+
}
76+
return data;
77+
}
78+
}

0 commit comments

Comments
 (0)