Copilot commented on code in PR #7176:
URL: https://github.com/apache/incubator-kie/pull/7176#discussion_r4231964648


##########
drools-test-coverage/test-compiler-integration/src/test/java/org/drools/compiler/integrationtests/PropertyChangeSupportTest.java:
##########
@@ -99,4 +137,117 @@ public void testPropertyChanged() {
 
         assertThat(fact.getValue()).isEqualTo("VAL1");
     }
-}
\ No newline at end of file
+
+    @Test
+    public void testDeleteUnregistersFactInsertedAsDynamic() {
+        // a fact inserted with the dynamic flag must be released on delete, 
even if its type isn't dynamic
+        String drl =
+                "import " + DynamicFact.class.getCanonicalName() + ";\n" +
+                "import " + Trigger.class.getCanonicalName() + ";\n" +
+                "global java.util.List facts\n" +
+                "rule insertDynamic\n" +
+                "   when\n" +
+                "     $t: Trigger()\n" +
+                "   then\n" +
+                "     delete($t);\n" +
+                "     DynamicFact fact = new DynamicFact();\n" +
+                "     fact.setName($t.getName());\n" +
+                "     facts.add(fact);\n" +
+                "     insert(fact, true);\n" +
+                " end\n" +
+                " \n" +
+                " rule deleteDynamic\n" +
+                "   when\n" +
+                "     $fact: DynamicFact()\n" +
+                "   then\n" +
+                "     delete($fact);\n" +
+                " end";
+
+        KieSession ksession = new KieHelper().addContent(drl, 
ResourceType.DRL).build().newKieSession();
+        try {
+            List<DynamicFact> facts = new ArrayList<>();
+            ksession.setGlobal("facts", facts);
+
+            for (int i = 0; i < 10; i++) {
+                ksession.insert(new Trigger("t" + i));
+                ksession.fireAllRules();
+            }
+
+            assertThat(ksession.getFactCount()).isZero();
+            assertThat(facts).hasSize(10).allSatisfy(fact -> 
assertThat(fact.getListenerCount()).isZero());
+        } finally {
+            ksession.dispose();
+        }
+    }
+
+    @Test
+    public void testClassLevelAnnotationMakesTypeDynamic() {
+        // the class annotation must work like declaring the type with 
@propertyChangeSupport in DRL
+        String drl =
+                "import " + AnnotatedDynamicFact.class.getCanonicalName() + 
";\n" +
+                "rule rule1\n" +
+                "   when\n" +
+                "     $fact: AnnotatedDynamicFact(name == \"user1\")\n" +
+                "   then\n" +
+                "     $fact.setName(\"user2\");\n" +
+                " end\n" +
+                " \n" +
+                " rule rule2\n" +
+                "   when\n" +
+                "     $fact: AnnotatedDynamicFact(name == \"user2\")\n" +
+                "   then\n" +
+                "     $fact.setValue($fact.getValue() + \"VAL1\");\n" +
+                " end";
+
+        KieSession ksession = new KieHelper().addContent(drl, 
ResourceType.DRL).build().newKieSession();
+        try {
+            AnnotatedDynamicFact fact = new AnnotatedDynamicFact();
+            fact.setName("user1");
+            fact.setValue("");
+
+            FactHandle handle = ksession.insert(fact);
+            assertThat(fact.getListenerCount()).isEqualTo(1);
+
+            ksession.fireAllRules(10);
+
+            assertThat(fact.getName()).isEqualTo("user2");
+            assertThat(fact.getValue()).isEqualTo("VAL1");
+
+            ksession.delete(handle);
+            assertThat(fact.getListenerCount()).isZero();
+        } finally {
+            ksession.dispose();
+        }
+    }
+
+    @Test
+    public void testExpiryUnregistersDynamicFact() {
+        String drl =
+                "import " + ExpiringDynamicFact.class.getCanonicalName() + 
";\n" +
+                "rule rule1\n" +
+                "   when\n" +
+                "     ExpiringDynamicFact()\n" +
+                "   then\n" +
+                " end";
+
+        KieBase kbase = new KieHelper().addContent(drl, 
ResourceType.DRL).build(EventProcessingOption.STREAM);
+        KieSessionConfiguration ksconf = 
KieServices.get().newKieSessionConfiguration();
+        ksconf.setOption(ClockTypeOption.PSEUDO);
+        KieSession ksession = kbase.newKieSession(ksconf, null);
+        try {
+            ExpiringDynamicFact fact = new ExpiringDynamicFact();
+            ksession.insert(fact);
+            ksession.fireAllRules();
+            assertThat(fact.getListenerCount()).isEqualTo(1);
+
+            SessionPseudoClock clock = ksession.getSessionClock();

Review Comment:
   `KieSession#getSessionClock()` typically returns `SessionClock`, so 
assigning directly to `SessionPseudoClock` is a compile-time type error unless 
the API method is generic in this codebase. Update this to cast explicitly (or 
change the variable type to `SessionClock` and cast only where needed).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to