Permalink
Browse files

fix duplicate rule name check in incremental compilation

  • Loading branch information...
1 parent d27de27 commit 9a31825082976820beeba4d35ec61683f54e2981 @mariofusco mariofusco committed Mar 20, 2013
@@ -484,6 +484,9 @@ PackageDescr drlToPackageDescr(Resource resource) throws DroolsParserException,
}
hasErrors = parser.hasErrors();
}
+ if (pkg != null) {
+ pkg.setResource(resource);
+ }
return hasErrors ? null : pkg;
}
@@ -1171,10 +1174,23 @@ private void validateUniqueRuleNames( final PackageDescr packageDescr ) {
rule.getColumn(),
packageDescr.getNamespace() ) );
}
- if (pkg != null && pkg.getRule( name ) != null) {
- this.results.add( new DuplicateRule( rule,
- packageDescr,
- this.configuration ) );
+ if (pkg != null) {
+ Rule duplicatedRule = pkg.getRule( name );
+ if (duplicatedRule != null) {
+ Resource resource = rule.getResource();
+ Resource duplicatedResource = duplicatedRule.getResource();
+ if (resource == null || duplicatedResource == null || duplicatedResource.getSourcePath().equals(resource.getSourcePath())) {
+ this.results.add( new DuplicateRule( rule,
+ packageDescr,
+ this.configuration ) );
+ } else {
+ this.results.add( new ParserError( rule.getResource(),
+ "Duplicate rule name: " + name,
+ rule.getLine(),
+ rule.getColumn(),
+ packageDescr.getNamespace() ) );
+ }
+ }
}
names.add( name );
}
@@ -272,6 +272,41 @@ public void testIncrementalCompilationAddTwoErrorsThenRemove1Error() throws Exce
}
@Test
+ public void testIncrementalCompilationWithDuplicatedRule() throws Exception {
+ String drl1 = "package org.drools.compiler\n" +
+ "rule R1 when\n" +
+ " $m : Message()\n" +
+ "then\n" +
+ "end\n";
+
+ String drl2 = "package org.drools.compiler\n" +
+ "rule R2 when\n" +
+ " $m : Message( message == \"Hello World\" )\n" +
+ "then\n" +
+ "end\n";
+
+ KieServices ks = KieServices.Factory.get();
+
+ KieFileSystem kfs = ks.newKieFileSystem()
+ .write( "src/main/resources/r1.drl", drl1 );
+
+ KieBuilder kieBuilder = ks.newKieBuilder( kfs ).buildAll();
+ assertEquals( 0, kieBuilder.getResults().getMessages( org.kie.api.builder.Message.Level.ERROR ).size() );
+
+ kfs.write( "src/main/resources/r2_1.drl", drl2 );
+ IncrementalResults addResults = ( (InternalKieBuilder) kieBuilder ).createFileSet( "src/main/resources/r2_1.drl" ).build();
+
+ assertEquals( 0, addResults.getAddedMessages().size() );
+ assertEquals( 0, addResults.getRemovedMessages().size() );
+
+ kfs.write( "src/main/resources/r2_2.drl", drl2 );
+ IncrementalResults removeResults = ( (InternalKieBuilder) kieBuilder ).createFileSet( "src/main/resources/r2_2.drl" ).build();
+
+ assertEquals( 1, removeResults.getAddedMessages().size() );
+ assertEquals( 0, removeResults.getRemovedMessages().size() );
+ }
+
+ @Test
public void testIncrementalCompilationAddErrorBuildAllMessages() throws Exception {
//Valid
String drl1 = "package org.drools.compiler\n" +
@@ -4454,7 +4454,7 @@ public void testEmptyAfterRetractInIndexedMemory() {
@Test
public void testRuleReplacement() throws Exception {
// test rule replacement
- Collection<KnowledgePackage> kpkgs = loadKnowledgePackages( "test_RuleNameClashes1.drl",
+ Collection<KnowledgePackage> kpkgs = loadKnowledgePackages( "test_RuleNameClashes3.drl",
"test_RuleNameClashes3.drl" );
assertEquals( 1,
kpkgs.iterator().next().getRules().size() );
@@ -51,7 +51,6 @@ public void testDuplicateXLSResources() throws Exception {
}
@Test
- @Ignore("This does not pass at the moment. Incremental Build gives different results to a full build")
public void testIncrementalCompilationDuplicateXLSResources() throws Exception {
KieServices ks = KieServices.Factory.get();

0 comments on commit 9a31825

Please sign in to comment.