KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > bean > VersionedObjectTester


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * 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 package org.jboss.test.aop.bean;
23
24 import org.jboss.aspects.versioned.Versioned;
25 import org.jboss.logging.Logger;
26 import org.jboss.system.ServiceMBeanSupport;
27
28 import javax.management.MBeanRegistration JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectName JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 /**
33  *
34  * @see Monitorable
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 37406 $
37  */

38 public class VersionedObjectTester
39    extends ServiceMBeanSupport
40    implements VersionedObjectTesterMBean, MBeanRegistration JavaDoc
41 {
42    // Constants ----------------------------------------------------
43
// Attributes ---------------------------------------------------
44
static Logger log = Logger.getLogger(VersionedObjectTester.class);
45    MBeanServer JavaDoc m_mbeanServer;
46
47    // Static -------------------------------------------------------
48

49    // Constructors -------------------------------------------------
50
public VersionedObjectTester()
51    {}
52
53    // Public -------------------------------------------------------
54

55    // MBeanRegistration implementation -----------------------------------
56
public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
57    throws Exception JavaDoc
58    {
59       m_mbeanServer = server;
60       return name;
61    }
62
63    public void postRegister(Boolean JavaDoc registrationDone)
64    {}
65    public void preDeregister() throws Exception JavaDoc
66    {}
67    public void postDeregister()
68    {}
69
70    protected void startService()
71       throws Exception JavaDoc
72    {
73    }
74
75    protected void stopService() {
76    }
77
78    public void testPerField()
79    {
80       try
81       {
82          log.info("TEST PER FIELD VERSIONING");
83          Address address = new Address("Marlborough Street", "Boston", "MA");
84          Person person = new Person("Bill", 32, address);
85          Versioned.makePerFieldVersioned(person);
86
87          log.info("test optimistic lock");
88          boolean exceptionThrown = false;
89          try
90          {
91             person.testOptimisticLock();
92          }
93          catch (Exception JavaDoc ignored)
94          {
95             exceptionThrown = true;
96             log.info("caught exception correctly: " + ignored.getMessage() + " exception type: " + ignored.getClass().getName());
97          }
98
99          if (!exceptionThrown) throw new Exception JavaDoc("Did not catch optimistic lock failure");
100          if (!person.getName().equals("William")) throw new Exception JavaDoc("optimistic lock failed, field was changed");
101
102          log.info("test rollback");
103          exceptionThrown = false;
104          try
105          {
106             person.testRollback();
107          }
108          catch (Exception JavaDoc ignored)
109          {
110             exceptionThrown = true;
111             log.info("caught exception correctly: " + ignored.getMessage() + " exception type: " + ignored.getClass().getName());
112          }
113          if (!exceptionThrown) throw new Exception JavaDoc("No rollback happened");
114          if (!person.getName().equals("William")) throw new Exception JavaDoc("rollback lock failed, field was changed");
115
116          log.info("test non transactional set");
117          person.setName("Burke");
118          log.info("see if name was reset");
119          if (!person.getName().equals("Burke")) throw new Exception JavaDoc("Failed to setname");
120
121          log.info("test transactional set");
122          person.setNameTransactional("Bill");
123          if (!person.getName().equals("Bill")) throw new Exception JavaDoc("Failed to setnametransactional");
124
125          log.info("test 2 transactions, 2 different fields");
126          person.testDifferentFields();
127          if (person.getAge() != 5) throw new Exception JavaDoc("test 2 transactions, 2 different fields failed");
128          if (!person.getName().equals("William")) throw new Exception JavaDoc("test 2 transactions, 2 different fields failed");
129
130
131          log.info("test optimistic lock with embedded object");
132          exceptionThrown = false;
133          try
134          {
135             person.testOptimisticLockWithAddress();
136          }
137          catch (Exception JavaDoc ignored)
138          {
139             exceptionThrown = true;
140             log.info("caught exception correctly: " + ignored.getMessage() + " exception type: " + ignored.getClass().getName());
141          }
142
143          if (!exceptionThrown) throw new Exception JavaDoc("Did not catch optimistic lock failure");
144          if (!person.getAddress().getCity().equals("Rutland")) throw new Exception JavaDoc("optimistic lock failed, field was changed");
145
146
147          log.info("test rollback for embedded object");
148          exceptionThrown = false;
149          try
150          {
151             person.testRollbackForAddress();
152          }
153          catch (Exception JavaDoc ignored)
154          {
155             exceptionThrown = true;
156             log.info("caught exception correctly: " + ignored.getMessage() + " exception type: " + ignored.getClass().getName());
157          }
158
159          if (!exceptionThrown) throw new Exception JavaDoc("Did not catch optimistic lock failure");
160          if (!person.getAddress().getCity().equals("Rutland")) throw new Exception JavaDoc("optimistic lock failed, field was changed");
161
162          log.info("test 2 fields for embedded object");
163          person.testDifferentFieldsForAddress();
164          if (!person.getAddress().getCity().equals("Rutland")) throw new Exception JavaDoc("field was not changed");
165          if (!person.getAddress().getState().equals("VT")) throw new Exception JavaDoc("field was not changed");
166
167
168          log.info("test list optimistic lock");
169          exceptionThrown = false;
170          try
171          {
172             person.testListOptimisticLock();
173          }
174          catch (Exception JavaDoc ignored)
175          {
176             exceptionThrown = true;
177             log.info("caught exception correctly: " + ignored.getMessage() + " exception type: " + ignored.getClass().getName());
178          }
179
180          if (!exceptionThrown) throw new Exception JavaDoc("Did not catch optimistic lock failure");
181          ArrayList JavaDoc hobbies = person.getHobbies();
182          if (hobbies.size() != 1) throw new Exception JavaDoc("optimistic lock failed, unexpected list size:" + hobbies.size());
183          if (!hobbies.get(0).toString().equals("football")) throw new Exception JavaDoc("optimistic lock failed. unexpected value in list");
184
185          log.info("test list rollback");
186          exceptionThrown = false;
187          try
188          {
189             person.testListRollback();
190          }
191          catch (Exception JavaDoc ignored)
192          {
193             exceptionThrown = true;
194             log.info("caught exception correctly: " + ignored.getMessage() + " exception type: " + ignored.getClass().getName());
195          }
196
197          if (!exceptionThrown) throw new Exception JavaDoc("Did not catch optimistic lock failure");
198          hobbies = person.getHobbies();
199          if (hobbies.size() != 1) throw new Exception JavaDoc("optimistic lock failed, unexpected list size:" + hobbies.size());
200          if (!hobbies.get(0).toString().equals("football")) throw new Exception JavaDoc("optimistic lock failed. unexpected value in list");
201
202          person.addHobby("basketball");
203          if (hobbies.size() != 2) throw new Exception JavaDoc("failed to add hobby");
204
205       }
206       catch (Throwable JavaDoc ex)
207       {
208          log.error("failed", ex);
209          throw new RuntimeException JavaDoc(ex.getMessage());
210       }
211    }
212
213    // Inner classes -------------------------------------------------
214
}
215
216
Popular Tags