From eb3f9a7d8fe0dd56f3a105ee1b737f2ad30ef89d Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Sat, 14 Sep 2024 18:40:47 +0200 Subject: [PATCH 01/13] First steps --- .../factmodel/traits/LogicalTraitTest.java | 69 +- .../compiler/factmodel/traits/TraitTest.java | 3303 ++++++++--------- .../traits/core/util/HierarchyTest.java | 189 +- 3 files changed, 1683 insertions(+), 1878 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java index f9217d22ac3..b168317bbea 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java @@ -26,6 +26,7 @@ import org.drools.core.common.InternalFactHandle; import org.drools.base.factmodel.traits.CoreWrapper; +import org.drools.base.factmodel.traits.Thing; import org.drools.base.factmodel.traits.TraitField; import org.drools.base.factmodel.traits.Traitable; import org.drools.base.factmodel.traits.TraitableBean; @@ -94,7 +95,7 @@ public void testShadowAlias() { KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList( ); + List list = new ArrayList( ); ks.setGlobal( "list", list ); ks.fireAllRules(); @@ -111,7 +112,7 @@ public void testShadowAlias() { LOGGER.debug( list.toString() ); assertThat(list.contains(false)).isFalse(); - assertThat(list.size()).isEqualTo(8); + assertThat(list).hasSize(8); } @@ -165,14 +166,14 @@ public void testShadowAliasTraitOnClass() { TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal( "list", list ); ks.fireAllRules(); for ( Object o : ks.getObjects() ) { LOGGER.debug( o.toString() ); } - assertThat(list).isEqualTo(List.of("ok")); + assertThat(list).containsExactly("ok"); try { ks = SerializationHelper.getSerialisedStatefulKnowledgeSession( ks, true ); @@ -241,14 +242,14 @@ public void testShadowAliasClassOnTrait() { TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal( "list", list ); ks.fireAllRules(); for ( Object o : ks.getObjects() ) { LOGGER.debug( o.toString() ); } - assertThat(list).isEqualTo(Arrays.asList("ok1", "ok2")); + assertThat(list).containsExactly("ok1", "ok2"); try { ks = SerializationHelper.getSerialisedStatefulKnowledgeSession( ks, true ); @@ -314,14 +315,14 @@ public void testShadowAliasTraitOnTrait() { TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal( "list", list ); ks.fireAllRules(); for ( Object o : ks.getObjects() ) { LOGGER.debug( o.toString() ); } - assertThat(list).isEqualTo(List.of("ok")); + assertThat(list).containsExactly("ok"); try { ks = SerializationHelper.getSerialisedStatefulKnowledgeSession( ks, true ); @@ -391,8 +392,8 @@ public void initializationConflictManagement() { TraitFactoryImpl.setMode(mode, knowledgeBase ); KieSession knowledgeSession = knowledgeBase.newKieSession(); - ArrayList list = new ArrayList(); - ArrayList list2 = new ArrayList(); + List list = new ArrayList<>(); + List list2 = new ArrayList<>(); knowledgeSession.setGlobal( "list", list ); knowledgeSession.setGlobal( "list2", list2 ); @@ -404,8 +405,8 @@ public void initializationConflictManagement() { LOGGER.debug( list.toString() ); LOGGER.debug( list2.toString() ); - assertThat(list).isEqualTo(Arrays.asList("1", null, "xyz", "xyz", "7", "aaa")); - assertThat(list2).isEqualTo(Arrays.asList(18, null, 37, 99, 37)); + assertThat(list).containsExactly("1", null, "xyz", "xyz", "7", "aaa"); + assertThat(list2).containsExactly(18, null, 37, 99, 37); try { knowledgeSession = SerializationHelper.getSerialisedStatefulKnowledgeSession( knowledgeSession, true ); @@ -473,8 +474,8 @@ public void testInitializationConflictManagementPrimitiveTypes() { TraitFactoryImpl.setMode(mode, knowledgeBase ); KieSession knowledgeSession = knowledgeBase.newKieSession(); - ArrayList list = new ArrayList(); - ArrayList list2 = new ArrayList(); + List list = new ArrayList<>(); + List list2 = new ArrayList<>(); knowledgeSession.setGlobal( "list", list ); knowledgeSession.setGlobal( "list2", list2 ); @@ -486,8 +487,8 @@ public void testInitializationConflictManagementPrimitiveTypes() { LOGGER.debug( list.toString() ); LOGGER.debug( list2.toString() ); - assertThat(list).isEqualTo(Arrays.asList(1.0, 0.0, 16.3, 16.3, 0.0, -0.72)); - assertThat(list2).isEqualTo(Arrays.asList(18, 0, 37, 99, 0)); + assertThat(list).containsExactly(1.0, 0.0, 16.3, 16.3, 0.0, -0.72); + assertThat(list2).containsExactly(18, 0, 37, 99, 0); try { knowledgeSession = SerializationHelper.getSerialisedStatefulKnowledgeSession( knowledgeSession, true ); @@ -556,11 +557,11 @@ public void testHardGetSetOnLogicallyTraitedField() { TraitFactoryImpl.setMode(mode, knowledgeBase ); KieSession knowledgeSession = knowledgeBase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal( "list", list ); knowledgeSession.fireAllRules(); - assertThat(list).isEqualTo(Arrays.asList(42)); + assertThat(list).containsExactly(42); knowledgeSession.insert( "x" ); knowledgeSession.fireAllRules(); @@ -573,7 +574,7 @@ public void testHardGetSetOnLogicallyTraitedField() { } assertThat(found).isTrue(); - assertThat(list).isEqualTo(Arrays.asList(42, 99)); + assertThat(list).containsExactly(42, 99); knowledgeSession.dispose(); } @@ -783,7 +784,7 @@ public void shadowAliasSelf() { TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal( "list", list ); ks.fireAllRules(); @@ -873,7 +874,7 @@ public void traitOnSet() { KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal( "list", list ); int n = ks.fireAllRules(); @@ -985,13 +986,13 @@ public void testShadowAliasTraitOnClassLogicalRetract() { TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal( "list", list ); FactHandle handle = ks.insert( "go" ); ks.fireAllRules(); - assertThat(list).isEqualTo(List.of("ok")); + assertThat(list).containsExactly("ok"); for ( Object o : ks.getObjects() ) { LOGGER.debug( o + " >> " + ((InternalFactHandle)ks.getFactHandle( o )).getEqualityKey() ); @@ -1006,11 +1007,11 @@ public void testShadowAliasTraitOnClassLogicalRetract() { TraitField fld = tb._getFieldTMS().getRegisteredTraitField("fld" ); Set> types = fld.getRangeTypes(); - assertThat(types.size()).isEqualTo(2); + assertThat(types).hasSize(2); TraitField fld2 = tb._getFieldTMS().getRegisteredTraitField("fld2" ); Set> types2 = fld2.getRangeTypes(); - assertThat(types2.size()).isEqualTo(1); + assertThat(types2).hasSize(1); } try { @@ -1026,7 +1027,7 @@ public void testShadowAliasTraitOnClassLogicalRetract() { LOGGER.debug( list.toString() ); - assertThat(list).isEqualTo(Arrays.asList("ok", "ok2", "ok3")); + assertThat(list).containsExactly("ok", "ok2", "ok3"); } @@ -1109,13 +1110,13 @@ public void testShadowAliasClassOnTraitLogicalRetract() { TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal( "list", list ); FactHandle handle = ks.insert( "go" ); ks.fireAllRules(); - assertThat(list).isEqualTo(Arrays.asList(1, 2)); + assertThat(list).containsExactly(1, 2); ks.retract( handle ); ks.fireAllRules(); @@ -1131,7 +1132,7 @@ public void testShadowAliasClassOnTraitLogicalRetract() { } LOGGER.debug( list.toString() ); - assertThat(list).isEqualTo(Arrays.asList(1, 2, 1, 2)); + assertThat(list).containsExactly(1, 2, 1, 2); } @@ -1241,11 +1242,11 @@ public void testTraitMismatchTypes() KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list",list); ksession.fireAllRules(); - assertThat(list.size() == 1 && list.contains(null)).isTrue(); + assertThat(list).hasSize(1).containsNull(); } @Test @@ -1295,11 +1296,11 @@ public void testTraitMismatchTypes2() KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list",list); ksession.fireAllRules(); - assertThat(list.size() == 1 && list.contains(null)).isTrue(); + assertThat(list).hasSize(1).containsNull(); } @Test @@ -1355,7 +1356,7 @@ public void testTraitMismatchTypes3() LOGGER.debug( "list" + list ); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list.get(0).getClass().getName()).isEqualTo("org.drools.base.factmodel.traits.test.Bar"); } } diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index 5b912937e0f..50dfdf89222 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -28,7 +28,6 @@ import org.drools.core.common.InternalWorkingMemory; import org.drools.core.common.ObjectTypeConfigurationRegistry; import org.drools.core.impl.InternalRuleBase; -import org.drools.core.reteoo.LeftTuple; import org.drools.core.reteoo.ObjectTypeConf; import org.drools.core.reteoo.ObjectTypeNode; import org.drools.core.reteoo.RuleTerminalNodeLeftTuple; @@ -44,7 +43,6 @@ import org.drools.traits.compiler.ReviseTraitTestWithPRAlwaysCategory; import org.drools.traits.core.factmodel.Entity; import org.drools.traits.core.factmodel.HierarchyEncoder; -import org.drools.traits.core.factmodel.LogicalTypeInconsistencyException; import org.drools.traits.core.factmodel.MapWrapper; import org.drools.traits.core.factmodel.TraitFactoryImpl; import org.drools.traits.core.factmodel.TraitProxyImpl; @@ -114,43 +112,39 @@ public class TraitTest extends CommonTraitTest { private static final Logger LOGGER = LoggerFactory.getLogger(TraitTest.class); - - private static long t0; - - + public VirtualPropertyMode mode; @Parameterized.Parameters - public static Collection modes() { - return Arrays.asList( new VirtualPropertyMode[][] + public static Collection modes() { + return Arrays.asList(new VirtualPropertyMode[][] { - { VirtualPropertyMode.MAP }, - { VirtualPropertyMode.TRIPLES } - } ); + { VirtualPropertyMode.MAP }, { VirtualPropertyMode.TRIPLES } + }); } - public TraitTest( VirtualPropertyMode m ) { + public TraitTest(VirtualPropertyMode m) { this.mode = m; } - private KieSession getSession( String... ruleFiles ) { + private KieSession getSession(String... ruleFiles) { KieHelper kieHelper = new KieHelper(); for (String file : ruleFiles) { - kieHelper.kfs.write( new ClassPathResource( file ) ); + kieHelper.kfs.write(new ClassPathResource(file)); } return kieHelper.build().newKieSession(); } - private KieSession getSessionFromString( String drl ) { - return new KieHelper().addContent( drl, ResourceType.DRL ).build().newKieSession(); + private KieSession getSessionFromString(String drl) { + return new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(); } - private KieBase getKieBaseFromString( String drl, KieBaseOption... options ) { - return new KieHelper().addContent( drl, ResourceType.DRL ).build(options); + private KieBase getKieBaseFromString(String drl, KieBaseOption... options) { + return new KieHelper().addContent(drl, ResourceType.DRL).build(options); } @Test - public void testRetract( ) { + public void testRetract() { String drl = "package org.drools.compiler.trait.test; \n" + "import org.drools.base.factmodel.traits.Traitable; \n" + "" + @@ -159,16 +153,16 @@ public void testRetract( ) { "" + "rule Init when then\n" + " Foo foo = new Foo(); \n" + - " don( foo, Bar.class ); \n" + + " don(foo, Bar.class); \n" + "end\n" + "rule Retract \n" + "when\n" + " $bar : Bar()\n" + "then\n" + - " delete( $bar ); \n" + + " delete($bar); \n" + "end\n"; - KieSession ks = getSessionFromString( drl ); + KieSession ks = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ks.getKieBase()); assertThat(ks.fireAllRules()).isEqualTo(2); @@ -177,59 +171,47 @@ public void testRetract( ) { LOGGER.debug(o.toString()); } - assertThat(ks.getObjects().size()).isEqualTo(0); + assertThat(ks.getObjects()).isEmpty(); } @Test - public void testTraitWrapGetAndSet() { + public void testTraitWrapGetAndSet() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); - kbuilder.add( res, - ResourceType.DRL ); + kbuilder.add(res, ResourceType.DRL); if (kbuilder.hasErrors()) { - fail( kbuilder.getErrors().toString() ); + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); - TraitFactoryImpl.setMode(mode, kb ); - kb.addPackages( kbuilder.getKnowledgePackages() ); + TraitFactoryImpl.setMode(mode, kb); + kb.addPackages(kbuilder.getKnowledgePackages()); TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - try { - FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", - "Imp" ); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - Class trait = kb.getFactType( "org.drools.compiler.trait.test", - "Student" ).getFactClass(); + FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); - Map virtualFields = imp._getDynamicProperties(); - Map wrapper = proxy.getFields(); + Map virtualFields = imp._getDynamicProperties(); + Map wrapper = proxy.getFields(); - wrapper.put( "name", - "john" ); + wrapper.put("name", "john"); - wrapper.put( "virtualField", - "xyz" ); + wrapper.put("virtualField", "xyz"); - wrapper.entrySet(); - assertThat(wrapper.size()).isEqualTo(4); - assertThat(virtualFields.size()).isEqualTo(2); + wrapper.entrySet(); + assertThat(wrapper).hasSize(4); + assertThat(virtualFields).hasSize(2); - assertThat(wrapper.get("name")).isEqualTo("john"); - assertThat(wrapper.get("virtualField")).isEqualTo("xyz"); + assertThat(wrapper.get("name")).isEqualTo("john"); + assertThat(wrapper.get("virtualField")).isEqualTo("xyz"); - assertThat(impClass.get(imp, - "name")).isEqualTo("john"); - - } catch (Exception e) { - fail( e.getMessage(), e ); - } + assertThat(impClass.get(imp, "name")).isEqualTo("john"); } @@ -237,33 +219,32 @@ public void testTraitWrapGetAndSet() { public void testTraitShed() { String source = "org/drools/compiler/factmodel/traits/testTraitShed.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); - ks.setGlobal( "list", - info ); + List info = new ArrayList<>(); + ks.setGlobal("list", info); - assertThat(info.isEmpty()).isTrue(); + assertThat(info).isEmpty(); ks.fireAllRules(); assertThat(info.contains("Student")).isTrue(); - assertThat(info.size()).isEqualTo(1); + assertThat(info).hasSize(1); - ks.insert( "hire" ); + ks.insert("hire"); ks.fireAllRules(); - Collection c = ks.getObjects(); + Collection c = ks.getObjects(); assertThat(info.contains("Worker")).isTrue(); - assertThat(info.size()).isEqualTo(2); + assertThat(info).hasSize(2); - ks.insert( "check" ); + ks.insert("check"); ks.fireAllRules(); - assertThat(info.size()).isEqualTo(4); + assertThat(info).hasSize(4); assertThat(info.contains("Conflict")).isTrue(); assertThat(info.contains("Nothing")).isTrue(); @@ -273,18 +254,17 @@ public void testTraitShed() { public void testTraitDon() { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); - ks.setGlobal( "list", - info ); + List info = new ArrayList<>(); + ks.setGlobal("list", info); ks.fireAllRules(); Collection wm = ks.getObjects(); - ks.insert( "go" ); + ks.insert("go"); ks.fireAllRules(); assertThat(info.contains("DON")).isTrue(); @@ -292,7 +272,7 @@ public void testTraitDon() { Iterator it = wm.iterator(); Object x = it.next(); - if ( x instanceof String ) { + if (x instanceof String) { x = it.next(); } @@ -304,12 +284,11 @@ public void testTraitDon() { public void testMixin() { String source = "org/drools/compiler/factmodel/traits/testTraitMixin.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); - ks.setGlobal( "list", - info ); + List info = new ArrayList<>(); + ks.setGlobal("list", info); ks.fireAllRules(); @@ -321,19 +300,18 @@ public void testMixin() { public void traitMethodsWithObjects() { String source = "org/drools/compiler/factmodel/traits/testTraitWrapping.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List errors = new ArrayList(); - ks.setGlobal( "list", - errors ); + ks.setGlobal("list", errors); ks.fireAllRules(); if (!errors.isEmpty()) { LOGGER.error(errors.toString()); } - assertThat(errors.isEmpty()).isTrue(); + assertThat(errors).isEmpty(); } @@ -342,655 +320,508 @@ public void traitMethodsWithObjects() { public void traitMethodsWithPrimitives() { String source = "org/drools/compiler/factmodel/traits/testTraitWrappingPrimitives.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List errors = new ArrayList(); - ks.setGlobal( "list", - errors ); + ks.setGlobal("list", errors); ks.fireAllRules(); if (!errors.isEmpty()) { LOGGER.error(errors.toString()); } - assertThat(errors.isEmpty()).isTrue(); + assertThat(errors).isEmpty(); } @Test - public void testTraitProxy() { + public void testTraitProxy() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); - kbuilder.add( res, - ResourceType.DRL ); + kbuilder.add(res, ResourceType.DRL); if (kbuilder.hasErrors()) { - fail( kbuilder.getErrors().toString() ); + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); - kb.addPackages( kbuilder.getKnowledgePackages() ); - TraitFactoryImpl.setMode(mode, kb ); + kb.addPackages(kbuilder.getKnowledgePackages()); + TraitFactoryImpl.setMode(mode, kb); TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - try { - FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", - "Imp" ); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - impClass.set( imp, - "name", - "aaa" ); - - Class trait = kb.getFactType( "org.drools.compiler.trait.test", - "Student" ).getFactClass(); - Class trait2 = kb.getFactType( "org.drools.compiler.trait.test", - "Role" ).getFactClass(); - - assertThat(trait).isNotNull(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); - proxy.getFields().put( "field", - "xyz" ); - // proxy.getFields().put("name", "aaa"); - - assertThat(proxy).isNotNull(); - - TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); - assertThat(proxy).isSameAs(proxy2); - - TraitProxyImpl proxy3 = (TraitProxyImpl) tFactory.getProxy(imp, - trait2 ); - assertThat(proxy3).isNotNull(); - assertThat(proxy3.getFields().get("field")).isEqualTo("xyz"); - assertThat(proxy3.getFields().get("name")).isEqualTo("aaa"); - - TraitableBean imp2 = (TraitableBean) impClass.newInstance(); - impClass.set( imp2, - "name", - "aaa" ); - TraitProxyImpl proxy4 = (TraitProxyImpl) tFactory.getProxy(imp2, - trait ); - // proxy4.getFields().put("name", "aaa"); - proxy4.getFields().put( "field", - "xyz" ); - - assertThat(proxy4).isEqualTo(proxy2); - - } catch (InstantiationException | IllegalAccessException | LogicalTypeInconsistencyException e) { - fail( e.getMessage(), e ); - } + FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + impClass.set(imp, "name", "aaa"); + + Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass(); + Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass(); + + assertThat(trait).isNotNull(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + proxy.getFields().put("field", "xyz"); + // proxy.getFields().put("name", "aaa"); + + assertThat(proxy).isNotNull(); + + TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(imp, trait); + assertThat(proxy).isSameAs(proxy2); + + TraitProxyImpl proxy3 = (TraitProxyImpl) tFactory.getProxy(imp, trait2); + assertThat(proxy3).isNotNull(); + assertThat(proxy3.getFields().get("field")).isEqualTo("xyz"); + assertThat(proxy3.getFields().get("name")).isEqualTo("aaa"); + + TraitableBean imp2 = (TraitableBean) impClass.newInstance(); + impClass.set(imp2, "name", "aaa"); + TraitProxyImpl proxy4 = (TraitProxyImpl) tFactory.getProxy(imp2, trait); + // proxy4.getFields().put("name", "aaa"); + proxy4.getFields().put("field", "xyz"); + + assertThat(proxy4).isEqualTo(proxy2); } @Test - public void testWrapperSize() { + public void testWrapperSize() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); - kbuilder.add( res, - ResourceType.DRL ); + kbuilder.add(res, ResourceType.DRL); if (kbuilder.hasErrors()) { - fail( kbuilder.getErrors().toString() ); + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); - kb.addPackages( kbuilder.getKnowledgePackages() ); + kb.addPackages(kbuilder.getKnowledgePackages()); - TraitFactoryImpl.setMode(mode, kb ); + TraitFactoryImpl.setMode(mode, kb); TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - try { - FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", - "Imp" ); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - FactType traitClass = kb.getFactType( "org.drools.compiler.trait.test", - "Student" ); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); - - Map virtualFields = imp._getDynamicProperties(); - Map wrapper = proxy.getFields(); - assertThat(wrapper.size()).isEqualTo(3); - assertThat(virtualFields.size()).isEqualTo(1); - - impClass.set(imp, - "name", - "john"); - assertThat(wrapper.size()).isEqualTo(3); - assertThat(virtualFields.size()).isEqualTo(1); - - proxy.getFields().put( "school", - "skol" ); - assertThat(wrapper.size()).isEqualTo(3); - assertThat(virtualFields.size()).isEqualTo(1); - - proxy.getFields().put( "surname", - "xxx" ); - assertThat(wrapper.size()).isEqualTo(4); - assertThat(virtualFields.size()).isEqualTo(2); - - // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity"); - // TraitableBean ind = (TraitableBean) indClass.newInstance(); - TraitableBean ind = new Entity(); - - TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, - trait ); - - Map virtualFields2 = ind._getDynamicProperties(); - Map wrapper2 = proxy2.getFields(); - assertThat(wrapper2.size()).isEqualTo(3); - assertThat(virtualFields2.size()).isEqualTo(3); - - traitClass.set( proxy2, - "name", - "john" ); - assertThat(wrapper2.size()).isEqualTo(3); - assertThat(virtualFields2.size()).isEqualTo(3); - - proxy2.getFields().put( "school", - "skol" ); - assertThat(wrapper2.size()).isEqualTo(3); - assertThat(virtualFields2.size()).isEqualTo(3); - - proxy2.getFields().put( "surname", - "xxx" ); - assertThat(wrapper2.size()).isEqualTo(4); - assertThat(virtualFields2.size()).isEqualTo(4); - - FactType traitClass2 = kb.getFactType( "org.drools.compiler.trait.test", - "Role" ); - Class trait2 = traitClass2.getFactClass(); - // TraitableBean ind2 = (TraitableBean) indClass.newInstance(); - TraitableBean ind2 = new Entity(); - - TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, - trait2 ); - - proxy99.getFields().put( "surname", - "xxx" ); - proxy99.getFields().put( "name", - "xyz" ); - proxy99.getFields().put( "school", - "skol" ); - - assertThat(proxy99.getFields().size()).isEqualTo(3); - - TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind2, - trait ); - - assertThat(proxy100.getFields().size()).isEqualTo(4); - - } catch ( Exception e ) { - fail( e.getMessage(), e ); - } + FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + + Map virtualFields = imp._getDynamicProperties(); + Map wrapper = proxy.getFields(); + assertThat(wrapper).hasSize(3); + assertThat(virtualFields).hasSize(1); + + impClass.set(imp, "name", "john"); + assertThat(wrapper).hasSize(3); + assertThat(virtualFields).hasSize(1); + + proxy.getFields().put("school", "skol"); + assertThat(wrapper).hasSize(3); + assertThat(virtualFields).hasSize(1); + + proxy.getFields().put("surname", "xxx"); + assertThat(wrapper).hasSize(4); + assertThat(virtualFields).hasSize(2); + + // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity"); + // TraitableBean ind = (TraitableBean) indClass.newInstance(); + TraitableBean ind = new Entity(); + + TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait); + + Map virtualFields2 = ind._getDynamicProperties(); + Map wrapper2 = proxy2.getFields(); + assertThat(wrapper2).hasSize(3); + assertThat(virtualFields2).hasSize(3); + + traitClass.set(proxy2, "name", "john"); + assertThat(wrapper2).hasSize(3); + assertThat(virtualFields2).hasSize(3); + + proxy2.getFields().put("school", "skol"); + assertThat(wrapper2).hasSize(3); + assertThat(virtualFields2).hasSize(3); + + proxy2.getFields().put("surname", "xxx"); + assertThat(wrapper2).hasSize(4); + assertThat(virtualFields2).hasSize(4); + + FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role"); + Class trait2 = traitClass2.getFactClass(); + // TraitableBean ind2 = (TraitableBean) indClass.newInstance(); + TraitableBean ind2 = new Entity(); + + TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2); + + proxy99.getFields().put("surname", "xxx"); + proxy99.getFields().put("name", "xyz"); + proxy99.getFields().put("school", "skol"); + + assertThat(proxy99.getFields()).hasSize(3); + + TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind2, trait); + + assertThat(proxy100.getFields()).hasSize(4); } @Test - public void testWrapperEmpty() { + public void testWrapperEmpty() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); - kbuilder.add( res, - ResourceType.DRL ); + + kbuilder.add(res, ResourceType.DRL); if (kbuilder.hasErrors()) { - fail( kbuilder.getErrors().toString() ); + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); - kb.addPackages( kbuilder.getKnowledgePackages() ); - TraitFactoryImpl.setMode(mode, kb ); + kb.addPackages(kbuilder.getKnowledgePackages()); + TraitFactoryImpl.setMode(mode, kb); TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - try { - FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", - "Imp" ); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - - FactType studentClass = kb.getFactType( "org.drools.compiler.trait.test", - "Student" ); - Class trait = studentClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); - - Map virtualFields = imp._getDynamicProperties(); - Map wrapper = proxy.getFields(); - assertThat(wrapper.isEmpty()).isFalse(); - - studentClass.set( proxy, - "name", - "john" ); - assertThat(wrapper.isEmpty()).isFalse(); - studentClass.set( proxy, - "name", - null ); - assertThat(wrapper.isEmpty()).isFalse(); - - studentClass.set( proxy, - "age", - 32 ); - assertThat(wrapper.isEmpty()).isFalse(); - - studentClass.set( proxy, - "age", - null ); - assertThat(wrapper.isEmpty()).isFalse(); - - // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity"); - TraitableBean ind = new Entity(); - - FactType RoleClass = kb.getFactType( "org.drools.compiler.trait.test", - "Role" ); - Class trait2 = RoleClass.getFactClass(); - TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, - trait2 ); - - Map wrapper2 = proxy2.getFields(); - assertThat(wrapper2.isEmpty()).isTrue(); - - proxy2.getFields().put( "name", - "john" ); - assertThat(wrapper2.isEmpty()).isFalse(); - - proxy2.getFields().put( "name", - null ); - assertThat(wrapper2.isEmpty()).isFalse(); - - } catch (Exception e) { - fail( e.getMessage(), e ); - } + FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + + FactType studentClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); + Class trait = studentClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + + Map wrapper = proxy.getFields(); + assertThat(wrapper).isNotEmpty(); + + studentClass.set(proxy, "name", "john"); + assertThat(wrapper).isNotEmpty(); + + studentClass.set(proxy, "name", null); + assertThat(wrapper).isNotEmpty(); + + studentClass.set(proxy, "age", 32); + assertThat(wrapper).isNotEmpty(); + + studentClass.set(proxy, "age", null); + assertThat(wrapper).isNotEmpty(); + + // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity"); + TraitableBean ind = new Entity(); + + FactType RoleClass = kb.getFactType("org.drools.compiler.trait.test", "Role"); + Class trait2 = RoleClass.getFactClass(); + TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait2); + + Map wrapper2 = proxy2.getFields(); + assertThat(wrapper2).isEmpty(); + + proxy2.getFields().put("name", "john"); + assertThat(wrapper2).isNotEmpty(); + + proxy2.getFields().put("name", null); + assertThat(wrapper2).isNotEmpty(); } @Test - public void testWrapperContainsKey() { + public void testWrapperContainsKey() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); - kbuilder.add( res, - ResourceType.DRL ); + kbuilder.add(res, ResourceType.DRL); if (kbuilder.hasErrors()) { - fail( kbuilder.getErrors().toString() ); + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); - kb.addPackages( kbuilder.getKnowledgePackages() ); + kb.addPackages(kbuilder.getKnowledgePackages()); - TraitFactoryImpl.setMode(mode, kb ); + TraitFactoryImpl.setMode(mode, kb); TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - try { - FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", - "Imp" ); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - impClass.set( imp, - "name", - "john" ); - - FactType traitClass = kb.getFactType( "org.drools.compiler.trait.test", - "Student" ); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); - - Map virtualFields = imp._getDynamicProperties(); - Map wrapper = proxy.getFields(); - - assertThat(wrapper.containsKey("name")).isTrue(); - assertThat(wrapper.containsKey("school")).isTrue(); - assertThat(wrapper.containsKey("age")).isTrue(); - assertThat(wrapper.containsKey("surname")).isFalse(); - - proxy.getFields().put( "school", - "skol" ); - proxy.getFields().put( "surname", - "xxx" ); - assertThat(wrapper.containsKey("surname")).isTrue(); - - // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity"); - TraitableBean ind = new Entity(); - - TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, - trait ); - - Map virtualFields2 = ind._getDynamicProperties(); - Map wrapper2 = proxy2.getFields(); - assertThat(wrapper2.containsKey("name")).isTrue(); - assertThat(wrapper2.containsKey("school")).isTrue(); - assertThat(wrapper2.containsKey("age")).isTrue(); - assertThat(wrapper2.containsKey("surname")).isFalse(); - - traitClass.set( proxy2, - "name", - "john" ); - proxy2.getFields().put( "school", - "skol" ); - proxy2.getFields().put( "surname", - "xxx" ); - assertThat(wrapper2.containsKey("surname")).isTrue(); - - FactType traitClass2 = kb.getFactType( "org.drools.compiler.trait.test", - "Role" ); - Class trait2 = traitClass2.getFactClass(); - TraitableBean ind2 = new Entity(); - - TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, - trait2 ); - Map wrapper99 = proxy99.getFields(); - - assertThat(wrapper99.containsKey("name")).isFalse(); - assertThat(wrapper99.containsKey("school")).isFalse(); - assertThat(wrapper99.containsKey("age")).isFalse(); - assertThat(wrapper99.containsKey("surname")).isFalse(); - - proxy99.getFields().put( "surname", - "xxx" ); - proxy99.getFields().put( "name", - "xyz" ); - proxy99.getFields().put( "school", - "skol" ); - - assertThat(wrapper99.containsKey("name")).isTrue(); - assertThat(wrapper99.containsKey("school")).isTrue(); - assertThat(wrapper99.containsKey("age")).isFalse(); - assertThat(wrapper99.containsKey("surname")).isTrue(); - assertThat(proxy99.getFields().size()).isEqualTo(3); - - TraitableBean ind0 = new Entity(); - - TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind0, - trait2 ); - Map wrapper100 = proxy100.getFields(); - assertThat(wrapper100.containsKey("name")).isFalse(); - assertThat(wrapper100.containsKey("school")).isFalse(); - assertThat(wrapper100.containsKey("age")).isFalse(); - assertThat(wrapper100.containsKey("surname")).isFalse(); - - TraitProxyImpl proxy101 = (TraitProxyImpl) tFactory.getProxy(ind0, - trait ); - // object gains properties by virtue of another trait - // so new props are accessible even using the old proxy - assertThat(wrapper100.containsKey("name")).isTrue(); - assertThat(wrapper100.containsKey("school")).isTrue(); - assertThat(wrapper100.containsKey("age")).isTrue(); - assertThat(wrapper100.containsKey("surname")).isFalse(); - - } catch (Exception e) { - fail( e.getMessage(), e ); - } + FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + impClass.set(imp, "name", "john"); + + FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + + Map wrapper = proxy.getFields(); + + assertThat(wrapper.containsKey("name")).isTrue(); + assertThat(wrapper.containsKey("school")).isTrue(); + assertThat(wrapper.containsKey("age")).isTrue(); + assertThat(wrapper.containsKey("surname")).isFalse(); + + proxy.getFields().put("school", "skol"); + proxy.getFields().put("surname", "xxx"); + assertThat(wrapper.containsKey("surname")).isTrue(); + + // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity"); + TraitableBean ind = new Entity(); + + TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait); + + Map wrapper2 = proxy2.getFields(); + assertThat(wrapper2.containsKey("name")).isTrue(); + assertThat(wrapper2.containsKey("school")).isTrue(); + assertThat(wrapper2.containsKey("age")).isTrue(); + assertThat(wrapper2.containsKey("surname")).isFalse(); + + traitClass.set(proxy2, "name", "john"); + proxy2.getFields().put("school", "skol"); + proxy2.getFields().put("surname", "xxx"); + assertThat(wrapper2.containsKey("surname")).isTrue(); + + FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role"); + Class trait2 = traitClass2.getFactClass(); + TraitableBean ind2 = new Entity(); + + TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2); + Map wrapper99 = proxy99.getFields(); + + assertThat(wrapper99.containsKey("name")).isFalse(); + assertThat(wrapper99.containsKey("school")).isFalse(); + assertThat(wrapper99.containsKey("age")).isFalse(); + assertThat(wrapper99.containsKey("surname")).isFalse(); + + proxy99.getFields().put("surname", "xxx"); + proxy99.getFields().put("name", "xyz"); + proxy99.getFields().put("school", "skol"); + + assertThat(wrapper99.containsKey("name")).isTrue(); + assertThat(wrapper99.containsKey("school")).isTrue(); + assertThat(wrapper99.containsKey("age")).isFalse(); + assertThat(wrapper99.containsKey("surname")).isTrue(); + assertThat(proxy99.getFields()).hasSize(3); + + TraitableBean ind0 = new Entity(); + + TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind0, trait2); + Map wrapper100 = proxy100.getFields(); + assertThat(wrapper100.containsKey("name")).isFalse(); + assertThat(wrapper100.containsKey("school")).isFalse(); + assertThat(wrapper100.containsKey("age")).isFalse(); + assertThat(wrapper100.containsKey("surname")).isFalse(); + + TraitProxyImpl proxy101 = (TraitProxyImpl) tFactory.getProxy(ind0, trait); + // object gains properties by virtue of another trait + // so new props are accessible even using the old proxy + assertThat(wrapper100.containsKey("name")).isTrue(); + assertThat(wrapper100.containsKey("school")).isTrue(); + assertThat(wrapper100.containsKey("age")).isTrue(); + assertThat(wrapper100.containsKey("surname")).isFalse(); } @Test - public void testInternalComponents1( ) { + public void testInternalComponents1() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); - kbuilder.add( res, - ResourceType.DRL ); + kbuilder.add(res, ResourceType.DRL); if (kbuilder.hasErrors()) { - fail( kbuilder.getErrors().toString() ); + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); - kb.addPackages( kbuilder.getKnowledgePackages() ); + kb.addPackages(kbuilder.getKnowledgePackages()); - TraitFactoryImpl.setMode(mode, kb ); + TraitFactoryImpl.setMode(mode, kb); TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - try { - FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", - "Imp" ); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - FactType traitClass = kb.getFactType( "org.drools.compiler.trait.test", - "Student" ); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); - Object proxyFields = proxy.getFields(); - Object coreTraits = imp._getTraitMap(); - Object coreProperties = imp._getDynamicProperties(); - - assertThat(proxy.getObject() instanceof TraitableBean).isTrue(); - - assertThat(proxyFields).isNotNull(); - assertThat(coreTraits).isNotNull(); - assertThat(coreProperties).isNotNull(); - - if ( mode == VirtualPropertyMode.MAP ) { - assertThat(proxyFields instanceof MapWrapper).isTrue(); - assertThat(coreTraits instanceof TraitTypeMapImpl).isTrue(); - assertThat(coreProperties instanceof HashMap).isTrue(); - } else { - assertThat(proxyFields.getClass().getName()).isEqualTo("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper"); + FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + Object proxyFields = proxy.getFields(); + Object coreTraits = imp._getTraitMap(); + Object coreProperties = imp._getDynamicProperties(); - assertThat(proxyFields instanceof TripleBasedStruct).isTrue(); - assertThat(coreTraits instanceof TraitTypeMapImpl).isTrue(); - assertThat(coreProperties instanceof TripleBasedBean).isTrue(); - } + assertThat(proxy.getObject() instanceof TraitableBean).isTrue(); + assertThat(proxyFields).isNotNull(); + assertThat(coreTraits).isNotNull(); + assertThat(coreProperties).isNotNull(); - StudentProxyImpl2 sp2 = new StudentProxyImpl2(new Imp2(), null ); - LOGGER.debug(sp2.toString()); + if (mode == VirtualPropertyMode.MAP) { + assertThat(proxyFields instanceof MapWrapper).isTrue(); + assertThat(coreTraits instanceof TraitTypeMapImpl).isTrue(); + assertThat(coreProperties instanceof HashMap).isTrue(); + } else { + assertThat(proxyFields.getClass().getName()).isEqualTo("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper"); - } catch ( Exception e ) { - fail( e.getMessage(), e ); - } + assertThat(proxyFields instanceof TripleBasedStruct).isTrue(); + assertThat(coreTraits instanceof TraitTypeMapImpl).isTrue(); + assertThat(coreProperties instanceof TripleBasedBean).isTrue(); + } + + + StudentProxyImpl2 sp2 = new StudentProxyImpl2(new Imp2(), null); + LOGGER.debug(sp2.toString()); } @Test - public void testWrapperKeySetAndValues() { + public void testWrapperKeySetAndValues() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); - kbuilder.add( res, - ResourceType.DRL ); + kbuilder.add(res, ResourceType.DRL); if (kbuilder.hasErrors()) { - fail( kbuilder.getErrors().toString() ); + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); - kb.addPackages( kbuilder.getKnowledgePackages() ); - TraitFactoryImpl.setMode(mode, kb ); + kb.addPackages(kbuilder.getKnowledgePackages()); + TraitFactoryImpl.setMode(mode, kb); TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - try { - FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", - "Imp" ); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - FactType traitClass = kb.getFactType( "org.drools.compiler.trait.test", - "Student" ); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); - - impClass.set( imp, - "name", - "john" ); - proxy.getFields().put( "surname", - "xxx" ); - proxy.getFields().put( "name2", - "john" ); - proxy.getFields().put( "nfield", - null ); - - Set set = new HashSet(); - set.add( "name" ); - set.add( "surname" ); - set.add( "age" ); - set.add( "school" ); - set.add( "name2" ); - set.add( "nfield" ); - - assertThat(proxy.getFields().keySet().size()).isEqualTo(6); - assertThat(proxy.getFields().keySet()).isEqualTo(set); - - Collection col1 = proxy.getFields().values(); - Collection col2 = Arrays.asList( "john", - null, - 0, - "xxx", - "john", - null ); - - Comparator comp = new Comparator() { - - public int compare( Object o1, Object o2 ) { - if (o1 == null && o2 != null) { - return 1; - } - if (o1 != null && o2 == null) { - return -1; - } - if (o1 == null && o2 == null) { - return 0; - } - return o1.toString().compareTo( o2.toString() ); - } - }; - - Collections.sort( (List) col1, - comp ); - Collections.sort( (List) col2, - comp ); - assertThat(col2).isEqualTo(col1); - - assertThat(proxy.getFields().containsValue(null)).isTrue(); - assertThat(proxy.getFields().containsValue("john")).isTrue(); - assertThat(proxy.getFields().containsValue(0)).isTrue(); - assertThat(proxy.getFields().containsValue("xxx")).isTrue(); - assertThat(proxy.getFields().containsValue("randomString")).isFalse(); - assertThat(proxy.getFields().containsValue(-96)).isFalse(); - - } catch (Exception e) { - fail( e.getMessage(), e ); - } + FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + + impClass.set(imp, "name", "john"); + proxy.getFields().put("surname", "xxx"); + proxy.getFields().put("name2", "john"); + proxy.getFields().put("nfield", null); + + assertThat(proxy.getFields()).hasSize(6); + assertThat(proxy.getFields().keySet()).contains("name", "surname", "age", "school", "name2", "nfield"); + + Collection col1 = proxy.getFields().values(); + Collection col2 = Arrays.asList("john", null, 0, "xxx", "john", null); + + Comparator comp = new Comparator() { + + public int compare(Object o1, Object o2) { + if (o1 == null && o2 != null) { + return 1; + } + if (o1 != null && o2 == null) { + return -1; + } + if (o1 == null && o2 == null) { + return 0; + } + return o1.toString().compareTo(o2.toString()); + } + }; + + Collections.sort((List) col1, comp); + Collections.sort((List) col2, comp); + assertThat(col2).isEqualTo(col1); + + assertThat(proxy.getFields().containsValue(null)).isTrue(); + assertThat(proxy.getFields().containsValue("john")).isTrue(); + assertThat(proxy.getFields().containsValue(0)).isTrue(); + assertThat(proxy.getFields().containsValue("xxx")).isTrue(); + assertThat(proxy.getFields().containsValue("randomString")).isFalse(); + assertThat(proxy.getFields().containsValue(-96)).isFalse(); } @Test - public void testWrapperClearAndRemove() { + public void testWrapperClearAndRemove() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); - kbuilder.add( res, - ResourceType.DRL ); + kbuilder.add(res, ResourceType.DRL); if (kbuilder.hasErrors()) { - fail( kbuilder.getErrors().toString() ); + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); - kb.addPackages( kbuilder.getKnowledgePackages() ); - TraitFactoryImpl.setMode(mode, kb ); + kb.addPackages(kbuilder.getKnowledgePackages()); + TraitFactoryImpl.setMode(mode, kb); TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - try { - FactType impClass = kb.getFactType( "org.drools.compiler.trait.test", - "Imp" ); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - impClass.set( imp, - "name", - "john" ); - FactType traitClass = kb.getFactType( "org.drools.compiler.trait.test", - "Student" ); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, - trait ); - - proxy.getFields().put( "surname", - "xxx" ); - proxy.getFields().put( "name2", - "john" ); - proxy.getFields().put( "nfield", - null ); - - Set set = new HashSet(); - set.add( "name" ); - set.add( "surname" ); - set.add( "age" ); - set.add( "school" ); - set.add( "name2" ); - set.add( "nfield" ); - - assertThat(proxy.getFields().keySet().size()).isEqualTo(6); - assertThat(proxy.getFields().keySet()).isEqualTo(set); - - proxy.getFields().clear(); - - Map fields = proxy.getFields(); - assertThat(fields.size()).isEqualTo(3); - assertThat(fields.containsKey("age")).isTrue(); - assertThat(fields.containsKey("school")).isTrue(); - assertThat(fields.containsKey("name")).isTrue(); - - assertThat(fields.get("age")).isEqualTo(0); - assertThat(fields.get("school")).isNull(); - assertThat(fields.get("name")).isNotNull(); - - proxy.getFields().put( "surname", - "xxx" ); - proxy.getFields().put( "name2", - "john" ); - proxy.getFields().put( "nfield", - null ); - proxy.getFields().put( "age", - 24 ); - - assertThat(proxy.getFields().get("name")).isEqualTo("john"); - assertThat(proxy.getFields().get("surname")).isEqualTo("xxx"); - assertThat(proxy.getFields().get("name2")).isEqualTo("john"); - assertThat(proxy.getFields().get("nfield")).isEqualTo(null); - assertThat(proxy.getFields().get("age")).isEqualTo(24); - assertThat(proxy.getFields().get("school")).isEqualTo(null); - - proxy.getFields().remove( "surname" ); - proxy.getFields().remove( "name2" ); - proxy.getFields().remove( "age" ); - proxy.getFields().remove( "school" ); - proxy.getFields().remove( "nfield" ); - assertThat(proxy.getFields().size()).isEqualTo(3); - - assertThat(proxy.getFields().get("age")).isEqualTo(0); - assertThat(proxy.getFields().get("school")).isEqualTo(null); - assertThat(proxy.getFields().get("name")).isEqualTo("john"); - - assertThat(proxy.getFields().get("nfield")).isEqualTo(null); - assertThat(proxy.getFields().containsKey("nfield")).isFalse(); - - assertThat(proxy.getFields().get("name2")).isEqualTo(null); - assertThat(proxy.getFields().containsKey("name2")).isFalse(); - - assertThat(proxy.getFields().get("surname")).isEqualTo(null); - assertThat(proxy.getFields().containsKey("surname")).isFalse(); - - } catch (Exception e) { - fail( e.getMessage(), e ); - } + FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + impClass.set(imp, "name", "john"); + FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + + proxy.getFields().put("surname", "xxx"); + proxy.getFields().put("name2", "john"); + proxy.getFields().put("nfield", null); + + Set set = new HashSet(); + set.add("name"); + set.add("surname"); + set.add("age"); + set.add("school"); + set.add("name2"); + set.add("nfield"); + + assertThat(proxy.getFields()).hasSize(6); + assertThat(proxy.getFields().keySet()).isEqualTo(set); + + proxy.getFields().clear(); + + Map fields = proxy.getFields(); + assertThat(fields).hasSize(3); + assertThat(fields.containsKey("age")).isTrue(); + assertThat(fields.containsKey("school")).isTrue(); + assertThat(fields.containsKey("name")).isTrue(); + + assertThat(fields.get("age")).isEqualTo(0); + assertThat(fields.get("school")).isNull(); + assertThat(fields.get("name")).isNotNull(); + + proxy.getFields().put("surname", "xxx"); + proxy.getFields().put("name2", "john"); + proxy.getFields().put("nfield", null); + proxy.getFields().put("age", 24); + + assertThat(proxy.getFields().get("name")).isEqualTo("john"); + assertThat(proxy.getFields().get("surname")).isEqualTo("xxx"); + assertThat(proxy.getFields().get("name2")).isEqualTo("john"); + assertThat(proxy.getFields().get("nfield")).isEqualTo(null); + assertThat(proxy.getFields().get("age")).isEqualTo(24); + assertThat(proxy.getFields().get("school")).isEqualTo(null); + + proxy.getFields().remove("surname"); + proxy.getFields().remove("name2"); + proxy.getFields().remove("age"); + proxy.getFields().remove("school"); + proxy.getFields().remove("nfield"); + assertThat(proxy.getFields()).hasSize(3); + + assertThat(proxy.getFields().get("age")).isEqualTo(0); + assertThat(proxy.getFields().get("school")).isEqualTo(null); + assertThat(proxy.getFields().get("name")).isEqualTo("john"); + + assertThat(proxy.getFields().get("nfield")).isEqualTo(null); + assertThat(proxy.getFields().containsKey("nfield")).isFalse(); + + assertThat(proxy.getFields().get("name2")).isEqualTo(null); + assertThat(proxy.getFields().containsKey("name2")).isFalse(); + + assertThat(proxy.getFields().get("surname")).isEqualTo(null); + assertThat(proxy.getFields().containsKey("surname")).isFalse(); } @Test - public void testIsAEvaluator( ) { + public void testIsAEvaluator() { String source = "package org.drools.compiler.trait.test;\n" + "\n" + "import org.drools.base.factmodel.traits.Traitable;\n" + @@ -1020,39 +851,38 @@ public void testIsAEvaluator( ) { "rule \"Init\"\n" + "when\n" + "then\n" + - " Imp core = new Imp( \"joe\" );\n" + - " insert( core );\n" + - " don( core, Person.class );\n" + - " don( core, Worker.class );\n" + + " Imp core = new Imp(\"joe\");\n" + + " insert(core);\n" + + " don(core, Person.class);\n" + + " don(core, Worker.class);\n" + "\n" + - " Imp core2 = new Imp( \"adam\" );\n" + - " insert( core2 );\n" + - " don( core2, Worker.class );\n" + + " Imp core2 = new Imp(\"adam\");\n" + + " insert(core2);\n" + + " don(core2, Worker.class);\n" + "end\n" + "\n" + "rule \"Mod\"\n" + "when\n" + - " $p : Person( name == \"joe\" )\n" + + " $p : Person(name == \"joe\")\n" + "then\n" + - " modify ($p) { setName( \"john\" ); }\n" + + " modify ($p) { setName(\"john\"); }\n" + "end\n" + "\n" + "rule \"Worker Students v6\"\n" + "when\n" + - " $x2 := Person( name == \"john\" )\n" + - " $x1 := Worker( core != $x2.core, this not isA $x2 )\n" + + " $x2 := Person(name == \"john\")\n" + + " $x1 := Worker(core != $x2.core, this not isA $x2)\n" + "then\n" + - " list.add( \"ok\" );\n" + + " list.add(\"ok\");\n" + "end\n" + "\n" + "\n"; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List info = new ArrayList(); - ks.setGlobal( "list", - info ); + ks.setGlobal("list", info); ks.fireAllRules(); LOGGER.debug(info.toString()); @@ -1065,12 +895,11 @@ public void testIsAEvaluator( ) { public void testIsA() { String source = "org/drools/compiler/factmodel/traits/testTraitIsA.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List info = new ArrayList(); - ks.setGlobal( "list", - info ); + ks.setGlobal("list", info); ks.fireAllRules(); @@ -1078,7 +907,7 @@ public void testIsA() { int num = 10; LOGGER.debug(info.toString()); - assertThat(info.size()).isEqualTo(num); + assertThat(info).hasSize(num); for (int j = 0; j < num; j++) { assertThat(info.contains("" + j)).isTrue(); } @@ -1091,17 +920,16 @@ public void testIsA() { public void testOverrideType() { String source = "org/drools/compiler/factmodel/traits/testTraitOverride.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List info = new ArrayList(); - ks.setGlobal( "list", - info ); + ks.setGlobal("list", info); try { ks.fireAllRules(); - fail( "An exception was expected since a trait can't override the type of a core class field with these settings " ); - } catch ( Throwable rde ) { + fail("An exception was expected since a trait can't override the type of a core class field with these settings "); + } catch (Throwable rde) { assertThat(rde.getCause() instanceof UnsupportedOperationException).isTrue(); } } @@ -1110,7 +938,7 @@ public void testOverrideType() { @Test - public void testOverrideType2( ) { + public void testOverrideType2() { String drl = "package org.drools.compiler.trait.test; \n" + "import org.drools.base.factmodel.traits.Traitable; \n" + "" + @@ -1122,16 +950,16 @@ public void testOverrideType2( ) { "" + "rule Don when then\n" + " Face face = new Face(); \n" + - " don( face, Mask.class ); \n" + + " don(face, Mask.class); \n" + "end\n"; - KieSession ks = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); try { ks.fireAllRules(); - fail( "An exception was expected since a trait can't override the type of a core class field with these settings " ); - } catch ( Throwable rde ) { + fail("An exception was expected since a trait can't override the type of a core class field with these settings "); + } catch (Throwable rde) { assertThat(rde.getCause() instanceof UnsupportedOperationException).isTrue(); } } @@ -1139,7 +967,7 @@ public void testOverrideType2( ) { @Test - public void testOverrideType3( ) { + public void testOverrideType3() { String drl = "package org.drools.compiler.trait.test; \n" + "import org.drools.base.factmodel.traits.Traitable; \n" + "" + @@ -1151,16 +979,16 @@ public void testOverrideType3( ) { "" + "rule Don when then\n" + " Face face = new Face(); \n" + - " don( face, Mask.class ); \n" + + " don(face, Mask.class); \n" + "end\n"; - KieSession ks = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); try { ks.fireAllRules(); - fail( "An exception was expected since a trait can't override the type of a core class field with these settings " ); - } catch ( Throwable rde ) { + fail("An exception was expected since a trait can't override the type of a core class field with these settings "); + } catch (Throwable rde) { assertThat(rde.getCause() instanceof UnsupportedOperationException).isTrue(); } } @@ -1170,19 +998,18 @@ public void testOverrideType3( ) { public void testTraitLegacy() { String source = "org/drools/compiler/factmodel/traits/testTraitLegacyTrait.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List info = new ArrayList(); - ks.setGlobal( "list", - info ); + ks.setGlobal("list", info); ks.fireAllRules(); printDebugInfoSessionObjects(ks.getObjects(), info); - assertThat(info.size()).isEqualTo(5); + assertThat(info).hasSize(5); assertThat(info.contains("OK")).isTrue(); assertThat(info.contains("OK2")).isTrue(); assertThat(info.contains("OK3")).isTrue(); @@ -1192,32 +1019,31 @@ public void testTraitLegacy() { } private void printDebugInfoSessionObjects(final Collection facts, final List globalList) { - LOGGER.debug( " -------------- " + facts.size() + " ---------------- " ); + LOGGER.debug(" -------------- " + facts.size() + " ---------------- "); for (Object o : facts) { - LOGGER.debug( "\t\t" + o ); + LOGGER.debug("\t\t" + o); } - LOGGER.debug( " -------------- ---------------- " ); - LOGGER.debug( globalList.toString() ); - LOGGER.debug( " -------------- ---------------- " ); + LOGGER.debug(" -------------- ---------------- "); + LOGGER.debug(globalList.toString()); + LOGGER.debug(" -------------- ---------------- "); } @Test public void testTraitCollections() { String source = "org/drools/compiler/factmodel/traits/testTraitCollections.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List info = new ArrayList(); - ks.setGlobal( "list", - info ); + ks.setGlobal("list", info); ks.fireAllRules(); printDebugInfoSessionObjects(ks.getObjects(), info); - assertThat(info.size()).isEqualTo(1); + assertThat(info).hasSize(1); assertThat(info.contains("OK")).isTrue(); } @@ -1226,12 +1052,11 @@ public void testTraitCollections() { public void testTraitCore() { String source = "org/drools/compiler/factmodel/traits/testTraitLegacyCore.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List info = new ArrayList(); - ks.setGlobal( "list", - info ); + ks.setGlobal("list", info); ks.fireAllRules(); @@ -1239,7 +1064,7 @@ public void testTraitCore() { assertThat(info.contains("OK")).isTrue(); assertThat(info.contains("OK2")).isTrue(); - assertThat(info.size()).isEqualTo(2); + assertThat(info).hasSize(2); } @@ -1247,12 +1072,11 @@ public void testTraitCore() { public void traitWithEquality() { String source = "org/drools/compiler/factmodel/traits/testTraitWithEquality.drl"; - KieSession ks = getSession( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List info = new ArrayList(); - ks.setGlobal( "list", - info ); + ks.setGlobal("list", info); ks.fireAllRules(); @@ -1267,13 +1091,11 @@ public void traitDeclared() { List trueTraits = new ArrayList(); List untrueTraits = new ArrayList(); - KieSession ks = getSession( "org/drools/compiler/factmodel/traits/testDeclaredFactTrait.drl" ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSession("org/drools/compiler/factmodel/traits/testDeclaredFactTrait.drl"); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); - ks.setGlobal( "trueTraits", - trueTraits ); - ks.setGlobal( "untrueTraits", - untrueTraits ); + ks.setGlobal("trueTraits", trueTraits); + ks.setGlobal("untrueTraits", untrueTraits); ks.fireAllRules(); ks.dispose(); @@ -1290,13 +1112,11 @@ public void traitPojo() { List trueTraits = new ArrayList(); List untrueTraits = new ArrayList(); - KieSession session = getSession( "org/drools/compiler/factmodel/traits/testPojoFactTrait.drl" ); - TraitFactoryImpl.setMode(mode, session.getKieBase() ); + KieSession session = getSession("org/drools/compiler/factmodel/traits/testPojoFactTrait.drl"); + TraitFactoryImpl.setMode(mode, session.getKieBase()); - session.setGlobal( "trueTraits", - trueTraits ); - session.setGlobal( "untrueTraits", - untrueTraits ); + session.setGlobal("trueTraits", trueTraits); + session.setGlobal("untrueTraits", untrueTraits); session.fireAllRules(); session.dispose(); @@ -1310,21 +1130,20 @@ public void traitPojo() { @Test public void testIsAOperator() { String source = "org/drools/compiler/factmodel/traits/testTraitIsA2.drl"; - KieSession ksession = getSession( source ); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + KieSession ksession = getSession(source); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - AgendaEventListener ael = mock( AgendaEventListener.class ); - ksession.addEventListener( ael ); + AgendaEventListener ael = mock(AgendaEventListener.class); + ksession.addEventListener(ael); - Person student = new Person("student", 18 ); - ksession.insert( student ); + Person student = new Person("student", 18); + ksession.insert(student); ksession.fireAllRules(); - ArgumentCaptor cap = ArgumentCaptor.forClass( AfterMatchFiredEvent.class ); - verify( ael, - times( 3 ) ).afterMatchFired( cap.capture() ); + ArgumentCaptor cap = ArgumentCaptor.forClass(AfterMatchFiredEvent.class); + verify(ael, times(3)).afterMatchFired(cap.capture()); List values = cap.getAllValues(); @@ -1351,7 +1170,7 @@ public void testManyTraits() { "" + "rule \"Nice\"\n" + "when\n" + - " $n : NiceMessage( $m : message )\n" + + " $n : NiceMessage($m : message)\n" + "then\n" + "end" + "\n" + @@ -1362,33 +1181,33 @@ public void testManyTraits() { " Message message = new Message();\n" + " message.setMessage(\"Hello World\");\n" + " insert(message);\n" + - " don( message, NiceMessage.class );\n" + + " don(message, NiceMessage.class);\n" + "\n" + " Message unreadMessage = new Message();\n" + " unreadMessage.setMessage(\"unread\");\n" + " insert(unreadMessage);\n" + - " don( unreadMessage, NiceMessage.class );\n" + + " don(unreadMessage, NiceMessage.class);\n" + "\n" + " Message oldMessage = new Message();\n" + " oldMessage.setMessage(\"old\");\n" + " insert(oldMessage);\n" + - " don( oldMessage, NiceMessage.class );" + + " don(oldMessage, NiceMessage.class);" + " list.add(\"OK\");\n" + " end"; - KieSession ksession = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + KieSession ksession = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); - Person student = new Person( "student", 18 ); - ksession.insert( student ); + Person student = new Person("student", 18); + ksession.insert(student); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list.contains("OK")).isTrue(); } @@ -1396,20 +1215,20 @@ public void testManyTraits() { @Test public void traitManyTimes() { - KieSession ksession = getSession( "org/drools/compiler/factmodel/traits/testTraitDonMultiple.drl" ); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + KieSession ksession = getSession("org/drools/compiler/factmodel/traits/testTraitDonMultiple.drl"); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { + for (Object o : ksession.getObjects()) { LOGGER.debug(o.toString()); } Collection x = ksession.getObjects(); - assertThat(ksession.getObjects().size()).isEqualTo(2); + assertThat(ksession.getObjects()).hasSize(2); - assertThat(list.size()).isEqualTo(5); + assertThat(list).hasSize(5); assertThat(list.get(0)).isEqualTo(0); assertThat(list.contains(1)).isTrue(); assertThat(list.contains(2)).isTrue(); @@ -1436,28 +1255,28 @@ public void traitsInBatchExecution() { "\n" + "rule \"create student\" \n" + " when\n" + - " $student : Person( age < 26 )\n" + + " $student : Person(age < 26)\n" + " then\n" + - " Student s = don( $student, Student.class );\n" + + " Student s = don($student, Student.class);\n" + " s.setSchool(\"Masaryk University\");\n" + "end\n" + "\n" + "rule \"print student\"\n" + " when\n" + - " student : Person( this isA Student )\n" + + " student : Person(this isA Student)\n" + " then" + - " list.add( 1 );\n" + + " list.add(1);\n" + "end\n" + "\n" + "rule \"print school\"\n" + " when\n" + - " Student( $school : school )\n" + + " Student($school : school)\n" + " then\n" + - " list.add( 2 );\n" + + " list.add(2);\n" + "end"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( str.getBytes() ), ResourceType.DRL ); + kbuilder.add(new ByteArrayResource(str.getBytes()), ResourceType.DRL); if (kbuilder.hasErrors()) { throw new RuntimeException(kbuilder.getErrors().toString()); @@ -1466,23 +1285,23 @@ public void traitsInBatchExecution() { List list = new ArrayList(); KieBase kbase = kbuilder.newKieBase(); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); StatelessKieSession ksession = kbase.newStatelessKieSession(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); List> commands = new ArrayList>(); Person student = new Person("student", 18); - commands.add( CommandFactory.newInsert( student )); + commands.add(CommandFactory.newInsert(student)); LOGGER.debug("Starting execution..."); commands.add(CommandFactory.newFireAllRules()); ksession.execute(CommandFactory.newBatchExecution(commands)); LOGGER.debug("Finished..."); - assertThat(list.size()).isEqualTo(2); + assertThat(list).hasSize(2); assertThat(list.contains(1)).isTrue(); assertThat(list.contains(2)).isTrue(); } @@ -1504,7 +1323,7 @@ public void testManyTraitsStateless() { "" + "rule \"Nice\"\n" + "when\n" + - " $n : NiceMessage( $m : message )\n" + + " $n : NiceMessage($m : message)\n" + "then\n" + "end" + "\n" + @@ -1515,31 +1334,31 @@ public void testManyTraitsStateless() { " Message message = new Message();\n" + " message.setMessage(\"Hello World\");\n" + " insert(message);\n" + - " don( message, NiceMessage.class );\n" + + " don(message, NiceMessage.class);\n" + "\n" + " Message unreadMessage = new Message();\n" + " unreadMessage.setMessage(\"unread\");\n" + " insert(unreadMessage);\n" + - " don( unreadMessage, NiceMessage.class );\n" + + " don(unreadMessage, NiceMessage.class);\n" + "\n" + " Message oldMessage = new Message();\n" + " oldMessage.setMessage(\"old\");\n" + " insert(oldMessage);\n" + - " don( oldMessage, NiceMessage.class );" + + " don(oldMessage, NiceMessage.class);" + " list.add(\"OK\");\n" + " end"; - KieBase kb = getKieBaseFromString( source ); - TraitFactoryImpl.setMode(mode, kb ); + KieBase kb = getKieBaseFromString(source); + TraitFactoryImpl.setMode(mode, kb); KieSession ksession = kb.newKieSession(); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); - ksession.execute( CommandFactory.newFireAllRules() ); + ksession.execute(CommandFactory.newFireAllRules()); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list.contains("OK")).isTrue(); } @@ -1576,37 +1395,37 @@ public void testAliasing() { "rule \"create student\" \n" + " when\n" + " then\n" + - " Person p = new Person( \"davide\", \"UniBoh\", \"Floor84\", 1 ); \n" + - " Student s = don( p, Student.class );\n" + + " Person p = new Person(\"davide\", \"UniBoh\", \"Floor84\", 1); \n" + + " Student s = don(p, Student.class);\n" + "end\n" + "\n" + "rule \"print school\"\n" + " when\n" + - " $student : Student( $school : school == \"UniBoh\", $f : fields, fields[ \"workPlace\" ] == \"UniBoh\" )\n" + + " $student : Student($school : school == \"UniBoh\", $f : fields, fields[ \"workPlace\" ] == \"UniBoh\")\n" + " then \n " + - " $student.setRank( 99 ); \n" + - " $f.put( \"school\", \"Skool\" ); \n" + - - " list.add( $school );\n" + - " list.add( $f.get( \"school\" ) );\n" + - " list.add( $student.getSchool() );\n" + - " list.add( $f.keySet() );\n" + - " list.add( $f.entrySet() );\n" + - " list.add( $f.values() );\n" + - " list.add( $f.containsKey( \"school\" ) );\n" + - " list.add( $student.getRank() );\n" + - " list.add( $f.get( \"address\" ) );\n" + + " $student.setRank(99); \n" + + " $f.put(\"school\", \"Skool\"); \n" + + + " list.add($school);\n" + + " list.add($f.get(\"school\"));\n" + + " list.add($student.getSchool());\n" + + " list.add($f.keySet());\n" + + " list.add($f.entrySet());\n" + + " list.add($f.values());\n" + + " list.add($f.containsKey(\"school\"));\n" + + " list.add($student.getRank());\n" + + " list.add($f.get(\"address\"));\n" + "end"; - KieSession ksession = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + KieSession ksession = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(9); + assertThat(list).hasSize(9); assertThat(list.contains("UniBoh")).isTrue(); assertThat(list.contains("Skool")).isTrue(); assertThat(((Collection) list.get(3)).containsAll(Arrays.asList("workPlace", "nomen", "level"))).isTrue(); @@ -1644,52 +1463,52 @@ public void testTraitLogicalRemoval() { "\n" + "rule \"Don Logical\"\n" + "when\n" + - " $s : String( this == \"trigger\" )\n" + + " $s : String(this == \"trigger\")\n" + "then\n" + - " Person p = new Person( \"john\" );\n" + - " insertLogical( p ); \n" + - " don( p, Student.class, true );\n" + + " Person p = new Person(\"john\");\n" + + " insertLogical(p); \n" + + " don(p, Student.class, true);\n" + "end\n" + " " + "rule \"Don Logical 2\"\n" + "when\n" + - " $s : String( this == \"trigger2\" )\n" + - " $p : Person( name == \"john\" )" + + " $s : String(this == \"trigger2\")\n" + + " $p : Person(name == \"john\")" + "then\n" + - " don( $p, Worker.class, true );\n" + + " don($p, Worker.class, true);\n" + "end"; KieSession ksession = getSessionFromString(drl); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); - FactHandle h = ksession.insert( "trigger" ); + FactHandle h = ksession.insert("trigger"); ksession.fireAllRules(); - assertThat(ksession.getObjects().size()).isEqualTo(3); + assertThat(ksession.getObjects()).hasSize(3); - ksession.delete( h ); + ksession.delete(h); ksession.fireAllRules(); - assertThat(ksession.getObjects().size()).isEqualTo(0); + assertThat(ksession.getObjects()).isEmpty(); - FactHandle h1 = ksession.insert( "trigger" ); - FactHandle h2 = ksession.insert( "trigger2" ); + FactHandle h1 = ksession.insert("trigger"); + FactHandle h2 = ksession.insert("trigger2"); ksession.fireAllRules(); - assertThat(ksession.getObjects().size()).isEqualTo(5); + assertThat(ksession.getObjects()).hasSize(5); - ksession.delete( h2 ); + ksession.delete(h2); ksession.fireAllRules(); - assertThat(ksession.getObjects().size()).isEqualTo(3); + assertThat(ksession.getObjects()).hasSize(3); - ksession.delete( h1 ); + ksession.delete(h1); ksession.fireAllRules(); - assertThat(ksession.getObjects().size()).isEqualTo(0); + assertThat(ksession.getObjects()).isEmpty(); } @@ -1706,7 +1525,7 @@ public void testTMSConsistencyWithNonTraitableBeans() { "rule \"Init\"\n" + "when\n" + "then\n" + - " insertLogical( new Person( \"x\", 18 ) );\n" + + " insertLogical(new Person(\"x\", 18));\n" + "end\n" + "\n" + "declare trait Student\n" + @@ -1716,29 +1535,29 @@ public void testTMSConsistencyWithNonTraitableBeans() { "\n" + "rule \"Trait\"\n" + "when\n" + - " $p : Person( )\n" + + " $p : Person()\n" + "then\n" + - " don( $p, Student.class, true );\n" + + " don($p, Student.class, true);\n" + "end\n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); ksession.fireAllRules(); - FactHandle personHandle = ksession.getFactHandles( new ClassObjectFilter( Person.class ) ).iterator().next(); + FactHandle personHandle = ksession.getFactHandles(new ClassObjectFilter(Person.class)).iterator().next(); InternalFactHandle h = ((InternalFactHandle) personHandle); - ObjectTypeConfigurationRegistry reg = h.getEntryPoint(( InternalWorkingMemory ) ksession).getObjectTypeConfigurationRegistry(); - ObjectTypeConf conf = reg.getOrCreateObjectTypeConf( h.getEntryPointId(), ((InternalFactHandle) personHandle).getObject() ); + ObjectTypeConfigurationRegistry reg = h.getEntryPoint((InternalWorkingMemory) ksession).getObjectTypeConfigurationRegistry(); + ObjectTypeConf conf = reg.getOrCreateObjectTypeConf(h.getEntryPointId(), ((InternalFactHandle) personHandle).getObject()); assertThat(conf.isTMSEnabled()).isTrue(); ksession.dispose(); @@ -1752,8 +1571,8 @@ public void testTMSConsistencyWithNonTraitableBeans() { public static class TBean { private String fld; public String getFld() { return fld; } - public void setFld( String fld ) { this.fld = fld; } - public TBean( String fld ) { this.fld = fld; } + public void setFld(String fld) { this.fld = fld; } + public TBean(String fld) { this.fld = fld; } } @@ -1777,46 +1596,44 @@ public void testTraitsLegacyWrapperCoherence() { "rule Init \n" + "when \n" + "then \n" + - " insert( new TBean(\"abc\") ); \n" + + " insert(new TBean(\"abc\")); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + - " $b : TBean( ) \n" + + " $b : TBean() \n" + "then \n" + - " Mask m = don( $b, Mask.class ); \n" + - " modify (m) { setXyz( 10 ); } \n" + - " list.add( m ); \n" + + " Mask m = don($b, Mask.class); \n" + + " modify (m) { setXyz(10); } \n" + + " list.add(m); \n" + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), - ResourceType.DRL ); + kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal("list", - list); + ksession.setGlobal("list", list); ksession.fireAllRules(); Collection yOld = ksession.getObjects(); - assertThat(yOld.size()).isEqualTo(2); + assertThat(yOld).hasSize(2); TraitableBean coreOld = null; - for ( Object o : yOld ) { - if ( o instanceof TraitableBean ) { + for (Object o : yOld) { + if (o instanceof TraitableBean) { coreOld = (TraitableBean) o; break; } @@ -1826,57 +1643,51 @@ public void testTraitsLegacyWrapperCoherence() { assertThat(coreOld.getClass().getSuperclass()).isSameAs(TBean.class); assertThat(((TBean) coreOld).getFld()).isEqualTo("abc"); - assertThat(coreOld._getDynamicProperties().size()).isEqualTo(1); - assertThat(coreOld._getTraitMap().size()).isEqualTo(1); + assertThat(coreOld._getDynamicProperties()).hasSize(1); + assertThat(coreOld._getTraitMap()).hasSize(1); } @Test - public void testHasTypes() { + public void testHasTypes() throws Exception { String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - Resource res = ResourceFactory.newClassPathResource( source ); + Resource res = ResourceFactory.newClassPathResource(source); assertThat(res).isNotNull(); kbuilder.add(res, ResourceType.DRL); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalRuleBase kb = KnowledgeBaseFactory.newKnowledgeBase(); kb.addPackages(kbuilder.getKnowledgePackages()); TraitFactoryImpl traitBuilder = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb); - TraitFactoryImpl.setMode(mode, kb ); - - try { - FactType impClass = kb.getFactType("org.drools.compiler.trait.test","Imp"); - TraitableBean imp = (TraitableBean) impClass.newInstance(); - impClass.set(imp, "name", "aaabcd"); - - Class trait = kb.getFactType("org.drools.compiler.trait.test","Student").getFactClass(); - Class trait2 = kb.getFactType("org.drools.compiler.trait.test","Role").getFactClass(); + TraitFactoryImpl.setMode(mode, kb); - assertThat(trait).isNotNull(); + FactType impClass = kb.getFactType("org.drools.compiler.trait.test","Imp"); + TraitableBean imp = (TraitableBean) impClass.newInstance(); + impClass.set(imp, "name", "aaabcd"); - TraitProxyImpl proxy = (TraitProxyImpl) traitBuilder.getProxy(imp, trait); - Thing thing = traitBuilder.getProxy(imp, Thing.class); + Class trait = kb.getFactType("org.drools.compiler.trait.test","Student").getFactClass(); + Class trait2 = kb.getFactType("org.drools.compiler.trait.test","Role").getFactClass(); - TraitableBean core = proxy.getObject(); + assertThat(trait).isNotNull(); + TraitProxyImpl proxy = (TraitProxyImpl) traitBuilder.getProxy(imp, trait); + Thing thing = traitBuilder.getProxy(imp, Thing.class); - TraitProxyImpl proxy2 = (TraitProxyImpl) traitBuilder.getProxy(imp, trait); - Thing thing2 = traitBuilder.getProxy(imp, Thing.class); + TraitableBean core = proxy.getObject(); - assertThat(proxy2).isSameAs(proxy); - assertThat(thing2).isSameAs(thing); - assertThat(core.getTraits().size()).isEqualTo(2); + TraitProxyImpl proxy2 = (TraitProxyImpl) traitBuilder.getProxy(imp, trait); + Thing thing2 = traitBuilder.getProxy(imp, Thing.class); + assertThat(proxy2).isSameAs(proxy); + assertThat(thing2).isSameAs(thing); - } catch ( Exception e ) { - fail( e.getMessage(), e ); - } + assertThat(core.getTraits()).hasSize(2); } @Test @@ -1898,44 +1709,42 @@ public void testTraitRedundancy() { "rule \"Don\" \n" + "no-loop \n" + "when \n" + - " $p : IPerson( age < 30 ) \n" + + " $p : IPerson(age < 30) \n" + "then \n" + - " don( $p, IStudent.class );\n" + + " don($p, IStudent.class);\n" + "end \n" + "" + "rule \"Check\" \n" + "no-loop \n" + "when \n" + - " $p : IPerson( this isA IStudent ) \n" + + " $p : IPerson(this isA IStudent) \n" + "then \n" + - " modify ($p) { setAge( 37 ); } \n" + - " shed( $p, IStudent.class );\n" + + " modify ($p) { setAge(37); } \n" + + " shed($p, IStudent.class);\n" + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( ResourceFactory.newByteArrayResource( str.getBytes() ), - ResourceType.DRL ); + kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL); InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal("list", - list); + ksession.setGlobal("list", list); - ksession.insert( new StudentImpl("skool", "john", 27 ) ); + ksession.insert(new StudentImpl("skool", "john", 27)); assertThat(ksession.fireAllRules()).isEqualTo(3); - for ( Object o : ksession.getObjects() ) { + for (Object o : ksession.getObjects()) { LOGGER.debug(o.toString()); } @@ -1963,34 +1772,34 @@ public void traitSimpleTypes() { " don($mark, PassMark.class);\n" + "end\n" + "" + - "rule \"Init\" when then insert( new ExamMark() ); end \n"; + "rule \"Init\" when then insert(new ExamMark()); end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - if ( o instanceof TraitableBean ) { + for (Object o : ksession.getObjects()) { + if (o instanceof TraitableBean) { TraitableBean tb = (TraitableBean) o; - assertThat(tb._getTraitMap().size()).isEqualTo(1); + assertThat(tb._getTraitMap()).hasSize(1); BitSet bs = new BitSet(); - bs.set( 0 ); + bs.set(0); assertThat(tb.getCurrentTypeCode()).isEqualTo(bs); } - if ( o instanceof TraitProxyImpl) { + if (o instanceof TraitProxyImpl) { TraitProxyImpl tp = (TraitProxyImpl) o; - assertThat(tp.listAssignedOtnTypeCodes().size()).isEqualTo(0); + assertThat(tp.listAssignedOtnTypeCodes()).isEmpty(); } } } @@ -2020,55 +1829,55 @@ public void testTraitEncoding() { "when\n" + " $x : Entity()\n" + "then\n" + - " don( $x, A.class );\n" + + " don($x, A.class);\n" + "end\n" + "" + "rule \"donManyThing\"\n" + "when\n" + - " String( this == \"y\" ) \n" + + " String(this == \"y\") \n" + " $x : Entity()\n" + "then\n" + - " don( $x, B.class );\n" + - " don( $x, D.class );\n" + - " don( $x, F.class );\n" + - " don( $x, E.class );\n" + - " don( $x, I.class );\n" + - " don( $x, K.class );\n" + - " don( $x, J.class );\n" + - " don( $x, C.class );\n" + - " don( $x, H.class );\n" + - " don( $x, G.class );\n" + - " don( $x, L.class );\n" + - " don( $x, M.class );\n" + - " don( $x, N.class );\n" + + " don($x, B.class);\n" + + " don($x, D.class);\n" + + " don($x, F.class);\n" + + " don($x, E.class);\n" + + " don($x, I.class);\n" + + " don($x, K.class);\n" + + " don($x, J.class);\n" + + " don($x, C.class);\n" + + " don($x, H.class);\n" + + " don($x, G.class);\n" + + " don($x, L.class);\n" + + " don($x, M.class);\n" + + " don($x, N.class);\n" + "end\n" ; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); TraitRegistryImpl tr = (TraitRegistryImpl) ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(kbase); - LOGGER.debug( tr.getHierarchy().toString() ); + LOGGER.debug(tr.getHierarchy().toString()); - Entity ent = new Entity( "x" ); + Entity ent = new Entity("x"); KieSession ksession = kbase.newKieSession(); - ksession.insert( ent ); + ksession.insert(ent); ksession.fireAllRules(); - assertThat(ent.getMostSpecificTraits().size()).isEqualTo(1); + assertThat(ent.getMostSpecificTraits()).hasSize(1); - ksession.insert( "y" ); + ksession.insert("y"); ksession.fireAllRules(); - LOGGER.debug( ent.getMostSpecificTraits().toString() ); - assertThat(ent.getMostSpecificTraits().size()).isEqualTo(2); + LOGGER.debug(ent.getMostSpecificTraits().toString()); + assertThat(ent.getMostSpecificTraits()).hasSize(2); } @@ -2076,56 +1885,56 @@ public void testTraitEncoding() { @Test public void testTraitActualTypeCodeWithEntities() { - testTraitActualTypeCodeWithEntities( "ent", mode ); + testTraitActualTypeCodeWithEntities("ent", mode); } @Test public void testTraitActualTypeCodeWithCoreMap() { - testTraitActualTypeCodeWithEntities( "kor", mode ); + testTraitActualTypeCodeWithEntities("kor", mode); } - void testTraitActualTypeCodeWithEntities( String trig, VirtualPropertyMode mode ) { + void testTraitActualTypeCodeWithEntities(String trig, VirtualPropertyMode mode) { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ClassPathResource( "org/drools/compiler/factmodel/traits/testComplexDonShed.drl" ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ClassPathResource("org/drools/compiler/factmodel/traits/testComplexDonShed.drl"), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (KieBase) kbase); - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); - ksession.insert( trig ); + ksession.insert(trig); ksession.fireAllRules(); - TraitableBean ent = (TraitableBean) ksession.getGlobal( "core" ); + TraitableBean ent = (TraitableBean) ksession.getGlobal("core"); assertThat(ent.getCurrentTypeCode()).isEqualTo(CodedHierarchyImpl.stringToBitSet("1")); - ksession.insert( "b" ); + ksession.insert("b"); ksession.fireAllRules(); assertThat(ent.getCurrentTypeCode()).isEqualTo(CodedHierarchyImpl.stringToBitSet("11")); - ksession.insert( "c" ); + ksession.insert("c"); ksession.fireAllRules(); assertThat(ent.getCurrentTypeCode()).isEqualTo(CodedHierarchyImpl.stringToBitSet("1011")); - ksession.insert( "e" ); + ksession.insert("e"); ksession.fireAllRules(); assertThat(ent.getCurrentTypeCode()).isEqualTo(CodedHierarchyImpl.stringToBitSet("11011")); - ksession.insert( "-c" ); + ksession.insert("-c"); ksession.fireAllRules(); assertThat(ent.getCurrentTypeCode()).isEqualTo(CodedHierarchyImpl.stringToBitSet("11")); - ksession.insert( "dg" ); + ksession.insert("dg"); ksession.fireAllRules(); assertThat(ent.getCurrentTypeCode()).isEqualTo(CodedHierarchyImpl.stringToBitSet("111111")); - ksession.insert( "-f" ); + ksession.insert("-f"); ksession.fireAllRules(); assertThat(ent.getCurrentTypeCode()).isEqualTo(CodedHierarchyImpl.stringToBitSet("111")); @@ -2148,68 +1957,68 @@ public void testTraitModifyCore() { "rule \"Init\" " + "when " + "then " + - " Person p = new Person( \"john\" ); " + - " insert( p ); " + + " Person p = new Person(\"john\"); " + + " insert(p); " + "end " + "" + "rule \"Don\" " + "no-loop " + "when " + - " $p : Person( name == \"john\" ) " + + " $p : Person(name == \"john\") " + "then " + - " don( $p, Student.class ); " + - " don( $p, Worker.class ); " + - " don( $p, StudentWorker.class ); " + - " don( $p, Assistant.class ); " + + " don($p, Student.class); " + + " don($p, Worker.class); " + + " don($p, StudentWorker.class); " + + " don($p, Assistant.class); " + "end " + "" + "rule \"Log S\" " + "when " + - " $t : Student() @Watch( name ) " + + " $t : Student() @Watch(name) " + "then " + - " list.add( $t.getName() ); " + + " list.add($t.getName()); " + "end " + "rule \"Log W\" " + "when " + - " $t : Worker() @Watch( name ) " + + " $t : Worker() @Watch(name) " + "then " + - " list.add( $t.getName() ); " + + " list.add($t.getName()); " + "end " + "rule \"Log SW\" " + "when " + - " $t : StudentWorker() @Watch( name ) " + + " $t : StudentWorker() @Watch(name) " + "then " + - " list.add( $t.getName() ); " + + " list.add($t.getName()); " + "end " + "rule \"Log RA\" " + "when " + - " $t : Assistant() @Watch( name ) " + + " $t : Assistant() @Watch(name) " + "then " + - " list.add( $t.getName() ); " + + " list.add($t.getName()); " + "end " + "" + "rule \"Mod\" " + "salience -10 " + "when " + - " $p : Person( name == \"john\" ) " + + " $p : Person(name == \"john\") " + "then " + - " modify ( $p ) { setName( \"alan\" ); } " + + " modify ($p) { setName(\"alan\"); } " + "end " + ""; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); int k = ksession.fireAllRules(); @@ -2237,71 +2046,71 @@ public void testTraitModifyCore2() { "rule \"Init\" " + "when " + "then " + - " Person p = new Person( \"john\" ); " + - " insert( p ); " + + " Person p = new Person(\"john\"); " + + " insert(p); " + "end " + "" + "rule \"Don\" " + "when " + - " $p : Person( name == \"john\" ) " + + " $p : Person(name == \"john\") " + "then " + - " don( $p, Worker.class ); " + - " don( $p, StudentWorker2.class ); " + - " don( $p, Assistant.class ); " + + " don($p, Worker.class); " + + " don($p, StudentWorker2.class); " + + " don($p, Assistant.class); " + "end " + "" + "rule \"Log S\" " + "when " + - " $t : Student() @watch( name ) " + + " $t : Student() @watch(name) " + "then " + "end " + "rule \"Log W\" " + "when " + - " $t : Worker() @watch( name ) " + + " $t : Worker() @watch(name) " + "then " + "end " + "rule \"Log SW\" " + "when " + - " $t : StudentWorker() @watch( name ) " + + " $t : StudentWorker() @watch(name) " + "then " + "end " + "rule \"Log RA\" " + "when " + - " $t : Assistant() @watch( name ) " + + " $t : Assistant() @watch(name) " + "then " + "end " + "rule \"Log Px\" " + "salience -1 " + "when " + - " $p : Person() @watch( name ) " + + " $p : Person() @watch(name) " + "then " + "end " + "" + "rule \"Mod\" " + "salience -10 " + "when " + - " String( this == \"go\" ) " + - " $p : Student( name == \"john\" ) " + + " String(this == \"go\") " + + " $p : Student(name == \"john\") " + "then " + - " modify ( $p ) { setName( \"alan\" ); } " + + " modify ($p) { setName(\"alan\"); } " + "end "; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (KieBase) kbase); // not relevant - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); int k = ksession.fireAllRules(); assertThat(k).isEqualTo(7); - ksession.insert( "go" ); + ksession.insert("go"); k = ksession.fireAllRules(); assertThat(k).isEqualTo(6); @@ -2324,48 +2133,48 @@ public void testTraitModifyCore2a() { "rule \"Init\" \n" + "when \n" + "then \n" + - " Person p = new Person( \"john\" ); \n" + - " insert( p ); \n" + + " Person p = new Person(\"john\"); \n" + + " insert(p); \n" + "end \n" + "" + "rule \"Don\" \n" + "when \n" + - " $p : Person( name == \"john\" ) \n" + + " $p : Person(name == \"john\") \n" + "then \n" + - " don( $p, Worker.class ); \n" + - " don( $p, StudentWorker.class ); \n" + + " don($p, Worker.class); \n" + + " don($p, StudentWorker.class); \n" + "end \n" + "" + "rule \"Log W\" \n" + "when \n" + - " $t : Worker( this isA StudentWorker ) @watch( name ) \n" + + " $t : Worker(this isA StudentWorker) @watch(name) \n" + "then \n" + - " list.add( true ); \n" + + " list.add(true); \n" + "end \n" + "rule \"Log SW\" \n" + "when \n" + - " $t : StudentWorker() @watch( name ) \n" + + " $t : StudentWorker() @watch(name) \n" + "then \n" + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (KieBase) kbase); // not relevant - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); - ArrayList list = new ArrayList( ); - ksession.setGlobal( "list", list ); + ArrayList list = new ArrayList(); + ksession.setGlobal("list", list); int k = ksession.fireAllRules(); assertThat(list.contains(true)).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); } @@ -2397,79 +2206,79 @@ public void testTraitModifyCore3() { "" + "rule \"Init\" when \n" + "then \n" + - " insert( new Core() );" + + " insert(new Core());" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + - " $x : Core( id == 0 )\n" + + " $x : Core(id == 0)\n" + "then\n" + - " don( $x, A.class );\n" + - " don( $x, B.class );\n" + - " don( $x, D.class );\n" + - " don( $x, F.class );\n" + - " don( $x, E.class );\n" + - " don( $x, I.class );\n" + - " don( $x, K.class );\n" + - " don( $x, J.class );\n" + - " don( $x, C.class );\n" + - " don( $x, H.class );\n" + - " don( $x, G.class );\n" + - " don( $x, L.class );\n" + - " don( $x, M.class );\n" + - " don( $x, N.class );\n" + + " don($x, A.class);\n" + + " don($x, B.class);\n" + + " don($x, D.class);\n" + + " don($x, F.class);\n" + + " don($x, E.class);\n" + + " don($x, I.class);\n" + + " don($x, K.class);\n" + + " don($x, J.class);\n" + + " don($x, C.class);\n" + + " don($x, H.class);\n" + + " don($x, G.class);\n" + + " don($x, L.class);\n" + + " don($x, M.class);\n" + + " don($x, N.class);\n" + "end\n" + "\n" + "\n" + "\n" + - "rule \"Log A\" when $x : A( id == 1 ) then list.add( 1 ); end \n" + - "rule \"Log B\" when $x : B( id == 1 ) then list.add( 2 ); end \n" + - "rule \"Log C\" when $x : C( id == 1 ) then list.add( 3 ); end \n" + - "rule \"Log D\" when $x : D( id == 1 ) then list.add( 4 ); end \n" + - "rule \"Log E\" when $x : E( id == 1 ) then list.add( 5 ); end \n" + - "rule \"Log F\" when $x : F( id == 1 ) then list.add( 6 ); end \n" + - "rule \"Log G\" when $x : G( id == 1 ) then list.add( 7 ); end \n" + - "rule \"Log H\" when $x : H( id == 1 ) then list.add( 8 ); end \n" + - "rule \"Log I\" when $x : I( id == 1 ) then list.add( 9 ); end \n" + - "rule \"Log J\" when $x : J( id == 1 ) then list.add( 10 ); end \n" + - "rule \"Log K\" when $x : K( id == 1 ) then list.add( 11 ); end \n" + - "rule \"Log L\" when $x : L( id == 1 ) then list.add( 12 ); end \n" + - "rule \"Log M\" when $x : M( id == 1 ) then list.add( 13 ); end \n" + - "rule \"Log N\" when $x : N( id == 1 ) then list.add( 14 ); end \n" + + "rule \"Log A\" when $x : A(id == 1) then list.add(1); end \n" + + "rule \"Log B\" when $x : B(id == 1) then list.add(2); end \n" + + "rule \"Log C\" when $x : C(id == 1) then list.add(3); end \n" + + "rule \"Log D\" when $x : D(id == 1) then list.add(4); end \n" + + "rule \"Log E\" when $x : E(id == 1) then list.add(5); end \n" + + "rule \"Log F\" when $x : F(id == 1) then list.add(6); end \n" + + "rule \"Log G\" when $x : G(id == 1) then list.add(7); end \n" + + "rule \"Log H\" when $x : H(id == 1) then list.add(8); end \n" + + "rule \"Log I\" when $x : I(id == 1) then list.add(9); end \n" + + "rule \"Log J\" when $x : J(id == 1) then list.add(10); end \n" + + "rule \"Log K\" when $x : K(id == 1) then list.add(11); end \n" + + "rule \"Log L\" when $x : L(id == 1) then list.add(12); end \n" + + "rule \"Log M\" when $x : M(id == 1) then list.add(13); end \n" + + "rule \"Log N\" when $x : N(id == 1) then list.add(14); end \n" + "" + - "rule \"Log Core\" when $x : Core( $id : id ) then end \n" + + "rule \"Log Core\" when $x : Core($id : id) then end \n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + - " String( this == \"go\" ) \n" + - " $x : Core( id == 0 ) \n" + + " String(this == \"go\") \n" + + " $x : Core(id == 0) \n" + "then \n" + - " modify ( $x ) { setId( 1 ); }" + + " modify ($x) { setId(1); }" + "end \n" + ""; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (KieBase) kbase); // not relevant - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); List list = new ArrayList(); KieSession ksession = kbase.newKieSession(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); ksession.fireAllRules(); - ksession.insert( "go" ); + ksession.insert("go"); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(14); - for ( int j = 1; j <= 14; j++ ) { + assertThat(list).hasSize(14); + for (int j = 1; j <= 14; j++) { assertThat(list.contains(j)).isTrue(); } @@ -2515,73 +2324,73 @@ public void testTraitModifyCoreWithPropertyReactivity() { "rule \"Init\" \n" + "when \n" + "then \n" + - " Person p = new Person( 109.99, \"john\", 18 ); \n" + - " insert( p ); \n" + + " Person p = new Person(109.99, \"john\", 18); \n" + + " insert(p); \n" + "end \n" + "" + "rule \"Don\" \n" + "when \n" + - " $p : Person( name == \"john\" ) \n" + + " $p : Person(name == \"john\") \n" + "then \n" + - " don( $p, StudentWorker.class ); \n" + - " don( $p, Assistant.class ); \n" + + " don($p, StudentWorker.class); \n" + + " don($p, Assistant.class); \n" + "end \n" + "" + "rule \"Log S\" \n" + "when \n" + - " $t : Student( age == 44 ) \n" + + " $t : Student(age == 44) \n" + "then \n" + - " list.add( 1 );\n " + + " list.add(1);\n " + "end \n" + "rule \"Log W\" \n" + "when \n" + - " $t : Worker( name == \"alan\" ) \n" + + " $t : Worker(name == \"alan\") \n" + "then \n" + - " list.add( 2 );\n " + + " list.add(2);\n " + "end \n" + "rule \"Log SW\" \n" + "when \n" + - " $t : StudentWorker( age == 44 ) \n" + + " $t : StudentWorker(age == 44) \n" + "then \n" + - " list.add( 3 );\n " + + " list.add(3);\n " + "end \n" + "rule \"Log Pers\" \n" + "when \n" + - " $t : Person( age == 44 ) \n" + + " $t : Person(age == 44) \n" + "then \n" + - " list.add( 4 );\n " + + " list.add(4);\n " + "end \n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + - " String( this == \"go\" ) \n" + - " $p : Student( name == \"john\" ) \n" + + " String(this == \"go\") \n" + + " $p : Student(name == \"john\") \n" + "then \n" + - " modify ( $p ) { setSchool( \"myschool\" ), setAge( 44 ), setName( \"alan\" ); } " + + " modify ($p) { setSchool(\"myschool\"), setAge(44), setName(\"alan\"); } " + "end \n"; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (KieBase) kbase); // not relevant - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); List list = new ArrayList(); KieSession ksession = kbase.newKieSession(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); int k = ksession.fireAllRules(); - ksession.insert( "go" ); + ksession.insert("go"); k = ksession.fireAllRules(); assertThat(k).isEqualTo(5); - assertThat(list.size()).isEqualTo(4); + assertThat(list).hasSize(4); assertThat(list.contains(1)).isTrue(); assertThat(list.contains(2)).isTrue(); assertThat(list.contains(3)).isTrue(); @@ -2609,22 +2418,22 @@ public void testTraitEncodeExtendingNonTrait() { KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s2.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s2.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalRuleBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder2.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder2.hasErrors() ) { - fail( kbuilder2.getErrors().toString() ); + kbuilder2.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder2.hasErrors()) { + fail(kbuilder2.getErrors().toString()); } - kbase.addPackages( kbuilder2.getKnowledgePackages() ); + kbase.addPackages(kbuilder2.getKnowledgePackages()); } @@ -2634,15 +2443,15 @@ public void testTraitEncodeExtendingNonTrait() { public void isAWithBackChaining() { String source = "org/drools/compiler/factmodel/traits/testTraitIsAWithBC.drl"; - KieSession ksession = getSession( source ); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + KieSession ksession = getSession(source); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); ksession.fireAllRules(); - ksession.insert( "Como" ); + ksession.insert("Como"); ksession.fireAllRules(); @@ -2653,7 +2462,7 @@ public void isAWithBackChaining() { @Test - public void testIsAEvaluatorOnClassification( ) { + public void testIsAEvaluatorOnClassification() { String source = "package t.x \n" + "\n" + "global java.util.List list; \n" + @@ -2674,7 +2483,7 @@ public void testIsAEvaluatorOnClassification( ) { "then\n" + " Entity o = new Entity();\n" + " insert(o);\n" + - " don( o, D.class ); \n" + + " don(o, D.class); \n" + "end\n" + "" + "rule Don when\n" + @@ -2684,26 +2493,26 @@ public void testIsAEvaluatorOnClassification( ) { "" + "rule \"Rule 0 >> http://t/x#D\"\n" + "when\n" + - " $t : org.drools.base.factmodel.traits.Thing( $c : core, this not isA t.x.E.class, this isA t.x.D.class ) " + + " $t : org.drools.base.factmodel.traits.Thing($c : core, this not isA t.x.E.class, this isA t.x.D.class) " + "then\n" + - " list.add( \"E\" ); \n" + - " don( $t, E.class ); \n" + + " list.add(\"E\"); \n" + + " don($t, E.class); \n" + "end\n" + "" + "rule React \n" + "when E() then \n" + - " list.add( \"X\" ); \n" + + " list.add(\"X\"); \n" + "end \n" ; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - assertThat(list.size()).isEqualTo(2); + assertThat(list).hasSize(2); assertThat(list.contains("E")).isTrue(); assertThat(list.contains("X")).isTrue(); @@ -2712,7 +2521,7 @@ public void testIsAEvaluatorOnClassification( ) { @Test - public void testShedWithTMS( ) { + public void testShedWithTMS() { String source = "package t.x \n" + "\n" + "global java.util.List list; \n" + @@ -2733,8 +2542,8 @@ public void testShedWithTMS( ) { "then\n" + " Entity o = new Entity();\n" + " insert(o);\n" + - " don( o, Thing.class ); \n" + - " don( o, D.class ); \n" + + " don(o, Thing.class); \n" + + " don(o, D.class); \n" + "end\n" + "" + "rule Don when\n" + @@ -2744,15 +2553,15 @@ public void testShedWithTMS( ) { "" + "rule \"Rule 0 >> http://t/x#D\"\n" + "when\n" + - " $t : org.drools.base.factmodel.traits.Thing( $c : core, _isTop(), this not isA t.x.E.class, this isA t.x.D.class ) " + + " $t : org.drools.base.factmodel.traits.Thing($c : core, _isTop(), this not isA t.x.E.class, this isA t.x.D.class) " + "then\n" + - " list.add( \"E\" ); \n" + - " don( $t, E.class ); \n" + + " list.add(\"E\"); \n" + + " don($t, E.class); \n" + "end\n" + "" + "rule React \n" + "when $x : E() then \n" + - " list.add( \"X\" ); \n" + + " list.add(\"X\"); \n" + "end \n" + "" + "rule Shed \n" + @@ -2760,31 +2569,31 @@ public void testShedWithTMS( ) { " $s : String() \n" + " $d : Entity() \n" + "then \n" + - " delete( $s ); \n" + - " shed( $d, D.class );\n" + + " delete($s); \n" + + " shed($d, D.class);\n" + "end \n" + "" ; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - LOGGER.debug( list.toString() ); - assertThat(list.size()).isEqualTo(2); + LOGGER.debug(list.toString()); + assertThat(list).hasSize(2); assertThat(list.contains("E")).isTrue(); assertThat(list.contains("X")).isTrue(); - ks.insert( "shed" ); + ks.insert("shed"); ks.fireAllRules(); - for ( Object o : ks.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ks.getObjects()) { + LOGGER.debug(o.toString()); } - assertThat(ks.getObjects().size()).isEqualTo(3); + assertThat(ks.getObjects()).hasSize(3); } @@ -2812,7 +2621,7 @@ public void testTraitInitialization() { "declare Bar\n" + " @Traitable()\n" + " hardList : List \n" + - " moreList : List = Arrays.asList( 1, 2, 3 ) \n" + + " moreList : List = Arrays.asList(1, 2, 3) \n" + "\n" + "end\n" + "" + @@ -2820,41 +2629,41 @@ public void testTraitInitialization() { "then\n" + " Bar o = new Bar();\n" + " insert(o);\n" + - " Thing t = don( o, Thing.class ); \n" + - " t.getFields().put( \"otraList\", Arrays.asList( 42 ) ); \n" + - " don( o, Foo.class ); \n" + + " Thing t = don(o, Thing.class); \n" + + " t.getFields().put(\"otraList\", Arrays.asList(42)); \n" + + " don(o, Foo.class); \n" + "end\n" + "" + "rule Don when\n" + - " $x : Foo( $h : hardList, $s : softList, $o : otraList, $m : moreList, $i : primFld, $d : primDbl ) \n" + + " $x : Foo($h : hardList, $s : softList, $o : otraList, $m : moreList, $i : primFld, $d : primDbl) \n" + "then \n" + - " list.add( $h ); \n" + - " list.add( $s ); \n" + - " list.add( $o ); \n" + - " list.add( $m ); \n" + - " list.add( $i ); \n" + - " list.add( $d ); \n" + + " list.add($h); \n" + + " list.add($s); \n" + + " list.add($o); \n" + + " list.add($m); \n" + + " list.add($i); \n" + + " list.add($d); \n" + "end\n" + "" ; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - assertThat(list.size()).isEqualTo(6); + assertThat(list).hasSize(6); assertThat(list.contains(null)).isFalse(); - List hard = (List) list.get( 0 ); - List soft = (List) list.get( 1 ); - List otra = (List) list.get( 2 ); - List more = (List) list.get( 3 ); + List hard = (List) list.get(0); + List soft = (List) list.get(1); + List otra = (List) list.get(2); + List more = (List) list.get(3); - assertThat(hard.isEmpty()).isTrue(); - assertThat(soft.isEmpty()).isTrue(); + assertThat(hard).isEmpty(); + assertThat(soft).isEmpty(); assertThat(Arrays.asList(1, 2, 3)).isEqualTo(more); assertThat(List.of(42)).isEqualTo(otra); @@ -2892,21 +2701,21 @@ public void testUnTraitedBean() { "end\n" + "" + "rule Check when\n" + - " $x : Bar( this not isA Foo ) \n" + + " $x : Bar(this not isA Foo) \n" + "then \n" + "end\n" + "rule Check2 when\n" + - " $x : Bar2( this not isA Foo ) \n" + + " $x : Bar2(this not isA Foo) \n" + "then \n" + "end\n" + ""; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); } @@ -2914,7 +2723,7 @@ public void testUnTraitedBean() { @Test - public void testIsAOptimization( ) { + public void testIsAOptimization() { String source = "package t.x \n" + "import java.util.*; \n" + "import org.drools.base.factmodel.traits.Thing \n" + @@ -2937,44 +2746,44 @@ public void testIsAOptimization( ) { "rule Init when\n" + "then\n" + " Kore k = new Kore();\n" + - " don( k, E.class ); \n" + + " don(k, E.class); \n" + "end\n" + "" + "rule Check_1 when\n" + - " $x : Kore( this isA [ B, D ] ) \n" + + " $x : Kore(this isA [ B, D ]) \n" + "then \n" + - " list.add( \" B+D \" ); \n" + + " list.add(\" B+D \"); \n" + "end\n" + "" + "rule Check_2 when\n" + - " $x : Kore( this isA [ A ] ) \n" + + " $x : Kore(this isA [ A ]) \n" + "then \n" + - " list.add( \" A \" ); \n" + + " list.add(\" A \"); \n" + "end\n" + "rule Check_3 when\n" + - " $x : Kore( this not isA [ F ] ) \n" + + " $x : Kore(this not isA [ F ]) \n" + "then \n" + - " list.add( \" F \" ); \n" + + " list.add(\" F \"); \n" + "end\n" + ""; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - assertThat(list.size()).isEqualTo(3); + assertThat(list).hasSize(3); } @Test - public void testTypeRefractionOnInsert( ) { + public void testTypeRefractionOnInsert() { String source = "package t.x \n" + "import java.util.*; \n" + "import org.drools.base.factmodel.traits.Thing \n" + @@ -2997,37 +2806,37 @@ public void testTypeRefractionOnInsert( ) { "rule Init when\n" + "then\n" + " Kore k = new Kore();\n" + - " don( k, B.class ); \n" + - " don( k, C.class ); \n" + - " don( k, D.class ); \n" + - " don( k, E.class ); \n" + - " don( k, A.class ); \n" + - " don( k, F.class ); \n" + + " don(k, B.class); \n" + + " don(k, C.class); \n" + + " don(k, D.class); \n" + + " don(k, E.class); \n" + + " don(k, A.class); \n" + + " don(k, F.class); \n" + "end\n" + "" + "rule Check_1 when\n" + - " $x : A( ) \n" + + " $x : A() \n" + "then \n" + - " list.add( $x ); \n" + + " list.add($x); \n" + "end\n" + ""; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); } @Test - public void testTypeRefractionOnQuery( ) { + public void testTypeRefractionOnQuery() { String source = "declare BaseObject\n" + "@Traitable\n" + "id : String @key\n" + @@ -3057,19 +2866,19 @@ public void testTypeRefractionOnQuery( ) { "end"; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); ks.fireAllRules(); - QueryResults res = ks.getQueryResults( "QueryTraitA" ); + QueryResults res = ks.getQueryResults("QueryTraitA"); - assertThat(res.size()).isEqualTo(1); + assertThat(res).hasSize(1); } @Test - public void testTypeRefractionOnQuery2( ) { + public void testTypeRefractionOnQuery2() { String source = "package t.x \n" + "import java.util.*; \n" + "import org.drools.base.factmodel.traits.Thing \n" + @@ -3093,43 +2902,43 @@ public void testTypeRefractionOnQuery2( ) { "rule Init when\n" + "then\n" + " Kore k = new Kore();\n" + - " don( k, C.class ); \n" + - " don( k, D.class ); \n" + - " don( k, E.class ); \n" + - " don( k, B.class ); \n" + - " don( k, A.class ); \n" + - " don( k, F.class ); \n" + - " don( k, G.class ); \n" + - " shed( k, B.class ); \n" + + " don(k, C.class); \n" + + " don(k, D.class); \n" + + " don(k, E.class); \n" + + " don(k, B.class); \n" + + " don(k, A.class); \n" + + " don(k, F.class); \n" + + " don(k, G.class); \n" + + " shed(k, B.class); \n" + "end\n" + "" + "rule RuleA\n" + "when \n" + - " $x : A( ) \n" + + " $x : A() \n" + "then \n" + "end\n" + " \n" + "query queryA1\n" + - " $x := A( ) \n" + + " $x := A() \n" + "end\n" + ""; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); QueryResults res; - res = ks.getQueryResults( "queryA1" ); - assertThat(res.size()).isEqualTo(1); + res = ks.getQueryResults("queryA1"); + assertThat(res).hasSize(1); } @Test - public void testNodePartitioningByProxies( ) { + public void testNodePartitioningByProxies() { String source = "package t.x " + "import java.util.*; " + "import org.drools.base.factmodel.traits.Thing " + @@ -3154,50 +2963,50 @@ public void testNodePartitioningByProxies( ) { "then " + " Kore k = new Kore(); " + - " don( k, C.class ); " + - " don( k, D.class ); " + - " don( k, B.class ); " + - " don( k, A.class ); " + - " don( k, F.class ); " + - " don( k, E.class ); " + - " don( k, G.class ); " + + " don(k, C.class); " + + " don(k, D.class); " + + " don(k, B.class); " + + " don(k, A.class); " + + " don(k, F.class); " + + " don(k, E.class); " + + " don(k, G.class); " + "end "; - for ( char c = 'A'; c <= 'G'; c++ ) { + for (char c = 'A'; c <= 'G'; c++) { String C = "" + c; source += "rule Rule" + C + - " when " + C + "() then list.add( '"+ C + "' ); end "; + " when " + C + "() then list.add('"+ C + "'); end "; } source += "rule RuleAll " + "when " + " A() B() C() D() E() F() G() " + "then " + - " list.add( 'Z' ); " + + " list.add('Z'); " + "end " + ""; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - LOGGER.debug( list.toString() ); + LOGGER.debug(list.toString()); assertThat(list).isEqualTo(Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'Z')); - for ( Object o : ks.getObjects(object -> object instanceof TraitableBean) ) { - Set otns = checkOTNPartitioning( (TraitableBean) o, ks ); - assertThat(otns.size()).isEqualTo(7); + for (Object o : ks.getObjects(object -> object instanceof TraitableBean)) { + Set otns = checkOTNPartitioning((TraitableBean) o, ks); + assertThat(otns).hasSize(7); } } @Test - public void testNodePartitioningByProxiesAfterShed( ) { + public void testNodePartitioningByProxiesAfterShed() { String source = "package t.x " + "import java.util.*; " + "import org.drools.base.factmodel.traits.Thing \n" + @@ -3222,22 +3031,22 @@ public void testNodePartitioningByProxiesAfterShed( ) { "then \n" + " Kore k = new Kore(); \n" + - " don( k, C.class ); \n" + - " don( k, D.class ); \n" + - " don( k, B.class ); \n" + - " don( k, A.class ); \n" + - " don( k, F.class ); \n" + - " don( k, E.class ); \n" + - " don( k, G.class ); \n" + - " shed( k, B.class ); \n" + + " don(k, C.class); \n" + + " don(k, D.class); \n" + + " don(k, B.class); \n" + + " don(k, A.class); \n" + + " don(k, F.class); \n" + + " don(k, E.class); \n" + + " don(k, G.class); \n" + + " shed(k, B.class); \n" + "end \n"; - for ( char c = 'A'; c <= 'G'; c++ ) { + for (char c = 'A'; c <= 'G'; c++) { String C = "" + c; source += "rule Rule" + C + " when \n" + C + "() \nthen\n" + " System.out.println(\"Rule " + C + "\");\n" + - " list.add( '"+ C + "' ); \n" + + " list.add('"+ C + "'); \n" + "end \n"; } @@ -3245,32 +3054,32 @@ public void testNodePartitioningByProxiesAfterShed( ) { "when \n" + " A() D() G() \n" + "then \n" + - " list.add( 'Z' ); \n" + + " list.add('Z'); \n" + "end \n" + ""; System.out.println(source); - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - LOGGER.debug( list.toString() ); + LOGGER.debug(list.toString()); assertThat(list).isEqualTo(Arrays.asList('A', 'D', 'G', 'Z')); - for ( Object o : ks.getObjects(object -> object instanceof TraitableBean) ) { - Set otns = checkOTNPartitioning( (TraitableBean) o, ks ); - assertThat(otns.size()).isEqualTo(3); + for (Object o : ks.getObjects(object -> object instanceof TraitableBean)) { + Set otns = checkOTNPartitioning((TraitableBean) o, ks); + assertThat(otns).hasSize(3); } } @Test - public void testTypeRefractionOnQueryWithIsA( ) { + public void testTypeRefractionOnQueryWithIsA() { String source = "package t.x \n" + "import java.util.*; \n" + "import org.drools.base.factmodel.traits.Thing \n" + @@ -3293,42 +3102,42 @@ public void testTypeRefractionOnQueryWithIsA( ) { "rule Init when\n" + "then\n" + " Kore k = new Kore();\n" + - " don( k, C.class ); \n" + - " don( k, D.class ); \n" + - " don( k, E.class ); \n" + - " don( k, B.class ); \n" + - " don( k, A.class ); \n" + - " don( k, F.class ); \n" + - " shed( k, B.class ); \n" + + " don(k, C.class); \n" + + " don(k, D.class); \n" + + " don(k, E.class); \n" + + " don(k, B.class); \n" + + " don(k, A.class); \n" + + " don(k, F.class); \n" + + " shed(k, B.class); \n" + "end\n" + "" + " \n" + "query queryA\n" + - " $x := Kore( this isA A ) \n" + + " $x := Kore(this isA A) \n" + "end\n" + ""; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - QueryResults res = ks.getQueryResults( "queryA" ); + QueryResults res = ks.getQueryResults("queryA"); Iterator iter = res.iterator(); - Object a = iter.next().get( "$x" ); + Object a = iter.next().get("$x"); assertThat(iter.hasNext()).isFalse(); - assertThat(res.size()).isEqualTo(1); + assertThat(res).hasSize(1); } @Test - public void testCoreUpdate4( ) { + public void testCoreUpdate4() { String source = "package t.x \n" + "import java.util.*; \n" + "import org.drools.base.factmodel.traits.Thing \n" + @@ -3350,8 +3159,8 @@ public void testCoreUpdate4( ) { "rule Init \n" + "when\n" + "then\n" + - " Kore k = new Kore( 44 );\n" + - " insert( k ); \n" + + " Kore k = new Kore(44);\n" + + " insert(k); \n" + "end\n" + "" + "" + @@ -3360,27 +3169,27 @@ public void testCoreUpdate4( ) { "when\n" + " $x : Kore() \n" + "then \n" + - " don( $x, A.class ); \n" + + " don($x, A.class); \n" + "end\n" + "rule React \n" + "salience 1" + "when\n" + - " $x : Kore( this isA A.class ) \n" + + " $x : Kore(this isA A.class) \n" + "then \n" + - " list.add( $x ); \n" + + " list.add($x); \n" + "end\n" + ""; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - for ( Object o : ks.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ks.getObjects()) { + LOGGER.debug(o.toString()); } - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); } @@ -3403,118 +3212,118 @@ public void traitLogicalSupportAnddelete() { " name : String\n" + "end\n" + "\n" + - "rule Init when then insert( new Person( \"john\" ) ); end \n" + + "rule Init when then insert(new Person(\"john\")); end \n" + "" + "rule \"Don Logical\"\n" + "when\n" + - " $s : String( this == \"trigger1\" )\n" + + " $s : String(this == \"trigger1\")\n" + " $p : Person() \n" + "then\n" + - " don( $p, Student.class, true );\n" + + " don($p, Student.class, true);\n" + "end\n" + "" + "rule \"Don Logical2\"\n" + "when\n" + - " $s : String( this == \"trigger2\" )\n" + + " $s : String(this == \"trigger2\")\n" + " $p : Person() \n" + "then\n" + - " don( $p, Student.class, true );\n" + + " don($p, Student.class, true);\n" + "end\n" + "" + "rule \"Undon \"\n" + "when\n" + - " $s : String( this == \"trigger3\" )\n" + + " $s : String(this == \"trigger3\")\n" + " $p : Person() \n" + "then\n" + - " shed( $p, org.drools.base.factmodel.traits.Thing.class ); " + - " delete( $s ); \n" + + " shed($p, org.drools.base.factmodel.traits.Thing.class); " + + " delete($s); \n" + "end\n" + " " + "rule \"Don Logical3\"\n" + "when\n" + - " $s : String( this == \"trigger4\" )\n" + + " $s : String(this == \"trigger4\")\n" + " $p : Person() \n" + "then\n" + - " don( $p, Student.class, true );" + + " don($p, Student.class, true);" + "end\n" + " " + "rule \"Undon 2\"\n" + "when\n" + - " $s : String( this == \"trigger5\" )\n" + + " $s : String(this == \"trigger5\")\n" + " $p : Person() \n" + "then\n" + - " delete( $s ); \n" + - " delete( $p ); \n" + + " delete($s); \n" + + " delete($p); \n" + "end\n" + ""; KieSession ksession = getSessionFromString(drl); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); - FactHandle h1 = ksession.insert( "trigger1" ); - FactHandle h2 = ksession.insert( "trigger2" ); + FactHandle h1 = ksession.insert("trigger1"); + FactHandle h2 = ksession.insert("trigger2"); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - LOGGER.debug( "---------------------------------" ); + LOGGER.debug("---------------------------------"); - assertThat(ksession.getObjects().size()).isEqualTo(4); + assertThat(ksession.getObjects()).hasSize(4); - ksession.delete( h1 ); + ksession.delete(h1); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - LOGGER.debug( "---------------------------------" ); + LOGGER.debug("---------------------------------"); - assertThat(ksession.getObjects().size()).isEqualTo(3); + assertThat(ksession.getObjects()).hasSize(3); - ksession.delete( h2 ); + ksession.delete(h2); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - LOGGER.debug( "---------------------------------" ); + LOGGER.debug("---------------------------------"); - assertThat(ksession.getObjects().size()).isEqualTo(1); + assertThat(ksession.getObjects()).hasSize(1); - ksession.insert( "trigger3" ); + ksession.insert("trigger3"); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString()); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - LOGGER.debug( "---------------------------------" ); + LOGGER.debug("---------------------------------"); - assertThat(ksession.getObjects().size()).isEqualTo(1); + assertThat(ksession.getObjects()).hasSize(1); - ksession.insert( "trigger4" ); + ksession.insert("trigger4"); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - LOGGER.debug( "---------------------------------" ); + LOGGER.debug("---------------------------------"); - assertThat(ksession.getObjects().size()).isEqualTo(3); + assertThat(ksession.getObjects()).hasSize(3); - ksession.insert( "trigger5" ); + ksession.insert("trigger5"); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - LOGGER.debug( "---------------------------------" ); + LOGGER.debug("---------------------------------"); - assertThat(ksession.getObjects().size()).isEqualTo(1); + assertThat(ksession.getObjects()).hasSize(1); } @Test @@ -3534,18 +3343,18 @@ public void testShedThing() { "" + "rule \"Init\" when \n" + "then \n" + - " insert( new Core() );" + + " insert(new Core());" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + - " $x : Core( id == 0 )\n" + + " $x : Core(id == 0)\n" + "then\n" + - " don( $x, A.class );\n" + - " don( $x, B.class );\n" + - " don( $x, C.class );\n" + - " don( $x, D.class );\n" + - " don( $x, E.class );\n" + + " don($x, A.class);\n" + + " don($x, B.class);\n" + + " don($x, C.class);\n" + + " don($x, D.class);\n" + + " don($x, E.class);\n" + "end\n" + "\n" + "\n" + @@ -3553,38 +3362,38 @@ public void testShedThing() { "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + - " $g : String( this == \"go\" ) \n" + - " $x : Core( id == 0 ) \n" + + " $g : String(this == \"go\") \n" + + " $x : Core(id == 0) \n" + "then \n" + - " shed( $x, Thing.class ); " + - " delete( $g ); \n\n" + + " shed($x, Thing.class); " + + " delete($g); \n\n" + "end \n" + ""; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (KieBase) kbase); // not relevant - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); List list = new ArrayList(); KieSession ksession = kbase.newKieSession(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); ksession.fireAllRules(); - ksession.insert( "go" ); + ksession.insert("go"); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - assertThat(ksession.getObjects().size()).isEqualTo(1); + assertThat(ksession.getObjects()).hasSize(1); } @@ -3605,18 +3414,18 @@ public void testdeleteThings() { "" + "rule \"Init\" when \n" + "then \n" + - " insert( new Core() );" + + " insert(new Core());" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + - " $x : Core( id == 0 )\n" + + " $x : Core(id == 0)\n" + "then\n" + - " don( $x, A.class );\n" + - " don( $x, B.class );\n" + - " don( $x, C.class );\n" + - " don( $x, D.class );\n" + - " don( $x, E.class );\n" + + " don($x, A.class);\n" + + " don($x, B.class);\n" + + " don($x, C.class);\n" + + " don($x, D.class);\n" + + " don($x, E.class);\n" + "end\n" + "\n" + "\n" + @@ -3624,42 +3433,42 @@ public void testdeleteThings() { "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + - " $g : String( this == \"go\" ) \n" + - " $x : Core( id == 0 ) \n" + + " $g : String(this == \"go\") \n" + + " $x : Core(id == 0) \n" + "then \n" + - " delete( $x ); \n\n" + - " delete( $g ); \n\n" + + " delete($x); \n\n" + + " delete($g); \n\n" + "end \n" + ""; KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kbuilder.add( new ByteArrayResource( s1.getBytes() ), ResourceType.DRL ); - if ( kbuilder.hasErrors() ) { - fail( kbuilder.getErrors().toString() ); + kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL); + if (kbuilder.hasErrors()) { + fail(kbuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); TraitFactoryImpl.setMode(mode, (KieBase) kbase); // not relevant - kbase.addPackages( kbuilder.getKnowledgePackages() ); + kbase.addPackages(kbuilder.getKnowledgePackages()); List list = new ArrayList(); KieSession ksession = kbase.newKieSession(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); ksession.fireAllRules(); - ksession.insert( "go" ); + ksession.insert("go"); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - assertThat(ksession.getObjects().size()).isEqualTo(0); + assertThat(ksession.getObjects()).isEmpty(); } @Test - public void traitLogicalRemovalSimple( ) { + public void traitLogicalRemovalSimple() { String drl = "package org.drools.compiler.trait.test;\n" + "\n" + "import org.drools.base.factmodel.traits.Traitable;\n" + @@ -3685,34 +3494,34 @@ public void traitLogicalRemovalSimple( ) { "\n" + "rule \"Don Logical\"\n" + "when\n" + - " $s : String( this == \"trigger\" )\n" + + " $s : String(this == \"trigger\")\n" + "then\n" + - " Person p = new Person( \"john\" );\n" + - " insert( p ); \n" + - " don( p, Student.class, true );\n" + - " don( p, Worker.class );\n" + - " don( p, Scholar.class );\n" + + " Person p = new Person(\"john\");\n" + + " insert(p); \n" + + " don(p, Student.class, true);\n" + + " don(p, Worker.class);\n" + + " don(p, Scholar.class);\n" + "end"; KieSession ksession = getSessionFromString(drl); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); - FactHandle h = ksession.insert( "trigger" ); + FactHandle h = ksession.insert("trigger"); ksession.fireAllRules(); - assertThat(ksession.getObjects().size()).isEqualTo(5); + assertThat(ksession.getObjects()).hasSize(5); - ksession.delete( h ); + ksession.delete(h); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { + for (Object o : ksession.getObjects()) { // lose the string and the Student proxy - LOGGER.debug( o.toString() ); + LOGGER.debug(o.toString()); } - assertThat(ksession.getObjects().size()).isEqualTo(3); + assertThat(ksession.getObjects()).hasSize(3); } @@ -3723,15 +3532,15 @@ public static class TraitableFoo { private String id; - public TraitableFoo( String id, int x, Object k ) { - setId( id ); + public TraitableFoo(String id, int x, Object k) { + setId(id); } public String getId() { return id; } - public void setId( String id ) { + public void setId(String id) { this.id = id; } } @@ -3740,14 +3549,14 @@ public void setId( String id ) { public static class XYZ extends TraitableFoo { public XYZ() { - super( null, 0, null ); + super(null, 0, null); } } @Test - public void testTraitDonLegacyClassWithoutEmptyConstructor( ) { + public void testTraitDonLegacyClassWithoutEmptyConstructor() { String drl = "package org.drools.compiler.trait.test;\n" + "\n" + "import " + TraitableFoo.class.getCanonicalName() + ";" + @@ -3760,30 +3569,30 @@ public void testTraitDonLegacyClassWithoutEmptyConstructor( ) { "rule \"Don\"\n" + "no-loop \n" + "when\n" + - " $f : TraitableFoo( )\n" + + " $f : TraitableFoo()\n" + "then\n" + - " Bar b = don( $f, Bar.class );\n" + + " Bar b = don($f, Bar.class);\n" + "end"; KieSession ksession = getSessionFromString(drl); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); - ksession.addEventListener( new DebugAgendaEventListener( ) ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); + ksession.addEventListener(new DebugAgendaEventListener()); - ksession.insert( new TraitableFoo( "xx", 0, null ) ); + ksession.insert(new TraitableFoo("xx", 0, null)); ksession.fireAllRules(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - assertThat(ksession.getObjects().size()).isEqualTo(2); + assertThat(ksession.getObjects()).hasSize(2); } @Test - public void testdeleteCoreObjectChained( ) { + public void testdeleteCoreObjectChained() { String source = "package org.drools.test;\n" + "import java.util.List; \n" + "import org.drools.base.factmodel.traits.Thing \n" + @@ -3805,8 +3614,8 @@ public void testdeleteCoreObjectChained( ) { "when\n" + " $s : String() \n" + "then\n" + - " Kore k = new Kore( 44 );\n" + - " insertLogical( k ); \n" + + " Kore k = new Kore(44);\n" + + " insertLogical(k); \n" + "end\n" + "" + "" + @@ -3815,7 +3624,7 @@ public void testdeleteCoreObjectChained( ) { "when\n" + " $x : Kore() \n" + "then \n" + - " don( $x, A.class ); \n" + + " don($x, A.class); \n" + "end\n" + "" + "" + @@ -3824,32 +3633,32 @@ public void testdeleteCoreObjectChained( ) { "when \n" + " $x : String() \n" + "then \n" + - " delete( $x ); \n" + + " delete($x); \n" + "end \n" + "\n"; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); - ks.insert( "go" ); + ks.insert("go"); ks.fireAllRules(); - for ( Object o : ks.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ks.getObjects()) { + LOGGER.debug(o.toString()); } - assertThat(ks.getObjects().size()).isEqualTo(0); + assertThat(ks.getObjects()).isEmpty(); ks.dispose(); } @Test - public void testUpdateLegacyClass( ) { + public void testUpdateLegacyClass() { String source = "package org.drools.text;\n" + "\n" + "global java.util.List list;\n" + @@ -3866,40 +3675,40 @@ public void testUpdateLegacyClass( ) { "rule \"Init\"\n" + "salience 10 \n" + "when\n" + - " $p : Person( this not isA Student )\n" + + " $p : Person(this not isA Student)\n" + "then\n" + - " don( $p, Student.class );\n" + + " don($p, Student.class);\n" + "end\n" + "\n" + "rule \"Go\"\n" + "when\n" + - " $s : String( this == \"X\" )\n" + + " $s : String(this == \"X\")\n" + " $p : Person()\n" + "then\n" + - " delete( $s ); \n" + - " modify( $p ) { setName( $s ); }\n" + + " delete($s); \n" + + " modify($p) { setName($s); }\n" + "end\n" + "\n" + "rule \"Mod\"\n" + "when\n" + - " Student( name == \"X\" )\n" + + " Student(name == \"X\")\n" + "then\n" + - " list.add( 0 );\n" + + " list.add(0);\n" + "end"; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); - ks.insert( new Person( "john", 32 ) ); - ks.insert( "X" ); + ks.insert(new Person("john", 32)); + ks.insert("X"); ks.fireAllRules(); assertThat(list.contains(0)).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); ks.dispose(); } @@ -3924,7 +3733,7 @@ public void testSoftPropertyClash() { " fld2 : int = 4 \n" + " fld3 : double = 4.0 \n" + " fld4 : String = \"hello\" \n" + - " fldZ : String = \"hello\" @Alias( \"fld5\" )\n" + + " fldZ : String = \"hello\" @Alias(\"fld5\")\n" + "end\n" + "declare trait Worker\n" + " @propertyReactive \n" + @@ -3932,11 +3741,11 @@ public void testSoftPropertyClash() { " fld2 : String = \"b\" \n " + " fld3 : int = 11 \n " + " fld4 : Class = Object.class \n " + - " fldY : int = 42 @Alias( \"fld5\" )\n" + + " fldY : int = 42 @Alias(\"fld5\")\n" + "end\n" + "" + "rule \"Init\" when then \n" + - " insert( new Person() ); \n" + + " insert(new Person()); \n" + "end \n" + "" + "\n" + @@ -3944,44 +3753,44 @@ public void testSoftPropertyClash() { "when\n" + " $p : Person() \n" + "then\n" + - " Student $s = (Student) don( $p, Student.class );\n" + - " modify ( $s ) { setId( \"xyz\" ); } " + + " Student $s = (Student) don($p, Student.class);\n" + + " modify ($s) { setId(\"xyz\"); } " + " " + - " Worker $w = don( $p, Worker.class );\n" + - " modify ( $w ) { setId( 99 ); } " + + " Worker $w = don($p, Worker.class);\n" + + " modify ($w) { setId(99); } " + "end\n" + "\n" + "rule \"Stud\"\n" + "when\n" + - " $s : Student( $sid : id == \"xyz\", $f2 : fld2, $f3 : fld3, $f4 : fld4, $f5 : fldZ )\n" + + " $s : Student($sid : id == \"xyz\", $f2 : fld2, $f3 : fld3, $f4 : fld4, $f5 : fldZ)\n" + "then\n" + - " list.add( $sid ); \n" + - " list.add( $f2 ); \n" + - " list.add( $f3 ); \n" + - " list.add( $f4 ); \n" + - " list.add( $f5 ); \n" + + " list.add($sid); \n" + + " list.add($f2); \n" + + " list.add($f3); \n" + + " list.add($f4); \n" + + " list.add($f5); \n" + "end\n" + "\n" + "rule \"Mod\"\n" + "when\n" + - " $w : Worker( $wid : id == 99, $f2 : fld2, $f3 : fld3, $f4 : fld4, $f5 : fldY )\n" + + " $w : Worker($wid : id == 99, $f2 : fld2, $f3 : fld3, $f4 : fld4, $f5 : fldY)\n" + "then\n" + - " list.add( $wid ); \n" + - " list.add( $f2 ); \n" + - " list.add( $f3 ); \n" + - " list.add( $f4 ); \n" + - " list.add( $f5 ); \n" + + " list.add($wid); \n" + + " list.add($f2); \n" + + " list.add($f3); \n" + + " list.add($f4); \n" + + " list.add($f5); \n" + "end"; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - assertThat(list.size()).isEqualTo(5); + assertThat(list).hasSize(5); assertThat(list).isEqualTo(Arrays.asList(99, "b", 11, Object.class, 42)); ks.dispose(); @@ -4035,40 +3844,40 @@ public void testMultipleModifications() { "rule \"init\"\n" + "when\n" + "then\n" + - " insert( new Person(\"1234\",\"IR\",true,true) );\n" + + " insert(new Person(\"1234\",\"IR\",true,true));\n" + "end\n" + "\n" + "rule \"check for being student\"\n" + "when\n" + - " $p : Person( $ssn : ssn, $pob : pob, isStudent == true )\n" + + " $p : Person($ssn : ssn, $pob : pob, isStudent == true)\n" + "then\n" + - " Student st = (Student) don( $p , Student.class );\n" + - " modify( st ){\n" + - " setStudyingCountry( \"US\" );\n" + + " Student st = (Student) don($p , Student.class);\n" + + " modify(st){\n" + + " setStudyingCountry(\"US\");\n" + " }\n" + "end\n" + "\n" + "rule \"check for IR\"\n" + "when\n" + - " $p : Person( pob == \"IR\" )\n" + + " $p : Person(pob == \"IR\")\n" + "then\n" + - " don( $p , IRCitizen.class );\n" + + " don($p , IRCitizen.class);\n" + "end\n" + "\n" + "rule \"check for being US citizen\"\n" + "when\n" + - " $s : Student( studyingCountry == \"US\" )\n" + + " $s : Student(studyingCountry == \"US\")\n" + "then\n" + - " don( $s , USCitizen.class );\n" + + " don($s , USCitizen.class);\n" + "end\n" + "\n" + "rule \"check for being worker\"\n" + "when\n" + - " $p : Student( hasAssistantship == true, $sc : studyingCountry )\n" + + " $p : Student(hasAssistantship == true, $sc : studyingCountry)\n" + "then\n" + - " Worker wr = (Worker) don( $p , Worker.class );\n" + - " modify( wr ){\n" + - " setWorkingCountry( $sc );\n" + + " Worker wr = (Worker) don($p , Worker.class);\n" + + " modify(wr){\n" + + " setWorkingCountry($sc);\n" + " }\n" + "\n" + "end\n" + @@ -4076,27 +3885,27 @@ public void testMultipleModifications() { "rule \"Join Full\"\n" + "salience -1\n" + "when\n" + - " Student( ) // $sc := studyingCountry )\n" + - " USCitizen( )\n" + - " IRCitizen( ) // $pob := pob )\n" + - " Worker( ) // pob == $pob , workingCountry == $sc )\n" + + " Student() // $sc := studyingCountry)\n" + + " USCitizen()\n" + + " IRCitizen() // $pob := pob)\n" + + " Worker() // pob == $pob , workingCountry == $sc)\n" + "then\n" + - " list.add( 1 ); " + + " list.add(1); " + "end\n" + "\n" + "\n"; - KieSession ks = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); HashMap map; ks.fireAllRules(); assertThat(list.contains(1)).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); ks.dispose(); @@ -4122,25 +3931,25 @@ public void testPropagation() { "declare trait I extends E,H @propertyReactive end\n" + "declare trait J extends I @propertyReactive end\n" + "" + - "rule Init when then X x = new X(); insert( x ); don( x, F.class); end \n"+ - "rule Go when String( this == \"go\" ) $x : X() then don( $x, H.class); end \n" + - "rule Go2 when String( this == \"go2\" ) $x : X() then don( $x, D.class); end \n" + + "rule Init when then X x = new X(); insert(x); don(x, F.class); end \n"+ + "rule Go when String(this == \"go\") $x : X() then don($x, H.class); end \n" + + "rule Go2 when String(this == \"go2\") $x : X() then don($x, D.class); end \n" + ""; - for ( int j = 'A'; j <= 'J'; j ++ ) { + for (int j = 'A'; j <= 'J'; j ++) { String x = "" + (char) j; - drl += "rule \"Log " + x + "\" when " + x + "() then list.add( \"" + x + "\" ); end \n"; + drl += "rule \"Log " + x + "\" when " + x + "() then list.add(\"" + x + "\"); end \n"; - drl += "rule \"Log II" + x + "\" salience -1 when " + x + "( "; + drl += "rule \"Log II" + x + "\" salience -1 when " + x + "("; drl += "this isA H"; - drl += " ) then list.add( \"H" + x + "\" ); end \n"; + drl += ") then list.add(\"H" + x + "\"); end \n"; } - KieSession ks = new KieHelper().addContent( drl, ResourceType.DRL ).build().newKieSession(); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); @@ -4149,13 +3958,13 @@ public void testPropagation() { assertThat(list.contains("C")).isTrue(); assertThat(list.contains("E")).isTrue(); assertThat(list.contains("F")).isTrue(); - assertThat(list.size()).isEqualTo(5); + assertThat(list).hasSize(5); list.clear(); - LOGGER.debug( "---------------------------------------" ); + LOGGER.debug("---------------------------------------"); - ks.insert( "go" ); + ks.insert("go"); ks.fireAllRules(); assertThat(list.contains("H")).isTrue(); @@ -4167,13 +3976,13 @@ public void testPropagation() { assertThat(list.contains("HF")).isTrue(); assertThat(list.contains("HG")).isTrue(); assertThat(list.contains("HH")).isTrue(); - LOGGER.debug( list.toString() ); - assertThat(list.size()).isEqualTo(9); + LOGGER.debug(list.toString()); + assertThat(list).hasSize(9); list.clear(); - LOGGER.debug( "---------------------------------------" ); + LOGGER.debug("---------------------------------------"); - ks.insert( "go2" ); + ks.insert("go2"); ks.fireAllRules(); assertThat(list.contains("D")).isTrue(); @@ -4186,7 +3995,7 @@ public void testPropagation() { assertThat(list.contains("HH")).isTrue(); assertThat(list.contains("HH")).isTrue(); assertThat(list.contains("HD")).isTrue(); - assertThat(list.size()).isEqualTo(9); + assertThat(list).hasSize(9); ks.dispose(); @@ -4205,25 +4014,25 @@ public void testParentBlockers() { "declare trait B @propertyReactive end\n" + "declare trait C extends A, B @propertyReactive end \n" + "" + - "rule Init when then X x = new X(); insert( x ); don( x, A.class); don( x, B.class); end \n"+ - "rule Go when String( this == \"go\" ) $x : X() then don( $x, C.class); end \n" + - "rule Go2 when String( this == \"go2\" ) $x : C() then end \n"; + "rule Init when then X x = new X(); insert(x); don(x, A.class); don(x, B.class); end \n"+ + "rule Go when String(this == \"go\") $x : X() then don($x, C.class); end \n" + + "rule Go2 when String(this == \"go2\") $x : C() then end \n"; - KieSession ks = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - ks.insert( "go" ); + ks.insert("go"); ks.fireAllRules(); - ks.insert( "go2" ); + ks.insert("go2"); ks.fireAllRules(); - LOGGER.debug( "---------------------------------------" ); + LOGGER.debug("---------------------------------------"); ks.dispose(); @@ -4241,36 +4050,36 @@ public void testTraitLogicalTMS() { "declare trait A @propertyReactive end\n" + "declare trait B @propertyReactive end\n" + "" + - "rule Init when then X x = new X(); insert( x ); end \n"+ - "rule Go when String( this == \"go\" ) $x : X() then don( $x, A.class, true ); don( $x, B.class, true ); end \n" + - "rule Go2 when String( this == \"go2\" ) $x : X() then don( $x, A.class ); end \n" + - "rule Go3 when String( this == \"go3\" ) $x : A() not B() then list.add( 100 ); end \n" + + "rule Init when then X x = new X(); insert(x); end \n"+ + "rule Go when String(this == \"go\") $x : X() then don($x, A.class, true); don($x, B.class, true); end \n" + + "rule Go2 when String(this == \"go2\") $x : X() then don($x, A.class); end \n" + + "rule Go3 when String(this == \"go3\") $x : A() not B() then list.add(100); end \n" + ""; - KieSession ks = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); - FactHandle handle = ks.insert( "go" ); + FactHandle handle = ks.insert("go"); ks.fireAllRules(); - ks.insert( "go2" ); + ks.insert("go2"); ks.fireAllRules(); - ks.delete( handle ); + ks.delete(handle); ks.fireAllRules(); - LOGGER.debug( "---------------------------------------" ); + LOGGER.debug("---------------------------------------"); - for ( Object o : ks.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ks.getObjects()) { + LOGGER.debug(o.toString()); } - ks.insert( "go3" ); + ks.insert("go3"); ks.fireAllRules(); assertThat(list).isEqualTo(List.of(100)); @@ -4294,7 +4103,7 @@ public void testTraitNoType() { "\n" + "\n" + "declare Parent\n" + - "@Traitable( logical = true )" + + "@Traitable(logical = true)" + "@propertyReactive\n" + "end\n" + "\n" + @@ -4310,15 +4119,15 @@ public void testTraitNoType() { "then\n" + " Parent p = new Parent();" + " insert(p);\n" + - " ChildTrait ct = don( p , ChildTrait.class );\n" + + " ChildTrait ct = don(p , ChildTrait.class);\n" + " list.add(\"correct1\");\n" + "end\n" + "\n" + "rule \"check\"\n" + "no-loop\n" + "when\n" + - " $c : ChildTrait($n : naam == \"kudak\", id == 1020 )\n" + - " $p : Thing( core == $c.core, fields[\"naam\"] == $n )\n" + + " $c : ChildTrait($n : naam == \"kudak\", id == 1020)\n" + + " $p : Thing(core == $c.core, fields[\"naam\"] == $n)\n" + "then\n" + " list.add(\"correct2\");\n" + "end"; @@ -4354,36 +4163,36 @@ public void testTraitdeleteOrder() { "when \n" + " $e : Entity() \n" + "then\n" + - " don( $e, A.class ); \n" + - " don( $e, C.class ); \n" + - " don( $e, B.class ); \n" + + " don($e, A.class); \n" + + " don($e, C.class); \n" + + " don($e, B.class); \n" + "end\n" + ""; KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - FactHandle handle = ksession.insert( new Entity( ) ); + FactHandle handle = ksession.insert(new Entity()); ksession.fireAllRules(); final ArrayList list = new ArrayList(); - ksession.addEventListener( new RuleRuntimeEventListener() { - public void objectInserted( org.kie.api.event.rule.ObjectInsertedEvent objectInsertedEvent ) { } - public void objectUpdated( org.kie.api.event.rule.ObjectUpdatedEvent objectUpdatedEvent ) { } - public void objectDeleted( org.kie.api.event.rule.ObjectDeletedEvent objectRetractedEvent ) { + ksession.addEventListener(new RuleRuntimeEventListener() { + public void objectInserted(org.kie.api.event.rule.ObjectInsertedEvent objectInsertedEvent) { } + public void objectUpdated(org.kie.api.event.rule.ObjectUpdatedEvent objectUpdatedEvent) { } + public void objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent objectRetractedEvent) { Object o = objectRetractedEvent.getOldObject(); - if ( o instanceof TraitProxyImpl) { - String traitName = ( (TraitProxyImpl) o )._getTraitName(); - list.add( traitName.substring( traitName.lastIndexOf( "." ) + 1 ) ); + if (o instanceof TraitProxyImpl) { + String traitName = ((TraitProxyImpl) o)._getTraitName(); + list.add(traitName.substring(traitName.lastIndexOf(".") + 1)); } } - } ); + }); - ksession.delete( handle ); + ksession.delete(handle); ksession.fireAllRules(); - LOGGER.debug( list.toString() ); + LOGGER.debug(list.toString()); assertThat(list).isEqualTo(Arrays.asList("B", "C", "A")); } @@ -4399,7 +4208,7 @@ public void testTraitWithManySoftFields() { "\n" + "declare trait Tx \n"; - for ( int j = 0; j < 150; j ++ ) { + for (int j = 0; j < 150; j ++) { drl += " fld" + j + " : String \n"; } @@ -4411,17 +4220,17 @@ public void testTraitWithManySoftFields() { "rule \"don\"\n" + "when \n" + "then\n" + - " don( new TBean(), Tx.class ); \n" + + " don(new TBean(), Tx.class); \n" + "end\n" + "" + ""; KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); ksession.fireAllRules(); - assertThat(ksession.getObjects().size()).isEqualTo(2); + assertThat(ksession.getObjects()).hasSize(2); } @@ -4446,21 +4255,21 @@ public int getdeletes() { } @Override - public void objectInserted( org.kie.api.event.rule.ObjectInsertedEvent event ) { - if ( ! ( event.getObject() instanceof String ) ) { + public void objectInserted(org.kie.api.event.rule.ObjectInsertedEvent event) { + if (! (event.getObject() instanceof String)) { inserts++; } } @Override - public void objectUpdated( org.kie.api.event.rule.ObjectUpdatedEvent event ) { - if ( ! ( event.getObject() instanceof String ) ) { + public void objectUpdated(org.kie.api.event.rule.ObjectUpdatedEvent event) { + if (! (event.getObject() instanceof String)) { updates++; } } - public void objectDeleted( org.kie.api.event.rule.ObjectDeletedEvent objectdeleteedEvent ) { - if ( ! ( objectdeleteedEvent.getOldObject() instanceof String ) ) { + public void objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent objectdeleteedEvent) { + if (! (objectdeleteedEvent.getOldObject() instanceof String)) { deletes++; } } @@ -4497,54 +4306,54 @@ public void testDonManyTraitsAtOnce() { "when \n" + "then\n" + " TBean t = new TBean(); \n" + - " don( t, A.class ); \n" + - " don( t, B.class ); \n" + + " don(t, A.class); \n" + + " don(t, B.class); \n" + "end\n" + "" + "rule \"Don 2\" " + "when \n" + - " $s : String( this == \"go\" ) \n" + + " $s : String(this == \"go\") \n" + " $t : TBean() \n" + "then \n" + - " list.add( 0 ); \n" + - " don( $t, Arrays.asList( C.class, D.class, E.class, F.class ), true ); \n" + + " list.add(0); \n" + + " don($t, Arrays.asList(C.class, D.class, E.class, F.class), true); \n" + "end \n" + "" + "rule Clear \n" + "when \n" + - " $s : String( this == \"undo\" ) \n" + + " $s : String(this == \"undo\") \n" + " $t : TBean() \n" + "then \n" + - " delete( $s ); \n" + - " delete( $t ); \n" + + " delete($s); \n" + + " delete($t); \n" + "end \n" + "" + "rule C \n" + "when\n" + - " B( this isA C ) \n" + + " B(this isA C) \n" + "then \n" + - " list.add( 1 ); \n" + + " list.add(1); \n" + "end \n" + "rule D \n" + "when\n" + - " D( this isA A, this isA C ) \n" + + " D(this isA A, this isA C) \n" + "then \n" + - " list.add( 2 ); \n" + + " list.add(2); \n" + "end \n"+ "rule E \n" + "when\n" + - " D( this isA A, this isA E ) \n" + + " D(this isA A, this isA E) \n" + "then \n" + - " list.add( 3 ); \n" + + " list.add(3); \n" + "end \n"; KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); ArrayList list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); CountingWorkingMemoryEventListener cwm = new CountingWorkingMemoryEventListener(); - ksession.addEventListener( cwm ); + ksession.addEventListener(cwm); ksession.fireAllRules(); @@ -4555,7 +4364,7 @@ public void testDonManyTraitsAtOnce() { assertThat(cwm.getUpdates()).isEqualTo(1); cwm.reset(); - FactHandle handle = ksession.insert( "go" ); + FactHandle handle = ksession.insert("go"); ksession.fireAllRules(); // don C, D, E, F at once : 4 inserts @@ -4565,7 +4374,7 @@ public void testDonManyTraitsAtOnce() { assertThat(cwm.getUpdates()).isEqualTo(3); cwm.reset(); - ksession.delete( handle ); + ksession.delete(handle); ksession.fireAllRules(); // logically asserted C, D, E, F are deleteed @@ -4575,11 +4384,11 @@ public void testDonManyTraitsAtOnce() { assertThat(cwm.getUpdates()).isEqualTo(0); cwm.reset(); - for ( Object o : ksession.getObjects() ) { - LOGGER.debug( o.toString() ); + for (Object o : ksession.getObjects()) { + LOGGER.debug(o.toString()); } - ksession.insert( "undo" ); + ksession.insert("undo"); ksession.fireAllRules(); // deleteing the core bean @@ -4590,7 +4399,7 @@ public void testDonManyTraitsAtOnce() { cwm.reset(); - assertThat(list.size()).isEqualTo(4); + assertThat(list).hasSize(4); assertThat(list.containsAll(Arrays.asList(0, 1, 2, 3))).isTrue(); } @@ -4614,24 +4423,24 @@ public void testDonManyTraitsAtOnce2() { "when \n" + "then\n" + " TBean t = new TBean(); \n" + - " don( t, A.class ); \n" + - " don( t, B.class ); \n" + + " don(t, A.class); \n" + + " don(t, B.class); \n" + "end\n" + "" + "rule \"Test Don A,B\" " + "when \n" + " A(this isA B) \n" + "then \n" + - " list.add( 0 ); \n" + + " list.add(0); \n" + "end \n"; KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); - TraitFactoryImpl.setMode(mode, ksession.getKieBase() ); + TraitFactoryImpl.setMode(mode, ksession.getKieBase()); ArrayList list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); CountingWorkingMemoryEventListener cwm = new CountingWorkingMemoryEventListener(); - ksession.addEventListener( cwm ); + ksession.addEventListener(cwm); ksession.fireAllRules(); @@ -4693,14 +4502,14 @@ public void testMultithreadingTraits() throws InterruptedException { " no-loop true\n" + " when\n" + " $p : Item ()\n" + - " not ItemStyle ( id == $p.id )\n" + + " not ItemStyle (id == $p.id)\n" + " then\n" + " don($p, ItemStyle.class);\n" + "end\n" + "rule \"Item Style - Adjustable\"" + " no-loop true" + " when" + - " $style : ItemStyle ( !adjustable )" + + " $style : ItemStyle (!adjustable)" + " Item (" + " id == $style.id " + " )" + @@ -4710,14 +4519,14 @@ public void testMultithreadingTraits() throws InterruptedException { " };" + "end"; KieBase kbase = getKieBaseFromString(s1); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); // might need to tweak these numbers. often works with 7-10,100,60, but often fails 15-20,100,60 int MAX_THREADS = 20; int MAX_REPETITIONS = 100; int MAX_WAIT_SECONDS = 60; - final ExecutorService executorService = Executors.newFixedThreadPool( MAX_THREADS ); + final ExecutorService executorService = Executors.newFixedThreadPool(MAX_THREADS); try { for (int threadIndex = 0; threadIndex < MAX_THREADS; threadIndex++) { executorService.execute(new TraitRulesThread(threadIndex, MAX_REPETITIONS, kbase.newKieSession())); @@ -4727,7 +4536,7 @@ public void testMultithreadingTraits() throws InterruptedException { executorService.awaitTermination(MAX_WAIT_SECONDS, TimeUnit.SECONDS); final List queuedTasks = executorService.shutdownNow(); - assertThat(queuedTasks.size()).isEqualTo(0); + assertThat(queuedTasks).isEmpty(); assertThat(executorService.isTerminated()).isEqualTo(true); } } @@ -4747,7 +4556,7 @@ public void testShedOneLastTrait() throws InterruptedException { "rule \"Don ItemStyle\"\n" + " when\n" + " then\n" + - " don( new Core(), Mask.class );\n" + + " don(new Core(), Mask.class);\n" + "end\n" + "" + "rule \"React\" \n" + @@ -4755,33 +4564,33 @@ public void testShedOneLastTrait() throws InterruptedException { " $s : String() \n" + " $m : Mask() \n" + "then \n" + - " delete( $s ); \n" + - " shed( $m, Mask.class ); \n" + + " delete($s); \n" + + " shed($m, Mask.class); \n" + "end\n" + "" + "rule Log \n" + "when \n" + " $t : Thing() \n" + "then \n" + - " list.add( $t.getClass().getName() ); \n" + + " list.add($t.getClass().getName()); \n" + "end \n"; KieBase kbase = getKieBaseFromString(s1); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); + knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list).isEqualTo(List.of("test.Mask.test.Core_Proxy")); - knowledgeSession.insert( "shed" ); + knowledgeSession.insert("shed"); knowledgeSession.fireAllRules(); - assertThat(list.size()).isEqualTo(2); + assertThat(list).hasSize(2); assertThat(list).isEqualTo(Arrays.asList("test.Mask.test.Core_Proxy", "org.drools.base.factmodel.traits.Thing.test.Core_Proxy")); } @@ -4800,37 +4609,37 @@ public void testShedThingCompletelyThenDonAgain() throws InterruptedException { "" + "rule \"Don ItemStyle\"\n" + " when\n" + - " $s : String( this == \"don1\" ) \n" + + " $s : String(this == \"don1\") \n" + " then\n" + - " delete( $s ); \n" + - " don( new Core(), Mask.class );\n" + + " delete($s); \n" + + " don(new Core(), Mask.class);\n" + "end\n" + "" + "rule \"Clear\" \n" + " when \n" + - " $s : String( this == \"shed1\" ) \n" + + " $s : String(this == \"shed1\") \n" + " $m : Mask() \n" + "then \n" + - " delete( $s ); \n" + - " shed( $m, Thing.class ); \n" + + " delete($s); \n" + + " shed($m, Thing.class); \n" + "end\n" + "" + "rule \"Add\" \n" + " when \n" + - " $s : String( this == \"don2\" ) \n" + + " $s : String(this == \"don2\") \n" + " $c : Core() \n" + "then \n" + - " delete( $s ); \n" + - " don( $c, Mask2.class ); \n" + + " delete($s); \n" + + " don($c, Mask2.class); \n" + "end\n" + "" + "rule \"Clear Again\" \n" + " when \n" + - " $s : String( this == \"shed2\" ) \n" + + " $s : String(this == \"shed2\") \n" + " $m : Mask2() \n" + "then \n" + - " delete( $s ); \n" + - " shed( $m, Mask2.class ); \n" + + " delete($s); \n" + + " shed($m, Mask2.class); \n" + "end\n" + "" + "" + @@ -4838,39 +4647,39 @@ public void testShedThingCompletelyThenDonAgain() throws InterruptedException { "when \n" + " $t : Thing() \n" + "then \n" + - " list.add( $t.getClass().getName() ); \n" + + " list.add($t.getClass().getName()); \n" + "end \n"; KieBase kbase = getKieBaseFromString(s1); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); + knowledgeSession.setGlobal("list", list); - knowledgeSession.insert( "don1" ); + knowledgeSession.insert("don1"); knowledgeSession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list).isEqualTo(List.of("test.Mask.test.Core_Proxy")); - knowledgeSession.insert( "shed1" ); + knowledgeSession.insert("shed1"); knowledgeSession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list).isEqualTo(List.of("test.Mask.test.Core_Proxy")); - knowledgeSession.insert( "don2" ); + knowledgeSession.insert("don2"); knowledgeSession.fireAllRules(); - LOGGER.debug( list.toString() ); - assertThat(list.size()).isEqualTo(2); + LOGGER.debug(list.toString()); + assertThat(list).hasSize(2); assertThat(list).isEqualTo(Arrays.asList("test.Mask.test.Core_Proxy", "test.Mask2.test.Core_Proxy")); - knowledgeSession.insert( "shed2" ); + knowledgeSession.insert("shed2"); knowledgeSession.fireAllRules(); - assertThat(list.size()).isEqualTo(3); + assertThat(list).hasSize(3); assertThat(list).isEqualTo(Arrays.asList("test.Mask.test.Core_Proxy", "test.Mask2.test.Core_Proxy", "org.drools.base.factmodel.traits.Thing.test.Core_Proxy")); } @@ -4889,22 +4698,22 @@ public void testTraitImplicitInsertionExceptionOnNonTraitable() throws Interrupt "rule \"Don ItemStyle\"\n" + " when\n" + " then\n" + - " don( new Core(), Mask.class );\n" + + " don(new Core(), Mask.class);\n" + "end\n" + "" + ""; KieBase kbase = getKieBaseFromString(s1); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); + knowledgeSession.setGlobal("list", list); try { knowledgeSession.fireAllRules(); - fail( "Core is not declared @Traitable, this test should have thrown an exception" ); - } catch ( Exception csq ) { + fail("Core is not declared @Traitable, this test should have thrown an exception"); + } catch (Exception csq) { assertThat(csq.getCause() instanceof IllegalStateException).isTrue(); } @@ -4914,7 +4723,7 @@ public void testTraitImplicitInsertionExceptionOnNonTraitable() throws Interrupt @Trait public static interface SomeTrait extends Thing { public String getFoo(); - public void setFoo( String foo ); + public void setFoo(String foo); } @Test @@ -4929,19 +4738,19 @@ public void testTraitLegacyTraitableWithLegacyTrait() { "rule \"Don ItemStyle\"\n" + " when\n" + " then\n" + - " don( new StudentImpl(), SomeTrait.class );\n" + + " don(new StudentImpl(), SomeTrait.class);\n" + "end\n"; KieBase kbase = getKieBaseFromString(s1); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); + knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(knowledgeSession.getObjects().size()).isEqualTo(2); + assertThat(knowledgeSession.getObjects()).hasSize(2); } @Test @@ -4958,24 +4767,24 @@ public void testIsALegacyTrait() { "rule \"Don ItemStyle\"\n" + " when\n" + " then\n" + - " insert( new StudentImpl() );\n" + - " don( new Entity(), IStudent.class );\n" + + " insert(new StudentImpl());\n" + + " don(new Entity(), IStudent.class);\n" + "end\n" + "" + "rule Check " + " when " + " $s : StudentImpl() " + - " $e : Entity( this isA $s ) " + + " $e : Entity(this isA $s) " + " then " + - " list.add( 1 ); " + + " list.add(1); " + " end "; KieBase kbase = getKieBaseFromString(s1); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); + knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); @@ -5001,39 +4810,39 @@ public void testClassLiteralsWithOr() { "when " + "then " + " Foo f = new Foo(); " + - " insert( f ); " + + " insert(f); " + "end " + "rule One " + "when " + - " $f : Foo( this not isA A ) " + + " $f : Foo(this not isA A) " + "then " + - " don( $f, A.class ); " + + " don($f, A.class); " + "end " + "rule Two " + "when " + - " $f : Foo( this not isA B ) " + + " $f : Foo(this not isA B) " + "then " + - " don( $f, B.class ); " + + " don($f, B.class); " + "end " + "rule Check " + "when " + - " $f : Foo( this isA B || this isA A ) " + + " $f : Foo(this isA B || this isA A) " + "then " + - " list.add( 1 ); " + + " list.add(1); " + "end " + ""; - KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent( drl, ResourceType.DRL ).build(); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build(); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession ksession = kbase.newKieSession(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); ksession.fireAllRules(); @@ -5067,39 +4876,39 @@ public void testIsASwappedArg() { "rule Init " + "when " + "then " + - " Foo f = new Foo( new StudentImpl() ); " + - " don( f, IStudent.class ); " + + " Foo f = new Foo(new StudentImpl()); " + + " don(f, IStudent.class); " + "end " + "rule Match1 " + "when " + - " $f : Foo( $x : object ) " + - " $p : StudentImpl( this isA $f ) from $x " + + " $f : Foo($x : object) " + + " $p : StudentImpl(this isA $f) from $x " + "then " + - " list.add( 1 ); " + + " list.add(1); " + "end " + "rule Match2 " + "when " + - " $f : Foo( $x : object ) " + - " $p : StudentImpl( $f isA this ) from $x " + + " $f : Foo($x : object) " + + " $p : StudentImpl($f isA this) from $x " + "then " + - " list.add( 2 ); " + + " list.add(2); " + "end " + ""; - KieBase kbase = loadKnowledgeBaseFromString( drl ); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = loadKnowledgeBaseFromString(drl); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession ksession = kbase.newKieSession(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(2); + assertThat(list).hasSize(2); assertThat(list.contains(1)).isTrue(); assertThat(list.contains(2)).isTrue(); @@ -5126,23 +4935,23 @@ public void testHierarchyEncodeOnPackageMerge() { TraitFactoryImpl.setMode(mode, (InternalRuleBase) knowledgeBase); KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kb.add( new ByteArrayResource( drl0.getBytes() ), ResourceType.DRL ); + kb.add(new ByteArrayResource(drl0.getBytes()), ResourceType.DRL); assertThat(kb.hasErrors()).isFalse(); - knowledgeBase.addPackages( kb.getKnowledgePackages() ); + knowledgeBase.addPackages(kb.getKnowledgePackages()); KnowledgeBuilder kb2 = KnowledgeBuilderFactory.newKnowledgeBuilder(); - kb2.add( new ByteArrayResource( drl1.getBytes() ), ResourceType.DRL ); - LOGGER.debug( kb2.getErrors().toString() ); + kb2.add(new ByteArrayResource(drl1.getBytes()), ResourceType.DRL); + LOGGER.debug(kb2.getErrors().toString()); assertThat(kb2.hasErrors()).isFalse(); - knowledgeBase.addPackages( kb2.getKnowledgePackages() ); + knowledgeBase.addPackages(kb2.getKnowledgePackages()); HierarchyEncoder hier = ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(knowledgeBase).getHierarchy(); - BitSet b = (BitSet) hier.getCode( "org.drools.test.B" ).clone(); - BitSet c = (BitSet) hier.getCode( "org.drools.test.C" ).clone(); + BitSet b = (BitSet) hier.getCode("org.drools.test.B").clone(); + BitSet c = (BitSet) hier.getCode("org.drools.test.C").clone(); - c.and( b ); + c.and(b); assertThat(c).isEqualTo(b); } @@ -5168,9 +4977,9 @@ public void testDonThenReinsert() throws InterruptedException { "" + "rule 'Don ItemStyle' " + " when\n" + - " $e : TBean( ) " + + " $e : TBean() " + " then " + - " don( $e, Mask.class );\n" + + " don($e, Mask.class);\n" + "end\n" + "" + "rule \"React\" \n" + @@ -5183,29 +4992,29 @@ public void testDonThenReinsert() throws InterruptedException { KieBase kbase = getKieBaseFromString(s1, EqualityBehaviorOption.IDENTITY); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); - TBean e = new TBean( "aaa" ); + knowledgeSession.setGlobal("list", list); + TBean e = new TBean("aaa"); int n = knowledgeSession.fireAllRules(); assertThat(n).isEqualTo(1); - knowledgeSession.insert( e ); + knowledgeSession.insert(e); n = knowledgeSession.fireAllRules(); assertThat(n).isEqualTo(2); - knowledgeSession.insert( e ); + knowledgeSession.insert(e); n = knowledgeSession.fireAllRules(); assertThat(n).isEqualTo(0); - knowledgeSession.delete( knowledgeSession.getFactHandle( e ) ); + knowledgeSession.delete(knowledgeSession.getFactHandle(e)); n = knowledgeSession.fireAllRules(); assertThat(n).isEqualTo(1); - assertThat(knowledgeSession.getObjects().size()).isEqualTo(0); + assertThat(knowledgeSession.getObjects()).isEmpty(); } @@ -5236,26 +5045,26 @@ public void testCastOnTheFly() throws InterruptedException { " dialect 'mvel' " + " when " + " then " + - " Foo o = insert( new Foo( 42 ) ).as( Foo.class ); " + - " list.add( o.getId() ); " + + " Foo o = insert(new Foo(42)).as(Foo.class); " + + " list.add(o.getId()); " + "end " + "rule Don " + " when " + " $f : Foo() " + " then " + - " Lower l = don( $f, Lower.class ); " + - " Upper u = bolster( $f ).as( Upper.class ); " + - " list.add( u.getId() + 1 ); " + + " Lower l = don($f, Lower.class); " + + " Upper u = bolster($f).as(Upper.class); " + + " list.add(u.getId() + 1); " + " end "; KieBase kbase = getKieBaseFromString(s1); - TraitFactoryImpl.setMode(mode, kbase ); + TraitFactoryImpl.setMode(mode, kbase); ArrayList list = new ArrayList(); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); + knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); @@ -5283,24 +5092,24 @@ public void testDonModify() { "when\n" + "then\n" + " Entity core = new Entity();\n" + - " insert( core );\n" + + " insert(core);\n" + "end\n" + "rule Trait when\n" + - " $core: Entity( )\n" + + " $core: Entity()\n" + "then\n" + - " IPerson x = don( $core, IPerson.class, true );\n" + - " IStudent s = don( $core, IStudent.class, true );\n" + - " Person p = don( $core, Person.class, true );\n" + + " IPerson x = don($core, IPerson.class, true);\n" + + " IStudent s = don($core, IStudent.class, true);\n" + + " Person p = don($core, Person.class, true);\n" + "end\n" + "rule R2 when\n" + - " $p: IPerson( name == null )\n" + + " $p: IPerson(name == null)\n" + "then\n" + "end\n"; - KieBase kbase = getKieBaseFromString( drl ); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = getKieBaseFromString(drl); + TraitFactoryImpl.setMode(mode, kbase); KieSession kSession = kbase.newKieSession(); assertThat(kSession.fireAllRules()).isEqualTo(3); @@ -5319,28 +5128,28 @@ public void testAlphaNodeSharing() { "rule Init " + "when " + "then " + - " don( new Entity(), Person.class ); " + + " don(new Entity(), Person.class); " + "end\n" + "rule One when" + - " $core: Entity( this isA Person ) " + + " $core: Entity(this isA Person) " + "then " + "end " + "rule Two when" + - " $core: Entity( this isA Person ) " + + " $core: Entity(this isA Person) " + "then " + "end " + "\n"; - final KieBase kbase = getKieBaseFromString( drl ); - TraitFactoryImpl.setMode(mode, kbase ); + final KieBase kbase = getKieBaseFromString(drl); + TraitFactoryImpl.setMode(mode, kbase); KieSession kSession = kbase.newKieSession(); assertThat(kSession.fireAllRules()).isEqualTo(3); - NamedEntryPoint nep = ( (NamedEntryPoint) kSession.getEntryPoint( EntryPointId.DEFAULT.getEntryPointId() ) ); - ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get( new ClassObjectType( Entity.class ) ); + NamedEntryPoint nep = ((NamedEntryPoint) kSession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId())); + ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get(new ClassObjectType(Entity.class)); assertThat(otn).isNotNull(); assertThat(otn.getObjectSinkPropagator().getSinks().length).isEqualTo(1); } @@ -5356,43 +5165,43 @@ public void testPartitionWithSiblingsOnDelete() { "declare trait C extends A @propertyReactive end " + "rule Trait when " + - " $core: Entity( ) " + + " $core: Entity() " + "then " + - " don( $core, A.class ); " + - " don( $core, B.class ); " + - " don( $core, C.class ); " + + " don($core, A.class); " + + " don($core, B.class); " + + " don($core, C.class); " + "end " + "rule Shed when " + " $s: String() " + " $core : Entity() " + "then " + - " shed( $core, C.class ); " + + " shed($core, C.class); " + "end " + - "rule RA when A() then list.add( 'A' ); end " + - "rule RB when B() then list.add( 'B' ); end " + - "rule RC when C() then list.add( 'C' ); end " + + "rule RA when A() then list.add('A'); end " + + "rule RB when B() then list.add('B'); end " + + "rule RC when C() then list.add('C'); end " + " "; - KieBase kbase = new KieHelper().addContent( drl, ResourceType.DRL ).build(); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build(); + TraitFactoryImpl.setMode(mode, kbase); KieSession ksession = kbase.newKieSession(); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); Entity e = new Entity(); - ksession.insert( e ); + ksession.insert(e); ksession.fireAllRules(); assertThat(list).isEqualTo(Arrays.asList('A', 'B', 'C')); - ksession.insert( "go" ); + ksession.insert("go"); ksession.fireAllRules(); - Set s = checkOTNPartitioning( e, ksession ); - assertThat(s.size()).isEqualTo(2); + Set s = checkOTNPartitioning(e, ksession); + assertThat(s).hasSize(2); assertThat(list).isEqualTo(Arrays.asList('A', 'B', 'C')); } @@ -5407,40 +5216,40 @@ public void testTupleIntegrityOnModification() { "declare trait A @propertyReactive value : int end " + "rule Trait when " + - " $core: Entity( ) " + + " $core: Entity() " + "then " + - " A o = don( $core, A.class ); " + + " A o = don($core, A.class); " + "end " + "rule Test when " + - " $x: A( value == 0 ) " + + " $x: A(value == 0) " + "then " + - " list.add( 0 ); " + + " list.add(0); " + "end " + "rule Check when " + - " $x: A( value == 42 ) " + + " $x: A(value == 42) " + "then " + - " list.add( 42 ); " + + " list.add(42); " + "end " + "rule Mood when " + - " $x : A( value != 42 ) " + + " $x : A(value != 42) " + "then " + - " modify ( $x ) { setValue( 42 ); } " + + " modify ($x) { setValue(42); } " + "end "; - KieBase kbase = getKieBaseFromString( drl ); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = getKieBaseFromString(drl); + TraitFactoryImpl.setMode(mode, kbase); KieSession ksession = kbase.newKieSession(); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); - ksession.insert( new Entity() ); + ksession.insert(new Entity()); ksession.fireAllRules(); - for ( final Object o : ksession.getObjects(object -> object.getClass().getName().contains( "test.A" )) ) { - InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle( o ); + for (final Object o : ksession.getObjects(object -> object.getClass().getName().contains("test.A"))) { + InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle(o); TupleImpl first = handle.getFirstLeftTuple(); assertThat(first instanceof RuleTerminalNodeLeftTuple).isTrue(); assertThat(((RuleTerminalNodeLeftTuple) first).getRule().getName()).isEqualTo("Check"); @@ -5462,9 +5271,9 @@ public void testShedVacancy() { "rule Trait when " + "then " + - " Entity e = new Entity( 'x1' ); " + - " don( e, C.class ); " + - " don( e, D.class ); " + + " Entity e = new Entity('x1'); " + + " don(e, C.class); " + + " don(e, D.class); " + "end " + "rule Mood when " + @@ -5476,63 +5285,63 @@ public void testShedVacancy() { " $s : String() " + " $x : Entity() " + "then " + - " delete( $s ); " + - " shed( $x, A.class ); " + + " delete($s); " + + " shed($x, A.class); " + "end " + ""; - KieBase kbase = getKieBaseFromString( drl ); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = getKieBaseFromString(drl); + TraitFactoryImpl.setMode(mode, kbase); KieSession ksession = kbase.newKieSession(); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); HierarchyEncoder hier = ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(((InternalKnowledgeBase) kbase)).getHierarchy(); - BitSet a = (BitSet) hier.getCode( "org.drools.test.A" ).clone(); - BitSet b = (BitSet) hier.getCode( "org.drools.test.B" ).clone(); - BitSet c = (BitSet) hier.getCode( "org.drools.test.C" ).clone(); - BitSet d = (BitSet) hier.getCode( "org.drools.test.D" ).clone(); + BitSet a = (BitSet) hier.getCode("org.drools.test.A").clone(); + BitSet b = (BitSet) hier.getCode("org.drools.test.B").clone(); + BitSet c = (BitSet) hier.getCode("org.drools.test.C").clone(); + BitSet d = (BitSet) hier.getCode("org.drools.test.D").clone(); int n = ksession.fireAllRules(); assertThat(n).isEqualTo(2); - LOGGER.debug( "---------------------------------------------------------------\n\n\n " ); + LOGGER.debug("---------------------------------------------------------------\n\n\n "); int counter = 0; - for ( Object o : ksession.getObjects() ) { - if ( o instanceof TraitProxyImpl) { + for (Object o : ksession.getObjects()) { + if (o instanceof TraitProxyImpl) { TraitProxyImpl tp = (TraitProxyImpl) o; - if ( tp._getTypeCode().equals( c ) ) { - assertThat(tp.listAssignedOtnTypeCodes().size()).isEqualTo(1); + if (tp._getTypeCode().equals(c)) { + assertThat(tp.listAssignedOtnTypeCodes()).hasSize(1); assertThat(tp.listAssignedOtnTypeCodes().contains(b)).isTrue(); counter++; - } else if ( tp._getTypeCode().equals( d ) ) { - assertThat(tp.listAssignedOtnTypeCodes().isEmpty()).isTrue(); + } else if (tp._getTypeCode().equals(d)) { + assertThat(tp.listAssignedOtnTypeCodes()).isEmpty(); counter++; } - } else if ( o instanceof TraitableBean ) { + } else if (o instanceof TraitableBean) { TraitableBean tb = (TraitableBean) o; - LOGGER.debug( tb.getCurrentTypeCode().toString() ); + LOGGER.debug(tb.getCurrentTypeCode().toString()); counter++; } } assertThat(counter).isEqualTo(3); - ksession.insert( "go" ); + ksession.insert("go"); ksession.fireAllRules(); - LOGGER.debug( "---------------------------------------------------------------\n\n\n " ); + LOGGER.debug("---------------------------------------------------------------\n\n\n "); int counter2 = 0; - for ( Object o : ksession.getObjects() ) { - if ( o instanceof TraitProxyImpl) { + for (Object o : ksession.getObjects()) { + if (o instanceof TraitProxyImpl) { TraitProxyImpl tp = (TraitProxyImpl) o; assertThat(tp._getTypeCode()).isEqualTo(d); - assertThat(tp.listAssignedOtnTypeCodes().size()).isEqualTo(1); + assertThat(tp.listAssignedOtnTypeCodes()).hasSize(1); assertThat(tp.listAssignedOtnTypeCodes().contains(b)).isTrue(); counter2++; - } else if ( o instanceof TraitableBean ) { + } else if (o instanceof TraitableBean) { TraitableBean tb = (TraitableBean) o; assertThat(tb.getCurrentTypeCode()).isEqualTo(d); counter2++; @@ -5554,29 +5363,29 @@ public void testExternalUpdateWithProxyRefreshInEqualityMode() { "end " + "rule Don when " + - " $x : ExtEntity( $id : id ) " + + " $x : ExtEntity($id : id) " + "then " + - " list.add( $id ); " + - " don( $x, Mask.class ); " + + " list.add($id); " + + " don($x, Mask.class); " + "end " + "rule Test when " + - " Mask( $n : num ) " + + " Mask($n : num) " + "then " + - " list.add( $n ); " + + " list.add($n); " + "end "; - KieBase kbase = getKieBaseFromString( drl, EqualityBehaviorOption.EQUALITY ); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = getKieBaseFromString(drl, EqualityBehaviorOption.EQUALITY); + TraitFactoryImpl.setMode(mode, kbase); KieSession ksession = kbase.newKieSession(); List list = new ArrayList(); - ksession.setGlobal( "list", list ); + ksession.setGlobal("list", list); - FactHandle handle = ksession.insert( new ExtEntity( "x1", 42 ) ); + FactHandle handle = ksession.insert(new ExtEntity("x1", 42)); ksession.fireAllRules(); - ksession.update( handle, new ExtEntity( "x1", 35 ) ); + ksession.update(handle, new ExtEntity("x1", 35)); ksession.fireAllRules(); assertThat(list).isEqualTo(Arrays.asList("x1", 42, "x1", 42)); @@ -5593,23 +5402,23 @@ public void testIsAInstanceOf() { "rule Test1 " + "when " + - " StudentImpl( this isA IStudent.class ) " + - "then list.add( 1 ); end " + + " StudentImpl(this isA IStudent.class) " + + "then list.add(1); end " + "rule Test2 " + "when " + - " IStudent( this isA StudentImpl.class ) " + - "then list.add( 2 ); end " + + " IStudent(this isA StudentImpl.class) " + + "then list.add(2); end " + ""; - KieBase kbase = getKieBaseFromString( drl ); - List list = new ArrayList( ); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = getKieBaseFromString(drl); + List list = new ArrayList(); + TraitFactoryImpl.setMode(mode, kbase); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); - knowledgeSession.insert( new StudentImpl( ) ); + knowledgeSession.setGlobal("list", list); + knowledgeSession.insert(new StudentImpl()); assertThat(knowledgeSession.fireAllRules()).isEqualTo(2); assertThat(list).isEqualTo(Arrays.asList(1, 2)); @@ -5624,18 +5433,18 @@ public void testIsAInstanceOfNonTraitable() { "rule Test1 " + "when " + - " Object( this isA String.class ) " + - "then list.add( 1 ); end " + + " Object(this isA String.class) " + + "then list.add(1); end " + ""; - KieBase kbase = getKieBaseFromString( drl ); - List list = new ArrayList( ); - TraitFactoryImpl.setMode(mode, kbase ); + KieBase kbase = getKieBaseFromString(drl); + List list = new ArrayList(); + TraitFactoryImpl.setMode(mode, kbase); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); - knowledgeSession.insert( "hello" ); + knowledgeSession.setGlobal("list", list); + knowledgeSession.insert("hello"); assertThat(knowledgeSession.fireAllRules()).isEqualTo(1); assertThat(list).isEqualTo(List.of(1)); @@ -5652,26 +5461,26 @@ public int getNum() { return num; } - public void setNum( int num ) { + public void setNum(int num) { this.num = num; } - public ExtEntity( String id, int num ) { - super( id ); + public ExtEntity(String id, int num) { + super(id); this.num = num; } } - protected Set checkOTNPartitioning( TraitableBean core, KieSession wm ) { + protected Set checkOTNPartitioning(TraitableBean core, KieSession wm) { Set otns = new HashSet(); - for ( Object o : core._getTraitMap().values() ) { + for (Object o : core._getTraitMap().values()) { TraitProxyImpl tp = (TraitProxyImpl) o; Set localNodes = tp.listAssignedOtnTypeCodes(); - for ( BitSet code : localNodes ) { + for (BitSet code : localNodes) { assertThat(otns.contains(code)).isFalse(); - otns.add( code ); + otns.add(code); } } @@ -5688,24 +5497,24 @@ public void testSerializeKieBaseWithTraits() { "rule Test1 " + "when " + - " StudentImpl( this isA IStudent.class ) " + - "then list.add( 1 ); end " + + " StudentImpl(this isA IStudent.class) " + + "then list.add(1); end " + "rule Test2 " + "when " + - " IStudent( this isA StudentImpl.class ) " + - "then list.add( 2 ); end " + + " IStudent(this isA StudentImpl.class) " + + "then list.add(2); end " + ""; - KieBase kbase = getKieBaseFromString( drl ); + KieBase kbase = getKieBaseFromString(drl); - List list = new ArrayList( ); - TraitFactoryImpl.setMode(mode, kbase ); + List list = new ArrayList(); + TraitFactoryImpl.setMode(mode, kbase); KieSession knowledgeSession = kbase.newKieSession(); - knowledgeSession.setGlobal( "list", list ); - knowledgeSession.insert( new StudentImpl( ) ); + knowledgeSession.setGlobal("list", list); + knowledgeSession.insert(new StudentImpl()); assertThat(knowledgeSession.fireAllRules()).isEqualTo(2); assertThat(list).isEqualTo(Arrays.asList(1, 2)); @@ -5739,27 +5548,27 @@ public void testMixin2() { "rule \"Zero\"\n" + "when\n" + "then\n" + - " insert( new Person() );\n" + + " insert(new Person());\n" + "end\n" + "\n" + "\n" + "rule \"Student\"\n" + "no-loop\n" + "when\n" + - " $p : Person( $name : name, $age : age < 25, $weight : weight )\n" + + " $p : Person($name : name, $age : age < 25, $weight : weight)\n" + "then\n" + - " Student s = don( $p, Student.class );\n" + - " s.setSchool( \"SomeSchool\" );\n" + - " s.learn( \" AI \" );\n" + + " Student s = don($p, Student.class);\n" + + " s.setSchool(\"SomeSchool\");\n" + + " s.learn(\" AI \");\n" + "end\n"; - KieSession ks = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); ks.fireAllRules(); } - @Trait( impl = ScholarImpl.class ) + @Trait(impl = ScholarImpl.class) public interface Scholar { void learn(String subject); } @@ -5772,16 +5581,16 @@ public static class ScholarImpl implements Scholar { public ScholarImpl() { } - public ScholarImpl( Thing arg ) { + public ScholarImpl(Thing arg) { this.core = arg; } - public void learn( String subject ) { - LOGGER.debug( "I " + core.getFields().get( "name" ) + ", now know everything about " + subject ); + public void learn(String subject) { + LOGGER.debug("I " + core.getFields().get("name") + ", now know everything about " + subject); } } - @Trait( impl = YImpl.class ) + @Trait(impl = YImpl.class) public interface Y { String getShared(); String getYValue(); @@ -5800,7 +5609,7 @@ public String getYValue() { } } - @Trait( impl = ZImpl.class ) + @Trait(impl = ZImpl.class) public interface Z { String getShared(); String getZValue(); @@ -5839,27 +5648,27 @@ private void checkMixinResolutionUsesOrder(String interfaces, String first) { "end\n" + "\n" + "\n" + - "declare X extends " + interfaces + " @Trait( mixinSolveConflicts = Trait.MixinConflictResolutionStrategy.DECLARATION_ORDER ) end\n" + + "declare X extends " + interfaces + " @Trait(mixinSolveConflicts = Trait.MixinConflictResolutionStrategy.DECLARATION_ORDER) end\n" + "\n" + "rule Init when\n" + "then\n" + - " insert( new Bean() );\n" + + " insert(new Bean());\n" + "end\n" + "\n" + "rule Exec no-loop when\n" + " $b : Bean()\n" + "then\n" + - " X x = don( $b, X.class );\n" + - " list.add( x.getYValue() );\n" + - " list.add( x.getZValue() );\n" + - " list.add( x.getShared() );\n" + + " X x = don($b, X.class);\n" + + " list.add(x.getYValue());\n" + + " list.add(x.getZValue());\n" + + " list.add(x.getShared());\n" + "end\n"; - KieSession ks = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); ks.fireAllRules(); @@ -5884,27 +5693,27 @@ public void testMixinWithConflictsThrowingError() { "end\n" + "\n" + "\n" + - "declare X extends Y,Z @Trait( mixinSolveConflicts = Trait.MixinConflictResolutionStrategy.ERROR_ON_CONFLICT ) end\n" + + "declare X extends Y,Z @Trait(mixinSolveConflicts = Trait.MixinConflictResolutionStrategy.ERROR_ON_CONFLICT) end\n" + "\n" + "rule Init when\n" + "then\n" + - " insert( new Bean() );\n" + + " insert(new Bean());\n" + "end\n" + "\n" + "rule Exec no-loop when\n" + " $b : Bean()\n" + "then\n" + - " X x = don( $b, X.class );\n" + - " list.add( x.getYValue() );\n" + - " list.add( x.getZValue() );\n" + - " list.add( x.getShared() );\n" + + " X x = don($b, X.class);\n" + + " list.add(x.getYValue());\n" + + " list.add(x.getZValue());\n" + + " list.add(x.getShared());\n" + "end\n"; - KieSession ks = getSessionFromString( drl ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(drl); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); List list = new ArrayList(); - ks.setGlobal( "list", list ); + ks.setGlobal("list", list); try { ks.fireAllRules(); @@ -5934,58 +5743,58 @@ public void testPreserveAllSetBitMask() { "" + "rule Init when\n" + "then " + - " Entity e1 = new Entity( \"X\" ); " + - " insert( e1 ); " + - " Entity e2 = new Entity( \"Y\" ); " + - " insert( e2 ); " + + " Entity e1 = new Entity(\"X\"); " + + " insert(e1); " + + " Entity e2 = new Entity(\"Y\"); " + + " insert(e2); " + " " + - " D d1 = don( e1, D.class, true ); " + - " F f2 = don( e2, F.class, true ); " + + " D d1 = don(e1, D.class, true); " + + " F f2 = don(e2, F.class, true); " + " " + - " modify ( d1 ) { getObjProp().add( f2.getCore() ); } " + - " modify ( f2.getCore() ) {} " + + " modify (d1) { getObjProp().add(f2.getCore()); } " + + " modify (f2.getCore()) {} " + "end " + "" + "rule Rec no-loop when\n" + - " MyThing( $x_0 := core, this isA D.class, $p : this#RootThing.objProp ) " + - " exists MyThing( $x_1 := core , core memberOf $p, this isA F.class ) " + + " MyThing($x_0 := core, this isA D.class, $p : this#RootThing.objProp) " + + " exists MyThing($x_1 := core , core memberOf $p, this isA F.class) " + "then " + - " don( $x_0, E.class, true ); " + + " don($x_0, E.class, true); " + "end " + "" + "rule Shed_2 when\n" + - " $s : String( this == \"go2\") " + - " $x : E( $objs : objProp ) " + - " $y : F( $z : core memberOf $objs ) " + + " $s : String(this == \"go2\") " + + " $x : E($objs : objProp) " + + " $y : F($z : core memberOf $objs) " + "then " + - " retract( $s ); " + - " modify ( $x ) { getObjProp().remove( $z ); } " + - " modify ( $y ) {} " + + " retract($s); " + + " modify ($x) { getObjProp().remove($z); } " + + " modify ($y) {} " + "end "; KieHelper helper = new KieHelper(); - KieBase kieBase = helper.addContent( drl, ResourceType.DRL ).getKieContainer().getKieBase(); - TraitFactoryImpl.setMode(mode, kieBase ); + KieBase kieBase = helper.addContent(drl, ResourceType.DRL).getKieContainer().getKieBase(); + TraitFactoryImpl.setMode(mode, kieBase); KieSession kSession = kieBase.newKieSession(); kSession.fireAllRules(); - kSession.insert( "go2" ); + kSession.insert("go2"); kSession.fireAllRules(); - for ( Object o : kSession.getObjects( new ClassObjectFilter( Entity.class ) ) ) { + for (Object o : kSession.getObjects(new ClassObjectFilter(Entity.class))) { Entity e = (Entity) o; - if ( e.getId().equals( "X" ) ) { + if (e.getId().equals("X")) { assertThat(e.hasTrait("t.x.D")).isTrue(); assertThat(e.hasTrait("t.x.E")).isFalse(); assertThat(e.hasTrait("t.x.F")).isFalse(); - assertThat(((List) e._getDynamicProperties().get("objProp")).size()).isEqualTo(0); - } else if ( e.getId().equals( "Y" ) ) { + assertThat(((List) e._getDynamicProperties().get("objProp"))).isEmpty(); + } else if (e.getId().equals("Y")) { assertThat(e.hasTrait("t.x.F")).isTrue(); assertThat(e.hasTrait("t.x.D")).isFalse(); assertThat(e.hasTrait("t.x.E")).isFalse(); } else { - fail( "Unrecognized entity in WM" ); + fail("Unrecognized entity in WM"); } } } diff --git a/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java b/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java index d460cfd45d5..1c1b9c3fc16 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java @@ -41,9 +41,9 @@ public class HierarchyTest { @Test public void testHierEncoderTrivial() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); encoder.encode("B", List.of("A")); encoder.encode("C", List.of("B")); encoder.encode( "D", Arrays.asList( "B", "C" ) ); @@ -58,13 +58,13 @@ public void testHierEncoderTrivial() { @Test public void testHierManyRoots() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "A", Collections.EMPTY_LIST ); - encoder.encode( "B", Collections.EMPTY_LIST ); - encoder.encode( "C", Collections.EMPTY_LIST ); - encoder.encode( "D", Collections.EMPTY_LIST ); - encoder.encode( "E", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); + encoder.encode( "B", Collections.emptyList()); + encoder.encode( "C", Collections.emptyList()); + encoder.encode( "D", Collections.emptyList()); + encoder.encode( "E", Collections.emptyList()); LOGGER.debug( encoder.toString() ); @@ -83,13 +83,13 @@ public void testHierManyRoots() { @Test public void testHierManyRootsPropagation() { - HierarchyEncoderImpl encoder = new HierarchyEncoderImpl(); + HierarchyEncoderImpl encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); encoder.encode("B", List.of("A")); encoder.encode("C", List.of("A")); encoder.encode( "D", Arrays.asList( "B", "C" ) ); - encoder.encode( "E", Collections.EMPTY_LIST ); + encoder.encode( "E", Collections.emptyList()); LOGGER.debug( encoder.toString() ); @@ -116,10 +116,10 @@ public void testHierManyRootsPropagation() { @Test public void testHierALotOfClasses() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); int N = 1194; - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); for ( int j = 1; j < N; j++ ) { encoder.encode( "X" + j, List.of("A")); } @@ -134,9 +134,9 @@ public void testHierALotOfClasses() { @Test public void testHierEncoderSimpleInheritance() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); encoder.encode("B", List.of("A")); encoder.encode("C", List.of("A")); encoder.encode("D", List.of("B")); @@ -160,9 +160,9 @@ public void testHierEncoderSimpleInheritance() { @Test public void testHierEncoderAnotherSimpleInheritance() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "R", Collections.EMPTY_LIST ); + encoder.encode( "R", Collections.emptyList()); encoder.encode("A1", List.of("R")); encoder.encode("A2", List.of("R")); encoder.encode("A3", List.of("R")); @@ -193,9 +193,9 @@ public void testHierEncoderAnotherSimpleInheritance() { @Test public void testHierEncoderAnotherSimpleInheritanceChangeOrder() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "R", Collections.EMPTY_LIST ); + encoder.encode( "R", Collections.emptyList()); encoder.encode("B1", List.of("R")); encoder.encode("B2", List.of("R")); @@ -233,9 +233,9 @@ public void testHierEncoderAnotherSimpleInheritanceChangeOrder() { @Test public void testHierEncoderBipartiteInheritance() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "R", Collections.EMPTY_LIST ); + encoder.encode( "R", Collections.emptyList()); encoder.encode("A1", List.of("R")); encoder.encode("A2", List.of("R")); encoder.encode("A3", List.of("R")); @@ -263,9 +263,9 @@ public void testHierEncoderBipartiteInheritance() { @Test public void testHierEncoderBipartiteInheritanceDiffOrder() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "R", Collections.EMPTY_LIST ); + encoder.encode( "R", Collections.emptyList()); encoder.encode("B1", List.of("R")); encoder.encode("B2", List.of("R")); @@ -296,9 +296,9 @@ public void testHierEncoderBipartiteInheritanceDiffOrder() { @Test public void testSquare() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "T", Collections.EMPTY_LIST ); + encoder.encode( "T", Collections.emptyList()); encoder.encode("A", List.of("T")); encoder.encode("B", List.of("T")); encoder.encode( "C", Arrays.asList( "A", "B" ) ); @@ -319,9 +319,9 @@ public void testSquare() { @Test public void testConflictArising() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); encoder.encode("B", List.of("A")); encoder.encode("C", List.of("A")); encoder.encode("D", List.of("B")); @@ -362,9 +362,9 @@ public void testConflictArising() { @Test public void testConflictArising2() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); encoder.encode("B", List.of("A")); encoder.encode("C", List.of("A")); encoder.encode("D", List.of("B")); @@ -391,9 +391,9 @@ public void testConflictArising2() { @Test public void testHierEncoderBipartiteStarInheritance() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "R", Collections.EMPTY_LIST ); + encoder.encode( "R", Collections.emptyList()); encoder.encode("B1", List.of("R")); encoder.encode("B2", List.of("R")); @@ -434,9 +434,9 @@ public void testHierEncoderBipartiteStarInheritance() { @Test public void testHierEncoderBipartiteStarInheritanceDiffOrder() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "R", Collections.EMPTY_LIST ); + encoder.encode( "R", Collections.emptyList()); encoder.encode("A1", List.of("R")); encoder.encode("A2", List.of("R")); @@ -491,9 +491,9 @@ private BitSet parseBitSet( String s ) { @Test public void testHierEncoderComplexInheritance() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); checkHier( encoder, 'A' ); encoder.encode("B", List.of("A")); @@ -562,12 +562,12 @@ private Long toLong( BitSet ns ) { return l; } - private void checkHier( HierarchyEncoder encoder, char fin ) { + private void checkHier( HierarchyEncoder encoder, char fin ) { List[] sups = new ArrayList[ fin - 'A' + 1 ]; for ( int j = 'A'; j <= fin; j++ ) { - sups[ j - 'A' ] = ((HierarchyEncoderImpl) encoder).ancestorValues( "" + (char) j ); + sups[ j - 'A' ] = ((HierarchyEncoderImpl) encoder).ancestorValues( "" + (char) j ); }; for ( int j = 'A' ; j < 'A' + sups.length ; j++ ) { @@ -577,7 +577,7 @@ private void checkHier( HierarchyEncoder encoder, char fin ) { BitSet xcode = encoder.getCode( x ); BitSet ycode = encoder.getCode( y ); - int subOf = ((HierarchyEncoderImpl) encoder).superset(xcode, ycode); + int subOf = ((HierarchyEncoderImpl) encoder).superset(xcode, ycode); if ( x.equals( y ) ) { assertThat(subOf).as(x + " vs " + y).isEqualTo(0); @@ -600,9 +600,9 @@ private void checkHier( HierarchyEncoder encoder, char fin ) { @Test public void testHierEncoderMoreInheritance() { - HierarchyEncoderImpl encoder = new HierarchyEncoderImpl(); + HierarchyEncoderImpl encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); encoder.encode("B", List.of("A")); encoder.encode("C", List.of("A")); encoder.encode("D", List.of("A")); @@ -656,9 +656,9 @@ public void testHierEncoderMoreInheritance() { @Test public void testSecondOrderInheritance() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "T", Collections.EMPTY_LIST ); + encoder.encode( "T", Collections.emptyList()); encoder.encode("A", List.of("T")); encoder.encode("B", List.of("T")); encoder.encode("C", List.of("T")); @@ -672,8 +672,8 @@ public void testSecondOrderInheritance() { LOGGER.debug( encoder.toString() ); - assertThat(((HierarchyEncoderImpl) encoder).superset(encoder.getCode("Z"), encoder.getCode("F")) < 0).isTrue() ; - assertThat(((HierarchyEncoderImpl) encoder).superset(encoder.getCode("F"), encoder.getCode("Z")) < 0).isTrue() ; + assertThat(((HierarchyEncoderImpl) encoder).superset(encoder.getCode("Z"), encoder.getCode("F")) < 0).isTrue() ; + assertThat(((HierarchyEncoderImpl) encoder).superset(encoder.getCode("F"), encoder.getCode("Z")) < 0).isTrue() ; } @@ -682,9 +682,9 @@ public void testSecondOrderInheritance() { @Test public void testDecoderAncestors() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "Thing", Collections.EMPTY_LIST ); + encoder.encode( "Thing", Collections.emptyList()); encoder.encode("A", List.of("Thing")); encoder.encode("Z", List.of("Thing")); encoder.encode( "B", Arrays.asList( "A", "Z" ) ); @@ -701,7 +701,7 @@ public void testDecoderAncestors() { LOGGER.debug( encoder.toString() ); BitSet b; - Collection x; + Collection x; b = parseBitSet( "1100111" ); x = encoder.upperAncestors(b); @@ -716,14 +716,14 @@ public void testDecoderAncestors() { assertThat(x.contains("S")).isTrue(); assertThat(x.contains("M")).isTrue(); assertThat(x.contains("Thing")).isTrue(); - assertThat(x.size()).isEqualTo(9); + assertThat(x).hasSize(9); b = parseBitSet( "100000" ); x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("Q")).isTrue(); assertThat(x.contains("Thing")).isTrue(); @@ -731,7 +731,7 @@ public void testDecoderAncestors() { x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); - assertThat(x.size()).isEqualTo(6); + assertThat(x).hasSize(6); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("Z")).isTrue(); assertThat(x.contains("B")).isTrue(); @@ -743,7 +743,7 @@ public void testDecoderAncestors() { x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); - assertThat(x.size()).isEqualTo(4); + assertThat(x).hasSize(4); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("Z")).isTrue(); assertThat(x.contains("C")).isTrue(); @@ -753,7 +753,7 @@ public void testDecoderAncestors() { x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("Thing")).isTrue(); @@ -761,7 +761,7 @@ public void testDecoderAncestors() { x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("Z")).isTrue(); assertThat(x.contains("Thing")).isTrue(); @@ -769,7 +769,7 @@ public void testDecoderAncestors() { x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("Thing")).isTrue(); @@ -777,7 +777,7 @@ public void testDecoderAncestors() { x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); - assertThat(x.size()).isEqualTo(4); + assertThat(x).hasSize(4); assertThat(x.contains("Thing")).isTrue(); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("B")).isTrue(); @@ -788,9 +788,9 @@ public void testDecoderAncestors() { @Test public void testDecoderDescendants() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "Thing", Collections.EMPTY_LIST ); + encoder.encode( "Thing", Collections.emptyList()); encoder.encode("A", List.of("Thing")); encoder.encode("Z", List.of("Thing")); encoder.encode( "B", Arrays.asList( "A", "Z" ) ); @@ -806,14 +806,11 @@ public void testDecoderDescendants() { LOGGER.debug( encoder.toString() ); - BitSet b; - Collection x; - - b = parseBitSet( "111" ); - x = encoder.lowerDescendants(b); + BitSet b = parseBitSet( "111" ); + Collection x = encoder.lowerDescendants(b); LOGGER.debug( "DESC " + x ); - assertThat(x.size()).isEqualTo(3); + assertThat(x).hasSize(3); assertThat(x.contains("C")).isTrue(); assertThat(x.contains("N")).isTrue(); assertThat(x.contains("T")).isTrue(); @@ -823,7 +820,7 @@ public void testDecoderDescendants() { x = encoder.lowerDescendants(b); LOGGER.debug( "DESC " + x ); - assertThat(x.size()).isEqualTo(5); + assertThat(x).hasSize(5); assertThat(x.contains("C")).isTrue(); assertThat(x.contains("N")).isTrue(); assertThat(x.contains("T")).isTrue(); @@ -835,7 +832,7 @@ public void testDecoderDescendants() { x = encoder.lowerDescendants(b); LOGGER.debug( "DESC " + x ); - assertThat(x.size()).isEqualTo(4); + assertThat(x).hasSize(4); assertThat(x.contains("Q")).isTrue(); assertThat(x.contains("T")).isTrue(); assertThat(x.contains("M")).isTrue(); @@ -848,14 +845,14 @@ public void testDecoderDescendants() { x = encoder.lowerDescendants(b); LOGGER.debug( "DESC " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("T")).isTrue(); b = parseBitSet( "1111" ); x = encoder.lowerDescendants(b); LOGGER.debug( "DESC " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("N")).isTrue(); @@ -863,7 +860,7 @@ public void testDecoderDescendants() { x = encoder.lowerDescendants(b); LOGGER.debug( "DESC " + x ); - assertThat(x.size()).isEqualTo(5); + assertThat(x).hasSize(5); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("B")).isTrue(); assertThat(x.contains("C")).isTrue(); @@ -875,7 +872,7 @@ public void testDecoderDescendants() { x = encoder.lowerDescendants(new BitSet()); LOGGER.debug( "DESC " + x ); - assertThat(x.size()).isEqualTo(13); + assertThat(x).hasSize(13); assertThat(x.contains("Z")).isTrue(); assertThat(x.contains("Thing")).isTrue(); @@ -884,9 +881,9 @@ public void testDecoderDescendants() { @Test public void testHierEncoderDecoderLower() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "Thing", Collections.EMPTY_LIST ); + encoder.encode( "Thing", Collections.emptyList()); encoder.encode("A", List.of("Thing")); encoder.encode("Z", List.of("Thing")); encoder.encode( "B", Arrays.asList( "A", "Z" ) ); @@ -902,16 +899,16 @@ public void testHierEncoderDecoderLower() { LOGGER.debug( encoder.toString() ); - Collection x; + Collection x; x = encoder.lowerBorder( encoder.metMembersCode(List.of("B"))); LOGGER.debug( "GCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("B")).isTrue(); x = encoder.immediateChildren( encoder.metMembersCode(List.of("B"))); LOGGER.debug( "GCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("N")).isTrue(); @@ -919,13 +916,13 @@ public void testHierEncoderDecoderLower() { x = encoder.lowerBorder( encoder.metMembersCode( Arrays.asList( "Z", "Q" ) ) ); LOGGER.debug( "GCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("T")).isTrue(); x = encoder.immediateChildren( encoder.metMembersCode( Arrays.asList( "Z", "Q" ) ) ); LOGGER.debug( "GCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("T")).isTrue(); @@ -934,14 +931,14 @@ public void testHierEncoderDecoderLower() { x = encoder.lowerBorder( encoder.metMembersCode( Arrays.asList( "A", "Z" ) ) ); LOGGER.debug( "GCS " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("B")).isTrue(); assertThat(x.contains("C")).isTrue(); x = encoder.immediateChildren( encoder.metMembersCode( Arrays.asList( "A", "Z" ) ) ); LOGGER.debug( "GCS " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("B")).isTrue(); assertThat(x.contains("C")).isTrue(); @@ -951,13 +948,13 @@ public void testHierEncoderDecoderLower() { x = encoder.lowerBorder( encoder.metMembersCode(List.of("Thing"))); LOGGER.debug( "GCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("Thing")).isTrue(); x = encoder.immediateChildren( encoder.metMembersCode(List.of("Thing"))); LOGGER.debug( "GCS " + x ); - assertThat(x.size()).isEqualTo(5); + assertThat(x).hasSize(5); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("Z")).isTrue(); assertThat(x.contains("P")).isTrue(); @@ -970,9 +967,9 @@ public void testHierEncoderDecoderLower() { @Test public void testHierEncoderDecoderUpper() { - HierarchyEncoder encoder = new HierarchyEncoderImpl(); + HierarchyEncoder encoder = new HierarchyEncoderImpl<>(); - encoder.encode( "Thing", Collections.EMPTY_LIST ); + encoder.encode( "Thing", Collections.emptyList()); encoder.encode("A", List.of("Thing")); encoder.encode("Z", List.of("Thing")); encoder.encode( "B", Arrays.asList( "A", "Z" ) ); @@ -988,16 +985,14 @@ public void testHierEncoderDecoderUpper() { LOGGER.debug( encoder.toString() ); - Collection x; - - x = encoder.upperBorder( encoder.metMembersCode(List.of("B"))); + Collection x = encoder.upperBorder( encoder.metMembersCode(List.of("B"))); LOGGER.debug( "LCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("B")).isTrue(); x = encoder.immediateParents( encoder.metMembersCode(List.of("B"))); LOGGER.debug( "LCS " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("Z")).isTrue(); @@ -1006,13 +1001,13 @@ public void testHierEncoderDecoderUpper() { x = encoder.upperBorder( encoder.jointMembersCode( Arrays.asList( "Z", "Q" ) ) ); LOGGER.debug( "LCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("Thing")).isTrue(); x = encoder.immediateParents( encoder.jointMembersCode( Arrays.asList( "Z", "Q" ) ) ); LOGGER.debug( "LCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("Thing")).isTrue(); @@ -1020,14 +1015,14 @@ public void testHierEncoderDecoderUpper() { x = encoder.upperBorder( encoder.jointMembersCode( Arrays.asList( "B", "C" ) ) ); LOGGER.debug( "LCS " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("Z")).isTrue(); x = encoder.immediateParents( encoder.jointMembersCode( Arrays.asList( "B", "C" ) ) ); LOGGER.debug( "LCS " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("A")).isTrue(); assertThat(x.contains("Z")).isTrue(); @@ -1035,13 +1030,13 @@ public void testHierEncoderDecoderUpper() { x = encoder.upperBorder( encoder.jointMembersCode(List.of("T"))); LOGGER.debug( "LCS " + x ); - assertThat(x.size()).isEqualTo(1); + assertThat(x).hasSize(1); assertThat(x.contains("T")).isTrue(); x = encoder.immediateParents( encoder.jointMembersCode(List.of("T"))); LOGGER.debug( "LCS " + x ); - assertThat(x.size()).isEqualTo(2); + assertThat(x).hasSize(2); assertThat(x.contains("C")).isTrue(); assertThat(x.contains("Q")).isTrue(); @@ -1057,7 +1052,7 @@ public void testHierEncoderDecoderUpper() { public void testClassInstanceHierarchies() { HierarchyEncoder encoder = new HierarchyEncoderImpl(); - BitSet ak = encoder.encode( "A", Collections.EMPTY_LIST ); + BitSet ak = encoder.encode( "A", Collections.emptyList()); BitSet bk = encoder.encode("B", List.of("A")); BitSet ck = encoder.encode("C", List.of("A")); BitSet dk = encoder.encode("D", List.of("B")); @@ -1139,7 +1134,7 @@ public void testClassInstanceHierarchies() { public void testUnwantedCodeOverriding() { HierarchyEncoder encoder = new HierarchyEncoderImpl(); - BitSet ak = encoder.encode( "A", Collections.EMPTY_LIST ); + BitSet ak = encoder.encode( "A", Collections.emptyList()); BitSet ck = encoder.encode("C", List.of("A")); BitSet dk = encoder.encode("D", List.of("A")); BitSet gk = encoder.encode( "G", Arrays.asList( "C", "D" ) ); @@ -1186,7 +1181,7 @@ public void testUnwantedCodeOverriding() { public void testDeepTree() { HierarchyEncoder encoder = new HierarchyEncoderImpl(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); encoder.encode("B", List.of("A")); @@ -1219,7 +1214,7 @@ public void testDeepTree() { public void testNestedTree() { HierarchyEncoder encoder = new HierarchyEncoderImpl(); - encoder.encode( "A", Collections.EMPTY_LIST ); + encoder.encode( "A", Collections.emptyList()); encoder.encode("B", List.of("A")); encoder.encode("C", List.of("B")); encoder.encode("D", List.of("B")); From becc8a82a02ac9c0e5aaf58cc3bab2cbbe69c8c6 Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Sat, 14 Sep 2024 18:55:48 +0200 Subject: [PATCH 02/13] More small cleanups --- .../factmodel/traits/LegacyTraitTest.java | 103 ++++++++---------- .../compiler/factmodel/traits/TraitTest.java | 7 +- .../traits/core/meta/org/MetadataTest.java | 8 +- 3 files changed, 52 insertions(+), 66 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LegacyTraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LegacyTraitTest.java index 6b54b117bfa..e8a27382f9e 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LegacyTraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LegacyTraitTest.java @@ -19,15 +19,14 @@ package org.drools.traits.compiler.factmodel.traits; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; +import java.util.List; import org.drools.base.factmodel.traits.Trait; import org.drools.base.factmodel.traits.Traitable; import org.drools.kiesession.rulebase.InternalKnowledgeBase; import org.drools.kiesession.rulebase.KnowledgeBaseFactory; import org.drools.traits.compiler.CommonTraitTest; -import org.drools.traits.core.factmodel.LogicalTypeInconsistencyException; import org.drools.traits.core.factmodel.TraitFactoryImpl; import org.drools.traits.core.factmodel.VirtualPropertyMode; import org.drools.traits.core.util.StandaloneTraitFactory; @@ -47,7 +46,6 @@ import org.slf4j.LoggerFactory; import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.fail; import static org.drools.traits.compiler.factmodel.traits.TraitTestUtils.createStandaloneTraitFactory; @RunWith(Parameterized.class) @@ -58,30 +56,26 @@ public class LegacyTraitTest extends CommonTraitTest { public VirtualPropertyMode mode; @Parameterized.Parameters - public static Collection modes() { - return Arrays.asList( new VirtualPropertyMode[][] - { - { VirtualPropertyMode.MAP }, - { VirtualPropertyMode.TRIPLES } - } ); + public static Collection modes() { + return List.of(VirtualPropertyMode.MAP, VirtualPropertyMode.TRIPLES); } - public LegacyTraitTest( VirtualPropertyMode m ) { + public LegacyTraitTest(VirtualPropertyMode m) { this.mode = m; } - private KieSession getSessionFromString( String drl ) { + private KieSession getSessionFromString(String drl) { KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); - knowledgeBuilder.add( ResourceFactory.newByteArrayResource( drl.getBytes() ), - ResourceType.DRL ); + knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), + ResourceType.DRL); if (knowledgeBuilder.hasErrors()) { - throw new RuntimeException( knowledgeBuilder.getErrors().toString() ); + throw new RuntimeException(knowledgeBuilder.getErrors().toString()); } InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(); - kbase.addPackages( knowledgeBuilder.getKnowledgePackages() ); + kbase.addPackages(knowledgeBuilder.getKnowledgePackages()); KieSession session = kbase.newKieSession(); return session; @@ -160,7 +154,7 @@ public void traitWithPojoInterface() { // enhanced so that declaration is not needed // "declare ProcedureImpl end " + "declare trait ExtendedProcedure " + - " @role( event )" + + " @role(event)" + "end " + // Surgery must be declared as trait, since it does not extend Thing @@ -174,54 +168,54 @@ public void traitWithPojoInterface() { "when " + " $p : ExtendedProcedure() " + "then " + - " don( $p, Surgery.class ); " + + " don($p, Surgery.class); " + "end " + "rule 'Test 1' " + "dialect 'mvel' " + "when " + - " $s1 : ExtendedProcedure( $subject : subject ) " + - " $s2 : ExtendedProcedure( subject == $subject ) " + + " $s1 : ExtendedProcedure($subject : subject) " + + " $s2 : ExtendedProcedure(subject == $subject) " + "then " + "end " + "rule 'Test 2' " + "dialect 'mvel' " + "when " + - " $s1 : ExtendedProcedure( $subject : subject.name ) " + - " $s2 : ExtendedProcedure( subject.name == $subject ) " + + " $s1 : ExtendedProcedure($subject : subject.name) " + + " $s2 : ExtendedProcedure(subject.name == $subject) " + "then " + "end " + "rule 'Test 3' " + "dialect 'mvel' " + "when " + - " $s1 : ExtendedProcedure( ) " + + " $s1 : ExtendedProcedure() " + "then " + - " update( $s1 ); " + + " update($s1); " + "end " + "\n"; - KieSession ks = getSessionFromString( source ); - TraitFactoryImpl.setMode(mode, ks.getKieBase() ); + KieSession ks = getSessionFromString(source); + TraitFactoryImpl.setMode(mode, ks.getKieBase()); ExtendedProcedureImpl procedure1 = new ExtendedProcedureImpl(); ExtendedProcedureImpl procedure2 = new ExtendedProcedureImpl(); PatientImpl patient1 = new PatientImpl(); patient1.setName("John"); - procedure1.setSubject( patient1 ); + procedure1.setSubject(patient1); procedure1.setPers(new PatientImpl()); PatientImpl patient2 = new PatientImpl(); patient2.setName("John"); - procedure2.setSubject( patient2 ); + procedure2.setSubject(patient2); procedure2.setPers(new PatientImpl()); - ks.insert( procedure1 ); - ks.insert( procedure2 ); + ks.insert(procedure1); + ks.insert(procedure2); - ks.fireAllRules( 500 ); + ks.fireAllRules(500); } @@ -256,63 +250,58 @@ public void traitWithMixedInterfacesExtendingEachOther() { "rule 'Bar Don'" + "when " + - " $b : BarImpl( this isA Foo.class, this not isA Foo2.class )\n" + + " $b : BarImpl(this isA Foo.class, this not isA Foo2.class)\n" + " String()\n" + "then " + - " list.add( 3 ); " + - " retract( $b ); " + + " list.add(3); " + + " retract($b); " + "end " + "rule 'Don Bar' " + "no-loop " + "when " + - " $b : Foo( ) " + + " $b : Foo() " + "then " + - " list.add( 1 ); " + - " don( $b, Foo2.class ); " + + " list.add(1); " + + " don($b, Foo2.class); " + "end " + "rule 'Cant really shed Foo but Foo2' " + "when " + " $b : Foo2() " + "then " + - " list.add( 2 ); " + - " shed( $b, Foo.class ); " + - " insert( \"done\" );" + + " list.add(2); " + + " shed($b, Foo.class); " + + " insert(\"done\");" + "end " + ""; - KieSession ks = getSessionFromString( source ); + KieSession ks = getSessionFromString(source); KieBase kieBase = ks.getKieBase(); TraitFactoryImpl.setMode(mode, kieBase); - ArrayList list = new ArrayList(); - ks.setGlobal( "list", list ); + List list = new ArrayList<>(); + ks.setGlobal("list", list); - ks.insert( new BarImpl() ); + ks.insert(new BarImpl()); int n = ks.fireAllRules(); - LOGGER.debug( list.toString() ); - assertThat(list).isEqualTo(Arrays.asList(1, 2, 3)); + LOGGER.debug(list.toString()); + assertThat(list).containsExactly(1, 2, 3); assertThat(n).isEqualTo(3); } @Test - public void testTraitWithNonAccessorMethodShadowing() { + public void testTraitWithNonAccessorMethodShadowing() throws Exception { StandaloneTraitFactory factory = createStandaloneTraitFactory(); - try { - SomeInterface r = (SomeInterface) factory.don( new SomeClass(), SomeInterface.class ); - r.prepare(); - assertThat(r.getFoo()).isEqualTo(42); - assertThat(r.doThis("that")).isEqualTo("I did that"); - } catch ( LogicalTypeInconsistencyException e ) { - e.printStackTrace(); - fail( e.getMessage() ); - } + SomeInterface r = (SomeInterface) factory.don(new SomeClass(), SomeInterface.class); + r.prepare(); + assertThat(r.getFoo()).isEqualTo(42); + assertThat(r.doThis("that")).isEqualTo("I did that"); } @Test() @@ -331,9 +320,9 @@ public void testPojoExtendInterface() { ""; KieHelper kh = new KieHelper(); - kh.addContent( s1, ResourceType.DRL ); + kh.addContent(s1, ResourceType.DRL); - assertThat(kh.verify().getMessages(Message.Level.ERROR).size()).isEqualTo(0); + assertThat(kh.verify().getMessages(Message.Level.ERROR)).isEmpty(); } } diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index 50dfdf89222..a7869b7fb4f 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -116,11 +116,8 @@ public class TraitTest extends CommonTraitTest { public VirtualPropertyMode mode; @Parameterized.Parameters - public static Collection modes() { - return Arrays.asList(new VirtualPropertyMode[][] - { - { VirtualPropertyMode.MAP }, { VirtualPropertyMode.TRIPLES } - }); + public static Collection modes() { + return List.of(VirtualPropertyMode.MAP, VirtualPropertyMode.TRIPLES); } public TraitTest(VirtualPropertyMode m) { diff --git a/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java b/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java index 6a40cc460c5..9f1bedb007c 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java @@ -103,7 +103,7 @@ public void testKlassAndSubKlassWithInterfaces() { assertThat(sk.prop.get(ski)).isEqualTo("bye"); LOGGER.debug( ski.map.toString()); - Map tgt = new HashMap(); + Map tgt = new HashMap<>(); tgt.put( "prop", "bye" ); tgt.put( "subProp", -99 ); assertThat(ski.map).isEqualTo(tgt); @@ -215,7 +215,7 @@ public void testURIsOnLegacyClasses() { @Test public void testDon() { Entity entity = new Entity( "123" ); - entity._setDynamicProperties( new HashMap( ) ); + entity._setDynamicProperties( new HashMap<>( ) ); entity._getDynamicProperties().put( "prop", "hello" ); Klass klass = Klass_.donKlass( entity ) @@ -229,7 +229,7 @@ public void testDon() { @Test public void testDonWithAttributes() { Entity entity = new Entity( "123" ); - entity._setDynamicProperties( new HashMap() ); + entity._setDynamicProperties( new HashMap<>() ); SubKlass klass = SubKlass_.donSubKlass(entity ) .setTraitFactory(createStandaloneTraitFactory()) @@ -259,7 +259,7 @@ public void testCollectionOrientedProperties() { AnotherKlass aki3 = AnotherKlass_.newAnotherKlass( "003" ).call(); AnotherKlass aki4 = AnotherKlass_.newAnotherKlass( "004" ).call(); - ArrayList initial = new ArrayList( Arrays.asList( aki0, aki1 ) ); + List initial = new ArrayList<>( List.of( aki0, aki1 ) ); SubKlass ski = SubKlass_.newSubKlass( URI.create( "123" ) ) .links( initial, Lit.SET ) .links( aki1, Lit.REMOVE ) From 4b91e9c5109f238f44c1f0f176f9f6a0004df312 Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Sat, 14 Sep 2024 20:53:59 +0200 Subject: [PATCH 03/13] More cleanups... --- .../compiler/factmodel/traits/TraitTest.java | 153 +++++++++--------- 1 file changed, 74 insertions(+), 79 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index a7869b7fb4f..66097e4288d 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -190,9 +190,9 @@ public void testTraitWrapGetAndSet() throws Exception { FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); TraitableBean imp = (TraitableBean) impClass.newInstance(); - Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass(); + Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); Map virtualFields = imp._getDynamicProperties(); Map wrapper = proxy.getFields(); @@ -233,8 +233,6 @@ public void testTraitShed() { ks.insert("hire"); ks.fireAllRules(); - Collection c = ks.getObjects(); - assertThat(info.contains("Worker")).isTrue(); assertThat(info).hasSize(2); @@ -267,7 +265,7 @@ public void testTraitDon() { assertThat(info.contains("DON")).isTrue(); assertThat(info.contains("SHED")).isTrue(); - Iterator it = wm.iterator(); + Iterator it = wm.iterator(); Object x = it.next(); if (x instanceof String) { x = it.next(); @@ -300,7 +298,7 @@ public void traitMethodsWithObjects() { KieSession ks = getSession(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List errors = new ArrayList(); + List errors = new ArrayList<>(); ks.setGlobal("list", errors); ks.fireAllRules(); @@ -320,7 +318,7 @@ public void traitMethodsWithPrimitives() { KieSession ks = getSession(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List errors = new ArrayList(); + List errors = new ArrayList<>(); ks.setGlobal("list", errors); ks.fireAllRules(); @@ -354,27 +352,27 @@ public void testTraitProxy() throws Exception { TraitableBean imp = (TraitableBean) impClass.newInstance(); impClass.set(imp, "name", "aaa"); - Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass(); - Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass(); + Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass(); + Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass(); assertThat(trait).isNotNull(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); proxy.getFields().put("field", "xyz"); // proxy.getFields().put("name", "aaa"); assertThat(proxy).isNotNull(); - TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(imp, trait); + TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); assertThat(proxy).isSameAs(proxy2); - TraitProxyImpl proxy3 = (TraitProxyImpl) tFactory.getProxy(imp, trait2); + TraitProxyImpl proxy3 = (TraitProxyImpl) tFactory.getProxy(imp, trait2, false); assertThat(proxy3).isNotNull(); assertThat(proxy3.getFields().get("field")).isEqualTo("xyz"); assertThat(proxy3.getFields().get("name")).isEqualTo("aaa"); TraitableBean imp2 = (TraitableBean) impClass.newInstance(); impClass.set(imp2, "name", "aaa"); - TraitProxyImpl proxy4 = (TraitProxyImpl) tFactory.getProxy(imp2, trait); + TraitProxyImpl proxy4 = (TraitProxyImpl) tFactory.getProxy(imp2, trait, false); // proxy4.getFields().put("name", "aaa"); proxy4.getFields().put("field", "xyz"); @@ -403,8 +401,8 @@ public void testWrapperSize() throws Exception { FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); TraitableBean imp = (TraitableBean) impClass.newInstance(); FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); Map virtualFields = imp._getDynamicProperties(); Map wrapper = proxy.getFields(); @@ -427,7 +425,7 @@ public void testWrapperSize() throws Exception { // TraitableBean ind = (TraitableBean) indClass.newInstance(); TraitableBean ind = new Entity(); - TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait); + TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait, false); Map virtualFields2 = ind._getDynamicProperties(); Map wrapper2 = proxy2.getFields(); @@ -447,11 +445,11 @@ public void testWrapperSize() throws Exception { assertThat(virtualFields2).hasSize(4); FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role"); - Class trait2 = traitClass2.getFactClass(); + Class trait2 = traitClass2.getFactClass(); // TraitableBean ind2 = (TraitableBean) indClass.newInstance(); TraitableBean ind2 = new Entity(); - TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2); + TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2, false); proxy99.getFields().put("surname", "xxx"); proxy99.getFields().put("name", "xyz"); @@ -459,7 +457,7 @@ public void testWrapperSize() throws Exception { assertThat(proxy99.getFields()).hasSize(3); - TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind2, trait); + TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind2, trait, false); assertThat(proxy100.getFields()).hasSize(4); @@ -487,8 +485,8 @@ public void testWrapperEmpty() throws Exception { TraitableBean imp = (TraitableBean) impClass.newInstance(); FactType studentClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); - Class trait = studentClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + Class trait = studentClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); Map wrapper = proxy.getFields(); assertThat(wrapper).isNotEmpty(); @@ -509,8 +507,8 @@ public void testWrapperEmpty() throws Exception { TraitableBean ind = new Entity(); FactType RoleClass = kb.getFactType("org.drools.compiler.trait.test", "Role"); - Class trait2 = RoleClass.getFactClass(); - TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait2); + Class trait2 = RoleClass.getFactClass(); + TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait2, false); Map wrapper2 = proxy2.getFields(); assertThat(wrapper2).isEmpty(); @@ -546,8 +544,8 @@ public void testWrapperContainsKey() throws Exception { impClass.set(imp, "name", "john"); FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); Map wrapper = proxy.getFields(); @@ -563,7 +561,7 @@ public void testWrapperContainsKey() throws Exception { // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity"); TraitableBean ind = new Entity(); - TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait); + TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait, false); Map wrapper2 = proxy2.getFields(); assertThat(wrapper2.containsKey("name")).isTrue(); @@ -577,10 +575,10 @@ public void testWrapperContainsKey() throws Exception { assertThat(wrapper2.containsKey("surname")).isTrue(); FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role"); - Class trait2 = traitClass2.getFactClass(); + Class trait2 = traitClass2.getFactClass(); TraitableBean ind2 = new Entity(); - TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2); + TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2, false); Map wrapper99 = proxy99.getFields(); assertThat(wrapper99.containsKey("name")).isFalse(); @@ -600,14 +598,14 @@ public void testWrapperContainsKey() throws Exception { TraitableBean ind0 = new Entity(); - TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind0, trait2); + TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind0, trait2, false); Map wrapper100 = proxy100.getFields(); assertThat(wrapper100.containsKey("name")).isFalse(); assertThat(wrapper100.containsKey("school")).isFalse(); assertThat(wrapper100.containsKey("age")).isFalse(); assertThat(wrapper100.containsKey("surname")).isFalse(); - TraitProxyImpl proxy101 = (TraitProxyImpl) tFactory.getProxy(ind0, trait); + TraitProxyImpl proxy101 = (TraitProxyImpl) tFactory.getProxy(ind0, trait, false); // object gains properties by virtue of another trait // so new props are accessible even using the old proxy assertThat(wrapper100.containsKey("name")).isTrue(); @@ -638,14 +636,12 @@ public void testInternalComponents1() throws Exception { FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); TraitableBean imp = (TraitableBean) impClass.newInstance(); FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); Object proxyFields = proxy.getFields(); Object coreTraits = imp._getTraitMap(); Object coreProperties = imp._getDynamicProperties(); - assertThat(proxy.getObject() instanceof TraitableBean).isTrue(); - assertThat(proxyFields).isNotNull(); assertThat(coreTraits).isNotNull(); assertThat(coreProperties).isNotNull(); @@ -690,8 +686,8 @@ public void testWrapperKeySetAndValues() throws Exception { FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp"); TraitableBean imp = (TraitableBean) impClass.newInstance(); FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); impClass.set(imp, "name", "john"); proxy.getFields().put("surname", "xxx"); @@ -701,8 +697,8 @@ public void testWrapperKeySetAndValues() throws Exception { assertThat(proxy.getFields()).hasSize(6); assertThat(proxy.getFields().keySet()).contains("name", "surname", "age", "school", "name2", "nfield"); - Collection col1 = proxy.getFields().values(); - Collection col2 = Arrays.asList("john", null, 0, "xxx", "john", null); + List col1 = new ArrayList<>(proxy.getFields().values()); + List col2 = Arrays.asList("john", null, 0, "xxx", "john", null); Comparator comp = new Comparator() { @@ -720,8 +716,8 @@ public int compare(Object o1, Object o2) { } }; - Collections.sort((List) col1, comp); - Collections.sort((List) col2, comp); + Collections.sort(col1, comp); + Collections.sort(col2, comp); assertThat(col2).isEqualTo(col1); assertThat(proxy.getFields().containsValue(null)).isTrue(); @@ -753,14 +749,14 @@ public void testWrapperClearAndRemove() throws Exception { TraitableBean imp = (TraitableBean) impClass.newInstance(); impClass.set(imp, "name", "john"); FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student"); - Class trait = traitClass.getFactClass(); - TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait); + Class trait = traitClass.getFactClass(); + TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait, false); proxy.getFields().put("surname", "xxx"); proxy.getFields().put("name2", "john"); proxy.getFields().put("nfield", null); - Set set = new HashSet(); + Set set = new HashSet<>(); set.add("name"); set.add("surname"); set.add("age"); @@ -878,7 +874,7 @@ public void testIsAEvaluator() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); + List info = new ArrayList<>(); ks.setGlobal("list", info); ks.fireAllRules(); @@ -895,7 +891,7 @@ public void testIsA() { KieSession ks = getSession(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); + List info = new ArrayList<>(); ks.setGlobal("list", info); ks.fireAllRules(); @@ -920,7 +916,7 @@ public void testOverrideType() { KieSession ks = getSession(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); + List info = new ArrayList<>(); ks.setGlobal("list", info); try { @@ -999,7 +995,7 @@ public void testTraitLegacy() { TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); + List info = new ArrayList<>(); ks.setGlobal("list", info); ks.fireAllRules(); @@ -1015,7 +1011,7 @@ public void testTraitLegacy() { } - private void printDebugInfoSessionObjects(final Collection facts, final List globalList) { + private void printDebugInfoSessionObjects(final Collection facts, final List globalList) { LOGGER.debug(" -------------- " + facts.size() + " ---------------- "); for (Object o : facts) { LOGGER.debug("\t\t" + o); @@ -1033,7 +1029,7 @@ public void testTraitCollections() { TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); + List info = new ArrayList<>(); ks.setGlobal("list", info); ks.fireAllRules(); @@ -1052,7 +1048,7 @@ public void testTraitCore() { KieSession ks = getSession(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); + List info = new ArrayList<>(); ks.setGlobal("list", info); ks.fireAllRules(); @@ -1072,7 +1068,7 @@ public void traitWithEquality() { KieSession ks = getSession(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List info = new ArrayList(); + List info = new ArrayList<>(); ks.setGlobal("list", info); ks.fireAllRules(); @@ -1196,7 +1192,7 @@ public void testManyTraits() { TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); Person student = new Person("student", 18); @@ -1215,14 +1211,13 @@ public void traitManyTimes() { KieSession ksession = getSession("org/drools/compiler/factmodel/traits/testTraitDonMultiple.drl"); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); ksession.fireAllRules(); for (Object o : ksession.getObjects()) { LOGGER.debug(o.toString()); } - Collection x = ksession.getObjects(); assertThat(ksession.getObjects()).hasSize(2); assertThat(list).hasSize(5); @@ -1279,7 +1274,7 @@ public void traitsInBatchExecution() { throw new RuntimeException(kbuilder.getErrors().toString()); } - List list = new ArrayList(); + List list = new ArrayList<>(); KieBase kbase = kbuilder.newKieBase(); TraitFactoryImpl.setMode(mode, kbase); @@ -1350,7 +1345,7 @@ public void testManyTraitsStateless() { KieSession ksession = kb.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); ksession.execute(CommandFactory.newFireAllRules()); @@ -1417,7 +1412,7 @@ public void testAliasing() { KieSession ksession = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); ksession.fireAllRules(); @@ -1425,8 +1420,8 @@ public void testAliasing() { assertThat(list).hasSize(9); assertThat(list.contains("UniBoh")).isTrue(); assertThat(list.contains("Skool")).isTrue(); - assertThat(((Collection) list.get(3)).containsAll(Arrays.asList("workPlace", "nomen", "level"))).isTrue(); - assertThat(((Collection) list.get(5)).containsAll(Arrays.asList("davide", "Skool", 0))).isTrue(); + assertThat((Collection) list.get(3)).contains("workPlace", "nomen", "level"); + assertThat((Collection) list.get(5)).contains("davide", "Skool", 0); assertThat(list.contains(true)).isTrue(); assertThat(list.contains("Floor84")).isTrue(); assertThat(list.contains(99)).isTrue(); @@ -1479,7 +1474,7 @@ public void testTraitLogicalRemoval() { KieSession ksession = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); FactHandle h = ksession.insert("trigger"); @@ -1625,7 +1620,7 @@ public void testTraitsLegacyWrapperCoherence() { ksession.fireAllRules(); - Collection yOld = ksession.getObjects(); + Collection yOld = ksession.getObjects(); assertThat(yOld).hasSize(2); TraitableBean coreOld = null; @@ -1667,19 +1662,19 @@ public void testHasTypes() throws Exception { TraitableBean imp = (TraitableBean) impClass.newInstance(); impClass.set(imp, "name", "aaabcd"); - Class trait = kb.getFactType("org.drools.compiler.trait.test","Student").getFactClass(); - Class trait2 = kb.getFactType("org.drools.compiler.trait.test","Role").getFactClass(); + Class trait = kb.getFactType("org.drools.compiler.trait.test","Student").getFactClass(); + Class trait2 = kb.getFactType("org.drools.compiler.trait.test","Role").getFactClass(); assertThat(trait).isNotNull(); - TraitProxyImpl proxy = (TraitProxyImpl) traitBuilder.getProxy(imp, trait); - Thing thing = traitBuilder.getProxy(imp, Thing.class); + TraitProxyImpl proxy = (TraitProxyImpl) traitBuilder.getProxy(imp, trait, false); + Thing thing = traitBuilder.getProxy(imp, Thing.class, false); TraitableBean core = proxy.getObject(); - TraitProxyImpl proxy2 = (TraitProxyImpl) traitBuilder.getProxy(imp, trait); - Thing thing2 = traitBuilder.getProxy(imp, Thing.class); + TraitProxyImpl proxy2 = (TraitProxyImpl) traitBuilder.getProxy(imp, trait, false); + Thing thing2 = traitBuilder.getProxy(imp, Thing.class, false); assertThat(proxy2).isSameAs(proxy); assertThat(thing2).isSameAs(thing); @@ -2014,7 +2009,7 @@ public void testTraitModifyCore() { kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); @@ -2165,10 +2160,10 @@ public void testTraitModifyCore2a() { kbase.addPackages(kbuilder.getKnowledgePackages()); KieSession ksession = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); - int k = ksession.fireAllRules(); + ksession.fireAllRules(); assertThat(list.contains(true)).isTrue(); assertThat(list).hasSize(1); @@ -2265,7 +2260,7 @@ public void testTraitModifyCore3() { kbase.addPackages(kbuilder.getKnowledgePackages()); - List list = new ArrayList(); + List list = new ArrayList<>(); KieSession ksession = kbase.newKieSession(); ksession.setGlobal("list", list); @@ -2443,7 +2438,7 @@ public void isAWithBackChaining() { KieSession ksession = getSession(source); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); ksession.fireAllRules(); @@ -2505,7 +2500,7 @@ public void testIsAEvaluatorOnClassification() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -2575,7 +2570,7 @@ public void testShedWithTMS() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -2647,17 +2642,17 @@ public void testTraitInitialization() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); assertThat(list).hasSize(6); assertThat(list.contains(null)).isFalse(); - List hard = (List) list.get(0); - List soft = (List) list.get(1); - List otra = (List) list.get(2); - List more = (List) list.get(3); + List hard = (List) list.get(0); + List soft = (List) list.get(1); + List otra = (List) list.get(2); + List more = (List) list.get(3); assertThat(hard).isEmpty(); assertThat(soft).isEmpty(); From efeba76bf8479a8b371f53975b52c04799cac3d5 Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Sat, 14 Sep 2024 21:14:05 +0200 Subject: [PATCH 04/13] More cleanups... --- .../compiler/factmodel/traits/TraitTest.java | 63 +++++++++---------- 1 file changed, 31 insertions(+), 32 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index 66097e4288d..ce678cef304 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -2706,7 +2706,7 @@ public void testUnTraitedBean() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -2764,7 +2764,7 @@ public void testIsAOptimization() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -2817,7 +2817,7 @@ public void testTypeRefractionOnInsert() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -2919,7 +2919,7 @@ public void testTypeRefractionOnQuery2() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -2982,7 +2982,7 @@ public void testNodePartitioningByProxies() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -3055,7 +3055,7 @@ public void testNodePartitioningByProxiesAfterShed() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -3113,7 +3113,7 @@ public void testTypeRefractionOnQueryWithIsA() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -3174,7 +3174,7 @@ public void testCoreUpdate4() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -3499,7 +3499,7 @@ public void traitLogicalRemovalSimple() { KieSession ksession = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); FactHandle h = ksession.insert("trigger"); @@ -3691,7 +3691,7 @@ public void testUpdateLegacyClass() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.insert(new Person("john", 32)); @@ -3777,7 +3777,7 @@ public void testSoftPropertyClash() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -3890,10 +3890,9 @@ public void testMultipleModifications() { KieSession ks = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); - HashMap map; ks.fireAllRules(); assertThat(list.contains(1)).isTrue(); @@ -3940,7 +3939,7 @@ public void testPropagation() { KieSession ks = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession(); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -4013,7 +4012,7 @@ public void testParentBlockers() { KieSession ks = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -4051,7 +4050,7 @@ public void testTraitLogicalTMS() { KieSession ks = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); @@ -4127,7 +4126,7 @@ public void testTraitNoType() { KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list",list); ksession.fireAllRules(); @@ -4167,7 +4166,7 @@ public void testTraitdeleteOrder() { FactHandle handle = ksession.insert(new Entity()); ksession.fireAllRules(); - final ArrayList list = new ArrayList(); + final List list = new ArrayList<>(); ksession.addEventListener(new RuleRuntimeEventListener() { public void objectInserted(org.kie.api.event.rule.ObjectInsertedEvent objectInsertedEvent) { } @@ -4341,7 +4340,7 @@ public void testDonManyTraitsAtOnce() { KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); CountingWorkingMemoryEventListener cwm = new CountingWorkingMemoryEventListener(); @@ -4428,7 +4427,7 @@ public void testDonManyTraitsAtOnce2() { KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); CountingWorkingMemoryEventListener cwm = new CountingWorkingMemoryEventListener(); @@ -4569,7 +4568,7 @@ public void testShedOneLastTrait() throws InterruptedException { KieBase kbase = getKieBaseFromString(s1); TraitFactoryImpl.setMode(mode, kbase); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); KieSession knowledgeSession = kbase.newKieSession(); knowledgeSession.setGlobal("list", list); @@ -4644,7 +4643,7 @@ public void testShedThingCompletelyThenDonAgain() throws InterruptedException { KieBase kbase = getKieBaseFromString(s1); TraitFactoryImpl.setMode(mode, kbase); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); KieSession knowledgeSession = kbase.newKieSession(); knowledgeSession.setGlobal("list", list); @@ -4773,7 +4772,7 @@ public void testIsALegacyTrait() { KieBase kbase = getKieBaseFromString(s1); TraitFactoryImpl.setMode(mode, kbase); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); KieSession knowledgeSession = kbase.newKieSession(); knowledgeSession.setGlobal("list", list); @@ -4831,7 +4830,7 @@ public void testClassLiteralsWithOr() { KieBase kbase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build(); TraitFactoryImpl.setMode(mode, kbase); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); KieSession ksession = kbase.newKieSession(); ksession.setGlobal("list", list); @@ -4893,7 +4892,7 @@ public void testIsASwappedArg() { KieBase kbase = loadKnowledgeBaseFromString(drl); TraitFactoryImpl.setMode(mode, kbase); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); KieSession ksession = kbase.newKieSession(); ksession.setGlobal("list", list); @@ -5053,7 +5052,7 @@ public void testCastOnTheFly() throws InterruptedException { KieBase kbase = getKieBaseFromString(s1); TraitFactoryImpl.setMode(mode, kbase); - ArrayList list = new ArrayList(); + List list = new ArrayList<>(); KieSession knowledgeSession = kbase.newKieSession(); knowledgeSession.setGlobal("list", list); @@ -5180,7 +5179,7 @@ public void testPartitionWithSiblingsOnDelete() { TraitFactoryImpl.setMode(mode, kbase); KieSession ksession = kbase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); Entity e = new Entity(); @@ -5234,7 +5233,7 @@ public void testTupleIntegrityOnModification() { KieBase kbase = getKieBaseFromString(drl); TraitFactoryImpl.setMode(mode, kbase); KieSession ksession = kbase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); ksession.insert(new Entity()); @@ -5371,7 +5370,7 @@ public void testExternalUpdateWithProxyRefreshInEqualityMode() { TraitFactoryImpl.setMode(mode, kbase); KieSession ksession = kbase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); FactHandle handle = ksession.insert(new ExtEntity("x1", 42)); @@ -5405,7 +5404,7 @@ public void testIsAInstanceOf() { ""; KieBase kbase = getKieBaseFromString(drl); - List list = new ArrayList(); + List list = new ArrayList<>(); TraitFactoryImpl.setMode(mode, kbase); KieSession knowledgeSession = kbase.newKieSession(); @@ -5431,7 +5430,7 @@ public void testIsAInstanceOfNonTraitable() { ""; KieBase kbase = getKieBaseFromString(drl); - List list = new ArrayList(); + List list = new ArrayList<>(); TraitFactoryImpl.setMode(mode, kbase); KieSession knowledgeSession = kbase.newKieSession(); @@ -5501,7 +5500,7 @@ public void testSerializeKieBaseWithTraits() { KieBase kbase = getKieBaseFromString(drl); - List list = new ArrayList(); + List list = new ArrayList<>(); TraitFactoryImpl.setMode(mode, kbase); KieSession knowledgeSession = kbase.newKieSession(); From 4845e0f28cd582bbeba4158f71f67cbaaeef503b Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Sat, 14 Sep 2024 21:36:09 +0200 Subject: [PATCH 05/13] More cleanups... --- .../compiler/factmodel/traits/TraitTest.java | 107 +++++++----------- 1 file changed, 39 insertions(+), 68 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index ce678cef304..ec979d12490 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -273,7 +273,7 @@ public void testTraitDon() { LOGGER.debug(x.getClass().toString()); LOGGER.debug(x.getClass().getSuperclass().toString()); - LOGGER.debug(Arrays.asList(x.getClass().getInterfaces()).toString()); + LOGGER.debug(List.of(x.getClass().getInterfaces()).toString()); } @Test public void testMixin() { @@ -2012,12 +2012,10 @@ public void testTraitModifyCore() { List list = new ArrayList<>(); ksession.setGlobal("list", list); - int k = ksession.fireAllRules(); - assertThat(list).isEqualTo(Arrays.asList("john", "john", "john", "john", "alan", "alan", "alan", "alan")); assertThat(k).isEqualTo(11); - + assertThat(list).containsExactly("john", "john", "john", "john", "alan", "alan", "alan", "alan"); } @@ -2165,8 +2163,8 @@ public void testTraitModifyCore2a() { ksession.fireAllRules(); - assertThat(list.contains(true)).isTrue(); assertThat(list).hasSize(1); + assertThat(list).contains(true); } @@ -2383,10 +2381,7 @@ public void testTraitModifyCoreWithPropertyReactivity() { assertThat(k).isEqualTo(5); assertThat(list).hasSize(4); - assertThat(list.contains(1)).isTrue(); - assertThat(list.contains(2)).isTrue(); - assertThat(list.contains(3)).isTrue(); - assertThat(list.contains(4)).isTrue(); + assertThat(list).contains(1, 2, 3, 4); } @@ -2447,7 +2442,7 @@ public void isAWithBackChaining() { ksession.fireAllRules(); - assertThat(list.contains("Italy")).isTrue(); + assertThat(list).contains("Italy"); } @@ -2505,8 +2500,7 @@ public void testIsAEvaluatorOnClassification() { ks.fireAllRules(); assertThat(list).hasSize(2); - assertThat(list.contains("E")).isTrue(); - assertThat(list.contains("X")).isTrue(); + assertThat(list).contains("E", "X"); } @@ -2576,8 +2570,7 @@ public void testShedWithTMS() { LOGGER.debug(list.toString()); assertThat(list).hasSize(2); - assertThat(list.contains("E")).isTrue(); - assertThat(list.contains("X")).isTrue(); + assertThat(list).contains("E", "X"); ks.insert("shed"); ks.fireAllRules(); @@ -2656,11 +2649,11 @@ public void testTraitInitialization() { assertThat(hard).isEmpty(); assertThat(soft).isEmpty(); - assertThat(Arrays.asList(1, 2, 3)).isEqualTo(more); - assertThat(List.of(42)).isEqualTo(otra); + assertThat(more).containsExactly(1, 2, 3); + assertThat(otra).containsExactly(42); - assertThat(list.contains(3)).isTrue(); - assertThat(list.contains(0.421)).isTrue(); + assertThat(list).contains(3); + assertThat(list).contains(0.421); } @@ -2982,12 +2975,12 @@ public void testNodePartitioningByProxies() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList<>(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); LOGGER.debug(list.toString()); - assertThat(list).isEqualTo(Arrays.asList('A', 'B', 'C', 'D', 'E', 'F', 'G', 'Z')); + assertThat(list).containsExactly('A', 'B', 'C', 'D', 'E', 'F', 'G', 'Z'); for (Object o : ks.getObjects(object -> object instanceof TraitableBean)) { @@ -3055,12 +3048,12 @@ public void testNodePartitioningByProxiesAfterShed() { KieSession ks = getSessionFromString(source); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - List list = new ArrayList<>(); + List list = new ArrayList<>(); ks.setGlobal("list", list); ks.fireAllRules(); LOGGER.debug(list.toString()); - assertThat(list).isEqualTo(Arrays.asList('A', 'D', 'G', 'Z')); + assertThat(list).containsExactly('A', 'D', 'G', 'Z'); for (Object o : ks.getObjects(object -> object instanceof TraitableBean)) { @@ -3699,8 +3692,8 @@ public void testUpdateLegacyClass() { ks.fireAllRules(); - assertThat(list.contains(0)).isTrue(); assertThat(list).hasSize(1); + assertThat(list).contains(0); ks.dispose(); } @@ -3783,7 +3776,7 @@ public void testSoftPropertyClash() { ks.fireAllRules(); assertThat(list).hasSize(5); - assertThat(list).isEqualTo(Arrays.asList(99, "b", 11, Object.class, 42)); + assertThat(list).containsExactly(99, "b", 11, Object.class, 42); ks.dispose(); } @@ -3944,12 +3937,8 @@ public void testPropagation() { ks.fireAllRules(); - assertThat(list.contains("A")).isTrue(); - assertThat(list.contains("B")).isTrue(); - assertThat(list.contains("C")).isTrue(); - assertThat(list.contains("E")).isTrue(); - assertThat(list.contains("F")).isTrue(); assertThat(list).hasSize(5); + assertThat(list).contains("A", "B", "C", "E", "F"); list.clear(); @@ -3958,17 +3947,10 @@ public void testPropagation() { ks.insert("go"); ks.fireAllRules(); - assertThat(list.contains("H")).isTrue(); - assertThat(list.contains("G")).isTrue(); - assertThat(list.contains("HA")).isTrue(); - assertThat(list.contains("HB")).isTrue(); - assertThat(list.contains("HC")).isTrue(); - assertThat(list.contains("HE")).isTrue(); - assertThat(list.contains("HF")).isTrue(); - assertThat(list.contains("HG")).isTrue(); - assertThat(list.contains("HH")).isTrue(); - LOGGER.debug(list.toString()); assertThat(list).hasSize(9); + assertThat(list).contains("H", "G", "HA", "HB", "HC", "HE", "HF", "HG", "HH"); + LOGGER.debug(list.toString()); + list.clear(); LOGGER.debug("---------------------------------------"); @@ -3976,17 +3958,8 @@ public void testPropagation() { ks.insert("go2"); ks.fireAllRules(); - assertThat(list.contains("D")).isTrue(); - assertThat(list.contains("HA")).isTrue(); - assertThat(list.contains("HB")).isTrue(); - assertThat(list.contains("HC")).isTrue(); - assertThat(list.contains("HE")).isTrue(); - assertThat(list.contains("HF")).isTrue(); - assertThat(list.contains("HG")).isTrue(); - assertThat(list.contains("HH")).isTrue(); - assertThat(list.contains("HH")).isTrue(); - assertThat(list.contains("HD")).isTrue(); assertThat(list).hasSize(9); + assertThat(list).contains("D", "HA", "HB", "HC", "HE", "HF", "HG", "HH", "HH", "HD"); ks.dispose(); @@ -4130,8 +4103,7 @@ public void testTraitNoType() { ksession.setGlobal("list",list); ksession.fireAllRules(); - assertThat(list.contains("correct1")).isTrue(); - assertThat(list.contains("correct2")).isTrue(); + assertThat(list).contains("correct1", "correct2"); } @@ -4184,7 +4156,7 @@ public void objectDeleted(org.kie.api.event.rule.ObjectDeletedEvent objectRetrac ksession.fireAllRules(); LOGGER.debug(list.toString()); - assertThat(list).isEqualTo(Arrays.asList("B", "C", "A")); + assertThat(list).containsExactly("B", "C", "A"); } @@ -4391,7 +4363,7 @@ public void testDonManyTraitsAtOnce() { assertThat(list).hasSize(4); - assertThat(list.containsAll(Arrays.asList(0, 1, 2, 3))).isTrue(); + assertThat(list).contains(0, 1, 2, 3); } @Test @@ -4582,7 +4554,7 @@ public void testShedOneLastTrait() throws InterruptedException { knowledgeSession.fireAllRules(); assertThat(list).hasSize(2); - assertThat(list).isEqualTo(Arrays.asList("test.Mask.test.Core_Proxy", "org.drools.base.factmodel.traits.Thing.test.Core_Proxy")); + assertThat(list).containsExactly("test.Mask.test.Core_Proxy", "org.drools.base.factmodel.traits.Thing.test.Core_Proxy"); } @@ -4665,13 +4637,13 @@ public void testShedThingCompletelyThenDonAgain() throws InterruptedException { LOGGER.debug(list.toString()); assertThat(list).hasSize(2); - assertThat(list).isEqualTo(Arrays.asList("test.Mask.test.Core_Proxy", "test.Mask2.test.Core_Proxy")); + assertThat(list).containsExactly("test.Mask.test.Core_Proxy", "test.Mask2.test.Core_Proxy"); knowledgeSession.insert("shed2"); knowledgeSession.fireAllRules(); assertThat(list).hasSize(3); - assertThat(list).isEqualTo(Arrays.asList("test.Mask.test.Core_Proxy", "test.Mask2.test.Core_Proxy", "org.drools.base.factmodel.traits.Thing.test.Core_Proxy")); + assertThat(list).containsExactly("test.Mask.test.Core_Proxy", "test.Mask2.test.Core_Proxy", "org.drools.base.factmodel.traits.Thing.test.Core_Proxy"); } @@ -4900,8 +4872,7 @@ public void testIsASwappedArg() { ksession.fireAllRules(); assertThat(list).hasSize(2); - assertThat(list.contains(1)).isTrue(); - assertThat(list.contains(2)).isTrue(); + assertThat(list).contains(1, 2); } @@ -5059,7 +5030,7 @@ public void testCastOnTheFly() throws InterruptedException { knowledgeSession.fireAllRules(); - assertThat(list).isEqualTo(Arrays.asList(42, 43)); + assertThat(list).containsExactly(42, 43); } @@ -5179,14 +5150,14 @@ public void testPartitionWithSiblingsOnDelete() { TraitFactoryImpl.setMode(mode, kbase); KieSession ksession = kbase.newKieSession(); - List list = new ArrayList<>(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); Entity e = new Entity(); ksession.insert(e); ksession.fireAllRules(); - assertThat(list).isEqualTo(Arrays.asList('A', 'B', 'C')); + assertThat(list).containsExactly('A', 'B', 'C'); ksession.insert("go"); ksession.fireAllRules(); @@ -5194,7 +5165,7 @@ public void testPartitionWithSiblingsOnDelete() { Set s = checkOTNPartitioning(e, ksession); assertThat(s).hasSize(2); - assertThat(list).isEqualTo(Arrays.asList('A', 'B', 'C')); + assertThat(list).containsExactly('A', 'B', 'C'); } @@ -5246,7 +5217,7 @@ public void testTupleIntegrityOnModification() { assertThat(((RuleTerminalNodeLeftTuple) first).getRule().getName()).isEqualTo("Check"); } - assertThat(list).isEqualTo(Arrays.asList(0, 42)); + assertThat(list).containsExactly(0, 42); } @Test @@ -5304,7 +5275,7 @@ public void testShedVacancy() { TraitProxyImpl tp = (TraitProxyImpl) o; if (tp._getTypeCode().equals(c)) { assertThat(tp.listAssignedOtnTypeCodes()).hasSize(1); - assertThat(tp.listAssignedOtnTypeCodes().contains(b)).isTrue(); + assertThat(tp.listAssignedOtnTypeCodes()).contains(b); counter++; } else if (tp._getTypeCode().equals(d)) { assertThat(tp.listAssignedOtnTypeCodes()).isEmpty(); @@ -5330,7 +5301,7 @@ public void testShedVacancy() { TraitProxyImpl tp = (TraitProxyImpl) o; assertThat(tp._getTypeCode()).isEqualTo(d); assertThat(tp.listAssignedOtnTypeCodes()).hasSize(1); - assertThat(tp.listAssignedOtnTypeCodes().contains(b)).isTrue(); + assertThat(tp.listAssignedOtnTypeCodes()).contains(b); counter2++; } else if (o instanceof TraitableBean) { TraitableBean tb = (TraitableBean) o; @@ -5379,7 +5350,7 @@ public void testExternalUpdateWithProxyRefreshInEqualityMode() { ksession.update(handle, new ExtEntity("x1", 35)); ksession.fireAllRules(); - assertThat(list).isEqualTo(Arrays.asList("x1", 42, "x1", 42)); + assertThat(list).containsExactly("x1", 42, "x1", 42); } @@ -5412,7 +5383,7 @@ public void testIsAInstanceOf() { knowledgeSession.insert(new StudentImpl()); assertThat(knowledgeSession.fireAllRules()).isEqualTo(2); - assertThat(list).isEqualTo(Arrays.asList(1, 2)); + assertThat(list).containsExactly(1, 2); } @@ -5508,7 +5479,7 @@ public void testSerializeKieBaseWithTraits() { knowledgeSession.insert(new StudentImpl()); assertThat(knowledgeSession.fireAllRules()).isEqualTo(2); - assertThat(list).isEqualTo(Arrays.asList(1, 2)); + assertThat(list).containsExactly(1, 2); } @Test From 2acc634b3c90e6d01683376e2eae7c8248780d40 Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Sat, 14 Sep 2024 21:52:17 +0200 Subject: [PATCH 06/13] More cleanups... --- .../compiler/factmodel/traits/TraitTest.java | 148 +++++++++--------- 1 file changed, 74 insertions(+), 74 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index ec979d12490..286e4188291 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -227,21 +227,21 @@ public void testTraitShed() { ks.fireAllRules(); - assertThat(info.contains("Student")).isTrue(); + assertThat(info).contains("Student"); assertThat(info).hasSize(1); ks.insert("hire"); ks.fireAllRules(); - assertThat(info.contains("Worker")).isTrue(); + assertThat(info).contains("Worker"); assertThat(info).hasSize(2); ks.insert("check"); ks.fireAllRules(); assertThat(info).hasSize(4); - assertThat(info.contains("Conflict")).isTrue(); - assertThat(info.contains("Nothing")).isTrue(); + assertThat(info).contains("Conflict"); + assertThat(info).contains("Nothing"); } @@ -262,8 +262,8 @@ public void testTraitDon() { ks.insert("go"); ks.fireAllRules(); - assertThat(info.contains("DON")).isTrue(); - assertThat(info.contains("SHED")).isTrue(); + assertThat(info).contains("DON"); + assertThat(info).contains("SHED"); Iterator it = wm.iterator(); Object x = it.next(); @@ -287,7 +287,7 @@ public void testMixin() { ks.fireAllRules(); - assertThat(info.contains("27")).isTrue(); + assertThat(info).contains("27"); } @@ -549,14 +549,14 @@ public void testWrapperContainsKey() throws Exception { Map wrapper = proxy.getFields(); - assertThat(wrapper.containsKey("name")).isTrue(); - assertThat(wrapper.containsKey("school")).isTrue(); - assertThat(wrapper.containsKey("age")).isTrue(); - assertThat(wrapper.containsKey("surname")).isFalse(); + assertThat(wrapper).containsKey("name"); + assertThat(wrapper).containsKey("school"); + assertThat(wrapper).containsKey("age"); + assertThat(wrapper).doesNotContainKey("surname"); proxy.getFields().put("school", "skol"); proxy.getFields().put("surname", "xxx"); - assertThat(wrapper.containsKey("surname")).isTrue(); + assertThat(wrapper).containsKey("surname"); // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity"); TraitableBean ind = new Entity(); @@ -564,15 +564,15 @@ public void testWrapperContainsKey() throws Exception { TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait, false); Map wrapper2 = proxy2.getFields(); - assertThat(wrapper2.containsKey("name")).isTrue(); - assertThat(wrapper2.containsKey("school")).isTrue(); - assertThat(wrapper2.containsKey("age")).isTrue(); - assertThat(wrapper2.containsKey("surname")).isFalse(); + assertThat(wrapper2).containsKey("name"); + assertThat(wrapper2).containsKey("school"); + assertThat(wrapper2).containsKey("age"); + assertThat(wrapper2).doesNotContainKey("surname"); traitClass.set(proxy2, "name", "john"); proxy2.getFields().put("school", "skol"); proxy2.getFields().put("surname", "xxx"); - assertThat(wrapper2.containsKey("surname")).isTrue(); + assertThat(wrapper2).containsKey("surname"); FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role"); Class trait2 = traitClass2.getFactClass(); @@ -581,37 +581,37 @@ public void testWrapperContainsKey() throws Exception { TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2, false); Map wrapper99 = proxy99.getFields(); - assertThat(wrapper99.containsKey("name")).isFalse(); - assertThat(wrapper99.containsKey("school")).isFalse(); - assertThat(wrapper99.containsKey("age")).isFalse(); - assertThat(wrapper99.containsKey("surname")).isFalse(); + assertThat(wrapper99).doesNotContainKey("name"); + assertThat(wrapper99).doesNotContainKey("school"); + assertThat(wrapper99).doesNotContainKey("age"); + assertThat(wrapper99).doesNotContainKey("surname"); proxy99.getFields().put("surname", "xxx"); proxy99.getFields().put("name", "xyz"); proxy99.getFields().put("school", "skol"); - assertThat(wrapper99.containsKey("name")).isTrue(); - assertThat(wrapper99.containsKey("school")).isTrue(); - assertThat(wrapper99.containsKey("age")).isFalse(); - assertThat(wrapper99.containsKey("surname")).isTrue(); + assertThat(wrapper99).containsKey("name"); + assertThat(wrapper99).containsKey("school"); + assertThat(wrapper99).doesNotContainKey("age"); + assertThat(wrapper99).containsKey("surname"); assertThat(proxy99.getFields()).hasSize(3); TraitableBean ind0 = new Entity(); TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind0, trait2, false); Map wrapper100 = proxy100.getFields(); - assertThat(wrapper100.containsKey("name")).isFalse(); - assertThat(wrapper100.containsKey("school")).isFalse(); - assertThat(wrapper100.containsKey("age")).isFalse(); - assertThat(wrapper100.containsKey("surname")).isFalse(); + assertThat(wrapper100).doesNotContainKey("name"); + assertThat(wrapper100).doesNotContainKey("school"); + assertThat(wrapper100).doesNotContainKey("age"); + assertThat(wrapper100).doesNotContainKey("surname"); TraitProxyImpl proxy101 = (TraitProxyImpl) tFactory.getProxy(ind0, trait, false); // object gains properties by virtue of another trait // so new props are accessible even using the old proxy - assertThat(wrapper100.containsKey("name")).isTrue(); - assertThat(wrapper100.containsKey("school")).isTrue(); - assertThat(wrapper100.containsKey("age")).isTrue(); - assertThat(wrapper100.containsKey("surname")).isFalse(); + assertThat(wrapper100).containsKey("name"); + assertThat(wrapper100).containsKey("school"); + assertThat(wrapper100).containsKey("age"); + assertThat(wrapper100).doesNotContainKey("surname"); } @@ -720,10 +720,10 @@ public int compare(Object o1, Object o2) { Collections.sort(col2, comp); assertThat(col2).isEqualTo(col1); - assertThat(proxy.getFields().containsValue(null)).isTrue(); - assertThat(proxy.getFields().containsValue("john")).isTrue(); - assertThat(proxy.getFields().containsValue(0)).isTrue(); - assertThat(proxy.getFields().containsValue("xxx")).isTrue(); + assertThat(proxy.getFields()).containsValue(null); + assertThat(proxy.getFields()).containsValue("john"); + assertThat(proxy.getFields()).containsValue(0); + assertThat(proxy.getFields()).containsValue("xxx"); assertThat(proxy.getFields().containsValue("randomString")).isFalse(); assertThat(proxy.getFields().containsValue(-96)).isFalse(); @@ -771,9 +771,9 @@ public void testWrapperClearAndRemove() throws Exception { Map fields = proxy.getFields(); assertThat(fields).hasSize(3); - assertThat(fields.containsKey("age")).isTrue(); - assertThat(fields.containsKey("school")).isTrue(); - assertThat(fields.containsKey("name")).isTrue(); + assertThat(fields).containsKey("age"); + assertThat(fields).containsKey("school"); + assertThat(fields).containsKey("name"); assertThat(fields.get("age")).isEqualTo(0); assertThat(fields.get("school")).isNull(); @@ -803,13 +803,13 @@ public void testWrapperClearAndRemove() throws Exception { assertThat(proxy.getFields().get("name")).isEqualTo("john"); assertThat(proxy.getFields().get("nfield")).isEqualTo(null); - assertThat(proxy.getFields().containsKey("nfield")).isFalse(); + assertThat(proxy.getFields()).doesNotContainKey("nfield"); assertThat(proxy.getFields().get("name2")).isEqualTo(null); - assertThat(proxy.getFields().containsKey("name2")).isFalse(); + assertThat(proxy.getFields()).doesNotContainKey("name2"); assertThat(proxy.getFields().get("surname")).isEqualTo(null); - assertThat(proxy.getFields().containsKey("surname")).isFalse(); + assertThat(proxy.getFields()).doesNotContainKey("surname"); } @@ -879,7 +879,7 @@ public void testIsAEvaluator() { ks.fireAllRules(); LOGGER.debug(info.toString()); - assertThat(info.contains("ok")).isTrue(); + assertThat(info).contains("ok"); } @@ -902,7 +902,7 @@ public void testIsA() { LOGGER.debug(info.toString()); assertThat(info).hasSize(num); for (int j = 0; j < num; j++) { - assertThat(info.contains("" + j)).isTrue(); + assertThat(info).contains("" + j); } } @@ -1003,11 +1003,11 @@ public void testTraitLegacy() { printDebugInfoSessionObjects(ks.getObjects(), info); assertThat(info).hasSize(5); - assertThat(info.contains("OK")).isTrue(); - assertThat(info.contains("OK2")).isTrue(); - assertThat(info.contains("OK3")).isTrue(); - assertThat(info.contains("OK4")).isTrue(); - assertThat(info.contains("OK5")).isTrue(); + assertThat(info).contains("OK"); + assertThat(info).contains("OK2"); + assertThat(info).contains("OK3"); + assertThat(info).contains("OK4"); + assertThat(info).contains("OK5"); } @@ -1037,7 +1037,7 @@ public void testTraitCollections() { printDebugInfoSessionObjects(ks.getObjects(), info); assertThat(info).hasSize(1); - assertThat(info.contains("OK")).isTrue(); + assertThat(info).contains("OK"); } @@ -1055,8 +1055,8 @@ public void testTraitCore() { printDebugInfoSessionObjects(ks.getObjects(), info); - assertThat(info.contains("OK")).isTrue(); - assertThat(info.contains("OK2")).isTrue(); + assertThat(info).contains("OK"); + assertThat(info).contains("OK2"); assertThat(info).hasSize(2); } @@ -1073,8 +1073,8 @@ public void traitWithEquality() { ks.fireAllRules(); - assertThat(info.contains("DON")).isTrue(); - assertThat(info.contains("EQUAL")).isTrue(); + assertThat(info).contains("DON"); + assertThat(info).contains("EQUAL"); } @@ -1093,9 +1093,9 @@ public void traitDeclared() { ks.fireAllRules(); ks.dispose(); - assertThat(trueTraits.contains(1)).isTrue(); + assertThat(trueTraits).contains(1); assertThat(trueTraits.contains(2)).isFalse(); - assertThat(untrueTraits.contains(2)).isTrue(); + assertThat(untrueTraits).contains(2); assertThat(untrueTraits.contains(1)).isFalse(); } @@ -1114,9 +1114,9 @@ public void traitPojo() { session.fireAllRules(); session.dispose(); - assertThat(trueTraits.contains(1)).isTrue(); + assertThat(trueTraits).contains(1); assertThat(trueTraits.contains(2)).isFalse(); - assertThat(untrueTraits.contains(2)).isTrue(); + assertThat(untrueTraits).contains(2); assertThat(untrueTraits.contains(1)).isFalse(); } @@ -1201,7 +1201,7 @@ public void testManyTraits() { ksession.fireAllRules(); assertThat(list).hasSize(1); - assertThat(list.contains("OK")).isTrue(); + assertThat(list).contains("OK"); } @@ -1222,10 +1222,10 @@ public void traitManyTimes() { assertThat(list).hasSize(5); assertThat(list.get(0)).isEqualTo(0); - assertThat(list.contains(1)).isTrue(); - assertThat(list.contains(2)).isTrue(); - assertThat(list.contains(3)).isTrue(); - assertThat(list.contains(4)).isTrue(); + assertThat(list).contains(1); + assertThat(list).contains(2); + assertThat(list).contains(3); + assertThat(list).contains(4); } // BZ #748752 @@ -1294,8 +1294,8 @@ public void traitsInBatchExecution() { LOGGER.debug("Finished..."); assertThat(list).hasSize(2); - assertThat(list.contains(1)).isTrue(); - assertThat(list.contains(2)).isTrue(); + assertThat(list).contains(1); + assertThat(list).contains(2); } @Test(timeout=10000) @@ -1351,7 +1351,7 @@ public void testManyTraitsStateless() { ksession.execute(CommandFactory.newFireAllRules()); assertThat(list).hasSize(1); - assertThat(list.contains("OK")).isTrue(); + assertThat(list).contains("OK"); } @@ -1418,13 +1418,13 @@ public void testAliasing() { ksession.fireAllRules(); assertThat(list).hasSize(9); - assertThat(list.contains("UniBoh")).isTrue(); - assertThat(list.contains("Skool")).isTrue(); + assertThat(list).contains("UniBoh"); + assertThat(list).contains("Skool"); assertThat((Collection) list.get(3)).contains("workPlace", "nomen", "level"); assertThat((Collection) list.get(5)).contains("davide", "Skool", 0); - assertThat(list.contains(true)).isTrue(); - assertThat(list.contains("Floor84")).isTrue(); - assertThat(list.contains(99)).isTrue(); + assertThat(list).contains(true); + assertThat(list).contains("Floor84"); + assertThat(list).contains(99); } @@ -2269,7 +2269,7 @@ public void testTraitModifyCore3() { assertThat(list).hasSize(14); for (int j = 1; j <= 14; j++) { - assertThat(list.contains(j)).isTrue(); + assertThat(list).contains(j); } @@ -3888,7 +3888,7 @@ public void testMultipleModifications() { ks.fireAllRules(); - assertThat(list.contains(1)).isTrue(); + assertThat(list).contains(1); assertThat(list).hasSize(1); ks.dispose(); From 0b45904dd6587d798bb716f725b8fa8c1db66a8c Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Wed, 18 Sep 2024 15:28:50 +0200 Subject: [PATCH 07/13] More cleanups, progressing --- .../TraitFieldsAndLegacyClassesTest.java | 100 +++++++++--------- .../compiler/factmodel/traits/TraitTest.java | 34 +++--- 2 files changed, 66 insertions(+), 68 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitFieldsAndLegacyClassesTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitFieldsAndLegacyClassesTest.java index dbee7e2a6cc..a1b24c23664 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitFieldsAndLegacyClassesTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitFieldsAndLegacyClassesTest.java @@ -47,12 +47,8 @@ public class TraitFieldsAndLegacyClassesTest extends CommonTraitTest { public VirtualPropertyMode mode; @Parameterized.Parameters - public static Collection modes() { - return Arrays.asList( new VirtualPropertyMode[][] - { - { VirtualPropertyMode.MAP }, - { VirtualPropertyMode.TRIPLES } - } ); + public static Collection modes() { + return List.of(VirtualPropertyMode.MAP , VirtualPropertyMode.TRIPLES ); } public TraitFieldsAndLegacyClassesTest( VirtualPropertyMode m ) { @@ -114,12 +110,13 @@ public void testTraitFieldUpdate0() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @@ -202,13 +199,14 @@ public void testTraitFieldUpdate1() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @@ -301,13 +299,13 @@ public void testTraitFieldUpdate2() { KieSession knowledgeSession = kBase.newKieSession(); TraitFactoryImpl.setMode(mode, kBase ); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @Test @@ -396,13 +394,14 @@ public void testTraitFieldUpdate3() { KieBase kBase = loadKnowledgeBaseFromString(drl); TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @Category(ReviseTraitTestWithPRAlwaysCategory.class) @@ -475,13 +474,13 @@ public void testTraitFieldUpdate4() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @Test @@ -559,13 +558,13 @@ public void testTraitFieldUpdate5() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @Test @@ -649,15 +648,14 @@ public void testTraitFieldUpdate6() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.contains("correct2")).isTrue(); - assertThat(list.size()).isEqualTo(2); + assertThat(list).hasSize(2); + assertThat(list).contains("correct", "correct2"); } @@ -733,13 +731,13 @@ public void testTraitFieldUpdate7() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @@ -818,13 +816,13 @@ public void testTraitFieldUpdate8() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @Test @@ -909,13 +907,13 @@ public void testTraitFieldUpdate9() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @Test @@ -1001,13 +999,13 @@ public void testTraitFieldUpdate10() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @Test @@ -1112,13 +1110,13 @@ public void testTraitTwoParentOneChild() { TraitFactoryImpl.setMode(mode, kBase ); KieSession knowledgeSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); knowledgeSession.setGlobal("list", list); knowledgeSession.fireAllRules(); - assertThat(list.contains("correct")).isTrue(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); + assertThat(list).contains("correct"); } @Test @Ignore @@ -1245,18 +1243,18 @@ public void testTraitWithPositionArgs(){ TraitFactoryImpl.setMode(mode, kBase ); KieSession kSession = kBase.newKieSession(); - List list = new ArrayList(); + List list = new ArrayList<>(); kSession.setGlobal("list", list); kSession.fireAllRules(); - assertThat(list.contains("initialized")).isTrue(); - assertThat(list.contains("student")).isTrue(); - assertThat(list.contains("IR citizen")).isTrue(); - assertThat(list.contains("US citizen")).isTrue(); - assertThat(list.contains("worker")).isTrue(); - assertThat(list.contains("You are working in US as student worker")).isTrue(); - assertThat(list.contains("You are studying and working at ASU")).isTrue(); + assertThat(list).contains("initialized"); + assertThat(list).contains("student"); + assertThat(list).contains("IR citizen"); + assertThat(list).contains("US citizen"); + assertThat(list).contains("worker"); + assertThat(list).contains("You are working in US as student worker"); + assertThat(list).contains("You are studying and working at ASU"); } diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index 286e4188291..dba1a89e457 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -724,8 +724,8 @@ public int compare(Object o1, Object o2) { assertThat(proxy.getFields()).containsValue("john"); assertThat(proxy.getFields()).containsValue(0); assertThat(proxy.getFields()).containsValue("xxx"); - assertThat(proxy.getFields().containsValue("randomString")).isFalse(); - assertThat(proxy.getFields().containsValue(-96)).isFalse(); + assertThat(proxy.getFields()).doesNotContainValue("randomString"); + assertThat(proxy.getFields()).doesNotContainValue(-96); } @@ -1094,9 +1094,9 @@ public void traitDeclared() { ks.dispose(); assertThat(trueTraits).contains(1); - assertThat(trueTraits.contains(2)).isFalse(); + assertThat(trueTraits).doesNotContain(2); assertThat(untrueTraits).contains(2); - assertThat(untrueTraits.contains(1)).isFalse(); + assertThat(untrueTraits).doesNotContain(1); } @Test @@ -1115,9 +1115,9 @@ public void traitPojo() { session.dispose(); assertThat(trueTraits).contains(1); - assertThat(trueTraits.contains(2)).isFalse(); + assertThat(trueTraits).doesNotContain(2); assertThat(untrueTraits).contains(2); - assertThat(untrueTraits.contains(1)).isFalse(); + assertThat(untrueTraits).doesNotContain(1); } @Test @@ -2640,7 +2640,7 @@ public void testTraitInitialization() { ks.fireAllRules(); assertThat(list).hasSize(6); - assertThat(list.contains(null)).isFalse(); + assertThat(list).doesNotContainNull(); List hard = (List) list.get(0); List soft = (List) list.get(1); @@ -3113,7 +3113,7 @@ public void testTypeRefractionOnQueryWithIsA() { QueryResults res = ks.getQueryResults("queryA"); Iterator iter = res.iterator(); Object a = iter.next().get("$x"); - assertThat(iter.hasNext()).isFalse(); + assertThat(iter).isExhausted(); assertThat(res).hasSize(1); @@ -4046,7 +4046,7 @@ public void testTraitLogicalTMS() { ks.insert("go3"); ks.fireAllRules(); - assertThat(list).isEqualTo(List.of(100)); + assertThat(list).containsExactly(100); ks.dispose(); } @@ -4407,7 +4407,7 @@ public void testDonManyTraitsAtOnce2() { ksession.fireAllRules(); - assertThat(list).isEqualTo(List.of(0)); + assertThat(list).containsExactly(0); assertThat(cwm.getdeletes()).isEqualTo(0); assertThat(cwm.getInserts()).isEqualTo(3); @@ -4548,7 +4548,7 @@ public void testShedOneLastTrait() throws InterruptedException { knowledgeSession.fireAllRules(); assertThat(list).hasSize(1); - assertThat(list).isEqualTo(List.of("test.Mask.test.Core_Proxy")); + assertThat(list).containsExactly("test.Mask.test.Core_Proxy"); knowledgeSession.insert("shed"); knowledgeSession.fireAllRules(); @@ -4624,13 +4624,13 @@ public void testShedThingCompletelyThenDonAgain() throws InterruptedException { knowledgeSession.fireAllRules(); assertThat(list).hasSize(1); - assertThat(list).isEqualTo(List.of("test.Mask.test.Core_Proxy")); + assertThat(list).containsExactly("test.Mask.test.Core_Proxy"); knowledgeSession.insert("shed1"); knowledgeSession.fireAllRules(); assertThat(list).hasSize(1); - assertThat(list).isEqualTo(List.of("test.Mask.test.Core_Proxy")); + assertThat(list).containsExactly("test.Mask.test.Core_Proxy"); knowledgeSession.insert("don2"); knowledgeSession.fireAllRules(); @@ -4751,7 +4751,7 @@ public void testIsALegacyTrait() { knowledgeSession.fireAllRules(); - assertThat(list).isEqualTo(List.of(1)); + assertThat(list).containsExactly(1); } @Category(ReviseTraitTestWithPRAlwaysCategory.class) @@ -4809,7 +4809,7 @@ public void testClassLiteralsWithOr() { ksession.fireAllRules(); - assertThat(list).isEqualTo(List.of(1)); + assertThat(list).containsExactly(1); } @@ -5409,7 +5409,7 @@ public void testIsAInstanceOfNonTraitable() { knowledgeSession.insert("hello"); assertThat(knowledgeSession.fireAllRules()).isEqualTo(1); - assertThat(list).isEqualTo(List.of(1)); + assertThat(list).containsExactly(1); } @@ -5441,7 +5441,7 @@ protected Set checkOTNPartitioning(TraitableBean core, KieSession wm) { Set localNodes = tp.listAssignedOtnTypeCodes(); for (BitSet code : localNodes) { - assertThat(otns.contains(code)).isFalse(); + assertThat(otns).doesNotContain(code); otns.add(code); } } From 937b2b4428b92dd274623381aa18545b8095db26 Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Wed, 18 Sep 2024 15:47:29 +0200 Subject: [PATCH 08/13] Fixed size assertions --- .../factmodel/traits/LogicalTraitTest.java | 12 ++-- .../factmodel/traits/TraitMapCoreTest.java | 63 ++++++++----------- .../TraitTypeGenerationTest.java | 20 +++--- .../core/factmodel/TripleStoreTest.java | 14 ++--- .../traits/core/util/HierarchyTest.java | 4 +- 5 files changed, 48 insertions(+), 65 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java index b168317bbea..508e4df5090 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java @@ -67,12 +67,8 @@ public class LogicalTraitTest extends CommonTraitTest { public VirtualPropertyMode mode; @Parameterized.Parameters - public static Collection modes() { - return Arrays.asList( new VirtualPropertyMode[][] - { - { VirtualPropertyMode.MAP }, - { VirtualPropertyMode.TRIPLES } - } ); + public static Collection modes() { + return List.of(VirtualPropertyMode.MAP, VirtualPropertyMode.TRIPLES); } public LogicalTraitTest( VirtualPropertyMode m ) { @@ -1178,7 +1174,7 @@ public void testSerial() { TraitFactoryImpl.setMode(mode, (InternalRuleBase) kbase); KieSession ks = kbase.newKieSession(); - ArrayList list = new ArrayList(); + List list = new ArrayList(); ks.setGlobal( "list", list ); ks.fireAllRules(); @@ -1350,7 +1346,7 @@ public void testTraitMismatchTypes3() KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(mode, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list",list); ksession.fireAllRules(); diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java index 97f8a3ac4da..32ddf8dc43e 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java @@ -138,7 +138,7 @@ public void donMapTest() { List list = new ArrayList(); ksession.setGlobal( "list", list ); - Map map = new HashMap(); + Map map = new HashMap<>(); map.put( "name", "john" ); map.put( "age", 18 ); @@ -363,7 +363,7 @@ public void testMapCoreAliasingLogicalTrueWithTypeClash( ) { KieSession ks = loadKnowledgeBaseFromString( source ).newKieSession(); TraitFactoryImpl.setMode(VirtualPropertyMode.MAP, ks.getKieBase() ); - List list = new ArrayList(); + List list = new ArrayList<>(); ks.setGlobal( "list", list ); Map map = new HashMap( ); @@ -373,7 +373,8 @@ public void testMapCoreAliasingLogicalTrueWithTypeClash( ) { ks.fireAllRules(); - assertThat(list.size() == 1 && list.get(0) == null).isTrue(); + assertThat(list).hasSize(1); + assertThat(list.get(0)).isNull(); } @Test @@ -454,13 +455,11 @@ public void testDrools216(){ KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(VirtualPropertyMode.MAP, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal( "list", list ); ksession.fireAllRules(); - assertThat(list.contains("initialized")).isTrue(); - assertThat(list.contains("student is donned")).isTrue(); - assertThat(list.contains("worker is donned")).isTrue(); + assertThat(list).contains("initialized", "student is donned", "worker is donned"); } @@ -541,13 +540,11 @@ public void testDrools217(){ KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(VirtualPropertyMode.MAP, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list", list); ksession.fireAllRules(); - assertThat(list.contains("initialized")).isTrue(); - assertThat(list.contains("student is donned")).isTrue(); - assertThat(list.contains("worker is donned")).isTrue(); + assertThat(list).contains("initialized", "student is donned", "worker is donned"); } @Test @@ -656,15 +653,11 @@ public void testDrools218(){ KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(VirtualPropertyMode.MAP, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal( "list", list ); ksession.fireAllRules(); - assertThat(list.contains("initialized")).isTrue(); - assertThat(list.contains("student is donned")).isTrue(); - assertThat(list.contains("worker is donned")).isTrue(); - assertThat(list.contains("studentworker is donned")).isTrue(); - assertThat(list.contains("tuitionWaiver is true")).isTrue(); + assertThat(list).contains("initialized", "student is donned", "worker is donned", "studentworker is donned", "tuitionWaiver is true"); } @Test @@ -758,16 +751,12 @@ public void testDrools219(){ KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(VirtualPropertyMode.MAP, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal( "list", list ); ksession.fireAllRules(); - assertThat(list.contains("initialized")).isTrue(); - assertThat(list.contains("student is donned")).isTrue(); - assertThat(list.contains("student has ID and SSN")).isTrue(); - assertThat(list.contains("student has personID and socialSecurity")).isTrue(); - assertThat(list.contains("citizen has socialSecurity")).isTrue(); - assertThat(list.contains("person has personID")).isTrue(); + assertThat(list).contains("initialized", "student is donned", "student has ID and SSN", + "student has personID and socialSecurity", "citizen has socialSecurity", "person has personID"); } @Test @@ -827,7 +816,7 @@ public void testMapTraitsMismatchTypes() ksession.insert( map ); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(2); + assertThat(list).hasSize(2); assertThat(list.get(0)).isNull(); assertThat(list.get(1)).isNotNull(); } @@ -874,15 +863,14 @@ public void testMapTraitNoType() KieSession ksession = loadKnowledgeBaseFromString(drl).newKieSession(); TraitFactoryImpl.setMode(VirtualPropertyMode.MAP, ksession.getKieBase()); - List list = new ArrayList(); + List list = new ArrayList<>(); ksession.setGlobal("list",list); Map map = new HashMap(); // map.put("name", "hulu"); ksession.insert(map); ksession.fireAllRules(); - assertThat(list.contains("correct1")).isTrue(); - assertThat(list.contains("correct2")).isTrue(); + assertThat(list).contains("correct1", "correct2"); } @Test(timeout=10000) @@ -930,7 +918,7 @@ public void testMapTraitMismatchTypes() ksession.insert(map); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list.get(0)).isEqualTo(null); } @@ -995,7 +983,7 @@ public void testMapTraitPossibilities1() ksession.setGlobal("list",list); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list.get(0)).isNotNull(); } @@ -1064,7 +1052,7 @@ public void testMapTraitPossibilities2() ksession.setGlobal("list",list); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list.get(0)).isNotNull(); } @@ -1133,7 +1121,7 @@ public void testMapTraitPossibilities3() ksession.setGlobal("list",list); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list.get(0)).isNotNull(); } @@ -1205,7 +1193,7 @@ public void testMapTraitPossibilities4() ksession.setGlobal("list",list); ksession.fireAllRules(); - assertThat(list.size()).isEqualTo(1); + assertThat(list).hasSize(1); assertThat(list.get(0)).isNotNull(); } @@ -1248,18 +1236,17 @@ public void donCustomMapTest() { List list = new ArrayList(); ksession.setGlobal( "list", list ); - HashMap map = new DomainMap(); + Map map = new DomainMap(); map.put( "name", "john" ); map.put( "age", 18 ); ksession.insert( map ); ksession.fireAllRules(); - assertThat(map.containsKey("height")).isTrue(); - assertThat(184.0).isEqualTo(map.get("height")); + assertThat(map).containsEntry("height", 184.0); - assertThat(ksession.getObjects().size()).isEqualTo(2); - assertThat(ksession.getObjects(new ClassObjectFilter( DomainMap.class )).size()).isEqualTo(1); + assertThat(ksession.getObjects()).hasSize(2); + assertThat(ksession.getObjects(new ClassObjectFilter( DomainMap.class ))).hasSize(1); } diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/integrationtests/TraitTypeGenerationTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/integrationtests/TraitTypeGenerationTest.java index b4f647b526c..83da5692de6 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/integrationtests/TraitTypeGenerationTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/integrationtests/TraitTypeGenerationTest.java @@ -194,7 +194,7 @@ public void testIsAWith2KContainers() { KieBuilder kbuilder = ks.newKieBuilder(kfs ); kbuilder.buildAll(); - assertThat(kbuilder.getResults().getMessages().size()).isEqualTo(0); + assertThat(kbuilder.getResults().getMessages()).isEmpty(); ks.newKieContainer( kbuilder.getKieModule().getReleaseId() ).getKieBase(); @@ -205,7 +205,7 @@ public void testIsAWith2KContainers() { ksession.setGlobal( "students", students ); ksession.insert( new Person("tom", 20 ) ); ksession.fireAllRules(); - assertThat(students.size()).isEqualTo(1); + assertThat(students).hasSize(1); } public static interface FooIntf { @@ -347,7 +347,7 @@ public void testRedeclareClassAsTrait() { KieHelper kh = new KieHelper(); kh.addContent( s1, ResourceType.DRL ); - assertThat(kh.verify().getMessages(Message.Level.ERROR).size()).isEqualTo(1); + assertThat(kh.verify().getMessages(Message.Level.ERROR)).hasSize(1); } public static class SomeClass {} @@ -381,7 +381,7 @@ public void testMultipleInheritanceWithPosition1() throws Exception { knowledgeBase.addPackages(kBuilder.getKnowledgePackages()); FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait"); - assertThat(sw.getFields().size()).isEqualTo(5); + assertThat(sw.getFields()).hasSize(5); assertThat(sw.getFields().get(0).getName()).isEqualTo("field0"); assertThat(sw.getFields().get(1).getName()).isEqualTo("field1"); assertThat(sw.getFields().get(2).getName()).isEqualTo("mfield1"); @@ -417,7 +417,7 @@ public void testMultipleInheritanceWithPosition2() throws Exception { knowledgeBase.addPackages(kBuilder.getKnowledgePackages()); FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait"); - assertThat(sw.getFields().size()).isEqualTo(5); + assertThat(sw.getFields()).hasSize(5); assertThat(sw.getFields().get(0).getName()).isEqualTo("mfield0"); assertThat(sw.getFields().get(1).getName()).isEqualTo("field0"); assertThat(sw.getFields().get(2).getName()).isEqualTo("mfield1"); @@ -453,7 +453,7 @@ public void testMultipleInheritanceWithPosition3() throws Exception { knowledgeBase.addPackages(kBuilder.getKnowledgePackages()); FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait"); - assertThat(sw.getFields().size()).isEqualTo(5); + assertThat(sw.getFields()).hasSize(5); assertThat(sw.getFields().get(0).getName()).isEqualTo("field0"); assertThat(sw.getFields().get(1).getName()).isEqualTo("field2"); assertThat(sw.getFields().get(2).getName()).isEqualTo("field1"); @@ -489,7 +489,7 @@ public void testMultipleInheritanceWithPosition4() throws Exception { knowledgeBase.addPackages(kBuilder.getKnowledgePackages()); FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait"); - assertThat(sw.getFields().size()).isEqualTo(5); + assertThat(sw.getFields()).hasSize(5); assertThat(sw.getFields().get(0).getName()).isEqualTo("field1"); assertThat(sw.getFields().get(1).getName()).isEqualTo("field2"); assertThat(sw.getFields().get(2).getName()).isEqualTo("mfield0"); @@ -525,7 +525,7 @@ public void testMultipleInheritanceWithPosition5() throws Exception { knowledgeBase.addPackages(kBuilder.getKnowledgePackages()); FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait"); - assertThat(sw.getFields().size()).isEqualTo(5); + assertThat(sw.getFields()).hasSize(5); assertThat(sw.getFields().get(0).getName()).isEqualTo("field1"); assertThat(sw.getFields().get(1).getName()).isEqualTo("field0"); assertThat(sw.getFields().get(2).getName()).isEqualTo("mfield1"); @@ -567,7 +567,7 @@ public void testMultipleInheritanceWithPosition6() throws Exception { knowledgeBase.addPackages(kBuilder.getKnowledgePackages()); FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait"); - assertThat(sw.getFields().size()).isEqualTo(6); + assertThat(sw.getFields()).hasSize(6); assertThat(sw.getFields().get(0).getName()).isEqualTo("field0"); assertThat(sw.getFields().get(1).getName()).isEqualTo("field1"); assertThat(sw.getFields().get(2).getName()).isEqualTo("field2"); @@ -610,7 +610,7 @@ public void testMultipleInheritanceWithPosition7() throws Exception { knowledgeBase.addPackages(kBuilder.getKnowledgePackages()); FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait"); - assertThat(sw.getFields().size()).isEqualTo(6); + assertThat(sw.getFields()).hasSize(6); assertThat(sw.getFields().get(0).getName()).isEqualTo("field0"); assertThat(sw.getFields().get(1).getName()).isEqualTo("field2"); assertThat(sw.getFields().get(2).getName()).isEqualTo("mfield0"); diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java index 6626e0771f6..4b644280e43 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java @@ -184,30 +184,30 @@ public void testQueryVariable() { tKey = new TripleImpl( V, "hasCity", V ); coll = store.getAll(tKey); assertThat(coll.containsAll(Arrays.asList(t3, t4))).isTrue(); - assertThat(coll.size()).isEqualTo(2); + assertThat(coll).hasSize(2); tKey = new TripleImpl( ind, V, V ); coll = store.getAll(tKey); assertThat(coll.containsAll(Arrays.asList(t1, t2, t3))).isTrue(); - assertThat(coll.size()).isEqualTo(3); + assertThat(coll).hasSize(3); tKey = new TripleImpl( ind2, V, "lancia" ); coll = store.getAll(tKey); assertThat(coll.containsAll(Arrays.asList(t5, t6))).isTrue(); - assertThat(coll.size()).isEqualTo(2); + assertThat(coll).hasSize(2); tKey = new TripleImpl( V, V, "lancia" ); coll = store.getAll(tKey); assertThat(coll.containsAll(Arrays.asList(t5, t6))).isTrue(); - assertThat(coll.size()).isEqualTo(2); + assertThat(coll).hasSize(2); tKey = new TripleImpl( V, V, V ); coll = store.getAll(tKey); assertThat(coll.containsAll(Arrays.asList(t1, t2, t3, t4, t5, t6))).isTrue(); - assertThat(coll.size()).isEqualTo(6); + assertThat(coll).hasSize(6); } @@ -272,13 +272,13 @@ public void testAddNary() { tKey = new TripleImpl( ind, "hasCar", V ); coll = store.getAll(tKey); - assertThat(coll.size()).isEqualTo(1); + assertThat(coll).hasSize(1); store.remove( new TripleImpl(ind, "hasCar", "ferrari") ); tKey = new TripleImpl( ind, "hasCar", V ); coll = store.getAll(tKey); - assertThat(coll.size()).isEqualTo(0); + assertThat(coll).isEmpty(); } diff --git a/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java b/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java index 1c1b9c3fc16..1bc8fc765cb 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java @@ -75,8 +75,8 @@ public void testHierManyRoots() { assertThat(encoder.getCode("E")).isEqualTo(parseBitSet("10000")); assertThat(encoder.size()).isEqualTo(5); - assertThat(encoder.getSortedMembers().size()).isEqualTo(5); - assertThat(encoder.getSortedMap().size()).isEqualTo(5); + assertThat(encoder.getSortedMembers()).hasSize(5); + assertThat(encoder.getSortedMap()).hasSize(5); } From 7d061c814de3028c2890cce5286abd3ebcaa39e1 Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Wed, 18 Sep 2024 15:58:17 +0200 Subject: [PATCH 09/13] Fixed instanceof in tests --- .../factmodel/traits/LogicalTraitTest.java | 4 ++-- .../factmodel/traits/StandaloneTest.java | 2 +- .../compiler/factmodel/traits/TraitTest.java | 22 +++++++++---------- .../traits/core/meta/org/MetadataTest.java | 2 +- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java index 508e4df5090..4aa2664ec93 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java @@ -998,7 +998,7 @@ public void testShadowAliasTraitOnClassLogicalRetract() { ks.fireAllRules(); for ( Object o : ks.getObjects( new ClassObjectFilter( ks.getKieBase().getFactType( "org.drools.test", "Y" ).getFactClass() ) ) ) { - assertThat(o instanceof TraitableBean).isTrue(); + assertThat(o).isInstanceOf(TraitableBean.class); TraitableBean tb = (TraitableBean) o; TraitField fld = tb._getFieldTMS().getRegisteredTraitField("fld" ); @@ -1118,7 +1118,7 @@ public void testShadowAliasClassOnTraitLogicalRetract() { ks.fireAllRules(); for ( Object o : ks.getObjects( new ClassObjectFilter( ks.getKieBase().getFactType( "org.drools.test", "Y" ).getFactClass() ) ) ) { - assertThat(o instanceof TraitableBean).isTrue(); + assertThat(o).isInstanceOf(TraitableBean.class); } try { diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/StandaloneTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/StandaloneTest.java index 992ccdb8907..f3c3b352cd3 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/StandaloneTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/StandaloneTest.java @@ -100,7 +100,7 @@ public void testLegacy() throws LogicalTypeInconsistencyException { LOGGER.debug( "Is foo instance of Thing? : " + (foo instanceof Thing) ); assertThat(foo.getName()).isEqualTo("john doe"); - assertThat(foo instanceof Thing).isTrue(); + assertThat(foo).isInstanceOf(Thing.class); } diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index dba1a89e457..99ff352389a 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -647,15 +647,15 @@ public void testInternalComponents1() throws Exception { assertThat(coreProperties).isNotNull(); if (mode == VirtualPropertyMode.MAP) { - assertThat(proxyFields instanceof MapWrapper).isTrue(); - assertThat(coreTraits instanceof TraitTypeMapImpl).isTrue(); - assertThat(coreProperties instanceof HashMap).isTrue(); + assertThat(proxyFields).isInstanceOf(MapWrapper.class); + assertThat(coreTraits).isInstanceOf(TraitTypeMapImpl.class); + assertThat(coreProperties).isInstanceOf(HashMap.class); } else { assertThat(proxyFields.getClass().getName()).isEqualTo("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper"); - assertThat(proxyFields instanceof TripleBasedStruct).isTrue(); - assertThat(coreTraits instanceof TraitTypeMapImpl).isTrue(); - assertThat(coreProperties instanceof TripleBasedBean).isTrue(); + assertThat(proxyFields).isInstanceOf(TripleBasedStruct.class); + assertThat(coreTraits).isInstanceOf(TraitTypeMapImpl.class); + assertThat(coreProperties).isInstanceOf(TripleBasedBean.class); } @@ -923,7 +923,7 @@ public void testOverrideType() { ks.fireAllRules(); fail("An exception was expected since a trait can't override the type of a core class field with these settings "); } catch (Throwable rde) { - assertThat(rde.getCause() instanceof UnsupportedOperationException).isTrue(); + assertThat(rde.getCause()).isInstanceOf(UnsupportedOperationException.class); } } @@ -953,7 +953,7 @@ public void testOverrideType2() { ks.fireAllRules(); fail("An exception was expected since a trait can't override the type of a core class field with these settings "); } catch (Throwable rde) { - assertThat(rde.getCause() instanceof UnsupportedOperationException).isTrue(); + assertThat(rde.getCause()).isInstanceOf(UnsupportedOperationException.class); } } @@ -982,7 +982,7 @@ public void testOverrideType3() { ks.fireAllRules(); fail("An exception was expected since a trait can't override the type of a core class field with these settings "); } catch (Throwable rde) { - assertThat(rde.getCause() instanceof UnsupportedOperationException).isTrue(); + assertThat(rde.getCause()).isInstanceOf(UnsupportedOperationException.class); } } @@ -4677,7 +4677,7 @@ public void testTraitImplicitInsertionExceptionOnNonTraitable() throws Interrupt knowledgeSession.fireAllRules(); fail("Core is not declared @Traitable, this test should have thrown an exception"); } catch (Exception csq) { - assertThat(csq.getCause() instanceof IllegalStateException).isTrue(); + assertThat(csq.getCause()).isInstanceOf(IllegalStateException.class); } } @@ -5213,7 +5213,7 @@ public void testTupleIntegrityOnModification() { for (final Object o : ksession.getObjects(object -> object.getClass().getName().contains("test.A"))) { InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle(o); TupleImpl first = handle.getFirstLeftTuple(); - assertThat(first instanceof RuleTerminalNodeLeftTuple).isTrue(); + assertThat(first).isInstanceOf(RuleTerminalNodeLeftTuple.class); assertThat(((RuleTerminalNodeLeftTuple) first).getRule().getName()).isEqualTo("Check"); } diff --git a/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java b/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java index 9f1bedb007c..04a19195b71 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java @@ -195,7 +195,7 @@ public void testURIs() { public void testNewInstance() { Klass klass = Klass_.newKlass( URI.create( "test" ) ).call(); assertThat(klass).isNotNull(); - assertThat(klass instanceof KlassImpl).isTrue(); + assertThat(klass).isInstanceOf(KlassImpl.class); SubKlass klass2 = SubKlass_.newSubKlass( URI.create( "test2" ) ).subProp( 42 ).prop( "hello" ).call(); From 45d9e5323bf148c4490f5e7b258ec80311742fb2 Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Wed, 18 Sep 2024 16:07:13 +0200 Subject: [PATCH 10/13] Fixed equals in assertj --- .../core/factmodel/ClassBuilderTest.java | 284 ++++++------------ 1 file changed, 88 insertions(+), 196 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java index 1440cdd373b..30b660104a0 100755 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java @@ -9,8 +9,7 @@ * * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an + * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations @@ -79,14 +78,10 @@ public void testBuildClass() { try { ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass1", - null, - new String[]{"java.io.Serializable"} ); - FieldDefinition intDef = new FieldDefinition("intAttr", - "int" ); + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass1", null, new String[]{"java.io.Serializable"} ); + FieldDefinition intDef = new FieldDefinition("intAttr", "int" ); - FieldDefinition stringDef = new FieldDefinition( "stringAttr", - "java.lang.String" );//"java.lang.String" ); + FieldDefinition stringDef = new FieldDefinition( "stringAttr", "java.lang.String" );//"java.lang.String" ); classDef.addField( intDef ); classDef.addField( stringDef ); @@ -94,10 +89,8 @@ public void testBuildClass() { - intDef.setReadWriteAccessor( store.getAccessor( clazz, - intDef.getName() ) ); - stringDef.setReadWriteAccessor( store.getAccessor( clazz, - stringDef.getName() ) ); + intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); + stringDef.setReadWriteAccessor( store.getAccessor( clazz, stringDef.getName() ) ); byte[] d = builder.buildClass( classDef, classLoader ); @@ -107,13 +100,11 @@ public void testBuildClass() { Serializable instance = (Serializable) clazz.newInstance(); String stringValue = "Atributo String ok"; - stringDef.setValue( instance, - stringValue ); + stringDef.setValue( instance, stringValue ); assertThat(stringDef.getValue(instance)).as("Attribute should have been correctly set").isEqualTo(stringValue); int intValue = 50; - intDef.setValue( instance, - new Integer( intValue ) ); + intDef.setValue( instance, new Integer( intValue ) ); assertThat(((Integer) intDef.getValue(instance)).intValue()).as("Attribute should have been correctly set").isEqualTo(intValue); // testing class rebuilding @@ -131,8 +122,7 @@ public void testBuildClass() { * @throws FileNotFoundException * @throws IOException */ - private void writeJar(byte[] data) throws FileNotFoundException, - IOException { + private void writeJar(byte[] data) throws FileNotFoundException, IOException { FileOutputStream out = new FileOutputStream("/Users/michaelneale/edson.jar"); JarOutputStream jout = new JarOutputStream( out ); JarEntry je = new JarEntry( "br/com/auster/TestClass1.class" ); @@ -147,29 +137,14 @@ public void testEquals() { try { ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass2", - null, - new String[]{} ); - FieldDefinition long1Def = new FieldDefinition( "longAttr1", - "long", - true ); - FieldDefinition long2Def = new FieldDefinition( "longAttr2", - "long", - true ); - FieldDefinition doubleDef = new FieldDefinition( "doubleAttr", - "double", - true ); - FieldDefinition intDef = new FieldDefinition( "intAttr", - "int", - true ); - FieldDefinition strDef = new FieldDefinition( "stringAttr", - "java.lang.String", - true ); - FieldDefinition dateDef = new FieldDefinition( "dateAttr", - "java.util.Date", - true ); - FieldDefinition str2Def = new FieldDefinition( "stringAttr2", - "java.lang.String" ); + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass2", null, new String[]{} ); + FieldDefinition long1Def = new FieldDefinition( "longAttr1", "long", true ); + FieldDefinition long2Def = new FieldDefinition( "longAttr2", "long", true ); + FieldDefinition doubleDef = new FieldDefinition( "doubleAttr", "double", true ); + FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); + FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", true ); + FieldDefinition dateDef = new FieldDefinition( "dateAttr", "java.util.Date", true ); + FieldDefinition str2Def = new FieldDefinition( "stringAttr2", "java.lang.String" ); classDef.addField( long1Def ); classDef.addField( long2Def ); classDef.addField( doubleDef ); @@ -179,85 +154,57 @@ public void testEquals() { classDef.addField( str2Def ); Class clazz = build(builder, classDef); - long1Def.setReadWriteAccessor( store.getAccessor( clazz, - long1Def.getName() ) ); - long2Def.setReadWriteAccessor( store.getAccessor( clazz, - long2Def.getName() ) ); - doubleDef.setReadWriteAccessor( store.getAccessor( clazz, - doubleDef.getName() ) ); - intDef.setReadWriteAccessor( store.getAccessor( clazz, - intDef.getName() ) ); - strDef.setReadWriteAccessor( store.getAccessor( clazz, - strDef.getName( ) ) ); - dateDef.setReadWriteAccessor( store.getAccessor( clazz, - dateDef.getName() ) ); - str2Def.setReadWriteAccessor( store.getAccessor( clazz, - str2Def.getName() ) ); + long1Def.setReadWriteAccessor( store.getAccessor( clazz, long1Def.getName() ) ); + long2Def.setReadWriteAccessor( store.getAccessor( clazz, long2Def.getName() ) ); + doubleDef.setReadWriteAccessor( store.getAccessor( clazz, doubleDef.getName() ) ); + intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); + strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName( ) ) ); + dateDef.setReadWriteAccessor( store.getAccessor( clazz, dateDef.getName() ) ); + str2Def.setReadWriteAccessor( store.getAccessor( clazz, str2Def.getName() ) ); Object x = clazz.newInstance(); Object y = clazz.newInstance(); - long1Def.setValue( x, - Long.valueOf( 20 ) ); - long2Def.setValue( x, - Long.valueOf( 30 ) ); - doubleDef.setValue( x, - Double.valueOf( 50.0 ) ); - intDef.setValue( x, - Integer.valueOf( 10 ) ); - strDef.setValue( x, - "abc" ); - dateDef.setValue( x, - new Date( 1000 ) ); - str2Def.setValue( x, - "instance1" ); - - long1Def.setValue( y, - Long.valueOf( 20 ) ); - long2Def.setValue( y, - Long.valueOf( 30 ) ); - doubleDef.setValue( y, - Double.valueOf( 50.0 ) ); - intDef.setValue( y, - Integer.valueOf( 10 ) ); - strDef.setValue( y, - "abc" ); - dateDef.setValue( y, - new Date( 1000 ) ); - str2Def.setValue( y, - "instance2" ); + long1Def.setValue( x, Long.valueOf( 20 ) ); + long2Def.setValue( x, Long.valueOf( 30 ) ); + doubleDef.setValue( x, Double.valueOf( 50.0 ) ); + intDef.setValue( x, Integer.valueOf( 10 ) ); + strDef.setValue( x, "abc" ); + dateDef.setValue( x, new Date( 1000 ) ); + str2Def.setValue( x, "instance1" ); + + long1Def.setValue( y, Long.valueOf( 20 ) ); + long2Def.setValue( y, Long.valueOf( 30 ) ); + doubleDef.setValue( y, Double.valueOf( 50.0 ) ); + intDef.setValue( y, Integer.valueOf( 10 ) ); + strDef.setValue( y, "abc" ); + dateDef.setValue( y, new Date( 1000 ) ); + str2Def.setValue( y, "instance2" ); Object o = new Object(); - assertThat(x.equals(x)).isTrue(); + assertThat(x).isEqualTo(x);; - assertThat(x.equals(o)).isFalse(); + assertThat(x).isNotEqualTo(o); - assertThat(x.equals(y)).isTrue(); + assertThat(x).isEqualTo(y);; - intDef.setValue( y, - Integer.valueOf( 1 ) ); - assertThat(x.equals(y)).isFalse(); + intDef.setValue( y, Integer.valueOf( 1 ) ); + assertThat(x).isNotEqualTo(y); - intDef.setValue( y, - Integer.valueOf( 10 ) ); - strDef.setValue( y, - "xyz" ); - assertThat(x.equals(y)).isFalse(); + intDef.setValue( y, Integer.valueOf( 10 ) ); + strDef.setValue( y, "xyz" ); + assertThat(x).isNotEqualTo(y); - strDef.setValue( y, - null ); - assertThat(x.equals(y)).isFalse(); + strDef.setValue( y, null ); + assertThat(x).isNotEqualTo(y); - strDef.setValue( y, - "abc" ); - dateDef.setValue( y, - new Date( 1 ) ); - assertThat(x.equals(y)).isFalse(); + strDef.setValue( y, "abc" ); + dateDef.setValue( y, new Date( 1 ) ); + assertThat(x).isNotEqualTo(y); - dateDef.setValue( y, - null ); - assertThat(x.equals(y)).isFalse(); + dateDef.setValue( y, null ); + assertThat(x).isNotEqualTo(y); } catch ( Exception e ) { e.printStackTrace(); @@ -270,30 +217,20 @@ public void testHashCode() { try { ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass3", - null, - new String[]{} ); - FieldDefinition intDef = new FieldDefinition( "intAttr", - "int", - true ); - FieldDefinition strDef = new FieldDefinition( "stringAttr", - "java.lang.String", - false ); + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass3", null, new String[]{} ); + FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); + FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", false ); classDef.addField( intDef ); classDef.addField( strDef ); Class clazz = build(builder, classDef); - intDef.setReadWriteAccessor( store.getAccessor( clazz, - intDef.getName() ) ); - strDef.setReadWriteAccessor( store.getAccessor( clazz, - strDef.getName() ) ); + intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); + strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName() ) ); Object x = clazz.newInstance(); - intDef.setValue( x, - new Integer( 10 ) ); - strDef.setValue( x, - "abc" ); + intDef.setValue( x, new Integer( 10 ) ); + strDef.setValue( x, "abc" ); assertThat(x.hashCode()).as("Wrong hashcode calculation").isEqualTo(31 + 10); assertThat(x.hashCode()).as("Wrong hashcode calculation").isEqualTo(x.hashCode()); @@ -309,29 +246,14 @@ public void testToString() { try { ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass4", - null, - new String[]{} ); - FieldDefinition long1Def = new FieldDefinition( "longAttr1", - "long", - true ); - FieldDefinition long2Def = new FieldDefinition( "longAttr2", - "long", - true ); - FieldDefinition doubleDef = new FieldDefinition( "doubleAttr", - "double", - true ); - FieldDefinition intDef = new FieldDefinition( "intAttr", - "int", - true ); - FieldDefinition strDef = new FieldDefinition( "stringAttr", - "java.lang.String", - true ); - FieldDefinition dateDef = new FieldDefinition( "dateAttr", - "java.util.Date", - true ); - FieldDefinition str2Def = new FieldDefinition( "stringAttr2", - "java.lang.String" ); + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass4", null, new String[]{} ); + FieldDefinition long1Def = new FieldDefinition( "longAttr1", "long", true ); + FieldDefinition long2Def = new FieldDefinition( "longAttr2", "long", true ); + FieldDefinition doubleDef = new FieldDefinition( "doubleAttr", "double", true ); + FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); + FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", true ); + FieldDefinition dateDef = new FieldDefinition( "dateAttr", "java.util.Date", true ); + FieldDefinition str2Def = new FieldDefinition( "stringAttr2", "java.lang.String" ); classDef.addField( long1Def ); classDef.addField( long2Def ); classDef.addField( doubleDef ); @@ -341,37 +263,23 @@ public void testToString() { classDef.addField( str2Def ); Class clazz = build(builder, classDef); - long1Def.setReadWriteAccessor( store.getAccessor( clazz, - long1Def.getName() ) ); - long2Def.setReadWriteAccessor( store.getAccessor( clazz, - long2Def.getName() ) ); - doubleDef.setReadWriteAccessor( store.getAccessor( clazz, - doubleDef.getName() ) ); - intDef.setReadWriteAccessor( store.getAccessor( clazz, - intDef.getName() ) ); - strDef.setReadWriteAccessor( store.getAccessor( clazz, - strDef.getName() ) ); - dateDef.setReadWriteAccessor( store.getAccessor( clazz, - dateDef.getName() ) ); - str2Def.setReadWriteAccessor( store.getAccessor( clazz, - str2Def.getName() ) ); + long1Def.setReadWriteAccessor( store.getAccessor( clazz, long1Def.getName() ) ); + long2Def.setReadWriteAccessor( store.getAccessor( clazz, long2Def.getName() ) ); + doubleDef.setReadWriteAccessor( store.getAccessor( clazz, doubleDef.getName() ) ); + intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); + strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName() ) ); + dateDef.setReadWriteAccessor( store.getAccessor( clazz, dateDef.getName() ) ); + str2Def.setReadWriteAccessor( store.getAccessor( clazz, str2Def.getName() ) ); Object x = clazz.newInstance(); - long1Def.setValue( x, - new Long( 20 ) ); - long2Def.setValue( x, - new Long( 30 ) ); - doubleDef.setValue( x, - new Double( 50.0 ) ); - intDef.setValue( x, - new Integer( 10 ) ); - strDef.setValue( x, - "abc" ); - dateDef.setValue( x, - new Date( 1000 ) ); - str2Def.setValue( x, - "instance1" ); + long1Def.setValue( x, new Long( 20 ) ); + long2Def.setValue( x, new Long( 30 ) ); + doubleDef.setValue( x, new Double( 50.0 ) ); + intDef.setValue( x, new Integer( 10 ) ); + strDef.setValue( x, "abc" ); + dateDef.setValue( x, new Date( 1000 ) ); + str2Def.setValue( x, "instance1" ); String result = x.toString(); @@ -395,16 +303,13 @@ public void testConstructorWithFields() { try { ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass5", - null, - new String[]{} ); + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass5", null, new String[]{} ); String[] types = new String[]{"byte", "short", "int", "long", "float", "double", "char", "java.lang.String", "boolean"}; FieldDefinition[] fields = new FieldDefinition[types.length]; for ( int i = 0; i < types.length; i++ ) { String attrName = types[i].substring( types[i].lastIndexOf( '.' ) + 1 ); - attrName = attrName.substring( 0, - 1 ).toLowerCase() + attrName.substring( 1 ) + "Attr"; + attrName = attrName.substring( 0, 1 ).toLowerCase() + attrName.substring( 1 ) + "Attr"; fields[i] = new FieldDefinition( attrName, // attr name types[i], // attr type i % 2 == 0 ); // half of them are key @@ -414,8 +319,7 @@ public void testConstructorWithFields() { Class clazz = build(builder, classDef); for ( FieldDefinition field : fields ) { - field.setReadWriteAccessor( store.getAccessor( clazz, - field.getName() ) ); + field.setReadWriteAccessor( store.getAccessor( clazz, field.getName() ) ); } Constructor< ? >[] cons = clazz.getConstructors(); @@ -434,15 +338,7 @@ public void testConstructorWithFields() { } // test actual invocation - Object instance = c.newInstance( (byte) 1, - (short) 2, - 3, - 4l, - 5.0f, - 6.0d, - 'a', - "xyz", - true ); + Object instance = c.newInstance( (byte) 1, (short) 2, 3, 4l, 5.0f, 6.0d, 'a', "xyz", true ); assertThat(fields[0].getValue(instance)).isEqualTo((byte) 1); assertThat(fields[1].getValue(instance)).isEqualTo((short) 2); @@ -462,11 +358,7 @@ public void testConstructorWithFields() { } } // test actual invocation - Object instance = c.newInstance( (byte) 1, - 3, - 5.0f, - 'a', - true ); + Object instance = c.newInstance( (byte) 1, 3, 5.0f, 'a', true ); assertThat(fields[0].getValue(instance)).isEqualTo((byte) 1); assertThat(fields[2].getValue(instance)).isEqualTo(3); From d51385c100312058d80da61b7e88fd21289b0def Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Wed, 18 Sep 2024 16:54:02 +0200 Subject: [PATCH 11/13] Cleaned up isTrue assertions --- .../factmodel/traits/LogicalTraitTest.java | 14 +- .../factmodel/traits/TraitMapCoreTest.java | 3 +- .../core/factmodel/ClassBuilderTest.java | 14 +- .../core/factmodel/TripleStoreTest.java | 20 +- .../traits/core/meta/org/MetadataTest.java | 57 ++---- .../traits/core/util/HierarchyTest.java | 182 +++++++++--------- 6 files changed, 133 insertions(+), 157 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java index 4aa2664ec93..ee001e74eee 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java @@ -91,7 +91,7 @@ public void testShadowAlias() { KieSession ks = kbase.newKieSession(); - List list = new ArrayList( ); + List list = new ArrayList<>( ); ks.setGlobal( "list", list ); ks.fireAllRules(); @@ -107,7 +107,7 @@ public void testShadowAlias() { } LOGGER.debug( list.toString() ); - assertThat(list.contains(false)).isFalse(); + assertThat(list).doesNotContain(Boolean.FALSE); assertThat(list).hasSize(8); } @@ -562,13 +562,9 @@ public void testHardGetSetOnLogicallyTraitedField() { knowledgeSession.insert( "x" ); knowledgeSession.fireAllRules(); - boolean found = false; - for ( Object o : knowledgeSession.getObjects( new ClassObjectFilter( Qty.class ) ) ) { - assertThat(((Qty) o).getNum()).isEqualTo((Integer) 99); - assertThat(((CoreWrapper) o)._getFieldTMS().get("num", Integer.class)).isEqualTo(99); - found = true; - } - assertThat(found).isTrue(); + Object o = knowledgeSession.getObjects(new ClassObjectFilter(Qty.class)).iterator().next(); + assertThat(((Qty) o).getNum()).isEqualTo((Integer) 99); + assertThat(((CoreWrapper) o)._getFieldTMS().get("num", Integer.class)).isEqualTo(99); assertThat(list).containsExactly(42, 99); knowledgeSession.dispose(); diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java index 32ddf8dc43e..5540d91abc1 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java @@ -145,8 +145,7 @@ public void donMapTest() { ksession.insert( map ); ksession.fireAllRules(); - assertThat(map.containsKey("height")).isTrue(); - assertThat(184.0).isEqualTo(map.get("height")); + assertThat(map).containsEntry("height", 184.0); } diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java index 30b660104a0..8284a8e4412 100755 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java @@ -283,13 +283,13 @@ public void testToString() { String result = x.toString(); - assertThat(result.contains(long1Def.getName())).isTrue(); - assertThat(result.contains(long2Def.getName())).isTrue(); - assertThat(result.contains(doubleDef.getName())).isTrue(); - assertThat(result.contains(intDef.getName())).isTrue(); - assertThat(result.contains(strDef.getName())).isTrue(); - assertThat(result.contains(dateDef.getName())).isTrue(); - assertThat(result.contains(str2Def.getName())).isTrue(); + assertThat(result).contains(long1Def.getName()); + assertThat(result).contains(long2Def.getName()); + assertThat(result).contains(doubleDef.getName()); + assertThat(result).contains(intDef.getName()); + assertThat(result).contains(strDef.getName()); + assertThat(result).contains(dateDef.getName()); + assertThat(result).contains(str2Def.getName()); } catch ( Exception e ) { e.printStackTrace(); diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java index 4b644280e43..e09bf9442c1 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java @@ -183,31 +183,30 @@ public void testQueryVariable() { tKey = new TripleImpl( V, "hasCity", V ); coll = store.getAll(tKey); - assertThat(coll.containsAll(Arrays.asList(t3, t4))).isTrue(); assertThat(coll).hasSize(2); - + assertThat(coll).containsExactlyInAnyOrder(t3, t4); tKey = new TripleImpl( ind, V, V ); coll = store.getAll(tKey); - assertThat(coll.containsAll(Arrays.asList(t1, t2, t3))).isTrue(); assertThat(coll).hasSize(3); + assertThat(coll).containsExactlyInAnyOrder(t1, t2, t3); tKey = new TripleImpl( ind2, V, "lancia" ); coll = store.getAll(tKey); - assertThat(coll.containsAll(Arrays.asList(t5, t6))).isTrue(); assertThat(coll).hasSize(2); + assertThat(coll).containsExactlyInAnyOrder(t5, t6); tKey = new TripleImpl( V, V, "lancia" ); coll = store.getAll(tKey); - assertThat(coll.containsAll(Arrays.asList(t5, t6))).isTrue(); assertThat(coll).hasSize(2); + assertThat(coll).containsExactlyInAnyOrder(t5, t6); tKey = new TripleImpl( V, V, V ); coll = store.getAll(tKey); - assertThat(coll.containsAll(Arrays.asList(t1, t2, t3, t4, t5, t6))).isTrue(); assertThat(coll).hasSize(6); + assertThat(coll).containsExactlyInAnyOrder(t1, t2, t3, t4, t5, t6); } @@ -245,14 +244,13 @@ public void testAddNary() { Triple tKey; - Triple t; Collection coll; tKey = new TripleImpl( ind, "hasName", V ); coll = store.getAll(tKey); - assertThat(coll.containsAll(Arrays.asList(new TripleImpl(ind, "hasName", "oscar"), + assertThat(coll).containsExactlyInAnyOrder(new TripleImpl(ind, "hasName", "oscar"), new TripleImpl(ind, "hasName", "mark"), - new TripleImpl(ind, "hasName", "daniel")))).isTrue(); + new TripleImpl(ind, "hasName", "daniel")); assertThat(store.contains(new TripleImpl(ind, "hasName", "marc"))).isFalse(); assertThat(store.contains(new TripleImpl(ind, "hasName", "mark"))).isTrue(); @@ -265,8 +263,8 @@ public void testAddNary() { tKey = new TripleImpl( ind, "hasCar", V ); coll = store.getAll(tKey); - assertThat(coll.containsAll(Arrays.asList(new TripleImpl(ind, "hasCar", "mini"), - new TripleImpl(ind, "hasCar", "ferrari")))).isTrue(); + assertThat(coll).containsExactlyInAnyOrder(new TripleImpl(ind, "hasCar", "mini"), + new TripleImpl(ind, "hasCar", "ferrari")); store.remove( new TripleImpl(ind, "hasCar", "mini") ); diff --git a/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java b/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java index 04a19195b71..ca9f21fc236 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java @@ -183,7 +183,7 @@ public void testURIs() { assertThat(AnotherKlass_.modify(aki).num(33).getUri()).isEqualTo(URI.create(uri.toString() + "/modify?num")); - assertThat(uri.toString().startsWith(aki.get_().getMetaClassInfo().getUri().toString())).isTrue(); + assertThat(uri.toString()).startsWith(aki.get_().getMetaClassInfo().getUri().toString()); assertThat(SubKlass_.newSubKlass(URI.create("http://www.test.org#SubKlass/123")).getUri()).isEqualTo(URI.create("http://www.test.org#SubKlass/123?create")); @@ -311,14 +311,13 @@ public void testOneToManyProperty() { assertThat(klass1.getOneAnother()).isSameAs(aki2); assertThat(klass2.getOneAnother()).isSameAs(aki); - assertThat(aki.getManyKlasses().contains(klass1)).isFalse(); - assertThat(aki2.getManyKlasses().contains(klass1)).isTrue(); - assertThat(aki.getManyKlasses().contains(klass2)).isTrue(); + assertThat(aki.getManyKlasses()).doesNotContain(klass1); + assertThat(aki2.getManyKlasses()).contains(klass1).doesNotContain(klass2); AnotherKlass_.modify( aki2 ).manyKlasses( klass1, Lit.REMOVE ).call(); assertThat(klass1.getOneAnother()).isNull(); - assertThat(aki2.getManyKlasses().contains(klass1)).isFalse(); + assertThat(aki2.getManyKlasses()).doesNotContain(klass1); } @@ -362,51 +361,35 @@ public void testManyToManyProperty() { AnotherKlass_.modify( aki2 ).manyMoreKlasses( klass2, Lit.ADD ).call(); AnotherKlass_.modify( aki1 ).manyMoreKlasses( klass2, Lit.ADD ).call(); - assertThat(klass1.getManyAnothers().contains(aki1)).isTrue(); - assertThat(klass1.getManyAnothers().contains(aki2)).isTrue(); - assertThat(klass2.getManyAnothers().contains(aki1)).isTrue(); - assertThat(klass2.getManyAnothers().contains(aki2)).isTrue(); + assertThat(klass1.getManyAnothers()).contains(aki1, aki2); + assertThat(klass2.getManyAnothers()).contains(aki1, aki2); - assertThat(aki1.getManyMoreKlasses().contains(klass1)).isTrue(); - assertThat(aki1.getManyMoreKlasses().contains(klass2)).isTrue(); - assertThat(aki2.getManyMoreKlasses().contains(klass1)).isTrue(); - assertThat(aki2.getManyMoreKlasses().contains(klass2)).isTrue(); + assertThat(aki1.getManyMoreKlasses()).contains(klass1, klass2); + assertThat(aki2.getManyMoreKlasses()).contains(klass1, klass2); AnotherKlass_.modify( aki2 ).manyMoreKlasses( klass2, Lit.REMOVE ).call(); - assertThat(klass1.getManyAnothers().contains(aki1)).isTrue(); - assertThat(klass1.getManyAnothers().contains(aki2)).isTrue(); - assertThat(klass2.getManyAnothers().contains(aki1)).isTrue(); - assertThat(klass2.getManyAnothers().contains(aki2)).isFalse(); + assertThat(klass1.getManyAnothers()).contains(aki1, aki2); + assertThat(klass2.getManyAnothers()).contains(aki1).doesNotContain(aki2); - assertThat(aki1.getManyMoreKlasses().contains(klass1)).isTrue(); - assertThat(aki1.getManyMoreKlasses().contains(klass2)).isTrue(); - assertThat(aki2.getManyMoreKlasses().contains(klass1)).isTrue(); - assertThat(aki2.getManyMoreKlasses().contains(klass2)).isFalse(); + assertThat(aki1.getManyMoreKlasses()).contains(klass1, klass2); + assertThat(aki2.getManyMoreKlasses()).contains(klass1).doesNotContain(klass2); AnotherKlass_.modify( aki2 ).manyMoreKlasses( klass2, Lit.ADD ).call(); - assertThat(klass1.getManyAnothers().contains(aki1)).isTrue(); - assertThat(klass1.getManyAnothers().contains(aki2)).isTrue(); - assertThat(klass2.getManyAnothers().contains(aki1)).isTrue(); - assertThat(klass2.getManyAnothers().contains(aki2)).isTrue(); + assertThat(klass1.getManyAnothers()).contains(aki1, aki2); + assertThat(klass2.getManyAnothers()).contains(aki1, aki2); - assertThat(aki1.getManyMoreKlasses().contains(klass1)).isTrue(); - assertThat(aki1.getManyMoreKlasses().contains(klass2)).isTrue(); - assertThat(aki2.getManyMoreKlasses().contains(klass1)).isTrue(); - assertThat(aki2.getManyMoreKlasses().contains(klass2)).isTrue(); + assertThat(aki1.getManyMoreKlasses()).contains(klass1, klass2); + assertThat(aki2.getManyMoreKlasses()).contains(klass1, klass2); AnotherKlass_.modify( aki2 ).manyMoreKlasses( klass2, Lit.SET ).call(); - assertThat(klass1.getManyAnothers().contains(aki1)).isTrue(); - assertThat(klass1.getManyAnothers().contains(aki2)).isFalse(); - assertThat(klass2.getManyAnothers().contains(aki1)).isTrue(); - assertThat(klass2.getManyAnothers().contains(aki2)).isTrue(); + assertThat(klass1.getManyAnothers()).contains(aki1).doesNotContain(aki2); + assertThat(klass2.getManyAnothers()).contains(aki1, aki2); - assertThat(aki1.getManyMoreKlasses().contains(klass1)).isTrue(); - assertThat(aki1.getManyMoreKlasses().contains(klass2)).isTrue(); - assertThat(aki2.getManyMoreKlasses().contains(klass1)).isFalse(); - assertThat(aki2.getManyMoreKlasses().contains(klass2)).isTrue(); + assertThat(aki1.getManyMoreKlasses()).contains(klass1, klass2); + assertThat(aki2.getManyMoreKlasses()).doesNotContain(klass1).contains(klass2); } diff --git a/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java b/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java index 1bc8fc765cb..6dc8d854971 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java @@ -99,17 +99,17 @@ public void testHierManyRootsPropagation() { BitSet d = encoder.getCode( "D" ); BitSet e = encoder.getCode( "E" ); - assertThat(encoder.superset(b, a) > 0).isTrue(); - assertThat(encoder.superset(c, a) > 0).isTrue(); - assertThat(encoder.superset(d, a) > 0).isTrue(); - assertThat(encoder.superset(d, b) > 0).isTrue(); - assertThat(encoder.superset(d, c) > 0).isTrue(); - - assertThat(encoder.superset(e, a) < 0).isTrue(); - assertThat(encoder.superset(e, b) < 0).isTrue(); - assertThat(encoder.superset(e, c) < 0).isTrue(); - assertThat(encoder.superset(e, d) < 0).isTrue(); - assertThat(encoder.superset(e, e) == 0).isTrue(); + assertThat(encoder.superset(b, a)).isGreaterThan(0); + assertThat(encoder.superset(c, a)).isGreaterThan(0); + assertThat(encoder.superset(d, a)).isGreaterThan(0); + assertThat(encoder.superset(d, b)).isGreaterThan(0); + assertThat(encoder.superset(d, c)).isGreaterThan(0); + + assertThat(encoder.superset(e, a)).isLessThan(0); + assertThat(encoder.superset(e, b)).isLessThan(0); + assertThat(encoder.superset(e, c)).isLessThan(0); + assertThat(encoder.superset(e, d)).isLessThan(0); + assertThat(encoder.superset(e, e)).isEqualTo(0); } @@ -547,7 +547,7 @@ public void testHierEncoderComplexInheritance() { BitSet ns = iter.next(); Long next = toLong( ns ); LOGGER.debug( next.toString() ); - assertThat(next > last).isTrue(); + assertThat(next).isGreaterThan(last); last = next; } } @@ -672,8 +672,8 @@ public void testSecondOrderInheritance() { LOGGER.debug( encoder.toString() ); - assertThat(((HierarchyEncoderImpl) encoder).superset(encoder.getCode("Z"), encoder.getCode("F")) < 0).isTrue() ; - assertThat(((HierarchyEncoderImpl) encoder).superset(encoder.getCode("F"), encoder.getCode("Z")) < 0).isTrue() ; + assertThat(((HierarchyEncoderImpl) encoder).superset(encoder.getCode("Z"), encoder.getCode("F"))).isLessThan(0); + assertThat(((HierarchyEncoderImpl) encoder).superset(encoder.getCode("F"), encoder.getCode("Z"))).isLessThan(0); } @@ -707,15 +707,15 @@ public void testDecoderAncestors() { x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("Z")).isTrue(); - assertThat(x.contains("C")).isTrue(); - assertThat(x.contains("Q")).isTrue(); - assertThat(x.contains("T")).isTrue(); - assertThat(x.contains("R")).isTrue(); - assertThat(x.contains("S")).isTrue(); - assertThat(x.contains("M")).isTrue(); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("Z"); + assertThat(x).contains("C"); + assertThat(x).contains("Q"); + assertThat(x).contains("T"); + assertThat(x).contains("R"); + assertThat(x).contains("S"); + assertThat(x).contains("M"); + assertThat(x).contains("Thing"); assertThat(x).hasSize(9); @@ -724,53 +724,53 @@ public void testDecoderAncestors() { LOGGER.debug( "ANC " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("Q")).isTrue(); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("Q"); + assertThat(x).contains("Thing"); b = parseBitSet( "1111" ); x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); assertThat(x).hasSize(6); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("Z")).isTrue(); - assertThat(x.contains("B")).isTrue(); - assertThat(x.contains("C")).isTrue(); - assertThat(x.contains("N")).isTrue(); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("Z"); + assertThat(x).contains("B"); + assertThat(x).contains("C"); + assertThat(x).contains("N"); + assertThat(x).contains("Thing"); b = parseBitSet( "111" ); x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); assertThat(x).hasSize(4); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("Z")).isTrue(); - assertThat(x.contains("C")).isTrue(); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("Z"); + assertThat(x).contains("C"); + assertThat(x).contains("Thing"); b = parseBitSet( "1" ); x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("Thing"); b = parseBitSet( "10" ); x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("Z")).isTrue(); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("Z"); + assertThat(x).contains("Thing"); b = parseBitSet( "0" ); x = encoder.upperAncestors(b); LOGGER.debug( "ANC " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("Thing"); b = parseBitSet( "1011" ); @@ -778,10 +778,10 @@ public void testDecoderAncestors() { LOGGER.debug( "ANC " + x ); assertThat(x).hasSize(4); - assertThat(x.contains("Thing")).isTrue(); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("B")).isTrue(); - assertThat(x.contains("Z")).isTrue(); + assertThat(x).contains("Thing"); + assertThat(x).contains("A"); + assertThat(x).contains("B"); + assertThat(x).contains("Z"); } @@ -811,9 +811,9 @@ public void testDecoderDescendants() { LOGGER.debug( "DESC " + x ); assertThat(x).hasSize(3); - assertThat(x.contains("C")).isTrue(); - assertThat(x.contains("N")).isTrue(); - assertThat(x.contains("T")).isTrue(); + assertThat(x).contains("C"); + assertThat(x).contains("N"); + assertThat(x).contains("T"); b = parseBitSet( "10" ); @@ -821,11 +821,11 @@ public void testDecoderDescendants() { LOGGER.debug( "DESC " + x ); assertThat(x).hasSize(5); - assertThat(x.contains("C")).isTrue(); - assertThat(x.contains("N")).isTrue(); - assertThat(x.contains("T")).isTrue(); - assertThat(x.contains("Z")).isTrue(); - assertThat(x.contains("B")).isTrue(); + assertThat(x).contains("C"); + assertThat(x).contains("N"); + assertThat(x).contains("T"); + assertThat(x).contains("Z"); + assertThat(x).contains("B"); b = parseBitSet( "100000" ); @@ -833,10 +833,10 @@ public void testDecoderDescendants() { LOGGER.debug( "DESC " + x ); assertThat(x).hasSize(4); - assertThat(x.contains("Q")).isTrue(); - assertThat(x.contains("T")).isTrue(); - assertThat(x.contains("M")).isTrue(); - assertThat(x.contains("O")).isTrue(); + assertThat(x).contains("Q"); + assertThat(x).contains("T"); + assertThat(x).contains("M"); + assertThat(x).contains("O"); @@ -846,14 +846,14 @@ public void testDecoderDescendants() { LOGGER.debug( "DESC " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("T")).isTrue(); + assertThat(x).contains("T"); b = parseBitSet( "1111" ); x = encoder.lowerDescendants(b); LOGGER.debug( "DESC " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("N")).isTrue(); + assertThat(x).contains("N"); b = parseBitSet( "1" ); @@ -861,11 +861,11 @@ public void testDecoderDescendants() { LOGGER.debug( "DESC " + x ); assertThat(x).hasSize(5); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("B")).isTrue(); - assertThat(x.contains("C")).isTrue(); - assertThat(x.contains("N")).isTrue(); - assertThat(x.contains("T")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("B"); + assertThat(x).contains("C"); + assertThat(x).contains("N"); + assertThat(x).contains("T"); LOGGER.debug(" +*******************************+ "); @@ -873,8 +873,8 @@ public void testDecoderDescendants() { LOGGER.debug( "DESC " + x ); assertThat(x).hasSize(13); - assertThat(x.contains("Z")).isTrue(); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("Z"); + assertThat(x).contains("Thing"); } @@ -904,12 +904,12 @@ public void testHierEncoderDecoderLower() { x = encoder.lowerBorder( encoder.metMembersCode(List.of("B"))); LOGGER.debug( "GCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("B")).isTrue(); + assertThat(x).contains("B"); x = encoder.immediateChildren( encoder.metMembersCode(List.of("B"))); LOGGER.debug( "GCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("N")).isTrue(); + assertThat(x).contains("N"); @@ -917,13 +917,13 @@ public void testHierEncoderDecoderLower() { LOGGER.debug( "GCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("T")).isTrue(); + assertThat(x).contains("T"); x = encoder.immediateChildren( encoder.metMembersCode( Arrays.asList( "Z", "Q" ) ) ); LOGGER.debug( "GCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("T")).isTrue(); + assertThat(x).contains("T"); @@ -932,15 +932,15 @@ public void testHierEncoderDecoderLower() { LOGGER.debug( "GCS " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("B")).isTrue(); - assertThat(x.contains("C")).isTrue(); + assertThat(x).contains("B"); + assertThat(x).contains("C"); x = encoder.immediateChildren( encoder.metMembersCode( Arrays.asList( "A", "Z" ) ) ); LOGGER.debug( "GCS " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("B")).isTrue(); - assertThat(x.contains("C")).isTrue(); + assertThat(x).contains("B"); + assertThat(x).contains("C"); @@ -949,17 +949,17 @@ public void testHierEncoderDecoderLower() { LOGGER.debug( "GCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("Thing"); x = encoder.immediateChildren( encoder.metMembersCode(List.of("Thing"))); LOGGER.debug( "GCS " + x ); assertThat(x).hasSize(5); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("Z")).isTrue(); - assertThat(x.contains("P")).isTrue(); - assertThat(x.contains("Q")).isTrue(); - assertThat(x.contains("R")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("Z"); + assertThat(x).contains("P"); + assertThat(x).contains("Q"); + assertThat(x).contains("R"); } @@ -988,13 +988,13 @@ public void testHierEncoderDecoderUpper() { Collection x = encoder.upperBorder( encoder.metMembersCode(List.of("B"))); LOGGER.debug( "LCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("B")).isTrue(); + assertThat(x).contains("B"); x = encoder.immediateParents( encoder.metMembersCode(List.of("B"))); LOGGER.debug( "LCS " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("Z")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("Z"); @@ -1002,13 +1002,13 @@ public void testHierEncoderDecoderUpper() { LOGGER.debug( "LCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("Thing"); x = encoder.immediateParents( encoder.jointMembersCode( Arrays.asList( "Z", "Q" ) ) ); LOGGER.debug( "LCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("Thing")).isTrue(); + assertThat(x).contains("Thing"); @@ -1016,29 +1016,29 @@ public void testHierEncoderDecoderUpper() { LOGGER.debug( "LCS " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("Z")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("Z"); x = encoder.immediateParents( encoder.jointMembersCode( Arrays.asList( "B", "C" ) ) ); LOGGER.debug( "LCS " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("A")).isTrue(); - assertThat(x.contains("Z")).isTrue(); + assertThat(x).contains("A"); + assertThat(x).contains("Z"); x = encoder.upperBorder( encoder.jointMembersCode(List.of("T"))); LOGGER.debug( "LCS " + x ); assertThat(x).hasSize(1); - assertThat(x.contains("T")).isTrue(); + assertThat(x).contains("T"); x = encoder.immediateParents( encoder.jointMembersCode(List.of("T"))); LOGGER.debug( "LCS " + x ); assertThat(x).hasSize(2); - assertThat(x.contains("C")).isTrue(); - assertThat(x.contains("Q")).isTrue(); + assertThat(x).contains("C"); + assertThat(x).contains("Q"); } From da7f8c699644e0dbba9ae661208cba171efd7fde Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Wed, 18 Sep 2024 17:31:08 +0200 Subject: [PATCH 12/13] Proper use of assertions on exceptions --- .../factmodel/traits/LogicalTraitTest.java | 8 +- .../compiler/factmodel/traits/TraitTest.java | 41 +- .../core/factmodel/ClassBuilderTest.java | 509 ++++++++---------- 3 files changed, 258 insertions(+), 300 deletions(-) diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java index ee001e74eee..bedb76d286e 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LogicalTraitTest.java @@ -77,7 +77,7 @@ public LogicalTraitTest( VirtualPropertyMode m ) { @Test - public void testShadowAlias() { + public void testShadowAlias() throws Exception { KnowledgeBuilder kbuilderImpl = KnowledgeBuilderFactory.newKnowledgeBuilder(); kbuilderImpl.add( ResourceFactory.newClassPathResource( "org/drools/compiler/factmodel/traits/testTraitedAliasing.drl" ), ResourceType.DRL ); @@ -100,11 +100,7 @@ public void testShadowAlias() { LOGGER.debug( o.toString() ); } - try { - ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true ); - } catch ( Exception e ) { - fail( e.getMessage(), e ); - } + ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true ); LOGGER.debug( list.toString() ); assertThat(list).doesNotContain(Boolean.FALSE); diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java index 99ff352389a..8c9142f1664 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitTest.java @@ -103,6 +103,8 @@ import java.util.concurrent.TimeUnit; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatException; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -919,12 +921,11 @@ public void testOverrideType() { List info = new ArrayList<>(); ks.setGlobal("list", info); - try { - ks.fireAllRules(); - fail("An exception was expected since a trait can't override the type of a core class field with these settings "); - } catch (Throwable rde) { - assertThat(rde.getCause()).isInstanceOf(UnsupportedOperationException.class); - } + assertThatException() + .as("An exception was expected since a trait can't override the type of a core class field with these settings ") + .isThrownBy(() -> ks.fireAllRules()) + .extracting(e -> e.getCause()) + .isInstanceOf(UnsupportedOperationException.class); } @@ -949,12 +950,11 @@ public void testOverrideType2() { KieSession ks = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - try { - ks.fireAllRules(); - fail("An exception was expected since a trait can't override the type of a core class field with these settings "); - } catch (Throwable rde) { - assertThat(rde.getCause()).isInstanceOf(UnsupportedOperationException.class); - } + assertThatException() + .as("An exception was expected since a trait can't override the type of a core class field with these settings ") + .isThrownBy(() -> ks.fireAllRules()) + .extracting(e -> e.getCause()) + .isInstanceOf(UnsupportedOperationException.class); } @@ -978,12 +978,12 @@ public void testOverrideType3() { KieSession ks = getSessionFromString(drl); TraitFactoryImpl.setMode(mode, ks.getKieBase()); - try { - ks.fireAllRules(); - fail("An exception was expected since a trait can't override the type of a core class field with these settings "); - } catch (Throwable rde) { - assertThat(rde.getCause()).isInstanceOf(UnsupportedOperationException.class); - } + + assertThatException() + .as("An exception was expected since a trait can't override the type of a core class field with these settings ") + .isThrownBy(() -> ks.fireAllRules()) + .extracting(e -> e.getCause()) + .isInstanceOf(UnsupportedOperationException.class); } @@ -5677,10 +5677,7 @@ public void testMixinWithConflictsThrowingError() { List list = new ArrayList(); ks.setGlobal("list", list); - try { - ks.fireAllRules(); - fail("don should fail due to the conflict in getShared() method"); - } catch (Exception e) { } + assertThatException().as("don should fail due to the conflict in getShared() method").isThrownBy(() -> ks.fireAllRules()); } @Test diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java index 8284a8e4412..cc0dc47cf07 100755 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java @@ -74,46 +74,40 @@ private Class build(ClassBuilder builder, ClassDefinition classDef) throws Excep * Test method for 'org.drools.core.common.asm.ClassBuilder.buildClass(ClassDefinition)' */ @Test - public void testBuildClass() { - try { - ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); + public void testBuildClass() throws Exception { + ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass1", null, new String[]{"java.io.Serializable"} ); - FieldDefinition intDef = new FieldDefinition("intAttr", "int" ); + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass1", null, new String[]{"java.io.Serializable"} ); + FieldDefinition intDef = new FieldDefinition("intAttr", "int" ); - FieldDefinition stringDef = new FieldDefinition( "stringAttr", "java.lang.String" );//"java.lang.String" ); - classDef.addField( intDef ); - classDef.addField( stringDef ); - - Class clazz = build(builder, classDef); + FieldDefinition stringDef = new FieldDefinition( "stringAttr", "java.lang.String" );//"java.lang.String" ); + classDef.addField( intDef ); + classDef.addField( stringDef ); + + Class clazz = build(builder, classDef); - - - intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); - stringDef.setReadWriteAccessor( store.getAccessor( clazz, stringDef.getName() ) ); + + + intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); + stringDef.setReadWriteAccessor( store.getAccessor( clazz, stringDef.getName() ) ); - byte[] d = builder.buildClass( classDef, classLoader ); + byte[] d = builder.buildClass( classDef, classLoader ); - assertThat(clazz).as("Returned class should be the same").isSameAs(classDef.getDefinedClass()); - assertThat(clazz.getName()).as("Class name should be equal").isEqualTo(classDef.getClassName()); + assertThat(clazz).as("Returned class should be the same").isSameAs(classDef.getDefinedClass()); + assertThat(clazz.getName()).as("Class name should be equal").isEqualTo(classDef.getClassName()); - Serializable instance = (Serializable) clazz.newInstance(); + Serializable instance = (Serializable) clazz.newInstance(); - String stringValue = "Atributo String ok"; - stringDef.setValue( instance, stringValue ); - assertThat(stringDef.getValue(instance)).as("Attribute should have been correctly set").isEqualTo(stringValue); + String stringValue = "Atributo String ok"; + stringDef.setValue( instance, stringValue ); + assertThat(stringDef.getValue(instance)).as("Attribute should have been correctly set").isEqualTo(stringValue); - int intValue = 50; - intDef.setValue( instance, new Integer( intValue ) ); - assertThat(((Integer) intDef.getValue(instance)).intValue()).as("Attribute should have been correctly set").isEqualTo(intValue); + int intValue = 50; + intDef.setValue( instance, new Integer( intValue ) ); + assertThat(((Integer) intDef.getValue(instance)).intValue()).as("Attribute should have been correctly set").isEqualTo(intValue); - // testing class rebuilding - clazz = build(builder, classDef); - - } catch ( Exception e ) { - e.printStackTrace(); - fail( "Error creating class" ); - } + // testing class rebuilding + clazz = build(builder, classDef); } /** @@ -133,265 +127,236 @@ private void writeJar(byte[] data) throws FileNotFoundException, IOException { } @Test - public void testEquals() { - try { - ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass2", null, new String[]{} ); - FieldDefinition long1Def = new FieldDefinition( "longAttr1", "long", true ); - FieldDefinition long2Def = new FieldDefinition( "longAttr2", "long", true ); - FieldDefinition doubleDef = new FieldDefinition( "doubleAttr", "double", true ); - FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); - FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", true ); - FieldDefinition dateDef = new FieldDefinition( "dateAttr", "java.util.Date", true ); - FieldDefinition str2Def = new FieldDefinition( "stringAttr2", "java.lang.String" ); - classDef.addField( long1Def ); - classDef.addField( long2Def ); - classDef.addField( doubleDef ); - classDef.addField( intDef ); - classDef.addField( strDef ); - classDef.addField( dateDef ); - classDef.addField( str2Def ); - - Class clazz = build(builder, classDef); - long1Def.setReadWriteAccessor( store.getAccessor( clazz, long1Def.getName() ) ); - long2Def.setReadWriteAccessor( store.getAccessor( clazz, long2Def.getName() ) ); - doubleDef.setReadWriteAccessor( store.getAccessor( clazz, doubleDef.getName() ) ); - intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); - strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName( ) ) ); - dateDef.setReadWriteAccessor( store.getAccessor( clazz, dateDef.getName() ) ); - str2Def.setReadWriteAccessor( store.getAccessor( clazz, str2Def.getName() ) ); - - Object x = clazz.newInstance(); - Object y = clazz.newInstance(); - - long1Def.setValue( x, Long.valueOf( 20 ) ); - long2Def.setValue( x, Long.valueOf( 30 ) ); - doubleDef.setValue( x, Double.valueOf( 50.0 ) ); - intDef.setValue( x, Integer.valueOf( 10 ) ); - strDef.setValue( x, "abc" ); - dateDef.setValue( x, new Date( 1000 ) ); - str2Def.setValue( x, "instance1" ); - - long1Def.setValue( y, Long.valueOf( 20 ) ); - long2Def.setValue( y, Long.valueOf( 30 ) ); - doubleDef.setValue( y, Double.valueOf( 50.0 ) ); - intDef.setValue( y, Integer.valueOf( 10 ) ); - strDef.setValue( y, "abc" ); - dateDef.setValue( y, new Date( 1000 ) ); - str2Def.setValue( y, "instance2" ); - - Object o = new Object(); - - assertThat(x).isEqualTo(x);; - - assertThat(x).isNotEqualTo(o); - - assertThat(x).isEqualTo(y);; - - intDef.setValue( y, Integer.valueOf( 1 ) ); - assertThat(x).isNotEqualTo(y); - - intDef.setValue( y, Integer.valueOf( 10 ) ); - strDef.setValue( y, "xyz" ); - assertThat(x).isNotEqualTo(y); - - strDef.setValue( y, null ); - assertThat(x).isNotEqualTo(y); - - strDef.setValue( y, "abc" ); - dateDef.setValue( y, new Date( 1 ) ); - assertThat(x).isNotEqualTo(y); - - dateDef.setValue( y, null ); - assertThat(x).isNotEqualTo(y); - - } catch ( Exception e ) { - e.printStackTrace(); - fail( "Exception not expected" ); - } + public void testEquals() throws Exception { + ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); + + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass2", null, new String[]{} ); + FieldDefinition long1Def = new FieldDefinition( "longAttr1", "long", true ); + FieldDefinition long2Def = new FieldDefinition( "longAttr2", "long", true ); + FieldDefinition doubleDef = new FieldDefinition( "doubleAttr", "double", true ); + FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); + FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", true ); + FieldDefinition dateDef = new FieldDefinition( "dateAttr", "java.util.Date", true ); + FieldDefinition str2Def = new FieldDefinition( "stringAttr2", "java.lang.String" ); + classDef.addField( long1Def ); + classDef.addField( long2Def ); + classDef.addField( doubleDef ); + classDef.addField( intDef ); + classDef.addField( strDef ); + classDef.addField( dateDef ); + classDef.addField( str2Def ); + + Class clazz = build(builder, classDef); + long1Def.setReadWriteAccessor( store.getAccessor( clazz, long1Def.getName() ) ); + long2Def.setReadWriteAccessor( store.getAccessor( clazz, long2Def.getName() ) ); + doubleDef.setReadWriteAccessor( store.getAccessor( clazz, doubleDef.getName() ) ); + intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); + strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName( ) ) ); + dateDef.setReadWriteAccessor( store.getAccessor( clazz, dateDef.getName() ) ); + str2Def.setReadWriteAccessor( store.getAccessor( clazz, str2Def.getName() ) ); + + Object x = clazz.newInstance(); + Object y = clazz.newInstance(); + + long1Def.setValue( x, Long.valueOf( 20 ) ); + long2Def.setValue( x, Long.valueOf( 30 ) ); + doubleDef.setValue( x, Double.valueOf( 50.0 ) ); + intDef.setValue( x, Integer.valueOf( 10 ) ); + strDef.setValue( x, "abc" ); + dateDef.setValue( x, new Date( 1000 ) ); + str2Def.setValue( x, "instance1" ); + + long1Def.setValue( y, Long.valueOf( 20 ) ); + long2Def.setValue( y, Long.valueOf( 30 ) ); + doubleDef.setValue( y, Double.valueOf( 50.0 ) ); + intDef.setValue( y, Integer.valueOf( 10 ) ); + strDef.setValue( y, "abc" ); + dateDef.setValue( y, new Date( 1000 ) ); + str2Def.setValue( y, "instance2" ); + + Object o = new Object(); + + assertThat(x).isEqualTo(x);; + + assertThat(x).isNotEqualTo(o); + + assertThat(x).isEqualTo(y);; + + intDef.setValue( y, Integer.valueOf( 1 ) ); + assertThat(x).isNotEqualTo(y); + + intDef.setValue( y, Integer.valueOf( 10 ) ); + strDef.setValue( y, "xyz" ); + assertThat(x).isNotEqualTo(y); + + strDef.setValue( y, null ); + assertThat(x).isNotEqualTo(y); + + strDef.setValue( y, "abc" ); + dateDef.setValue( y, new Date( 1 ) ); + assertThat(x).isNotEqualTo(y); + + dateDef.setValue( y, null ); + assertThat(x).isNotEqualTo(y); } @Test - public void testHashCode() { - try { - ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass3", null, new String[]{} ); - FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); - FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", false ); - classDef.addField( intDef ); - classDef.addField( strDef ); + public void testHashCode() throws Exception { + ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - Class clazz = build(builder, classDef); - intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); - strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName() ) ); + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass3", null, new String[]{} ); + FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); + FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", false ); + classDef.addField( intDef ); + classDef.addField( strDef ); - Object x = clazz.newInstance(); + Class clazz = build(builder, classDef); + intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); + strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName() ) ); - intDef.setValue( x, new Integer( 10 ) ); - strDef.setValue( x, "abc" ); + Object x = clazz.newInstance(); - assertThat(x.hashCode()).as("Wrong hashcode calculation").isEqualTo(31 + 10); - assertThat(x.hashCode()).as("Wrong hashcode calculation").isEqualTo(x.hashCode()); + intDef.setValue( x, new Integer( 10 ) ); + strDef.setValue( x, "abc" ); - } catch ( Exception e ) { - e.printStackTrace(); - fail( "Exception not expected" ); - } + assertThat(x.hashCode()).as("Wrong hashcode calculation").isEqualTo(31 + 10); + assertThat(x.hashCode()).as("Wrong hashcode calculation").isEqualTo(x.hashCode()); } @Test - public void testToString() { - try { - ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass4", null, new String[]{} ); - FieldDefinition long1Def = new FieldDefinition( "longAttr1", "long", true ); - FieldDefinition long2Def = new FieldDefinition( "longAttr2", "long", true ); - FieldDefinition doubleDef = new FieldDefinition( "doubleAttr", "double", true ); - FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); - FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", true ); - FieldDefinition dateDef = new FieldDefinition( "dateAttr", "java.util.Date", true ); - FieldDefinition str2Def = new FieldDefinition( "stringAttr2", "java.lang.String" ); - classDef.addField( long1Def ); - classDef.addField( long2Def ); - classDef.addField( doubleDef ); - classDef.addField( intDef ); - classDef.addField( strDef ); - classDef.addField( dateDef ); - classDef.addField( str2Def ); - - Class clazz = build(builder, classDef); - long1Def.setReadWriteAccessor( store.getAccessor( clazz, long1Def.getName() ) ); - long2Def.setReadWriteAccessor( store.getAccessor( clazz, long2Def.getName() ) ); - doubleDef.setReadWriteAccessor( store.getAccessor( clazz, doubleDef.getName() ) ); - intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); - strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName() ) ); - dateDef.setReadWriteAccessor( store.getAccessor( clazz, dateDef.getName() ) ); - str2Def.setReadWriteAccessor( store.getAccessor( clazz, str2Def.getName() ) ); - - Object x = clazz.newInstance(); - - long1Def.setValue( x, new Long( 20 ) ); - long2Def.setValue( x, new Long( 30 ) ); - doubleDef.setValue( x, new Double( 50.0 ) ); - intDef.setValue( x, new Integer( 10 ) ); - strDef.setValue( x, "abc" ); - dateDef.setValue( x, new Date( 1000 ) ); - str2Def.setValue( x, "instance1" ); - - String result = x.toString(); - - assertThat(result).contains(long1Def.getName()); - assertThat(result).contains(long2Def.getName()); - assertThat(result).contains(doubleDef.getName()); - assertThat(result).contains(intDef.getName()); - assertThat(result).contains(strDef.getName()); - assertThat(result).contains(dateDef.getName()); - assertThat(result).contains(str2Def.getName()); - - } catch ( Exception e ) { - e.printStackTrace(); - fail( "Exception not expected" ); - } + public void testToString() throws Exception { + ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); + + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass4", null, new String[]{} ); + FieldDefinition long1Def = new FieldDefinition( "longAttr1", "long", true ); + FieldDefinition long2Def = new FieldDefinition( "longAttr2", "long", true ); + FieldDefinition doubleDef = new FieldDefinition( "doubleAttr", "double", true ); + FieldDefinition intDef = new FieldDefinition( "intAttr", "int", true ); + FieldDefinition strDef = new FieldDefinition( "stringAttr", "java.lang.String", true ); + FieldDefinition dateDef = new FieldDefinition( "dateAttr", "java.util.Date", true ); + FieldDefinition str2Def = new FieldDefinition( "stringAttr2", "java.lang.String" ); + classDef.addField( long1Def ); + classDef.addField( long2Def ); + classDef.addField( doubleDef ); + classDef.addField( intDef ); + classDef.addField( strDef ); + classDef.addField( dateDef ); + classDef.addField( str2Def ); + + Class clazz = build(builder, classDef); + long1Def.setReadWriteAccessor( store.getAccessor( clazz, long1Def.getName() ) ); + long2Def.setReadWriteAccessor( store.getAccessor( clazz, long2Def.getName() ) ); + doubleDef.setReadWriteAccessor( store.getAccessor( clazz, doubleDef.getName() ) ); + intDef.setReadWriteAccessor( store.getAccessor( clazz, intDef.getName() ) ); + strDef.setReadWriteAccessor( store.getAccessor( clazz, strDef.getName() ) ); + dateDef.setReadWriteAccessor( store.getAccessor( clazz, dateDef.getName() ) ); + str2Def.setReadWriteAccessor( store.getAccessor( clazz, str2Def.getName() ) ); + + Object x = clazz.newInstance(); + + long1Def.setValue( x, new Long( 20 ) ); + long2Def.setValue( x, new Long( 30 ) ); + doubleDef.setValue( x, new Double( 50.0 ) ); + intDef.setValue( x, new Integer( 10 ) ); + strDef.setValue( x, "abc" ); + dateDef.setValue( x, new Date( 1000 ) ); + str2Def.setValue( x, "instance1" ); + + String result = x.toString(); + + assertThat(result).contains(long1Def.getName()); + assertThat(result).contains(long2Def.getName()); + assertThat(result).contains(doubleDef.getName()); + assertThat(result).contains(intDef.getName()); + assertThat(result).contains(strDef.getName()); + assertThat(result).contains(dateDef.getName()); + assertThat(result).contains(str2Def.getName()); } @Test - public void testConstructorWithFields() { - try { - ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - - ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass5", null, new String[]{} ); - - String[] types = new String[]{"byte", "short", "int", "long", "float", "double", "char", "java.lang.String", "boolean"}; - FieldDefinition[] fields = new FieldDefinition[types.length]; - for ( int i = 0; i < types.length; i++ ) { - String attrName = types[i].substring( types[i].lastIndexOf( '.' ) + 1 ); - attrName = attrName.substring( 0, 1 ).toLowerCase() + attrName.substring( 1 ) + "Attr"; - fields[i] = new FieldDefinition( attrName, // attr name - types[i], // attr type - i % 2 == 0 ); // half of them are key - classDef.addField( fields[i] ); - } - - Class clazz = build(builder, classDef); - - for ( FieldDefinition field : fields ) { - field.setReadWriteAccessor( store.getAccessor( clazz, field.getName() ) ); - } - - Constructor< ? >[] cons = clazz.getConstructors(); - - assertThat(cons.length).isEqualTo(3); - for ( Constructor< ? > c : cons ) { - Class< ? >[] ptypes = c.getParameterTypes(); - if ( ptypes.length == 0 ) { - // default constructor - } else if ( ptypes.length == fields.length ) { - // constructor with fields - for ( int i = 0; i < ptypes.length; i++ ) { - if ( !ptypes[i].equals( fields[i].getType() ) ) { - fail( "Wrong parameter in constructor. index=" + i + " expected=" + fields[i].getType() + " found=" + ptypes[i] ); - } - } - - // test actual invocation - Object instance = c.newInstance( (byte) 1, (short) 2, 3, 4l, 5.0f, 6.0d, 'a', "xyz", true ); - - assertThat(fields[0].getValue(instance)).isEqualTo((byte) 1); - assertThat(fields[1].getValue(instance)).isEqualTo((short) 2); - assertThat(fields[2].getValue(instance)).isEqualTo(3); - assertThat(fields[3].getValue(instance)).isEqualTo(4l); - assertThat(fields[4].getValue(instance)).isEqualTo(5.0f); - assertThat(fields[5].getValue(instance)).isEqualTo(6.0d); - assertThat(fields[6].getValue(instance)).isEqualTo('a'); - assertThat(fields[7].getValue(instance)).isEqualTo("xyz"); - assertThat(fields[8].getValue(instance)).isEqualTo(true); - } else if ( ptypes.length == ( fields.length / 2 +1 ) ) { // as defined in the beginning of the test - // constructor with key fields - int i = 0; - for ( FieldDefinition field : fields ) { - if ( field.isKey() && !ptypes[i++].equals( field.getType() ) ) { - fail( "Wrong parameter in constructor. index=" + i + " expected=" + field.getType() + " found=" + ptypes[i] ); - } - } - // test actual invocation - Object instance = c.newInstance( (byte) 1, 3, 5.0f, 'a', true ); - - assertThat(fields[0].getValue(instance)).isEqualTo((byte) 1); - assertThat(fields[2].getValue(instance)).isEqualTo(3); - assertThat(fields[4].getValue(instance)).isEqualTo(5.0f); - assertThat(fields[6].getValue(instance)).isEqualTo('a'); - assertThat(fields[8].getValue(instance)).isEqualTo(true); - - } else { - fail( "Unexpected constructor: " + c.toString() ); - } - } - - } catch ( Exception e ) { - e.printStackTrace(); - fail( "Unexpected Exception: " + e.getMessage() ); - } + public void testConstructorWithFields() throws Exception { + ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); + + ClassDefinition classDef = new ClassDefinition( "org.drools.TestClass5", null, new String[]{} ); + + String[] types = new String[]{"byte", "short", "int", "long", "float", "double", "char", "java.lang.String", "boolean"}; + FieldDefinition[] fields = new FieldDefinition[types.length]; + for ( int i = 0; i < types.length; i++ ) { + String attrName = types[i].substring( types[i].lastIndexOf( '.' ) + 1 ); + attrName = attrName.substring( 0, 1 ).toLowerCase() + attrName.substring( 1 ) + "Attr"; + fields[i] = new FieldDefinition( attrName, // attr name + types[i], // attr type + i % 2 == 0 ); // half of them are key + classDef.addField( fields[i] ); + } + + Class clazz = build(builder, classDef); + + for ( FieldDefinition field : fields ) { + field.setReadWriteAccessor( store.getAccessor( clazz, field.getName() ) ); + } + + Constructor< ? >[] cons = clazz.getConstructors(); + + assertThat(cons.length).isEqualTo(3); + for ( Constructor< ? > c : cons ) { + Class< ? >[] ptypes = c.getParameterTypes(); + if ( ptypes.length == 0 ) { + // default constructor + } else if ( ptypes.length == fields.length ) { + // constructor with fields + for ( int i = 0; i < ptypes.length; i++ ) { + if ( !ptypes[i].equals( fields[i].getType() ) ) { + fail( "Wrong parameter in constructor. index=" + i + " expected=" + fields[i].getType() + " found=" + ptypes[i] ); + } + } + + // test actual invocation + Object instance = c.newInstance( (byte) 1, (short) 2, 3, 4l, 5.0f, 6.0d, 'a', "xyz", true ); + + assertThat(fields[0].getValue(instance)).isEqualTo((byte) 1); + assertThat(fields[1].getValue(instance)).isEqualTo((short) 2); + assertThat(fields[2].getValue(instance)).isEqualTo(3); + assertThat(fields[3].getValue(instance)).isEqualTo(4l); + assertThat(fields[4].getValue(instance)).isEqualTo(5.0f); + assertThat(fields[5].getValue(instance)).isEqualTo(6.0d); + assertThat(fields[6].getValue(instance)).isEqualTo('a'); + assertThat(fields[7].getValue(instance)).isEqualTo("xyz"); + assertThat(fields[8].getValue(instance)).isEqualTo(true); + } else if ( ptypes.length == ( fields.length / 2 +1 ) ) { // as defined in the beginning of the test + // constructor with key fields + int i = 0; + for ( FieldDefinition field : fields ) { + if ( field.isKey() && !ptypes[i++].equals( field.getType() ) ) { + fail( "Wrong parameter in constructor. index=" + i + " expected=" + field.getType() + " found=" + ptypes[i] ); + } + } + // test actual invocation + Object instance = c.newInstance( (byte) 1, 3, 5.0f, 'a', true ); + + assertThat(fields[0].getValue(instance)).isEqualTo((byte) 1); + assertThat(fields[2].getValue(instance)).isEqualTo(3); + assertThat(fields[4].getValue(instance)).isEqualTo(5.0f); + assertThat(fields[6].getValue(instance)).isEqualTo('a'); + assertThat(fields[8].getValue(instance)).isEqualTo(true); + + } else { + fail( "Unexpected constructor: " + c.toString() ); + } + } } @Test - public void testGetResourcesJBRULES3122() { - try { - ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); - - ClassDefinition classDef = new ClassDefinition("org.drools.TestClass4", null, new String[] {}); - Class clazz = build(builder, classDef); - ClassLoader cl = clazz.getClassLoader(); - - // We expect normal classloader stuff to work - assertThat(cl.getResources("not-there.txt").hasMoreElements()).isFalse(); - } catch (Exception e) { - e.printStackTrace(); - fail("Exception not expected: " + e.getMessage()); - } + public void testGetResourcesJBRULES3122() throws Exception { + ClassBuilder builder = new TraitClassBuilderFactory().getBeanClassBuilder(); + + ClassDefinition classDef = new ClassDefinition("org.drools.TestClass4", null, new String[] {}); + Class clazz = build(builder, classDef); + ClassLoader cl = clazz.getClassLoader(); + + // We expect normal classloader stuff to work + assertThat(cl.getResources("not-there.txt").hasMoreElements()).isFalse(); } } From b87710112d7d1d4a0d5c1b26f0782d9d1bff1edc Mon Sep 17 00:00:00 2001 From: Paolo Bizzarri Date: Thu, 10 Oct 2024 16:46:47 +0200 Subject: [PATCH 13/13] Removed last bits of JUnit5 from tests --- drools-traits/pom.xml | 6 +- .../UseOfRuleFlowGroupPlusLockOnTest.java | 2 +- .../traits/compiler/CommonTraitTest.java | 4 +- .../factmodel/traits/LegacyTraitTest.java | 35 +++----- .../factmodel/traits/StandaloneTest.java | 6 +- .../TraitFieldsAndLegacyClassesTest.java | 88 ++++++++++--------- .../factmodel/traits/TraitMapCoreTest.java | 21 +++-- .../TraitTypeGenerationTest.java | 4 +- .../core/factmodel/ClassBuilderTest.java | 6 +- .../factmodel/InstancesHashcodedTest.java | 2 +- .../traits/core/factmodel/JeneratorTest.java | 2 +- .../core/factmodel/TripleStoreTest.java | 2 +- .../traits/core/meta/org/MetadataTest.java | 2 +- .../traits/core/util/HierarchyTest.java | 2 +- 14 files changed, 95 insertions(+), 87 deletions(-) diff --git a/drools-traits/pom.xml b/drools-traits/pom.xml index f2c5f685fd1..70d75678a7b 100644 --- a/drools-traits/pom.xml +++ b/drools-traits/pom.xml @@ -184,10 +184,10 @@ - junit - junit + org.junit.jupiter + junit-jupiter test - + org.mockito mockito-core diff --git a/drools-traits/src/test/java/org/drools/traits/UseOfRuleFlowGroupPlusLockOnTest.java b/drools-traits/src/test/java/org/drools/traits/UseOfRuleFlowGroupPlusLockOnTest.java index 76a68c0a133..12460df4967 100644 --- a/drools-traits/src/test/java/org/drools/traits/UseOfRuleFlowGroupPlusLockOnTest.java +++ b/drools-traits/src/test/java/org/drools/traits/UseOfRuleFlowGroupPlusLockOnTest.java @@ -19,8 +19,8 @@ package org.drools.traits; import org.drools.kiesession.agenda.DefaultAgenda; +import org.junit.jupiter.api.Test; import org.drools.core.reteoo.ReteDumper; -import org.junit.Test; import org.kie.api.KieBase; import org.kie.api.event.rule.DebugAgendaEventListener; import org.kie.api.io.ResourceType; diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/CommonTraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/CommonTraitTest.java index 7f42e4fd45e..efa4b0786d1 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/CommonTraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/CommonTraitTest.java @@ -21,7 +21,7 @@ import org.drools.kiesession.rulebase.InternalKnowledgeBase; import org.drools.kiesession.rulebase.KnowledgeBaseFactory; import org.drools.traits.core.base.evaluators.IsAEvaluatorDefinition; -import org.junit.BeforeClass; +import org.junit.jupiter.api.BeforeAll; import org.kie.api.KieBase; import org.kie.api.io.ResourceType; import org.kie.internal.builder.KnowledgeBuilder; @@ -33,7 +33,7 @@ public class CommonTraitTest { - @BeforeClass + @BeforeAll public static void beforeClass() { System.setProperty(EvaluatorOption.PROPERTY_NAME + "isA", IsAEvaluatorDefinition.class.getName()); } diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LegacyTraitTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LegacyTraitTest.java index e8a27382f9e..47f7e41b0d7 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LegacyTraitTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/LegacyTraitTest.java @@ -30,9 +30,8 @@ import org.drools.traits.core.factmodel.TraitFactoryImpl; import org.drools.traits.core.factmodel.VirtualPropertyMode; import org.drools.traits.core.util.StandaloneTraitFactory; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.kie.api.KieBase; import org.kie.api.builder.Message; import org.kie.api.definition.type.PropertyReactive; @@ -48,24 +47,14 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.drools.traits.compiler.factmodel.traits.TraitTestUtils.createStandaloneTraitFactory; -@RunWith(Parameterized.class) public class LegacyTraitTest extends CommonTraitTest { private static final Logger LOGGER = LoggerFactory.getLogger(LegacyTraitTest.class); - public VirtualPropertyMode mode; - - @Parameterized.Parameters public static Collection modes() { return List.of(VirtualPropertyMode.MAP, VirtualPropertyMode.TRIPLES); } - public LegacyTraitTest(VirtualPropertyMode m) { - this.mode = m; - } - - - private KieSession getSessionFromString(String drl) { KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), @@ -143,8 +132,9 @@ public void setPers(Pers pers) { } - @Test - public void traitWithPojoInterface() { + @ParameterizedTest + @MethodSource("modes") + public void traitWithPojoInterface(VirtualPropertyMode mode) { String source = "package org.drools.compiler.test;\n" + "import " + LegacyTraitTest.Procedure.class.getCanonicalName() + ";\n" + "import " + LegacyTraitTest.class.getCanonicalName() + ";\n" + @@ -231,8 +221,9 @@ public static interface Trunk extends Root { } @Trait public static interface Foo extends Trunk { } - @Test - public void traitWithMixedInterfacesExtendingEachOther() { + @ParameterizedTest + @MethodSource("modes") + public void traitWithMixedInterfacesExtendingEachOther(VirtualPropertyMode mode) { String source = "package org.drools.compiler.test;" + "import " + BarImpl.class.getCanonicalName() + "; " + "import " + Foo.class.getCanonicalName() + "; " + @@ -295,8 +286,9 @@ public void traitWithMixedInterfacesExtendingEachOther() { - @Test - public void testTraitWithNonAccessorMethodShadowing() throws Exception { + @ParameterizedTest + @MethodSource("modes") + public void testTraitWithNonAccessorMethodShadowing(VirtualPropertyMode mode) throws Exception { StandaloneTraitFactory factory = createStandaloneTraitFactory(); SomeInterface r = (SomeInterface) factory.don(new SomeClass(), SomeInterface.class); r.prepare(); @@ -304,8 +296,9 @@ public void testTraitWithNonAccessorMethodShadowing() throws Exception { assertThat(r.doThis("that")).isEqualTo("I did that"); } - @Test() - public void testPojoExtendInterface() { + @ParameterizedTest + @MethodSource("modes") + public void testPojoExtendInterface(VirtualPropertyMode mode) { // DROOLS-697 // It is now allowed for a declared type to extend an interface // The interface itself will be added to the implements part of the generated class diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/StandaloneTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/StandaloneTest.java index f3c3b352cd3..61b4d12d593 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/StandaloneTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/StandaloneTest.java @@ -25,8 +25,8 @@ import org.drools.base.factmodel.traits.Thing; import org.drools.traits.core.util.StandaloneTraitFactory; import org.drools.wiring.api.classloader.ProjectClassLoader; -import org.junit.Before; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,7 +39,7 @@ public class StandaloneTest { private StandaloneTraitFactory factory; - @Before + @BeforeEach public void init() { ProjectClassLoader loader = ProjectClassLoader.createProjectClassLoader(); factory = createStandaloneTraitFactory(); diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitFieldsAndLegacyClassesTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitFieldsAndLegacyClassesTest.java index a1b24c23664..0ecf89bd251 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitFieldsAndLegacyClassesTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitFieldsAndLegacyClassesTest.java @@ -27,11 +27,10 @@ import org.drools.traits.compiler.ReviseTraitTestWithPRAlwaysCategory; import org.drools.traits.core.factmodel.TraitFactoryImpl; import org.drools.traits.core.factmodel.VirtualPropertyMode; -import org.junit.Ignore; -import org.junit.Test; import org.junit.experimental.categories.Category; -import org.junit.runner.RunWith; -import org.junit.runners.Parameterized; +import org.junit.jupiter.api.Disabled; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; import org.kie.api.KieBase; import org.kie.api.definition.type.FactType; import org.kie.api.io.ResourceType; @@ -41,23 +40,18 @@ import static org.assertj.core.api.Assertions.assertThat; -@RunWith(Parameterized.class) public class TraitFieldsAndLegacyClassesTest extends CommonTraitTest { - public VirtualPropertyMode mode; - @Parameterized.Parameters - public static Collection modes() { - return List.of(VirtualPropertyMode.MAP , VirtualPropertyMode.TRIPLES ); + public static Collection modes() { + return List.of(VirtualPropertyMode.MAP , VirtualPropertyMode.TRIPLES ); } - public TraitFieldsAndLegacyClassesTest( VirtualPropertyMode m ) { - this.mode = m; - } - @Test - public void testTraitFieldUpdate0() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate0(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits0;\n" + @@ -122,8 +116,9 @@ public void testTraitFieldUpdate0() { - @Test - public void testTraitFieldUpdate1() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate1(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits;\n" + @@ -210,8 +205,9 @@ public void testTraitFieldUpdate1() { } - @Test - public void testTraitFieldUpdate2() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate2(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits2;\n" + @@ -308,8 +304,9 @@ public void testTraitFieldUpdate2() { assertThat(list).contains("correct"); } - @Test - public void testTraitFieldUpdate3() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate3(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits3;\n" + @@ -405,8 +402,9 @@ public void testTraitFieldUpdate3() { } @Category(ReviseTraitTestWithPRAlwaysCategory.class) - @Test - public void testTraitFieldUpdate4() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate4(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits4;\n" + @@ -483,8 +481,9 @@ public void testTraitFieldUpdate4() { assertThat(list).contains("correct"); } - @Test - public void testTraitFieldUpdate5() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate5(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits5;\n" + @@ -567,8 +566,9 @@ public void testTraitFieldUpdate5() { assertThat(list).contains("correct"); } - @Test - public void testTraitFieldUpdate6() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate6(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits6;\n" + @@ -659,8 +659,9 @@ public void testTraitFieldUpdate6() { } - @Test - public void testTraitFieldUpdate7() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate7(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits;\n" + @@ -743,8 +744,9 @@ public void testTraitFieldUpdate7() { - @Test - public void testTraitFieldUpdate8() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate8(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits8;\n" + @@ -825,8 +827,9 @@ public void testTraitFieldUpdate8() { assertThat(list).contains("correct"); } - @Test - public void testTraitFieldUpdate9() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate9(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits9;\n" + @@ -916,8 +919,9 @@ public void testTraitFieldUpdate9() { assertThat(list).contains("correct"); } - @Test - public void testTraitFieldUpdate10() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitFieldUpdate10(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits;\n" + @@ -1008,8 +1012,9 @@ public void testTraitFieldUpdate10() { assertThat(list).contains("correct"); } - @Test - public void testTraitTwoParentOneChild() { + @ParameterizedTest + @MethodSource("modes") + public void testTraitTwoParentOneChild(VirtualPropertyMode mode) { String drl = "" + "package org.drools.factmodel.traits;\n" + @@ -1119,8 +1124,10 @@ public void testTraitTwoParentOneChild() { assertThat(list).contains("correct"); } - @Test @Ignore - public void testTraitWithPositionArgs(){ + @ParameterizedTest + @Disabled + @MethodSource("modes") + public void testTraitWithPositionArgs(VirtualPropertyMode mode){ String drl = "" + "package org.drools.traits.test;\n" + @@ -1258,8 +1265,9 @@ public void testTraitWithPositionArgs(){ } - @Test - public void singlePositionTraitTest(){ + @ParameterizedTest + @MethodSource("modes") + public void singlePositionTraitTest(VirtualPropertyMode mode){ String drl = "" + diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java index 5540d91abc1..d4d9cae880c 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/factmodel/traits/TraitMapCoreTest.java @@ -29,7 +29,8 @@ import org.drools.traits.core.factmodel.TraitFactoryImpl; import org.drools.base.factmodel.traits.Traitable; import org.drools.traits.core.factmodel.VirtualPropertyMode; -import org.junit.Test; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; import org.kie.api.runtime.KieSession; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +41,8 @@ public class TraitMapCoreTest extends CommonTraitTest { private static final Logger LOGGER = LoggerFactory.getLogger(TraitMapCoreTest.class); - @Test(timeout=10000) + @Test() + @Timeout(value = 10000) public void testMapCoreManyTraits( ) { String source = "package org.drools.test;\n" + "\n" + @@ -98,7 +100,8 @@ public void testMapCoreManyTraits( ) { assertThat(map.get("GPA")).isEqualTo(3.0); } - @Test(timeout=10000) + @Test() + @Timeout(value = 10000) public void donMapTest() { String source = "package org.drools.traits.test; \n" + "import java.util.*\n;" + @@ -149,7 +152,8 @@ public void donMapTest() { } - @Test(timeout=10000) + @Test() + @Timeout(value = 10000) public void testMapCore2( ) { String source = "package org.drools.base.factmodel.traits.test;\n" + "\n" + @@ -261,7 +265,8 @@ public void testMapCore2( ) { } - @Test(timeout=10000) + @Test() + @Timeout(value = 10000) public void testMapCoreAliasing( ) { String source = "package org.drools.base.factmodel.traits.test;\n" + "\n" + @@ -324,7 +329,8 @@ public void testMapCoreAliasing( ) { } - @Test(timeout=10000) + @Test() + @Timeout(value = 10000) public void testMapCoreAliasingLogicalTrueWithTypeClash( ) { String source = "package org.drools.base.factmodel.traits.test;\n" + "\n" + @@ -872,7 +878,8 @@ public void testMapTraitNoType() assertThat(list).contains("correct1", "correct2"); } - @Test(timeout=10000) + @Test() + @Timeout(value = 10000) public void testMapTraitMismatchTypes() { String drl = "" + diff --git a/drools-traits/src/test/java/org/drools/traits/compiler/integrationtests/TraitTypeGenerationTest.java b/drools-traits/src/test/java/org/drools/traits/compiler/integrationtests/TraitTypeGenerationTest.java index 83da5692de6..51d42a2f5c4 100644 --- a/drools-traits/src/test/java/org/drools/traits/compiler/integrationtests/TraitTypeGenerationTest.java +++ b/drools-traits/src/test/java/org/drools/traits/compiler/integrationtests/TraitTypeGenerationTest.java @@ -30,7 +30,7 @@ import org.drools.traits.compiler.CommonTraitTest; import org.drools.traits.compiler.Person; import org.drools.traits.core.factmodel.Entity; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.kie.api.KieBase; import org.kie.api.KieServices; import org.kie.api.builder.KieBuilder; @@ -82,7 +82,7 @@ public void setB(final int b) { } } - @Test(timeout=10000) + @Test public void testWithDeclaredTypeAndTraitInDifferentPackages() { // DROOLS-91 final String str1 = diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java index cc0dc47cf07..10f6dffe16f 100755 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/ClassBuilderTest.java @@ -33,9 +33,9 @@ import org.drools.base.factmodel.FieldDefinition; import org.drools.core.rule.JavaDialectRuntimeData; import org.drools.wiring.dynamic.PackageClassLoader; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.drools.wiring.api.classloader.ProjectClassLoader; -import org.junit.Before; -import org.junit.Test; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; @@ -47,7 +47,7 @@ public class ClassBuilderTest { ClassLoader classLoader; JavaDialectRuntimeData data; - @Before + @BeforeEach public void setUp() throws Exception { data = new JavaDialectRuntimeData(); } diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/InstancesHashcodedTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/InstancesHashcodedTest.java index 7e04e9e46a4..c33ad8e600c 100755 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/InstancesHashcodedTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/InstancesHashcodedTest.java @@ -28,8 +28,8 @@ import org.drools.base.factmodel.FieldDefinition; import org.drools.core.rule.JavaDialectRuntimeData; import org.drools.wiring.dynamic.PackageClassLoader; +import org.junit.jupiter.api.Test; import org.drools.wiring.api.classloader.ProjectClassLoader; -import org.junit.Test; import static org.drools.util.ClassUtils.convertClassToResourcePath; diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/JeneratorTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/JeneratorTest.java index fd656cfb569..134ad78c190 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/JeneratorTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/JeneratorTest.java @@ -22,7 +22,7 @@ import java.util.jar.JarEntry; import java.util.jar.JarInputStream; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java b/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java index e09bf9442c1..288f463b008 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/factmodel/TripleStoreTest.java @@ -23,7 +23,7 @@ import java.util.Collection; import java.util.List; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.kie.api.runtime.rule.Variable; import static org.assertj.core.api.Assertions.assertThat; diff --git a/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java b/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java index ca9f21fc236..4f454dd3283 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/meta/org/MetadataTest.java @@ -40,7 +40,7 @@ import org.drools.traits.core.metadata.Lit; import org.drools.traits.core.metadata.MetadataContainer; import org.drools.traits.core.metadata.With; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java b/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java index 6dc8d854971..94c7d520aea 100644 --- a/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java +++ b/drools-traits/src/test/java/org/drools/traits/core/util/HierarchyTest.java @@ -21,7 +21,7 @@ import org.drools.traits.core.factmodel.CodedHierarchy; import org.drools.traits.core.factmodel.HierarchyEncoder; import org.drools.traits.core.factmodel.IndexedTypeHierarchy; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory;