@Test
  public void testRemovePackage() throws Exception {
    final org.drools.rule.Package pkg1 = new org.drools.rule.Package("org.droos.test");
    pkg1.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    pkg1.addGlobal("global1", Object.class);
    pkg1.addGlobal("global2", Object.class);

    final org.drools.rule.Package pkg2 = new org.drools.rule.Package("org.droos.test");
    pkg2.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    pkg2.addGlobal("global1", Object.class);
    pkg2.addGlobal("global3", Object.class);

    final org.drools.rule.Package pkg3 = new org.drools.rule.Package("org.droos.test2");
    pkg3.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    pkg3.addGlobal("global3", Object.class);
    pkg3.addGlobal("global4", Object.class);

    this.ruleBase.addPackage(pkg1);
    this.ruleBase.addPackage(pkg2);
    this.ruleBase.addPackage(pkg3);

    this.ruleBase.removePackage(pkg1.getName());
    // packages were partially merged when adding, so removal
    // shall left only package 3 behind
    assertLength(1, this.ruleBase.getPackages());
    assertLength(2, this.ruleBase.getGlobals().values());

    this.ruleBase.removePackage(pkg3.getName());
    assertLength(0, this.ruleBase.getPackages());
    assertLength(0, this.ruleBase.getGlobals().values());
  }
 @Test
 public void testKeepReference() throws Exception {
   /* Make sure the RuleBase is referencing all 4 Working Memories */
   assertLength(4, this.ruleBase.getStatefulSessions());
   assertContains(this.wm1, this.ruleBase.getStatefulSessions());
   assertContains(this.wm2, this.ruleBase.getStatefulSessions());
   assertContains(this.wm3, this.ruleBase.getStatefulSessions());
   assertContains(this.wm4, this.ruleBase.getStatefulSessions());
 }
  @Test
  public void testAddPackage() throws Exception {
    final org.drools.rule.Package pkg1 = new org.drools.rule.Package("org.droos.test");
    pkg1.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    pkg1.addGlobal("global1", Object.class);
    pkg1.addGlobal("global2", Object.class);

    final org.drools.rule.Package pkg2 = new org.drools.rule.Package("org.droos.test");
    pkg2.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    pkg2.addGlobal("global1", Object.class);
    pkg2.addGlobal("global3", Object.class);

    final org.drools.rule.Package pkg3 = new org.drools.rule.Package("org.droos.test2");
    pkg3.setClassFieldAccessorCache(
        new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    pkg3.addGlobal("global3", Object.class);
    pkg3.addGlobal("global4", Object.class);

    this.ruleBase.addPackage(pkg1);
    // one package
    assertLength(1, this.ruleBase.getPackages());
    // two globals
    assertLength(2, this.ruleBase.getGlobals().values());
    // two globals in the package also
    assertLength(2, this.ruleBase.getPackages()[0].getGlobals().values());

    this.ruleBase.addPackage(pkg2);
    // packages merged, so still 1 package
    assertLength(1, this.ruleBase.getPackages());
    // globals merged, so 3 globals total
    assertLength(3, this.ruleBase.getGlobals().values());
    // three globals in the package also
    assertLength(3, this.ruleBase.getPackages()[0].getGlobals().values());

    this.ruleBase.addPackage(pkg3);
    // new package, so now we have 2 package
    assertLength(2, this.ruleBase.getPackages());
    // globals partially merged, so 4 globals total
    assertLength(4, this.ruleBase.getGlobals().values());
    // two globals in the package
    final org.drools.rule.Package[] pkgs = this.ruleBase.getPackages();
    for (int i = 0; i < pkgs.length; i++) {
      if (pkgs[i].getName().equals(pkg3.getName())) {
        assertLength(2, pkgs[i].getGlobals().values());
      }
    }
  }
 @Test
 public void testNoKeepReference() throws Exception {
   SessionConfiguration conf = new SessionConfiguration();
   conf.setKeepReference(false);
   final WorkingMemory wm5 = this.ruleBase.newStatefulSession(conf, null);
   final WorkingMemory wm6 = this.ruleBase.newStatefulSession(conf, null);
   assertLength(4, this.ruleBase.getStatefulSessions());
   assertNotContains(wm5, this.ruleBase.getStatefulSessions());
   assertNotContains(wm6, this.ruleBase.getStatefulSessions());
 }
  @Test
  public void testDispose() throws Exception {
    /*
     * Now lets test the dispose method on the WorkingMemory itself. dispose
     * doesn't need GC
     */
    this.wm3.dispose();

    /* Check only wm3 was removed */
    assertLength(3, this.ruleBase.getStatefulSessions());
    assertNotContains(this.wm3, this.ruleBase.getStatefulSessions());
  }
  /**
   * Tests the assertion of objects into LeftInputAdapterNode
   *
   * @throws Exception
   */
  @Test
  public void testAssertObjectWithoutMemory() throws Exception {
    final PropagationContext context =
        new PropagationContextImpl(0, PropagationContext.ASSERTION, null, null, null);

    final ReteooWorkingMemory workingMemory =
        new ReteooWorkingMemory(1, (ReteooRuleBase) RuleBaseFactory.newRuleBase());

    final LeftInputAdapterNode liaNode =
        new LeftInputAdapterNode(1, new MockObjectSource(15), buildContext);
    final MockLeftTupleSink sink = new MockLeftTupleSink();
    liaNode.addTupleSink(sink);

    final Object string1 = "cheese";

    // assert object
    final DefaultFactHandle f0 = (DefaultFactHandle) workingMemory.insert(string1);
    liaNode.assertObject(f0, context, workingMemory);

    final List asserted = sink.getAsserted();
    assertLength(1, asserted);
    final Tuple tuple0 = (Tuple) ((Object[]) asserted.get(0))[0];
    assertSame(string1, workingMemory.getObject(tuple0.get(0)));
  }