use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieBuilderTest method testJavaSourceFileAndDrlDeployWithClassFilter.
@Test
public void testJavaSourceFileAndDrlDeployWithClassFilter() {
final String allowedJava = "package org.drools.compiler;\n" + "public class JavaSourceMessage { }\n";
final String filteredJava = "package org.drools.compiler;\n" + "public class ClassCausingClassNotFoundException { non.existing.Type foo() { return null; } }\n";
final String drl = "package org.drools.compiler;\n" + "import org.drools.compiler.JavaSourceMessage;" + "rule R1 when\n" + " $m : JavaSourceMessage()\n" + "then\n" + "end\n";
final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"kbase1\">\n" + " <ksession name=\"ksession1\" default=\"true\"/>\n" + " </kbase>\n" + "</kmodule>";
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
final Resource allowedJavaResource = ResourceFactory.newByteArrayResource(allowedJava.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/JavaSourceMessage.java");
final Resource filteredJavaResource = ResourceFactory.newByteArrayResource(filteredJava.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/ClassCausingClassNotFoundException.java");
final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
final Predicate<String> filter = fileName -> !fileName.endsWith("org/drools/compiler/ClassCausingClassNotFoundException.java");
KieModule km = null;
try {
km = createAndDeployJar(ks, kmodule, filter, releaseId1, allowedJavaResource, filteredJavaResource, drlResource);
} catch (final IllegalStateException ise) {
if (ise.getMessage().contains("org/drools/compiler/ClassCausingClassNotFoundException.java")) {
fail("Build failed because source file was not filtered out.");
} else {
throw ise;
}
}
final KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
try {
final Class<?> messageClass = kieContainer.getClassLoader().loadClass("org.drools.compiler.JavaSourceMessage");
assertNotNull(messageClass);
} catch (final ClassNotFoundException e) {
throw new IllegalStateException("Loading the java class failed.", e);
}
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieBuilderTest method testValidXsdTargetNamespace.
@Test
public void testValidXsdTargetNamespace() {
final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"kbase1\">\n" + " <ksession name=\"ksession1\" default=\"true\"/>\n" + " </kbase>\n" + "</kmodule>";
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
ks.newKieContainer(km.getReleaseId());
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieBuilderTest method testOldXsdTargetNamespace.
@Test
public void testOldXsdTargetNamespace() {
final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://jboss.org/kie/6.0.0/kmodule\">\n" + " <kbase name=\"kbase1\">\n" + " <ksession name=\"ksession1\" default=\"true\"/>\n" + " </kbase>\n" + "</kmodule>";
final KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1);
ks.newKieContainer(km.getReleaseId());
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieBuilderTest method testJavaSourceFileAndDrlDeploy.
@Test
public void testJavaSourceFileAndDrlDeploy() {
final String java = "package org.drools.compiler;\n" + "public class JavaSourceMessage { }\n";
final String drl = "package org.drools.compiler;\n" + "import org.drools.compiler.JavaSourceMessage;" + "rule R1 when\n" + " $m : JavaSourceMessage()\n" + "then\n" + "end\n";
final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"kbase1\">\n" + " <ksession name=\"ksession1\" default=\"true\"/>\n" + " </kbase>\n" + "</kmodule>";
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
final Resource javaResource = ResourceFactory.newByteArrayResource(java.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/JavaSourceMessage.java");
final Resource drlResource = ResourceFactory.newByteArrayResource(drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, javaResource, drlResource);
final KieContainer kieContainer = ks.newKieContainer(km.getReleaseId());
try {
final Class<?> messageClass = kieContainer.getClassLoader().loadClass("org.drools.compiler.JavaSourceMessage");
assertNotNull(messageClass);
} catch (final ClassNotFoundException e) {
throw new IllegalStateException("Loading the java class failed.", e);
}
}
use of org.kie.api.builder.KieModule in project drools by kiegroup.
the class KieBuilderTest method testResourceInclusion.
@Test
public void testResourceInclusion() {
final String drl1 = "package org.drools.compiler\n" + "rule R1 when\n" + " $m : Message()\n" + "then\n" + "end\n";
final String drl2 = "package org.drools.compiler\n" + "rule R2 when\n" + " $m : Message( message == \"Hi Universe\" )\n" + "then\n" + "end\n";
final String drl3 = "package org.drools.compiler\n" + "rule R3 when\n" + " $m : Message( message == \"Hello World\" )\n" + "then\n" + "end\n";
final String drl4 = "package org.drools.compiler\n" + "rule R4 when\n" + " $m : Message( message == \"Hello Earth\" )\n" + "then\n" + "end\n";
final String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"kbase1\" default=\"true\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\" scope=\"javax.enterprise.context.ApplicationScoped\">\n" + " <ksession name=\"ksession1\" type=\"stateful\" default=\"true\" clockType=\"realtime\" scope=\"javax.enterprise.context.ApplicationScoped\"/>\n" + " </kbase>\n" + "</kmodule>";
final KieServices ks = KieServices.Factory.get();
// Create an in-memory jar for version 1.0.0
final ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-kie-builder", "1.0.0");
final Resource r1 = ResourceFactory.newByteArrayResource(drl1.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/drl1.drl");
final Resource r2 = ResourceFactory.newByteArrayResource(drl2.getBytes()).setResourceType(ResourceType.GDRL).setSourcePath("kbase1/drl2.gdrl");
final Resource r3 = ResourceFactory.newByteArrayResource(drl3.getBytes()).setResourceType(ResourceType.RDRL).setSourcePath("kbase1/drl3.rdrl");
final Resource r4 = ResourceFactory.newByteArrayResource(drl4.getBytes()).setResourceType(ResourceType.TDRL).setSourcePath("kbase1/drl4.tdrl");
final KieModule km = createAndDeployJar(ks, kmodule, releaseId1, r1, r2, r3, r4);
final InternalKieModule ikm = (InternalKieModule) km;
assertNotNull(ikm.getResource(r1.getSourcePath()));
assertNotNull(ikm.getResource(r2.getSourcePath()));
assertNotNull(ikm.getResource(r3.getSourcePath()));
assertNotNull(ikm.getResource(r4.getSourcePath()));
// Create a session and fire rules
final KieContainer kc = ks.newKieContainer(km.getReleaseId());
final KieSession ksession = kc.newKieSession();
ksession.insert(new Message("Hello World"));
assertEquals(2, ksession.fireAllRules());
ksession.dispose();
}
Aggregations