Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 150   Methods: 10
NCLOC: 103   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LocalTest.java 50% 73.2% 80% 73.6%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source.
 3    * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 4    * as indicated by the @author tags. See the copyright.txt file in the
 5    * distribution for a full listing of individual contributors.
 6    *
 7    * This is free software; you can redistribute it and/or modify it
 8    * under the terms of the GNU Lesser General Public License as
 9    * published by the Free Software Foundation; either version 2.1 of
 10    * the License, or (at your option) any later version.
 11    *
 12    * This software is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15    * Lesser General Public License for more details.
 16    *
 17    * You should have received a copy of the GNU Lesser General Public
 18    * License along with this software; if not, write to the Free
 19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21    */
 22   
 23    package org.jboss.cache.pojo.observer;
 24   
 25    import junit.framework.TestCase;
 26    import junit.framework.Test;
 27    import junit.framework.TestSuite;
 28    import org.apache.commons.logging.Log;
 29    import org.apache.commons.logging.LogFactory;
 30    import org.jboss.cache.pojo.PojoCache;
 31    import org.jboss.cache.pojo.PojoCacheFactory;
 32    import org.jboss.cache.pojo.PojoCacheListener;
 33    import org.jboss.cache.pojo.observable.Observer;
 34    import org.jboss.cache.pojo.observable.Subject;
 35    import org.jboss.cache.pojo.test.Person;
 36    import org.jboss.cache.pojo.test.Address;
 37    import org.jboss.cache.pojo.test.Student;
 38    import org.jboss.aop.proxy.ClassProxy;
 39   
 40    import java.util.List;
 41    import java.util.Map;
 42    import java.util.HashMap;
 43    import java.util.ArrayList;
 44    import java.util.Set;
 45    import java.util.HashSet;
 46    import java.lang.reflect.Field;
 47   
 48    /**
 49    * This is a test case illustrating how to subscribe to POJO events using Observer.
 50    *
 51    * @author Ben Wang
 52    */
 53   
 54    public class LocalTest extends TestCase
 55    {
 56    Log log = LogFactory.getLog(LocalTest.class);
 57    PojoCache cache_;
 58   
 59  2 public LocalTest(String name)
 60    {
 61  2 super(name);
 62    }
 63   
 64  2 protected void setUp() throws Exception
 65    {
 66  2 super.setUp();
 67  2 log.info("setUp() ....");
 68  2 String configFile = "META-INF/local-service.xml";
 69  2 boolean toStart = false;
 70  2 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 71  2 cache_.start();
 72    }
 73   
 74  2 protected void tearDown() throws Exception
 75    {
 76  2 super.tearDown();
 77  2 cache_.stop();
 78    }
 79   
 80    // public void testDummy() {}
 81   
 82  2 private Person createPerson(String id, String name, int age)
 83    {
 84  2 Person p = new Person();
 85  2 p.setName(name);
 86  2 p.setAge(age);
 87  2 Address add = new Address();
 88  2 add.setZip(95123);
 89  2 add.setCity("San Jose");
 90  2 p.setAddress(add);
 91  2 cache_.attach(id, p);
 92  2 return p;
 93    }
 94   
 95  0 private Student createStudent(String id, String name, int age, String grade)
 96    {
 97  0 Student p = new Student();
 98  0 p.setName(name);
 99  0 p.setAge(age);
 100  0 p.setYear(grade);
 101  0 Address add = new Address();
 102  0 add.setZip(95123);
 103  0 add.setCity("San Jose");
 104  0 p.setAddress(add);
 105  0 cache_.attach(id, p);
 106  0 return p;
 107    }
 108   
 109  2 public void testSimple() throws Exception
 110    {
 111  2 log.info("testSimple() ....");
 112  2 Person p = createPerson("/person/test1", "Joe Black", 32);
 113  2 assertEquals((Object) "Joe Black", p.getName());
 114   
 115  2 MyListener listener = new MyListener(p);
 116  2 ((Subject)p).addObserver(listener);
 117   
 118  2 p.setAge(40);
 119  2 assertTrue("Subject and pojo should be the same ", listener.isSame);
 120    }
 121   
 122  2 public static Test suite() throws Exception
 123    {
 124  2 return new TestSuite(LocalTest.class);
 125    }
 126   
 127   
 128  0 public static void main(String[] args) throws Exception
 129    {
 130  0 junit.textui.TestRunner.run(suite());
 131    }
 132   
 133    public class MyListener implements Observer
 134    {
 135    Object pojo;
 136    public boolean isSame;
 137   
 138  2 public MyListener(Object pojo)
 139    {
 140  2 this.pojo = pojo;
 141  2 isSame = false;
 142    }
 143   
 144  4 public void fireChange(Subject subject, Field modifiedField, boolean pre)
 145    {
 146  4 System.out.println(" Subject: " +subject);
 147  4 isSame = (pojo == subject) ? true: false;
 148    }
 149    }
 150    }