/** * Sets the log level. * * @param clazz the class for the logger. * @param logLevel the log level to set. */ protected void setLogLevel(Class clazz, LogLevel logLevel) { loggingHelper.setLogLevel(clazz, logLevel); }
/** * Adds a test appender. * * @param appenderName the appender name to add. * * @return the string writer associated with the writer appender. */ protected StringWriter addLoggingWriterAppender(String appenderName) { return loggingHelper.addLoggingWriterAppender(appenderName); }
/** * Executes a command without logging. The logging will be temporarily turned off during the execution of the command and then restored once the command has * finished executing. * * @param loggingClasses the list of logging classes to turn off * @param command the command to execute * * @throws Exception if any errors were encountered */ protected void executeWithoutLogging(List<Class<?>> loggingClasses, Command command) throws Exception { loggingHelper.executeWithoutLogging(loggingClasses, command); }
/** * Cleanup the Log4J files created. * * @param configPath the configuration path. * @param outputPath the output path. * * @throws IOException if any problems were encountered while cleaning up the files. */ private void cleanup(Path configPath, Path outputPath) throws IOException { // Shutdown the logging which will release the lock on the output file. loggingHelper.shutdownLogging(); // Delete the Log4J configuration we created in the setup. if (Files.exists(configPath)) { Files.delete(configPath); } // If we created a Log4J output file (not always), then delete it. if (Files.exists(outputPath)) { Files.delete(outputPath); } } }
/** * Gets the log level for the specified logger. * * @param clazz the class for the logger. */ protected LogLevel getLogLevel(Class clazz) { return loggingHelper.getLogLevel(clazz); }
/** * Removes a logging appender. * * @param appenderName the appender name. */ protected void removeLoggingAppender(String appenderName) { loggingHelper.removeLoggingAppender(appenderName); }
@PostConstruct public void initialize() throws MalformedURLException, FactoryConfigurationError { /* * Explicitly set the log4j configuration file location. This configuration is only necessary at the core layer of the application. The higher layers * will use the configurer override provided by the DAO layer test configurations. */ loggingHelper.initLogging("test", "classpath:herd-log4j-test.xml"); } }
@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); } }
/** * Gets the log level for the specified logger. * * @param loggerName the logger name to get the level for. */ protected LogLevel getLogLevel(String loggerName) { return loggingHelper.getLogLevel(loggerName); }
/** * Sets the log level. * * @param loggerName the logger name (e.g. Myclass.class.getName()). * @param logLevel the log level to set. */ protected void setLogLevel(String loggerName, LogLevel logLevel) { loggingHelper.setLogLevel(loggerName, logLevel); }
@Test public void testLog4JDbOverrideConfigurationClob() 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 CLOB column. insertDbLog4JConfigurationFromResourceLocation(LOG4J_CONFIG_FILENAME, 0, outputPath, ConfigurationEntity.COLUMN_VALUE_CLOB, configKey); // Shutdown the previously configured logging so we can reinitialize it below. loggingHelper.shutdownLogging(); // Setup the Log4J overridable configurer to use the database location - using the standard CLOB column. Log4jOverridableConfigurer log4jConfigurer = getLog4jOverridableConfigurerForDb(configKey); 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); } }
/** * Executes a command without logging. The logging will be temporarily turned off during the execution of the command and then restored once the command has * finished executing. * * @param loggingClass the logging class to turn off. If null is specified, the command will be executed with no logging changes * @param command the command to execute * * @throws Exception if any errors were encountered. */ protected void executeWithoutLogging(Class<?> loggingClass, Command command) throws Exception { loggingHelper.executeWithoutLogging(loggingClass, command); }
/** * Creates the appropriate business object data entries for an upload. * * @param businessObjectDataStatusCode the target business object data status. * * @return the upload single initiation response created during the upload flow. */ public UploadSingleInitiationResponse createUploadedFileData(String businessObjectDataStatusCode) { loggingHelper.setLogLevel(UploadDownloadHelperServiceImpl.class, LogLevel.OFF); // Create source and target business object formats database entities which are required to initiate an upload. createDatabaseEntitiesForUploadDownloadTesting(); // Initiate a file upload. UploadSingleInitiationResponse resultUploadSingleInitiationResponse = uploadDownloadService.initiateUploadSingle(createUploadSingleInitiationRequest()); // Complete the upload. uploadDownloadService.performCompleteUploadSingleMessage( resultUploadSingleInitiationResponse.getSourceBusinessObjectData().getStorageUnits().get(0).getStorageFiles().get(0).getFilePath()); // Update the target business object data status to valid. Normally this would happen as part of the completion request, but since the status update // happens asynchronously, this will not happen within a unit test context which is why we are setting it explicitly. businessObjectDataDao.getBusinessObjectDataByAltKey( businessObjectDataHelper.getBusinessObjectDataKey(resultUploadSingleInitiationResponse.getTargetBusinessObjectData())) .setStatus(businessObjectDataStatusDao.getBusinessObjectDataStatusByCode(businessObjectDataStatusCode)); resultUploadSingleInitiationResponse.getTargetBusinessObjectData().setStatus(businessObjectDataStatusCode); // Return the initiate upload single response. return resultUploadSingleInitiationResponse; }
loggingHelper.shutdownLogging();
@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); } }