Skip to content

Commit 821b77f

Browse files
committed
Fixed some details in unit tests
1 parent 770ca32 commit 821b77f

File tree

3 files changed

+108
-41
lines changed

3 files changed

+108
-41
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.mongodb;
18+
19+
import com.mongodb.client.MongoClient;
20+
import org.apache.logging.log4j.core.LoggerContext;
21+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
22+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
23+
import org.junit.jupiter.api.Test;
24+
25+
@UsingMongoDb
26+
@LoggerContextSource("MongoDbNoDatabaseAndCollectionNameIT.xml")
27+
// Print debug status logger output upon failure
28+
@UsingStatusListener
29+
class MongoDbNoDatabaseAndCollectionNameIT extends AbstractMongoDbCappedIT {
30+
31+
@Test
32+
@Override
33+
protected void test(LoggerContext ctx, MongoClient mongoClient) {
34+
super.test(ctx, mongoClient);
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="
21+
https://logging.apache.org/xml/ns
22+
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
23+
<Appenders>
24+
<NoSql name="MONGO">
25+
<MongoDb
26+
connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbNoDatabaseAndCollectionNameIT"
27+
capped="true"
28+
collectionSize="1073741824"
29+
/>
30+
</NoSql>
31+
</Appenders>
32+
<Loggers>
33+
<Root level="ALL">
34+
<AppenderRef ref="MONGO" />
35+
</Root>
36+
</Loggers>
37+
</Configuration>

log4j-mongodb4/src/test/java/org/apache/logging/log4j/mongodb4/MongoDb4ProviderTest.java

Lines changed: 35 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,100 +22,94 @@
2222

2323
class MongoDb4ProviderTest {
2424

25-
private String validConnectionStringWithoutDatabase = "mongodb://localhost:27017";
26-
private String validConnectionStringWithDatabase = "mongodb://localhost:27017/logging";
27-
private String validConnectionStringWithDatabaseAndCollection = "mongodb://localhost:27017/logging.logs";
25+
private static final String CON_STR_WO_DB = "mongodb://localhost:27017";
26+
private static final String CON_STR_W_DB = "mongodb://localhost:27017/logging";
27+
private static final String CON_STR_DB_COLL = "mongodb://localhost:27017/logging.logs";
2828

29-
private String collectionName = "logsTest";
30-
private String databaseName = "loggingTest";
29+
private static final String collectionName = "logsTest";
30+
private static final String databaseName = "loggingTest";
3131

3232
@Test
3333
void createProviderWithDatabaseAndCollectionProvidedViaConfig() {
3434

3535
MongoDb4Provider provider = MongoDb4Provider.newBuilder()
36-
.setConnectionStringSource(this.validConnectionStringWithoutDatabase)
37-
.setDatabaseName(this.databaseName)
38-
.setCollectionName(this.collectionName)
36+
.setConnectionStringSource(CON_STR_WO_DB)
37+
.setDatabaseName(databaseName)
38+
.setCollectionName(collectionName)
3939
.build();
4040

41-
assertNotNull(provider, "Returned provider is null");
41+
assertNotNull(provider);
4242
assertEquals(
43-
this.collectionName,
44-
provider.getConnection().getCollection().getNamespace().getCollectionName(),
45-
"Collection names do not match");
43+
collectionName,
44+
provider.getConnection().getCollection().getNamespace().getCollectionName());
4645
assertEquals(
47-
this.databaseName,
48-
provider.getConnection().getCollection().getNamespace().getDatabaseName(),
49-
"Database names do not match");
46+
databaseName,
47+
provider.getConnection().getCollection().getNamespace().getDatabaseName());
5048
}
5149

5250
@Test
5351
void createProviderWithoutDatabaseName() {
5452

5553
MongoDb4Provider provider = MongoDb4Provider.newBuilder()
56-
.setConnectionStringSource(this.validConnectionStringWithoutDatabase)
54+
.setConnectionStringSource(CON_STR_WO_DB)
5755
.build();
5856

59-
assertNull(provider, "Provider should be null but was not");
57+
assertNull(provider);
6058
}
6159

6260
@Test
6361
void createProviderWithoutDatabaseNameWithCollectionName() {
6462

6563
MongoDb4Provider provider = MongoDb4Provider.newBuilder()
66-
.setConnectionStringSource(this.validConnectionStringWithoutDatabase)
67-
.setCollectionName(this.collectionName)
64+
.setConnectionStringSource(CON_STR_WO_DB)
65+
.setCollectionName(collectionName)
6866
.build();
6967

70-
assertNull(provider, "Provider should be null but was not");
68+
assertNull(provider);
7169
}
7270

7371
@Test
7472
void createProviderWithoutCollectionName() {
7573

7674
MongoDb4Provider provider = MongoDb4Provider.newBuilder()
77-
.setConnectionStringSource(this.validConnectionStringWithoutDatabase)
78-
.setDatabaseName(this.databaseName)
75+
.setConnectionStringSource(CON_STR_WO_DB)
76+
.setDatabaseName(databaseName)
7977
.build();
8078

81-
assertNull(provider, "Provider should be null but was not");
79+
assertNull(provider);
8280
}
8381

8482
@Test
8583
void createProviderWithDatabaseOnConnectionString() {
8684
MongoDb4Provider provider = MongoDb4Provider.newBuilder()
87-
.setConnectionStringSource(this.validConnectionStringWithDatabase)
88-
.setCollectionName(this.collectionName)
85+
.setConnectionStringSource(CON_STR_W_DB)
86+
.setCollectionName(collectionName)
8987
.build();
9088

91-
assertNotNull(provider, "Provider should not be null");
89+
assertNotNull(provider);
9290
assertEquals(
93-
this.collectionName,
94-
provider.getConnection().getCollection().getNamespace().getCollectionName(),
95-
"Provider should be null but was not");
91+
collectionName,
92+
provider.getConnection().getCollection().getNamespace().getCollectionName());
9693
assertEquals(
9794
"logging",
98-
provider.getConnection().getCollection().getNamespace().getDatabaseName(),
99-
"Database names do not match");
95+
provider.getConnection().getCollection().getNamespace().getDatabaseName());
10096
}
10197

10298
@Test
10399
void createProviderConfigOverridesConnectionString() {
104100

105101
MongoDb4Provider provider = MongoDb4Provider.newBuilder()
106-
.setConnectionStringSource(this.validConnectionStringWithDatabaseAndCollection)
107-
.setCollectionName(this.collectionName)
108-
.setDatabaseName(this.databaseName)
102+
.setConnectionStringSource(CON_STR_DB_COLL)
103+
.setCollectionName(collectionName)
104+
.setDatabaseName(databaseName)
109105
.build();
110106

111-
assertNotNull(provider, "Provider should not be null");
107+
assertNotNull(provider);
112108
assertEquals(
113-
this.collectionName,
114-
provider.getConnection().getCollection().getNamespace().getCollectionName(),
115-
"Collection name does not match provided configuration");
109+
collectionName,
110+
provider.getConnection().getCollection().getNamespace().getCollectionName());
116111
assertEquals(
117-
this.databaseName,
118-
provider.getConnection().getCollection().getNamespace().getDatabaseName(),
119-
"Database name does not match provided configuration");
112+
databaseName,
113+
provider.getConnection().getCollection().getNamespace().getDatabaseName());
120114
}
121115
}

0 commit comments

Comments
 (0)