Search in sources :

Example 96 with Cheese

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

the class EvalTest method testEvalException.

@Test
public void testEvalException() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "function boolean throwException(Object object) {\n" + "    throw new Exception( \"this should throw an exception\" );\n" + "}\n" + "\n" + "rule \"Throw Eval Exception\"\n" + "    when\n" + "        cheese : Cheese( )\n" + "         eval( throwException( cheese ) )\n" + "    then\n" + "\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("eval-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final Cheese brie = new Cheese("brie", 12);
        try {
            ksession.insert(brie);
            ksession.fireAllRules();
            fail("Should throw an Exception from the Eval");
        } catch (final Exception e) {
            assertTrue(e.getCause().getMessage().contains("this should throw an exception"));
        }
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) Cheese(org.drools.testcoverage.common.model.Cheese) KieSession(org.kie.api.runtime.KieSession) InvocationTargetException(java.lang.reflect.InvocationTargetException) Test(org.junit.Test)

Example 97 with Cheese

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

the class MatchesTest method testMatchesNotMatchesCheese.

@Test
public void testMatchesNotMatchesCheese() {
    final String drl = "package org.drools.compiler.integrationtests.operators;\n" + "import " + Cheese.class.getCanonicalName() + ";\n" + "global java.util.List list;\n" + "\n" + "rule \"Cheese matches stilton\"\n" + "    salience 10\n" + "    when\n" + "        stilton : Cheese( type matches \"[Ss]tilto[^0-9]\" )\n" + "    then\n" + "        list.add( stilton );\n" + "end   \n" + "\n" + "rule \"Cheese not matches\"\n" + "    when\n" + "        brie : Cheese( type not matches \"(stil.*|mu\\\\w*|brie\\\\d|aged.*|.*prov.*)\" )\n" + "    then\n" + "        list.add( brie );\n" + "end   \n" + "\n" + "rule \"Cheese matches with space\"\n" + "    salience -10\n" + "    when\n" + "        stilton : Cheese( type matches \"aged stilton\" )\n" + "    then\n" + "        list.add( stilton );\n" + "end   \n" + "\n" + "rule \"Cheese matches with ^ and escaped s\"\n" + "    salience -20\n" + "    when\n" + "        prov : Cheese( type matches \"^provolone\\\\s*\" )\n" + "    then\n" + "        list.add( prov );\n" + "end";
    final KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("matches-test", kieBaseTestConfiguration, drl);
    final KieSession ksession = kbase.newKieSession();
    try {
        final List list = new ArrayList();
        ksession.setGlobal("list", list);
        final Cheese stilton = new Cheese("stilton", 12);
        final Cheese stilton2 = new Cheese("stilton2", 12);
        final Cheese agedStilton = new Cheese("aged stilton", 12);
        final Cheese brie = new Cheese("brie", 10);
        final Cheese brie2 = new Cheese("brie2", 10);
        final Cheese muzzarella = new Cheese("muzzarella", 10);
        final Cheese muzzarella2 = new Cheese("muzzarella2", 10);
        final Cheese provolone = new Cheese("provolone", 10);
        final Cheese provolone2 = new Cheese("another cheese (provolone)", 10);
        ksession.insert(stilton);
        ksession.insert(stilton2);
        ksession.insert(agedStilton);
        ksession.insert(brie);
        ksession.insert(brie2);
        ksession.insert(muzzarella);
        ksession.insert(muzzarella2);
        ksession.insert(provolone);
        ksession.insert(provolone2);
        ksession.fireAllRules();
        assertEquals(4, list.size());
        assertEquals(stilton, list.get(0));
        assertEquals(brie, list.get(1));
        assertEquals(agedStilton, list.get(2));
        assertEquals(provolone, list.get(3));
    } finally {
        ksession.dispose();
    }
}
Also used : KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Cheese(org.drools.testcoverage.common.model.Cheese) Test(org.junit.Test)

Aggregations

Cheese (org.drools.testcoverage.common.model.Cheese)97 KieBase (org.kie.api.KieBase)92 KieSession (org.kie.api.runtime.KieSession)88 Test (org.junit.Test)85 ArrayList (java.util.ArrayList)65 Person (org.drools.testcoverage.common.model.Person)47 List (java.util.List)33 FactHandle (org.kie.api.runtime.rule.FactHandle)22 Cheesery (org.drools.testcoverage.common.model.Cheesery)11 Arrays.asList (java.util.Arrays.asList)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 BigDecimal (java.math.BigDecimal)3 LeftTupleSink (org.drools.core.reteoo.LeftTupleSink)3 AfterMatchFiredEvent (org.kie.api.event.rule.AfterMatchFiredEvent)3 AgendaEventListener (org.kie.api.event.rule.AgendaEventListener)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 LeftInputAdapterNode (org.drools.core.reteoo.LeftInputAdapterNode)2