use of org.kie.api.builder.ReleaseId in project drools by kiegroup.
the class KieBuilderTest method testNoProjectXml.
@Test
public void testNoProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
String namespace = "org.kie.test";
ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0-SNAPSHOT");
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
generatePomXML(kfs, releaseId);
generateMessageClass(kfs, namespace);
generateRule(kfs, namespace);
MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
use of org.kie.api.builder.ReleaseId in project drools by kiegroup.
the class KieBuilderTest method testInvalidProjectXml.
@Test
public void testInvalidProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
String namespace = "org.kie.test";
KieModuleModel kProj = createKieProject(namespace);
ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0-SNAPSHOT");
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
generatePomXML(kfs, releaseId);
kfs.writeKModuleXML("xxxx");
generateMessageClass(kfs, namespace);
generateRule(kfs, namespace);
KieBuilder kieBuilder = createKieBuilder(kfs);
kieBuilder.buildAll();
assertTrue(kieBuilder.getResults().hasMessages(Level.ERROR));
}
use of org.kie.api.builder.ReleaseId in project drools by kiegroup.
the class KieBuilderTest method testNoPomAndProjectXml.
@Test
public void testNoPomAndProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
String namespace = "org.kie.test";
ReleaseId releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
generateMessageClass(kfs, namespace);
generateRule(kfs, namespace);
MemoryFileSystem mfs = ((KieFileSystemImpl) kfs).asMemoryFileSystem();
createAndTestKieContainer(releaseId, createKieBuilder(kfs), null);
}
use of org.kie.api.builder.ReleaseId in project drools by kiegroup.
the class KieBuilderTest method testSetPomModelReuse.
@Test
public void testSetPomModelReuse() throws IOException {
String namespace = "org.kie.test";
ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "pomModelReuse", "1.0-SNAPSHOT");
String pom = KieBuilderImpl.generatePomXml(releaseId);
KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
kfs.writePomXML(pom);
// Create a KieBuilder instance
KieBuilder kieBuilder1 = createKieBuilder(kfs);
kieBuilder1.buildAll();
// Get PomModel to re-use in second KieBuilder instance
PomModel pomModel = ((KieBuilderImpl) kieBuilder1).getPomModel();
kfs.writePomXML(pom);
// Create another KieBuilder instance with the same KieFileSystem, setting PomModel
KieBuilder kieBuilder2 = createKieBuilder(kfs);
((KieBuilderImpl) kieBuilder2).setPomModel(pomModel);
kieBuilder2.buildAll();
// Read pom.xml from first KieBuilder's KieModule
InternalKieModule kieModule1 = (InternalKieModule) ((KieBuilderImpl) kieBuilder1).getKieModuleIgnoringErrors();
final Reader reader1 = kieModule1.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
int charCode;
String readPom1 = "";
while ((charCode = reader1.read()) != -1) {
readPom1 = readPom1 + (char) charCode;
}
reader1.close();
assertEquals(pom, readPom1);
// Read pom.xml from second KieBuilder's KieModule
InternalKieModule kieModule2 = (InternalKieModule) ((KieBuilderImpl) kieBuilder2).getKieModuleIgnoringErrors();
final Reader reader2 = kieModule2.getResource("META-INF/maven/org.kie.test/pomModelReuse/pom.xml").getReader();
String readPom2 = "";
while ((charCode = reader2.read()) != -1) {
readPom2 = readPom2 + (char) charCode;
}
reader1.close();
assertEquals(pom, readPom2);
}
use of org.kie.api.builder.ReleaseId in project drools by kiegroup.
the class ClassLoaderTest method testClassLoaderFromPojo.
@Test
public void testClassLoaderFromPojo() throws IOException {
// DROOLS-1108
final String source = "package org.drools.testdrl;\n" + "public class MyPojo {\n" + " public String getUrlPath() {" + " return getClass().getClassLoader().getResource(\"META-INF/foo.xml\").getPath();\n" + " }\n" + "}\n";
final String drl1 = "package org.drools.testdrl;\n" + "import org.drools.testdrl.MyPojo;\n" + "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + " insert(new MyPojo());\n" + "end\n" + "rule R2 when\n" + " $m : MyPojo()\n" + "then\n" + " list.add($m.getUrlPath());\n" + "end\n";
final String foo = "<xyz/>\n";
final KieServices ks = KieServices.Factory.get();
final ReleaseId releaseId = ks.newReleaseId("org.kie", "test-cl", "1.0.0");
final KieFileSystem kfs = ks.newKieFileSystem();
final KieModuleModel kproj = ks.newKieModuleModel();
final KieBaseModel kieBaseModel1 = kproj.newKieBaseModel("testKbase").setDefault(true).addPackage("org.drools.testdrl");
final KieSessionModel ksession1 = kieBaseModel1.newKieSessionModel("testKsession").setDefault(true);
kfs.writeKModuleXML(kproj.toXML());
kfs.write("src/main/resources/org/drools/testdrl/rules1.drl", drl1);
kfs.write("src/main/java/org/drools/testdrl/MyPojo.java", source);
kfs.write("src/main/resources/META-INF/foo.xml", foo);
kfs.generateAndWritePomXML(releaseId);
final KieBuilder kieBuilder = ks.newKieBuilder(kfs);
assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty());
final KieContainer kieContainer = ks.newKieContainer(releaseId);
final ClassLoader classLoader = kieContainer.getClassLoader();
final URL url = classLoader.getResource("META-INF/foo.xml");
assertNotNull(url);
final KieSession ksession = kieContainer.newKieSession("testKsession");
final List<URL> list = new ArrayList<>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals(url.getPath(), list.get(0));
ksession.dispose();
}
Aggregations