Skip to content

Commit d451aef

Browse files
hulkobappkarwasz
authored andcommitted
refactor log4j-jpa to use junit5
1 parent cbe7e91 commit d451aef

15 files changed

+294
-311
lines changed

log4j-jpa/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,6 @@
8686
<scope>test</scope>
8787
</dependency>
8888
<!-- Test Dependencies -->
89-
<dependency>
90-
<groupId>org.junit.vintage</groupId>
91-
<artifactId>junit-vintage-engine</artifactId>
92-
<scope>test</scope>
93-
</dependency>
9489
<dependency>
9590
<groupId>org.eclipse.persistence</groupId>
9691
<artifactId>org.eclipse.persistence.jpa</artifactId>

log4j-jpa/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/AbstractJpaAppenderTest.java

+58-59
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.db.jpa;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
23-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertTrue;
2424

2525
import java.io.ByteArrayOutputStream;
2626
import java.io.PrintWriter;
@@ -34,12 +34,11 @@
3434
import org.apache.logging.log4j.core.LoggerContext;
3535
import org.apache.logging.log4j.core.config.ConfigurationFactory;
3636
import org.apache.logging.log4j.core.config.DefaultConfiguration;
37-
import org.apache.logging.log4j.core.test.categories.Appenders;
3837
import org.apache.logging.log4j.status.StatusLogger;
39-
import org.junit.Test;
40-
import org.junit.experimental.categories.Category;
38+
import org.junit.jupiter.api.Tag;
39+
import org.junit.jupiter.api.Test;
4140

42-
@Category(Appenders.Jpa.class)
41+
@Tag("Appenders.Jpa")
4342
public abstract class AbstractJpaAppenderTest {
4443
private final String databaseType;
4544
private Connection connection;
@@ -68,8 +67,8 @@ public void tearDown() throws SQLException {
6867
try {
6968
final String appenderName = "databaseAppender";
7069
final Appender appender = context.getConfiguration().getAppender(appenderName);
71-
assertNotNull("The appender '" + appenderName + "' should not be null.", appender);
72-
assertTrue("The appender should be a JpaAppender.", appender instanceof JpaAppender);
70+
assertNotNull(appender, "The appender '" + appenderName + "' should not be null.");
71+
assertTrue(appender instanceof JpaAppender, "The appender should be a JpaAppender.");
7372
((JpaAppender) appender).getManager().close();
7473
} finally {
7574
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
@@ -108,44 +107,44 @@ public void testBaseJpaEntityAppender() throws SQLException {
108107
final Statement statement = this.connection.createStatement();
109108
final ResultSet resultSet = statement.executeQuery("SELECT * FROM jpaBaseLogEntry ORDER BY id");
110109

111-
assertTrue("There should be at least one row.", resultSet.next());
110+
assertTrue(resultSet.next(), "There should be at least one row.");
112111

113112
long date = resultSet.getTimestamp("eventDate").getTime();
114-
assertTrue("The date should be later than pre-logging (1).", date >= millis);
115-
assertTrue("The date should be earlier than now (1).", date <= System.currentTimeMillis());
116-
assertEquals("The level column is not correct (1).", "INFO", resultSet.getString("level"));
117-
assertEquals("The logger column is not correct (1).", logger1.getName(), resultSet.getString("logger"));
113+
assertTrue(date >= millis, "The date should be later than pre-logging (1).");
114+
assertTrue(date <= System.currentTimeMillis(), "The date should be earlier than now (1).");
115+
assertEquals("INFO", resultSet.getString("level"), "The level column is not correct (1).");
116+
assertEquals(logger1.getName(), resultSet.getString("logger"), "The logger column is not correct (1).");
118117
assertEquals(
119-
"The message column is not correct (1).", "Test my message 01.", resultSet.getString("message"));
120-
assertNull("The exception column is not correct (1).", resultSet.getString("exception"));
118+
"Test my message 01.", resultSet.getString("message"), "The message column is not correct (1).");
119+
assertNull(resultSet.getString("exception"), "The exception column is not correct (1).");
121120

122-
assertTrue("There should be at least two rows.", resultSet.next());
121+
assertTrue(resultSet.next(), "There should be at least two rows.");
123122

124123
date = resultSet.getTimestamp("eventDate").getTime();
125-
assertTrue("The date should be later than pre-logging (2).", date >= millis);
126-
assertTrue("The date should be earlier than now (2).", date <= System.currentTimeMillis());
127-
assertEquals("The level column is not correct (2).", "ERROR", resultSet.getString("level"));
128-
assertEquals("The logger column is not correct (2).", logger1.getName(), resultSet.getString("logger"));
124+
assertTrue(date >= millis, "The date should be later than pre-logging (2).");
125+
assertTrue(date <= System.currentTimeMillis(), "The date should be earlier than now (2).");
126+
assertEquals("ERROR", resultSet.getString("level"), "The level column is not correct (2).");
127+
assertEquals(logger1.getName(), resultSet.getString("logger"), "The logger column is not correct (2).");
129128
assertEquals(
130-
"The message column is not correct (2).",
131129
"This is another message 02.",
132-
resultSet.getString("message"));
133-
assertEquals("The exception column is not correct (2).", stackTrace, resultSet.getString("exception"));
130+
resultSet.getString("message"),
131+
"The message column is not correct (2).");
132+
assertEquals(stackTrace, resultSet.getString("exception"), "The exception column is not correct (2).");
134133

135-
assertTrue("There should be three rows.", resultSet.next());
134+
assertTrue(resultSet.next(), "There should be three rows.");
136135

137136
date = resultSet.getTimestamp("eventDate").getTime();
138-
assertTrue("The date should be later than pre-logging (3).", date >= millis);
139-
assertTrue("The date should be earlier than now (3).", date <= System.currentTimeMillis());
140-
assertEquals("The level column is not correct (3).", "WARN", resultSet.getString("level"));
141-
assertEquals("The logger column is not correct (3).", logger2.getName(), resultSet.getString("logger"));
137+
assertTrue(date >= millis, "The date should be later than pre-logging (3).");
138+
assertTrue(date <= System.currentTimeMillis(), "The date should be earlier than now (3).");
139+
assertEquals("WARN", resultSet.getString("level"), "The level column is not correct (3).");
140+
assertEquals(logger2.getName(), resultSet.getString("logger"), "The logger column is not correct (3).");
142141
assertEquals(
143-
"The message column is not correct (3).",
144142
"A final warning has been issued.",
145-
resultSet.getString("message"));
146-
assertNull("The exception column is not correct (3).", resultSet.getString("exception"));
143+
resultSet.getString("message"),
144+
"The message column is not correct (3).");
145+
assertNull(resultSet.getString("exception"), "The exception column is not correct (3).");
147146

148-
assertFalse("There should not be four rows.", resultSet.next());
147+
assertFalse(resultSet.next(), "There should not be four rows.");
149148
} finally {
150149
this.tearDown();
151150
}
@@ -174,43 +173,43 @@ public void testBasicJpaEntityAppender() throws SQLException {
174173
final Statement statement = this.connection.createStatement();
175174
final ResultSet resultSet = statement.executeQuery("SELECT * FROM jpaBasicLogEntry ORDER BY id");
176175

177-
assertTrue("There should be at least one row.", resultSet.next());
176+
assertTrue(resultSet.next(), "There should be at least one row.");
178177

179178
long date = resultSet.getLong("timemillis");
180-
assertTrue("The date should be later than pre-logging (1).", date >= millis);
181-
assertTrue("The date should be earlier than now (1).", date <= System.currentTimeMillis());
182-
assertEquals("The level column is not correct (1).", "DEBUG", resultSet.getString("level"));
183-
assertEquals("The logger column is not correct (1).", logger1.getName(), resultSet.getString("loggerName"));
184-
assertEquals("The message column is not correct (1).", "Test my debug 01.", resultSet.getString("message"));
185-
assertNull("The exception column is not correct (1).", resultSet.getString("thrown"));
179+
assertTrue(date >= millis, "The date should be later than pre-logging (1).");
180+
assertTrue(date <= System.currentTimeMillis(), "The date should be earlier than now (1).");
181+
assertEquals("DEBUG", resultSet.getString("level"), "The level column is not correct (1).");
182+
assertEquals(logger1.getName(), resultSet.getString("loggerName"), "The logger column is not correct (1).");
183+
assertEquals("Test my debug 01.", resultSet.getString("message"), "The message column is not correct (1).");
184+
assertNull(resultSet.getString("thrown"), "The exception column is not correct (1).");
186185

187-
assertTrue("There should be at least two rows.", resultSet.next());
186+
assertTrue(resultSet.next(), "There should be at least two rows.");
188187

189188
date = resultSet.getLong("timemillis");
190-
assertTrue("The date should be later than pre-logging (2).", date >= millis);
191-
assertTrue("The date should be earlier than now (2).", date <= System.currentTimeMillis());
192-
assertEquals("The level column is not correct (2).", "WARN", resultSet.getString("level"));
193-
assertEquals("The logger column is not correct (2).", logger1.getName(), resultSet.getString("loggerName"));
189+
assertTrue(date >= millis, "The date should be later than pre-logging (2).");
190+
assertTrue(date <= System.currentTimeMillis(), "The date should be earlier than now (2).");
191+
assertEquals("WARN", resultSet.getString("level"), "The level column is not correct (2).");
192+
assertEquals(logger1.getName(), resultSet.getString("loggerName"), "The logger column is not correct (2).");
194193
assertEquals(
195-
"The message column is not correct (2).",
196194
"This is another warning 02.",
197-
resultSet.getString("message"));
198-
assertEquals("The exception column is not correct (2).", stackTrace, resultSet.getString("thrown"));
195+
resultSet.getString("message"),
196+
"The message column is not correct (2).");
197+
assertEquals(stackTrace, resultSet.getString("thrown"), "The exception column is not correct (2).");
199198

200-
assertTrue("There should be three rows.", resultSet.next());
199+
assertTrue(resultSet.next(), "There should be three rows.");
201200

202201
date = resultSet.getLong("timemillis");
203-
assertTrue("The date should be later than pre-logging (3).", date >= millis);
204-
assertTrue("The date should be earlier than now (3).", date <= System.currentTimeMillis());
205-
assertEquals("The level column is not correct (3).", "FATAL", resultSet.getString("level"));
206-
assertEquals("The logger column is not correct (3).", logger2.getName(), resultSet.getString("loggerName"));
202+
assertTrue(date >= millis, "The date should be later than pre-logging (3).");
203+
assertTrue(date <= System.currentTimeMillis(), "The date should be earlier than now (3).");
204+
assertEquals("FATAL", resultSet.getString("level"), "The level column is not correct (3).");
205+
assertEquals(logger2.getName(), resultSet.getString("loggerName"), "The logger column is not correct (3).");
207206
assertEquals(
208-
"The message column is not correct (3).",
209207
"A fatal warning has been issued.",
210-
resultSet.getString("message"));
211-
assertNull("The exception column is not correct (3).", resultSet.getString("thrown"));
208+
resultSet.getString("message"),
209+
"The message column is not correct (3).");
210+
assertNull(resultSet.getString("thrown"), "The exception column is not correct (3).");
212211

213-
assertFalse("There should not be four rows.", resultSet.next());
212+
assertFalse(resultSet.next(), "There should not be four rows.");
214213
} finally {
215214
this.tearDown();
216215
}

log4j-jpa/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/JpaHsqldbAppenderTest.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.db.jpa;
1818

19-
import static org.junit.Assert.assertNull;
19+
import static org.junit.jupiter.api.Assertions.assertNull;
2020

2121
import java.sql.Connection;
2222
import java.sql.DriverManager;
2323
import java.sql.SQLException;
2424
import java.sql.Statement;
2525
import org.apache.logging.log4j.core.LogEvent;
26-
import org.apache.logging.log4j.core.test.categories.Appenders;
27-
import org.junit.Test;
28-
import org.junit.experimental.categories.Category;
26+
import org.junit.jupiter.api.Tag;
27+
import org.junit.jupiter.api.Test;
2928

30-
@Category(Appenders.Jpa.class)
29+
@Tag("Appenders.Jpa")
3130
public class JpaHsqldbAppenderTest extends AbstractJpaAppenderTest {
3231
private static final String USER_ID = "sa";
3332
private static final String PASSWORD = "123";
@@ -62,47 +61,47 @@ protected Connection setUpConnection() throws SQLException {
6261
public void testNoEntityClassName() {
6362
final JpaAppender appender = JpaAppender.createAppender("name", null, null, null, null, "jpaAppenderTestUnit");
6463

65-
assertNull("The appender should be null.", appender);
64+
assertNull(appender, "The appender should be null.");
6665
}
6766

6867
@Test
6968
public void testNoPersistenceUnitName() {
7069
final JpaAppender appender =
7170
JpaAppender.createAppender("name", null, null, null, TestBaseEntity.class.getName(), null);
7271

73-
assertNull("The appender should be null.", appender);
72+
assertNull(appender, "The appender should be null.");
7473
}
7574

7675
@Test
7776
public void testBadEntityClassName() {
7877
final JpaAppender appender =
7978
JpaAppender.createAppender("name", null, null, null, "com.foo.Bar", "jpaAppenderTestUnit");
8079

81-
assertNull("The appender should be null.", appender);
80+
assertNull(appender, "The appender should be null.");
8281
}
8382

8483
@Test
8584
public void testNonLogEventEntity() {
8685
final JpaAppender appender =
8786
JpaAppender.createAppender("name", null, null, null, Object.class.getName(), "jpaAppenderTestUnit");
8887

89-
assertNull("The appender should be null.", appender);
88+
assertNull(appender, "The appender should be null.");
9089
}
9190

9291
@Test
9392
public void testBadConstructorEntity01() {
9493
final JpaAppender appender = JpaAppender.createAppender(
9594
"name", null, null, null, BadConstructorEntity1.class.getName(), "jpaAppenderTestUnit");
9695

97-
assertNull("The appender should be null.", appender);
96+
assertNull(appender, "The appender should be null.");
9897
}
9998

10099
@Test
101100
public void testBadConstructorEntity02() {
102101
final JpaAppender appender = JpaAppender.createAppender(
103102
"name", null, null, null, BadConstructorEntity2.class.getName(), "jpaAppenderTestUnit");
104103

105-
assertNull("The appender should be null.", appender);
104+
assertNull(appender, "The appender should be null.");
106105
}
107106

108107
@SuppressWarnings("unused")

log4j-jpa/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/LogEventEntityTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.db.jpa;
1818

19+
import static org.junit.jupiter.api.Assertions.assertNotSame;
20+
1921
import java.util.Map;
2022
import org.apache.logging.log4j.Level;
2123
import org.apache.logging.log4j.Marker;
@@ -25,8 +27,7 @@
2527
import org.apache.logging.log4j.core.time.Instant;
2628
import org.apache.logging.log4j.core.time.MutableInstant;
2729
import org.apache.logging.log4j.message.Message;
28-
import org.junit.Assert;
29-
import org.junit.Test;
30+
import org.junit.jupiter.api.Test;
3031

3132
public class LogEventEntityTest {
3233

@@ -116,12 +117,12 @@ public long getTimeMillis() {
116117
return 0;
117118
}
118119
};
119-
Assert.assertNotSame(logEvent, logEvent.toImmutable());
120+
assertNotSame(logEvent, logEvent.toImmutable());
120121
}
121122

122123
@Test
123124
public void testToImmutable_TestBaseEntity() {
124125
final LogEvent logEvent = new TestBaseEntity();
125-
Assert.assertNotSame(logEvent, logEvent.toImmutable());
126+
assertNotSame(logEvent, logEvent.toImmutable());
126127
}
127128
}

log4j-jpa/src/test/java/org/apache/logging/log4j/core/appender/db/jpa/converter/ContextDataAttributeConverterTest.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,21 @@
1616
*/
1717
package org.apache.logging.log4j.core.appender.db.jpa.converter;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNull;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertNull;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

22-
import org.apache.logging.log4j.core.test.categories.Appenders;
2323
import org.apache.logging.log4j.util.SortedArrayStringMap;
2424
import org.apache.logging.log4j.util.StringMap;
25-
import org.junit.Before;
26-
import org.junit.Test;
27-
import org.junit.experimental.categories.Category;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Tag;
27+
import org.junit.jupiter.api.Test;
2828

29-
@Category(Appenders.Jpa.class)
29+
@Tag("Appenders.Jpa")
3030
public class ContextDataAttributeConverterTest {
3131
private ContextDataAttributeConverter converter;
3232

33-
@Before
33+
@BeforeEach
3434
public void setUp() {
3535
this.converter = new ContextDataAttributeConverter();
3636
}
@@ -42,7 +42,7 @@ public void testConvertToDatabaseColumn01() {
4242
map.putValue("key2", "value2");
4343

4444
assertEquals(
45-
"The converted value is not correct.", map.toString(), this.converter.convertToDatabaseColumn(map));
45+
map.toString(), this.converter.convertToDatabaseColumn(map), "The converted value is not correct.");
4646
}
4747

4848
@Test
@@ -53,16 +53,16 @@ public void testConvertToDatabaseColumn02() {
5353
map.putValue("myKey", "yourValue");
5454

5555
assertEquals(
56-
"The converted value is not correct.", map.toString(), this.converter.convertToDatabaseColumn(map));
56+
map.toString(), this.converter.convertToDatabaseColumn(map), "The converted value is not correct.");
5757
}
5858

5959
@Test
6060
public void testConvertNullToDatabaseColumn() {
61-
assertNull("The converted value should be null.", this.converter.convertToDatabaseColumn(null));
61+
assertNull(this.converter.convertToDatabaseColumn(null), "The converted value should be null.");
6262
}
6363

64-
@Test(expected = UnsupportedOperationException.class)
64+
@Test
6565
public void testConvertToEntityAttribute() {
66-
this.converter.convertToEntityAttribute(null);
66+
assertThrows(UnsupportedOperationException.class, () -> this.converter.convertToEntityAttribute(null));
6767
}
6868
}

0 commit comments

Comments
 (0)