|
From: <jbo...@li...> - 2006-06-15 03:46:01
|
Author: mic...@jb...
Date: 2006-06-14 23:45:54 -0400 (Wed, 14 Jun 2006)
New Revision: 4764
Modified:
labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderTest.java
Log:
JBRULES-311
Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2006-06-15 03:44:45 UTC (rev 4763)
+++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2006-06-15 03:45:54 UTC (rev 4764)
@@ -19,9 +19,11 @@
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
+import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import org.apache.commons.jci.compilers.CompilationResult;
import org.apache.commons.jci.compilers.JavaCompiler;
@@ -160,11 +162,9 @@
public void addPackage(final PackageDescr packageDescr) {
- if ( packageDescr.getName() == null || "".equals( packageDescr.getName() ) ) {
+ validatePackageName( packageDescr );
+ validateUniqueRuleNames( packageDescr );
- throw new MissingPackageNameException( "Missing package name for rule package." );
- }
-
if ( this.pkg != null ) {
//mergePackage( packageDescr ) ;
mergePackage( this.pkg,
@@ -187,6 +187,25 @@
}
}
+ private void validatePackageName(final PackageDescr packageDescr) {
+ if ( packageDescr.getName() == null || "".equals( packageDescr.getName() ) ) {
+
+ throw new MissingPackageNameException( "Missing package name for rule package." );
+ }
+ }
+
+ private void validateUniqueRuleNames(final PackageDescr packageDescr) {
+ Set names = new HashSet();
+ for ( Iterator iter = packageDescr.getRules().iterator(); iter.hasNext(); ) {
+ RuleDescr rule = (RuleDescr) iter.next();
+ String name = rule.getName();
+ if (names.contains( name )) {
+ this.results.add( new ParserError("Duplicate rule name: " + name, rule.getLine(), rule.getColumn()) );
+ }
+ names.add( name );
+ }
+ }
+
private Package newPackage(final PackageDescr packageDescr) {
final Package pkg = new Package( packageDescr.getName(),
this.configuration.getClassLoader() );
Modified: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderTest.java
===================================================================
--- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderTest.java 2006-06-15 03:44:45 UTC (rev 4763)
+++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/PackageBuilderTest.java 2006-06-15 03:45:54 UTC (rev 4764)
@@ -725,7 +725,64 @@
assertLength( 0,
builder.getErrors() );
}
+
+ public void testDuplicateRuleNames() throws Exception {
+
+ final PackageBuilder builder = new PackageBuilder();
+ final PackageDescr packageDescr = new PackageDescr( "p1" );
+
+ RuleDescr ruleDescr = new RuleDescr( "rule-1" );
+ packageDescr.addRule( ruleDescr );
+ AndDescr lhs = new AndDescr();
+ ruleDescr.setLhs( lhs );
+ ColumnDescr columnDescr = new ColumnDescr( Cheese.class.getName(),
+ "stilton" );
+ LiteralDescr literalDescr = new LiteralDescr( "type",
+ "==",
+ null );
+ columnDescr.addDescr( literalDescr );
+ ruleDescr.setConsequence( "" );
+
+ ruleDescr = new RuleDescr( "rule-1" );
+ ruleDescr.setLocation( 42, 43 );
+ packageDescr.addRule( ruleDescr );
+ lhs = new AndDescr();
+ ruleDescr.setLhs( lhs );
+ columnDescr = new ColumnDescr( Cheese.class.getName(),
+ "stilton" );
+ literalDescr = new LiteralDescr( "type",
+ "!=",
+ null );
+ columnDescr.addDescr( literalDescr );
+ ruleDescr.setConsequence( "" );
+
+
+ ruleDescr = new RuleDescr( "rule-2" );
+ ruleDescr.setLocation( 42, 43 );
+ packageDescr.addRule( ruleDescr );
+ lhs = new AndDescr();
+ ruleDescr.setLhs( lhs );
+ columnDescr = new ColumnDescr( Cheese.class.getName(),
+ "stilton" );
+ literalDescr = new LiteralDescr( "type",
+ "!=",
+ null );
+ columnDescr.addDescr( literalDescr );
+ ruleDescr.setConsequence( "" );
+
+ builder.addPackage( packageDescr );
+
+
+
+ assertLength( 1,
+ builder.getErrors() );
+ ParserError err = (ParserError) builder.getErrors()[0];
+ assertEquals(42, err.getRow());
+ assertEquals(43, err.getCol());
+
+ }
+
private void createReturnValueRule(final PackageDescr packageDescr,
final String expression) {
final RuleDescr ruleDescr = new RuleDescr( "rule-1" );
|