Search in sources :

Example 26 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeSameSessionAddingGlobal.

@Test
public void testKJarUpgradeSameSessionAddingGlobal() {
    // DROOLS-523
    final String drl1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "global java.lang.String foo\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String drl2_1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_1 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    final String drl2_2 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "global java.lang.String foo\n" + "rule R2_2 when\n" + "   $m : Message( message == foo )\n" + "then\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl1, drl2_1);
    // Create a session and fire rules
    final KieContainer kc = ks.newKieContainer(releaseId1);
    final KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    // Create a new jar for version 1.1.0
    final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, drl1, drl2_2);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    ksession.setGlobal("foo", "Hello World");
    // continue working with the session
    ksession.insert(new Message("Hello World"));
    assertEquals(2, ksession.fireAllRules());
}
Also used : Message(org.drools.testcoverage.common.model.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 27 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class IncrementalCompilationTest method testDeletedFile.

@Test
public void testDeletedFile() {
    final String drl1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String drl2 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-delete", "1.0.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl1, drl2);
    final KieContainer kieContainer = ks.newKieContainer(releaseId1);
    final KieContainer kieContainer2 = ks.newKieContainer(releaseId1);
    KieSession ksession = kieContainer.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(2, ksession.fireAllRules());
    final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-delete", "1.0.1");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, (String) null, drl2);
    kieContainer.updateToVersion(releaseId2);
    // test with the old ksession ...
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    // ... and with a brand new one
    ksession = kieContainer.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    // check that the second kieContainer hasn't been affected by the update of the first one
    final KieSession ksession2 = kieContainer2.newKieSession();
    ksession2.insert(new Message("Hello World"));
    assertEquals(2, ksession2.fireAllRules());
}
Also used : Message(org.drools.testcoverage.common.model.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 28 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeSameSession.

@Test
public void testKJarUpgradeSameSession() {
    final String drl1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R1 when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final String drl2_1 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_1 when\n" + "   $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
    final String drl2_2 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2_2 when\n" + "   $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl1, drl2_1);
    // Create a session and fire rules
    final KieContainer kc = ks.newKieContainer(releaseId1);
    final KieSession ksession = kc.newKieSession();
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());
    // Create a new jar for version 1.1.0
    final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, drl1, drl2_2);
    // try to update the container to version 1.1.0
    kc.updateToVersion(releaseId2);
    // continue working with the session
    ksession.insert(new Message("Hello World"));
    assertEquals(3, ksession.fireAllRules());
}
Also used : Message(org.drools.testcoverage.common.model.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 29 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class IncrementalCompilationTest method testIncrementalCompilationWithInvalidDRL.

@Test
public void testIncrementalCompilationWithInvalidDRL() {
    final String drl1 = "Smurf";
    final String drl2_1 = "package org.drools.compiler\n" + "rule R2\n" + "when\n" + "   $m : NonExistentClass()\n" + "then\n" + "end\n";
    final String drl2_2 = "package org.drools.compiler\n" + "import " + Message.class.getCanonicalName() + ";\n" + "rule R2\n" + "when\n" + "   $m : Message()\n" + "then\n" + "end\n";
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();
    // First file contains errors
    kfs.write("src/main/resources/r1.drl", drl1);
    final KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(DrlProject.class);
    final Results results1 = kieBuilder.getResults();
    assertEquals(2, results1.getMessages().size());
    // Second file also contains errors.. expect some added messages
    kfs.write("src/main/resources/r2.drl", drl2_1);
    final IncrementalResults results2 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(1, results2.getAddedMessages().size());
    assertEquals(0, results2.getRemovedMessages().size());
    // Correct second file... expect original errors relating to the file to be removed
    kfs.write("src/main/resources/r2.drl", drl2_2);
    final IncrementalResults results3 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r2.drl").build();
    assertEquals(0, results3.getAddedMessages().size());
    assertEquals(1, results3.getRemovedMessages().size());
    // Remove first file... expect related errors to be removed
    kfs.delete("src/main/resources/r1.drl");
    final IncrementalResults results4 = ((InternalKieBuilder) kieBuilder).createFileSet("src/main/resources/r1.drl").build();
    assertEquals(0, results4.getAddedMessages().size());
    assertEquals(2, results4.getRemovedMessages().size());
}
Also used : Message(org.drools.testcoverage.common.model.Message) KieFileSystem(org.kie.api.builder.KieFileSystem) IncrementalResults(org.kie.internal.builder.IncrementalResults) Results(org.kie.api.builder.Results) KieServices(org.kie.api.KieServices) InternalKieBuilder(org.kie.internal.builder.InternalKieBuilder) KieBuilder(org.kie.api.builder.KieBuilder) IncrementalResults(org.kie.internal.builder.IncrementalResults) Test(org.junit.Test)

Example 30 with Message

use of org.drools.testcoverage.common.model.Message in project drools by kiegroup.

the class IncrementalCompilationTest method testKJarUpgradeDRLWithSpace.

private void testKJarUpgradeDRLWithSpace(final String drl1, final String drl2, final String factString, final int firstFireCount, final int secondFireCount) {
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade", "1.0.0");
    KieUtil.getKieModuleFromDrls(releaseId1, kieBaseTestConfiguration, drl1);
    final KieContainer kc = ks.newKieContainer(releaseId1);
    final KieSession ksession = kc.newKieSession();
    ksession.insert(new Message(factString));
    assertEquals(firstFireCount, ksession.fireAllRules());
    final ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade", "1.1.0");
    KieUtil.getKieModuleFromDrls(releaseId2, kieBaseTestConfiguration, drl2);
    kc.updateToVersion(releaseId2);
    assertEquals(secondFireCount, ksession.fireAllRules());
}
Also used : Message(org.drools.testcoverage.common.model.Message) KieServices(org.kie.api.KieServices) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) KieContainer(org.kie.api.runtime.KieContainer)

Aggregations

Message (org.drools.testcoverage.common.model.Message)39 Test (org.junit.Test)32 KieBase (org.kie.api.KieBase)21 KieSession (org.kie.api.runtime.KieSession)20 MessageEvent (org.drools.testcoverage.common.model.MessageEvent)18 KieServices (org.kie.api.KieServices)14 SessionPseudoClock (org.kie.api.time.SessionPseudoClock)14 KieContainer (org.kie.api.runtime.KieContainer)13 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)13 ReleaseId (org.kie.api.builder.ReleaseId)11 ArrayList (java.util.ArrayList)4 KieFileSystem (org.kie.api.builder.KieFileSystem)4 KieBuilder (org.kie.api.builder.KieBuilder)3 Resource (org.kie.api.io.Resource)3 IncrementalResults (org.kie.internal.builder.IncrementalResults)3 InternalKieBuilder (org.kie.internal.builder.InternalKieBuilder)3 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Cheese (org.drools.testcoverage.common.model.Cheese)2 KieSessionConfiguration (org.kie.api.runtime.KieSessionConfiguration)2