Code Search for Developers
 
 
  

TermFinderFacadeBeanTestWithKB.java from Texai at Krugle


Show TermFinderFacadeBeanTestWithKB.java syntax highlighted

/*
 * TermFinderFacadeBeanTestWithKB.java
 * JUnit based test
 *
 * Created on November 1, 2006, 10:11 PM
 *
 * Copyright (C) 2006 Stephen L. Reed.
 *
 * This program is free software; you can redistribute it and/or modify it under the terms
 * of the GNU General Public License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package org.texai.kb.ejb.session;

import java.util.ArrayList;
import java.util.List;
import junit.framework.*;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import net.sf.ehcache.CacheManager;
import org.texai.kb.CacheInitializer;
import org.texai.kb.Constants;
import org.texai.kb.ejb.entity.AbstractReifiedTerm;
import org.texai.kb.ejb.entity.AbstractTerm;
import org.texai.kb.ejb.entity.AtomicTerm;
import org.texai.kb.ejb.entity.PString;
import org.texai.kb.ejb.session.shared.AssociationFinderBean;
import org.texai.kb.ejb.session.shared.TermDefinitionAccessorBean;
import org.texai.kb.ejb.session.shared.TermFinderFacadeBean;

/**
 *
 * @author reed
 */
public class TermFinderFacadeBeanTestWithKB extends TestCase {
  
  /** the entity manager factory */
  private static EntityManagerFactory entityManagerFactory;
  
  /** the entity manager */
  private static EntityManager entityManager;
  
  public TermFinderFacadeBeanTestWithKB(String testName) {
    super(testName);
  }

  protected void setUp() throws Exception {
    if (entityManager == null) {
      System.out.println("oneTimeSetup");
      try {
        getClass().getClassLoader().setDefaultAssertionStatus(true);
        CacheInitializer.initializeCaches();
        System.out.println("setUp: creating EntityManager");
        entityManagerFactory = Persistence.createEntityManagerFactory("TestPersistenceUnit");
        entityManager = entityManagerFactory.createEntityManager();
        System.out.println("entityManager created");
     } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
      }
    }
    if (entityManager.getTransaction().isActive()) {
      entityManager.getTransaction().rollback();
    }
    try {
    } catch (Exception e) {
      e.printStackTrace();
      fail(e.getMessage());
    }
  }

  protected void tearDown() throws Exception {
  }

  /**
   * Test of getReasonWhyFormulaIsNotWellFormed method, of class org.texai.kb.ejb.session.TermFinderFacadeBean.
   */
  public void testGetReasonWhyFormulaIsNotWellFormed() {
    System.out.println("getReasonWhyFormulaIsNotWellFormed");
    EntityTransaction entityTransaction = entityManager.getTransaction();
    entityTransaction.begin();
    assertTrue(entityTransaction.isActive());
    
    TermFinderFacadeBean instance = new TermFinderFacadeBean();
    instance.setEntityManager(entityManager);
    final TermDefinitionAccessorBean termDefinitionAccessor = new TermDefinitionAccessorBean();
    termDefinitionAccessor.setTermFinderFacade(instance);
    final AssociationFinderBean associationFinder = new AssociationFinderBean();
    associationFinder.setEntityManager(entityManager);
    associationFinder.setTermFinderFacade(instance);
    termDefinitionAccessor.setAssociationFinder(associationFinder);
    instance.setTermDefinitionAccessor(termDefinitionAccessor);
    
    AbstractReifiedTerm context = instance.findAtomicTermByTermName(Constants.TERM_NAME_THING);
    List<AbstractTerm> args = new ArrayList<AbstractTerm>(0);
    AtomicTerm predicate = instance.findAtomicTermByTermName(Constants.TERM_NAME_THING);
    String result = instance.getReasonWhyFormulaIsNotWellFormed(
          predicate,
          args,
          context);
    assertEquals("no arity asserted for predicate Thing", result);
    
    predicate = instance.findAtomicTermByTermName(Constants.TERM_NAME_GENLS);
    result = instance.getReasonWhyFormulaIsNotWellFormed(
          predicate,
          args,
          context);
    assertEquals("wrong number of args for predicate, arity: 2, args: []", result);
    
    args.add(new PString("abc"));
    args.add(instance.findAtomicTermByTermName(Constants.TERM_NAME_THING));
    result = instance.getReasonWhyFormulaIsNotWellFormed(
          predicate,
          args,
          context);
    assertEquals("Thing is not a context (Microtheory)", result);
    
    context = instance.findAtomicTermByTermName(Constants.TERM_NAME_BASE_KB);
    result = instance.getReasonWhyFormulaIsNotWellFormed(
          predicate,
          args,
          context);
    assertEquals("argument \"abc\" having types [ControlCharacterFreeString, CharacterString]\n" +
          "  is not required type Collection\n" +
          "  at argument position 1\n" +
          "  of predicate genls\n" +
          "  having args [\"abc\", Thing]", result);
    
    args.clear();
    args.add(instance.findAtomicTermByTermName("EiffelTower"));
    args.add(instance.findAtomicTermByTermName(Constants.TERM_NAME_THING));
    result = instance.getReasonWhyFormulaIsNotWellFormed(
          predicate,
          args,
          context);
    assertEquals("argument EiffelTower having types [Location-Underspecified, Thing, " +
            "Trajector-Underspecified, Individual]\n" +
            "  is not required type Collection\n" +
            "  at argument position 1\n" +
            "  of predicate genls\n" +
            "  having args [EiffelTower, Thing]", result);
    
    predicate = instance.findAtomicTermByTermName(Constants.TERM_NAME_ISA);
    result = instance.getReasonWhyFormulaIsNotWellFormed(
          predicate,
          args,
          context);
    assertEquals("", result);
    
    entityTransaction.commit();
    assertTrue(!entityTransaction.isActive());
    System.out.println("  getReasonWhyFormulaIsNotWellFormed OK");
  }
    
  
  /** Performs one time tear down of test harness. This must be the last test method. */
  public void testOneTimeTearDown() {
    System.out.println("oneTimeTearDown");
    CacheManager.getInstance().shutdown();
    entityManager.close();
    entityManagerFactory.close();
  }
}




See more files for this project here

Texai

Texai is an chatbot that intelligently seeks to acquire knowledge and friendly behaviors.

Project homepage: http://sourceforge.net/projects/texai
Programming language(s): Java,Shell Script,XML
License: other

  AssociationFinderBeanTest.java
  DomainEntityLoaderBeanTest.java
  DomainEntityPersisterBeanTest.java
  TermDefinitionAccessorBeanTest.java
  TermFinderFacadeBeanTest.java
  TermFinderFacadeBeanTestWithKB.java