Skip to content

Commit

Permalink
Fix Cron example code
Browse files Browse the repository at this point in the history
  • Loading branch information
Roger Hughes committed Feb 22, 2014
1 parent 48aa0c7 commit e36ac01
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 93 deletions.
15 changes: 8 additions & 7 deletions error-track/src/main/java/com/captaindebug/errortrack/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
*/
package com.captaindebug.errortrack;

import org.springframework.beans.factory.access.BeanFactoryLocator;
import org.springframework.context.access.ContextSingletonBeanFactoryLocator;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
* Create an application context that loads the Spring application context.
*
* Note: DON'T close the context, as we want it to keep going after the main
* thread exits. Use the @SuppressWarnings annotation instead.
*
* @author Roger
*
*/
public class Main {

@SuppressWarnings("resource")
public static void main(String[] arg) {

// Load the ContextSingletonBeanFactoryLocator
BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");

// Get hold of the factory to use and start the quartz job.
factoryLocator.useBeanFactory("quartzContextFactory");
new ClassPathXmlApplicationContext("error-track-context.xml");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

Expand All @@ -35,7 +36,12 @@ public class FileValidator implements Validator {
private Integer extraLineCount;

@Autowired
private RegexValidator regexValidator;
@Qualifier("scan-for")
private RegexValidator scanForValidator;

@Autowired(required = false)
@Qualifier("exclude")
private RegexValidator excludeValidator;

@Autowired
private FileAgeValidator fileAgeValidator;
Expand Down Expand Up @@ -95,14 +101,22 @@ private boolean isNotNull(Object obj) {

private void processLine(String line, String filePath, int lineNumber, BufferedReader in) throws IOException {

if (regexValidator.validate(line)) {
if (validateExcludes(line) && scanForValidator.validate(line)) {
List<String> lines = new ArrayList<String>();
lines.add(line);
addExtraDetailLines(in, lines);
report.addResult(filePath, lineNumber, lines);
}
}

private boolean validateExcludes(String line) {
boolean retVal = true;
if (isNotNull(excludeValidator)) {
retVal = !excludeValidator.validate(line);
}
return retVal;
}

private void addExtraDetailLines(BufferedReader in, List<String> lines) throws IOException {

for (int i = 0; i < extraLineCount; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.captaindebug.errortrack.Validator;

/**
* A Regular expression validator.
*
* @author Roger
*
*/
@Service
public class RegexValidator implements InitializingBean, Validator {
public class RegexValidator implements Validator {

private static final Logger logger = LoggerFactory.getLogger(RegexValidator.class);

@Value("${scan.for}")
private String scanFor;
private final Pattern pattern;

private Pattern pattern;
public RegexValidator(String regex) {
pattern = Pattern.compile(regex);
logger.info("loaded regex: {}", regex);
}

@Override
public <T> boolean validate(T line) {
Expand All @@ -40,15 +40,4 @@ public <T> boolean validate(T line) {

return retVal;
}

/**
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {

pattern = Pattern.compile(scanFor);
logger.info("loaded regex: {}", scanFor);
}

}
3 changes: 2 additions & 1 deletion error-track/src/main/resources/app.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
scan.in=/Library/Tomcat/logs
# A regex defining what to look for - or what not to include
scan.for=^.*Exception.*
exclude=^.*IllegalStateException.*
# The number of following lines to add to the report
following.lines=10
# Where to email the report
email.to=info@captaindebug.com
# The max age of a file in days
max.days=1
max.days=1000
18 changes: 0 additions & 18 deletions error-track/src/main/resources/beanRefContext.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,44 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

<bean name="FileLocatorJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<context:component-scan base-package="com.captaindebug.errortrack" />


<bean id="scan-for" class="com.captaindebug.errortrack.validator.RegexValidator" >
<constructor-arg value="${scan.for}" />
</bean>
<bean id="exclude" class="com.captaindebug.errortrack.validator.RegexValidator" >
<constructor-arg value="${exclude}" />
</bean>

<!-- Configurer that replaces ${...} placeholders with values from a properties
file -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">
<list>
<value>app.properties</value>
<!-- List other property files here -->
<!-- value>mail.properties</value -->
</list>
</property>
</bean>


<bean id="FileLocatorJob"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">

<property name="targetObject" ref="fileLocator" />
<property name="targetMethod" value="findFile" />

</bean>
<!-- <bean name="DONOTUSE" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass"
value="com.captaindebug.errortrack.quartz.FileLocatorJob" />
</bean>

-->

<bean id="FileLocatorTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="FileLocatorJob" />
<!-- run every morning at 2 AM -->
Expand Down
23 changes: 0 additions & 23 deletions error-track/src/main/resources/node-context.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ public class FileValidatorTest {
private FileValidator instance;

@Mock
private RegexValidator regexValidator;
private RegexValidator scanForValidator;

@Mock
private RegexValidator excludeValidator;

@Mock
private FileAgeValidator fileAgeValidator;
Expand Down Expand Up @@ -56,7 +59,7 @@ BufferedReader createBufferedReader(File file) throws FileNotFoundException {
};

ReflectionTestUtils.setField(instance, "extraLineCount", Integer.valueOf(10));
ReflectionTestUtils.setField(instance, "regexValidator", regexValidator);
ReflectionTestUtils.setField(instance, "scanForValidator", scanForValidator);
ReflectionTestUtils.setField(instance, "fileAgeValidator", fileAgeValidator);
ReflectionTestUtils.setField(instance, "report", report);
}
Expand All @@ -80,7 +83,7 @@ public void testValiate_returns_true_when_valid_file_date_with_no_error_found()
final String aline = "This line is okay";
when(bufferedReader.readLine()).thenReturn(aline).thenReturn(null);

when(regexValidator.validate(aline)).thenReturn(false);
when(scanForValidator.validate(aline)).thenReturn(false);

boolean result = instance.validate(file);
assertTrue(result);
Expand All @@ -101,8 +104,58 @@ public void testValiate_returns_true_when_valid_file_date_with_and_error_found()
final String errorLine = "Exception blah blah blah";
when(bufferedReader.readLine()).thenReturn(aline).thenReturn(errorLine).thenReturn(null);

when(regexValidator.validate(aline)).thenReturn(false);
when(regexValidator.validate(errorLine)).thenReturn(true);
when(scanForValidator.validate(aline)).thenReturn(false);
when(scanForValidator.validate(errorLine)).thenReturn(true);

boolean result = instance.validate(file);
assertTrue(result);
verify(report).addFile(anyString());

verify(report).addResult(anyString(), anyInt(), (List<String>) anyObject());
}

@SuppressWarnings("unchecked")
@Test
public void testValiate_returns_false_when_valid_file_date_with_and_error_found_and_exclude_match() throws IOException {

ReflectionTestUtils.setField(instance, "excludeValidator", excludeValidator);

when(fileAgeValidator.validate(file)).thenReturn(true);
when(file.getPath()).thenReturn("The/File/Path/name.log");

final String aline = "This line is okay";
final String errorLine = "Exception blah blah blah";
when(bufferedReader.readLine()).thenReturn(aline).thenReturn(errorLine).thenReturn(null);

when(scanForValidator.validate(aline)).thenReturn(false);
when(scanForValidator.validate(errorLine)).thenReturn(true);
when(excludeValidator.validate(aline)).thenReturn(false);
when(excludeValidator.validate(errorLine)).thenReturn(true);

boolean result = instance.validate(file);
assertTrue(result);
verify(report).addFile(anyString());

verify(report, never()).addResult(anyString(), anyInt(), (List<String>) anyObject());
}

@SuppressWarnings("unchecked")
@Test
public void testValiate_returns_true_when_valid_file_date_with_and_error_found_and_exclude__no_match() throws IOException {

ReflectionTestUtils.setField(instance, "excludeValidator", excludeValidator);

when(fileAgeValidator.validate(file)).thenReturn(true);
when(file.getPath()).thenReturn("The/File/Path/name.log");

final String aline = "This line is okay";
final String errorLine = "Exception blah blah blah";
when(bufferedReader.readLine()).thenReturn(aline).thenReturn(errorLine).thenReturn(null);

when(scanForValidator.validate(aline)).thenReturn(false);
when(scanForValidator.validate(errorLine)).thenReturn(true);
when(excludeValidator.validate(aline)).thenReturn(false);
when(excludeValidator.validate(errorLine)).thenReturn(false);

boolean result = instance.validate(file);
assertTrue(result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;

import com.captaindebug.errortrack.validator.RegexValidator;

/**
* @author Roger
Expand All @@ -26,45 +23,39 @@ public class RegexValidatorTest {
@Before
public void setUp() throws Exception {

instance = new RegexValidator();
instance = new RegexValidator("^.*Exception.*");
}

/**
* Test method for {@link com.captaindebug.errortrack.validator.RegexValidator#validate(java.lang.String)}
* Test method for
* {@link com.captaindebug.errortrack.validator.RegexValidator#validate(java.lang.String)}
*/
@Test
public void testValidate_exception_found() throws Exception {

ReflectionTestUtils.setField(instance, "scanFor", "^.*Exception.*");
instance.afterPropertiesSet();

boolean result = instance
.validate("catalina.out:Exception in thread \"Studio Teletype\" java.lang.NoClassDefFoundError: org/apache/log4j/spi/ThrowableInformation");
assertTrue(result);
}

/**
* Test method for {@link com.captaindebug.errortrack.validator.RegexValidator#validate(java.lang.String)}
* Test method for
* {@link com.captaindebug.errortrack.validator.RegexValidator#validate(java.lang.String)}
*/
@Test
public void testValidate_no_exception_found() throws Exception {

ReflectionTestUtils.setField(instance, "scanFor", "^.*Exception.*");
instance.afterPropertiesSet();

boolean result = instance.validate("This is okay");
assertFalse(result);
}

/**
* Test method for {@link com.captaindebug.errortrack.validator.RegexValidator#validate(java.lang.String)}
* Test method for
* {@link com.captaindebug.errortrack.validator.RegexValidator#validate(java.lang.String)}
*/
@Test(expected = NullPointerException.class)
public void testValidate_null_line() throws Exception {

ReflectionTestUtils.setField(instance, "scanFor", "^.*Exception.*");
instance.afterPropertiesSet();

boolean result = instance.validate(null);
assertFalse(result);
}
Expand Down

0 comments on commit e36ac01

Please sign in to comment.