Skip to content

Commit

Permalink
adding unit test to check that loggers respect the property log4jdbc.…
Browse files Browse the repository at this point in the history
…suppress.generated.keys.exception
  • Loading branch information
[email protected] committed Jun 6, 2014
1 parent f351951 commit b162c91
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ public CheckLoggingFunctionalites()
{
super();
}

/**
* Init properties common to tests for any logger. Extending classes should override
* this method with a tag {@code BeforeClass}, and call this parent method.
*/
public static void initProperties() {
System.setProperty("log4jdbc.suppress.generated.keys.exception", "true");
}

/**
* Reinit the properties common to tests for any logger. Extending classes should override
* this method with a tag {@code AfterClass}, and call this parent method.
*/
public static void reinitProperties() {
System.clearProperty("log4jdbc.suppress.generated.keys.exception");
}

/**
* A test which goes through the whole process => Connection, statement, ResultSet
Expand Down Expand Up @@ -359,6 +375,37 @@ public void checkExceptionOccured() throws IOException
mock.deregister();

}

/**
* Check that loggers respect the property
* {@code log4jdbc.suppress.generated.keys.exception}, that is set to {@code true}
* by the method {@code #initProperties()}.
*/
@Test
public void checkFilteredExceptionOccured() throws IOException {
// Creation of fake parameters
Exception e = new Exception("test");
// Create a fake connection using Mockito

MockDriverUtils mock = new MockDriverUtils();

ConnectionSpy cs = new ConnectionSpy(mock.getMockConnection(),
TestSpyLogDelegator);

// Run the method after ensuring the log file is empty
emptyLogFile();
TestSpyLogDelegator.exceptionOccured(cs,
SpyLogDelegator.GET_GENERATED_KEYS_METHOD_CALL, e, "SELECT * FROM Test", 1L);

// Check the result

assertFalse("The logger did not respect the property " +
"log4jdbc.suppress.generated.keys.exception",
CheckLoggingFunctionalites.readLogFile(1).contains(" ERROR "));

// clean all mock objects
mock.deregister();
}

/**
* Unit test for the method methodReturned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected Logger getLogger()
@BeforeClass
public static void initProperties()
{
CheckLoggingFunctionalites.initProperties();
//with JDBC 3 we need to load the DriverSpy manually, but loading
//Log4j2SpyLogDelegator will do it.
System.setProperty("log4jdbc.spylogdelegator.name",
Expand All @@ -53,6 +54,7 @@ public static void initProperties()
@AfterClass
public static void reinitProperties()
{
CheckLoggingFunctionalites.reinitProperties();
System.clearProperty("log4jdbc.spylogdelegator.name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected Logger getLogger()
@BeforeClass
public static void initProperties()
{
CheckLoggingFunctionalites.initProperties();
//with JDBC 3 we need to load the DriverSpy manually, but loading
//Slf4jSpyLogDelegator will do it.
System.setProperty("log4jdbc.spylogdelegator.name",
Expand All @@ -54,6 +55,7 @@ public static void initProperties()
@AfterClass
public static void reinitProperties()
{
CheckLoggingFunctionalites.reinitProperties();
System.clearProperty("log4jdbc.spylogdelegator.name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ public CheckLoggingFunctionalites()
{
super();
}

/**
* Init properties common to tests for any logger. Extending classes should override
* this method with a tag {@code BeforeClass}, and call this parent method.
*/
public static void initProperties() {
System.setProperty("log4jdbc.suppress.generated.keys.exception", "true");
}

/**
* Reinit the properties common to tests for any logger. Extending classes should override
* this method with a tag {@code AfterClass}, and call this parent method.
*/
public static void reinitProperties() {
System.clearProperty("log4jdbc.suppress.generated.keys.exception");
}

/**
* A test which goes through the whole process => Connection, statement, ResultSet
Expand Down Expand Up @@ -361,6 +377,37 @@ public void checkExceptionOccured() throws IOException
mock.deregister();

}

/**
* Check that loggers respect the property
* {@code log4jdbc.suppress.generated.keys.exception}, that is set to {@code true}
* by the method {@code #initProperties()}.
*/
@Test
public void checkFilteredExceptionOccured() throws IOException {
// Creation of fake parameters
Exception e = new Exception("test");
// Create a fake connection using Mockito

MockDriverUtils mock = new MockDriverUtils();

ConnectionSpy cs = new ConnectionSpy(mock.getMockConnection(),
TestSpyLogDelegator);

// Run the method after ensuring the log file is empty
emptyLogFile();
TestSpyLogDelegator.exceptionOccured(cs,
SpyLogDelegator.GET_GENERATED_KEYS_METHOD_CALL, e, "SELECT * FROM Test", 1L);

// Check the result

assertFalse("The logger did not respect the property " +
"log4jdbc.suppress.generated.keys.exception",
CheckLoggingFunctionalites.readLogFile(1).contains(" ERROR "));

// clean all mock objects
mock.deregister();
}

/**
* Unit test for the method methodReturned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected Logger getLogger()
@BeforeClass
public static void initProperties()
{
CheckLoggingFunctionalites.initProperties();
System.setProperty("log4jdbc.spylogdelegator.name",
Log4j2SpyLogDelegator.class.getName());
TestSpyLogDelegator = SpyLogFactory.getSpyLogDelegator();
Expand All @@ -51,6 +52,7 @@ public static void initProperties()
@AfterClass
public static void reinitProperties()
{
CheckLoggingFunctionalites.reinitProperties();
System.clearProperty("log4jdbc.spylogdelegator.name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected Logger getLogger()
@BeforeClass
public static void initProperties()
{
CheckLoggingFunctionalites.initProperties();
System.setProperty("log4jdbc.spylogdelegator.name",
Slf4jSpyLogDelegator.class.getName());
TestSpyLogDelegator = SpyLogFactory.getSpyLogDelegator();
Expand All @@ -52,6 +53,7 @@ public static void initProperties()
@AfterClass
public static void reinitProperties()
{
CheckLoggingFunctionalites.reinitProperties();
System.clearProperty("log4jdbc.spylogdelegator.name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ public CheckLoggingFunctionalites()
{
super();
}

/**
* Init properties common to tests for any logger. Extending classes should override
* this method with a tag {@code BeforeClass}, and call this parent method.
*/
public static void initProperties() {
System.setProperty("log4jdbc.suppress.generated.keys.exception", "true");
}

/**
* Reinit the properties common to tests for any logger. Extending classes should override
* this method with a tag {@code AfterClass}, and call this parent method.
*/
public static void reinitProperties() {
System.clearProperty("log4jdbc.suppress.generated.keys.exception");
}

/**
* A test which goes through the whole process => Connection, statement, ResultSet
Expand Down Expand Up @@ -360,6 +376,37 @@ public void checkExceptionOccured() throws IOException
mock.deregister();

}

/**
* Check that loggers respect the property
* {@code log4jdbc.suppress.generated.keys.exception}, that is set to {@code true}
* by the method {@code #initProperties()}.
*/
@Test
public void checkFilteredExceptionOccured() throws IOException {
// Creation of fake parameters
Exception e = new Exception("test");
// Create a fake connection using Mockito

MockDriverUtils mock = new MockDriverUtils();

ConnectionSpy cs = new ConnectionSpy(mock.getMockConnection(),
TestSpyLogDelegator);

// Run the method after ensuring the log file is empty
emptyLogFile();
TestSpyLogDelegator.exceptionOccured(cs,
SpyLogDelegator.GET_GENERATED_KEYS_METHOD_CALL, e, "SELECT * FROM Test", 1L);

// Check the result

assertFalse("The logger did not respect the property " +
"log4jdbc.suppress.generated.keys.exception",
CheckLoggingFunctionalites.readLogFile(1).contains(" ERROR "));

// clean all mock objects
mock.deregister();
}

/**
* Unit test for the method methodReturned
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected Logger getLogger()
@BeforeClass
public static void initProperties()
{
CheckLoggingFunctionalites.initProperties();
System.setProperty("log4jdbc.spylogdelegator.name",
Log4j2SpyLogDelegator.class.getName());
TestSpyLogDelegator = SpyLogFactory.getSpyLogDelegator();
Expand All @@ -51,6 +52,7 @@ public static void initProperties()
@AfterClass
public static void reinitProperties()
{
CheckLoggingFunctionalites.reinitProperties();
System.clearProperty("log4jdbc.spylogdelegator.name");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected Logger getLogger()
@BeforeClass
public static void initProperties()
{
CheckLoggingFunctionalites.initProperties();
System.setProperty("log4jdbc.spylogdelegator.name",
Slf4jSpyLogDelegator.class.getName());
TestSpyLogDelegator = SpyLogFactory.getSpyLogDelegator();
Expand All @@ -52,6 +53,7 @@ public static void initProperties()
@AfterClass
public static void reinitProperties()
{
CheckLoggingFunctionalites.reinitProperties();
System.clearProperty("log4jdbc.spylogdelegator.name");
}

Expand Down

0 comments on commit b162c91

Please sign in to comment.