public void testPatterns() throws Exception {
StatelessSession session = getStatelessSession( this.getClass().getResourceAsStream( "Patterns.drl" ) );
session.setAgendaFilter( new RuleNameMatchesAgendaFilter( "Pattern that is always true" ) );
VerifierReport result = VerifierReportFactory.newVerifierReport();
Collection<Object> data = new ArrayList<Object>();
session.setGlobal( "result",
result );
VerifierRule rule1 = VerifierComponentMockFactory.createRule1();
// This pattern is always true.
Pattern pattern1 = VerifierComponentMockFactory.createPattern1();
SubPattern pp1 = new SubPattern( pattern1,
0 );
AlwaysTrue alwaysTrue1 = new AlwaysTrue( pp1 );
SubPattern pp2 = new SubPattern( pattern1,
1 );
AlwaysTrue alwaysTrue2 = new AlwaysTrue( pp2 );
// This pattern is okay.
Pattern pattern2 = new Pattern( rule1 );
pattern2.setName( "testPattern2" );
SubPattern pp3 = new SubPattern( pattern2,
0 );
SubPattern pp4 = new SubPattern( pattern2,
1 );
AlwaysTrue alwaysTrue4 = new AlwaysTrue( pp4 );
data.add( rule1 );
data.add( pattern1 );
data.add( pp1 );
data.add( pp2 );
data.add( alwaysTrue1 );
data.add( alwaysTrue2 );
data.add( pattern2 );
data.add( pp3 );
data.add( pp4 );
data.add( alwaysTrue4 );
session.executeWithResults( data );
Iterator<VerifierMessageBase> iter = result.getBySeverity( Severity.NOTE ).iterator();
boolean works = false;
while ( iter.hasNext() ) {
Object o = (Object) iter.next();
if ( o instanceof VerifierMessage ) {
VerifierMessage message = (VerifierMessage) o;
if ( message.getFaulty().equals( pattern1 ) ) {
works = true;
} else {
fail( "There can be only one. (And this is not the one)" );
}
}
}
assertEquals( 0,
result.getBySeverity( Severity.ERROR ).size() );
assertEquals( 0,
result.getBySeverity( Severity.WARNING ).size() );
assertEquals( 1,
result.getBySeverity( Severity.NOTE ).size() );
assertTrue( works );
}