use of org.kie.api.io.Resource 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.io.Resource 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.io.Resource in project drools by kiegroup.
the class KieBuilderTest method testInvalidXsdTargetNamespace.
@Test
public void testInvalidXsdTargetNamespace() {
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/doesNotExist\">\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");
Assertions.assertThatThrownBy(() -> createAndDeployJar(ks, kmodule, releaseId1, r1)).isInstanceOf(IllegalStateException.class).hasMessageContaining("XSD validation failed");
}
use of org.kie.api.io.Resource 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.io.Resource 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);
}
}
Aggregations