[jboss-cvs] JBossCache/tests/functional/org/jboss/cache/pojo/observer ...

Ben Wang bwang at jboss.com
Sat Jan 13 10:55:13 EST 2007


  User: bwang   
  Date: 07/01/13 10:55:13

  Added:       tests/functional/org/jboss/cache/pojo/observer 
                        LocalTest.java
  Log:
  JBCACHE-922 Merged src-50 and tests-50 into src and tests, respectively.
  
  Revision  Changes    Path
  1.1      date: 2007/01/13 15:55:13;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/observer/LocalTest.java
  
  Index: LocalTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software 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
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  
  package org.jboss.cache.pojo.observer;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.PojoCacheListener;
  import org.jboss.cache.pojo.observable.Observer;
  import org.jboss.cache.pojo.observable.Subject;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.pojo.test.Student;
  import org.jboss.aop.proxy.ClassProxy;
  
  import java.util.List;
  import java.util.Map;
  import java.util.HashMap;
  import java.util.ArrayList;
  import java.util.Set;
  import java.util.HashSet;
  import java.lang.reflect.Field;
  
  /**
   * This is a test case illustrating how to subscribe to POJO events using Observer.
   *
   * @author Ben Wang
   */
  
  public class LocalTest extends TestCase
  {
     Log log = LogFactory.getLog(LocalTest.class);
     PojoCache cache_;
  
     public LocalTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     private Person createPerson(String id, String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        Address add = new Address();
        add.setZip(95123);
        add.setCity("San Jose");
        p.setAddress(add);
        cache_.attach(id, p);
        return p;
     }
  
     private Student createStudent(String id, String name, int age, String grade)
     {
        Student p = new Student();
        p.setName(name);
        p.setAge(age);
        p.setYear(grade);
        Address add = new Address();
        add.setZip(95123);
        add.setCity("San Jose");
        p.setAddress(add);
        cache_.attach(id, p);
        return p;
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        Person p = createPerson("/person/test1", "Joe Black", 32);
        assertEquals((Object) "Joe Black", p.getName());
  
        MyListener listener = new MyListener(p);
        ((Subject)p).addObserver(listener);
  
        p.setAge(40);
        assertTrue("Subject and pojo should be the same ", listener.isSame);
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(LocalTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
     public class MyListener implements Observer
     {
        Object pojo;
        public boolean isSame;
  
        public MyListener(Object pojo)
        {
           this.pojo = pojo;
           isSame = false;
        }
  
        public void fireChange(Subject subject, Field modifiedField, boolean pre)
        {
           System.out.println(" Subject: " +subject);
           isSame = (pojo == subject) ? true: false;
        }
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list