Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setDataSource(DaoSpringModuleConfig.getHerdDataSource()); log4jConfigurer.setTableName(ConfigurationEntity.TABLE_NAME); log4jConfigurer.setSelectColumn(ConfigurationEntity.COLUMN_VALUE_CLOB); log4jConfigurer.setWhereColumn(ConfigurationEntity.COLUMN_KEY); log4jConfigurer.setWhereValue(ConfigurationValue.LOG4J_OVERRIDE_CONFIGURATION.getKey()); log4jConfigurer.setOverrideResourceLocation(ConfigurationHelper.getProperty(ConfigurationValue.LOG4J_OVERRIDE_RESOURCE_LOCATION, environment)); log4jConfigurer.setDefaultResourceLocation("classpath:herd-log4j.xml");
returnString = convertClob((Clob) resultSetValue);
@Bean public static Log4jOverridableConfigurer log4jConfigurer() { Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setDefaultResourceLocation(TEST_LOG4J_CONFIG_RESOURCE_LOCATION); log4jConfigurer.setOverrideResourceLocation("non_existent_override_location"); return log4jConfigurer; } }
/** * Gets a newly created Log4J overridable configurer for a database. * * @param configKey the configuration key to use as the where value. * * @return the Log4J overridable configurer. */ private Log4jOverridableConfigurer getLog4jOverridableConfigurerForDb(String configKey) { Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setTableName(ConfigurationEntity.TABLE_NAME); log4jConfigurer.setSelectColumn(ConfigurationEntity.COLUMN_VALUE_CLOB); log4jConfigurer.setWhereColumn(ConfigurationEntity.COLUMN_KEY); log4jConfigurer.setWhereValue(configKey); log4jConfigurer.setDataSource(DaoSpringModuleConfig.getHerdDataSource()); log4jConfigurer.setApplicationContext(applicationContext); return log4jConfigurer; }
@Test public void testLog4JNonExistentOverrideLocation() throws Exception { Path configPath = getRandomLog4jConfigPath(); Path outputPath = getRandomLog4jOutputPath(); try { // Write a Log4J configuration file that will create a random output file. writeFileFromResourceLocation(LOG4J_CONFIG_FILENAME, configPath, outputPath, 0); Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setApplicationContext(applicationContext); log4jConfigurer.setDefaultResourceLocation(DaoEnvTestSpringModuleConfig.TEST_LOG4J_CONFIG_RESOURCE_LOCATION); log4jConfigurer.setOverrideResourceLocation("non_existent_override_location"); log4jConfigurer.postProcessBeforeInitialization(null, null); // Since an override location doesn't exist, the default location which doesn't create a log file will get used and the override log file won't get // created. assertTrue("Log4J output file exists, but shouldn't.", Files.notExists(outputPath)); } finally { cleanup(configPath, outputPath); } }
@Test public void testLog4JNoOverrideLocationOrDefaultLocation() throws Exception { Path configPath = getRandomLog4jConfigPath(); Path outputPath = getRandomLog4jOutputPath(); try { // Write a Log4J configuration file that will create a random output file. writeFileFromResourceLocation(LOG4J_CONFIG_FILENAME, configPath, outputPath, 0); Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setApplicationContext(applicationContext); log4jConfigurer.postProcessBeforeInitialization(null, null); // There is no database, override, or default location. This will display an error, but no logging will be configured. // An error will display on system.err which we don't have a way to check so we'll at least ensure that a Log4J output file didn't get created. assertTrue("Log4J output file exists, but shouldn't.", Files.notExists(outputPath)); } finally { cleanup(configPath, outputPath); } }
@Test public void testLog4JDbOverrideConfigurationNoClob() throws Exception { Path configPath = getRandomLog4jConfigPath(); Path outputPath = getRandomLog4jOutputPath(); String configKey = null; try { // Create a random configuration key to use when inserting the Log4J configuration into the database. configKey = ConfigurationValue.LOG4J_OVERRIDE_CONFIGURATION.getKey() + UUID.randomUUID().toString().substring(0, 5); // Insert the Log4J configuration into the database using the non-CLOB column. insertDbLog4JConfigurationFromResourceLocation(LOG4J_CONFIG_NO_CLOB_FILENAME, 0, outputPath, ConfigurationEntity.COLUMN_VALUE, configKey); // Shutdown the previously configured logging so we can reinitialize it below. loggingHelper.shutdownLogging(); // Setup the Log4J overridable configurer to use the database location, but override the select column to use the non-CLOB column. Log4jOverridableConfigurer log4jConfigurer = getLog4jOverridableConfigurerForDb(configKey); log4jConfigurer.setSelectColumn(ConfigurationEntity.COLUMN_VALUE); log4jConfigurer.postProcessBeforeInitialization(null, null); // The database override location does exist and should create a log file. assertTrue("Log4J output file doesn't exist, but should.", Files.exists(outputPath)); } finally { cleanup(configPath, outputPath); deleteDbLog4JConfiguration(configKey); } }
@Test public void testLog4JExistentOverrideLocation() throws Exception { Path configPath = getRandomLog4jConfigPath(); Path outputPath = getRandomLog4jOutputPath(); try { // Write a Log4J configuration file that will create a random output file. writeFileFromResourceLocation(LOG4J_CONFIG_FILENAME, configPath, outputPath, 1); loggingHelper.shutdownLogging(); Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setApplicationContext(applicationContext); log4jConfigurer.setDefaultResourceLocation(DaoEnvTestSpringModuleConfig.TEST_LOG4J_CONFIG_RESOURCE_LOCATION); log4jConfigurer.setOverrideResourceLocation(configPath.toAbsolutePath().toUri().toURL().toString()); log4jConfigurer.postProcessBeforeInitialization(null, null); // The override location does exist and should create a log file. assertTrue("Log4J output file doesn't exist, but should.", Files.exists(outputPath)); } finally { cleanup(configPath, outputPath); } }
/** * The Log4J configuration used by JUnits. It is defined in the DAO tier so all tiers that extend it can take advantage of it. * <p/> * IMPORTANT: Ensure this method is static since the returned Log4jOverridableConfigurer is a bean factory post processor (BFPP). If it weren't static, * autowiring and injection on this @Configuration class won't work due to lifecycle issues. See "Bootstrapping" comment in @Bean annotation for more * details. * * @return the Log4J overridable configurer. */ @Bean public static Log4jOverridableConfigurer log4jConfigurer() { Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setDefaultResourceLocation(TEST_LOG4J_CONFIG_RESOURCE_LOCATION); log4jConfigurer.setOverrideResourceLocation("non_existent_override_location"); return log4jConfigurer; }
@Test public void testLog4JNonExistentOverrideLocationAndDefaultLocation() throws Exception { Path configPath = getRandomLog4jConfigPath(); Path outputPath = getRandomLog4jOutputPath(); try { // Write a Log4J configuration file that will create a random output file. writeFileFromResourceLocation(LOG4J_CONFIG_FILENAME, configPath, outputPath, 0); Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setApplicationContext(applicationContext); log4jConfigurer.setDefaultResourceLocation("non_existent_default_location"); log4jConfigurer.setOverrideResourceLocation("non_existent_override_location"); log4jConfigurer.postProcessBeforeInitialization(null, null); // This is similar to testLog4JNoLocationOrDefaultLocation except we are specifying explicit invalid locations instead of no locations. assertTrue("Log4J output file exists, but shouldn't.", Files.notExists(outputPath)); } finally { cleanup(configPath, outputPath); } }
returnString = convertClob((Clob) resultSetValue);
@Test public void testLog4JBlankOverrideLocationAndDefaultLocation() throws Exception { Path configPath = getRandomLog4jConfigPath(); Path outputPath = getRandomLog4jOutputPath(); try { // Write a Log4J configuration file that will create a random output file. writeFileFromResourceLocation(LOG4J_CONFIG_FILENAME, configPath, outputPath, 0); Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setApplicationContext(applicationContext); log4jConfigurer.setDefaultResourceLocation(" "); log4jConfigurer.setOverrideResourceLocation(" "); log4jConfigurer.postProcessBeforeInitialization(null, null); // There is a blank default and override location so no log file will get created. // An error will display on system.err which we don't have a way to check so we'll at least ensure that a Log4J output file didn't get created. assertTrue("Log4J output file exists, but shouldn't.", Files.notExists(outputPath)); } finally { cleanup(configPath, outputPath); } }
@Test @Ignore // This works locally, but fails in Jenkins for some reason. We'll need to investigate at some point. public void testLog4JFileWithRefreshInterval() throws Exception { Path configPath = getRandomLog4jConfigPath(); Path outputPath = getRandomLog4jOutputPath(); try { // Write the standard JUnit Log4J configuration that won't create an output file. writeFileFromResourceLocation(DaoEnvTestSpringModuleConfig.TEST_LOG4J_CONFIG_RESOURCE_LOCATION, configPath, outputPath, 1); // Initialize Log4J with a refresh interval of 1/2 second. This will cause Log4J to check for configuration updates every second. Log4jOverridableConfigurer log4jConfigurer = new Log4jOverridableConfigurer(); log4jConfigurer.setApplicationContext(applicationContext); log4jConfigurer.setDefaultResourceLocation(DaoEnvTestSpringModuleConfig.TEST_LOG4J_CONFIG_RESOURCE_LOCATION); log4jConfigurer.setOverrideResourceLocation(configPath.toAbsolutePath().toUri().toURL().toString()); log4jConfigurer.postProcessBeforeInitialization(null, null); // First ensure that the Log4J output file doesn't exist. assertTrue("Log4J output file exists, but shouldn't.", Files.notExists(outputPath)); // Replace the Log4J configuration file with the one that will create an output file. writeFileFromResourceLocation(LOG4J_CONFIG_FILENAME, configPath, outputPath, 1); // Sleep one second which will give Log4J a chance to read the new configuration file which should create an output file. Thread.sleep(3000); // Ensure that the Log4J output file now exists. assertTrue("Log4J output file doesn't exist, but should.", Files.exists(outputPath)); } finally { cleanup(configPath, outputPath); } }