001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.aop.bean;
023:
024: import org.jboss.aspects.versioned.Versioned;
025: import org.jboss.logging.Logger;
026: import org.jboss.system.ServiceMBeanSupport;
027:
028: import javax.management.MBeanRegistration;
029: import javax.management.MBeanServer;
030: import javax.management.ObjectName;
031: import java.util.ArrayList;
032:
033: /**
034: *
035: * @see Monitorable
036: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
037: * @version $Revision: 57211 $
038: */
039: public class VersionedObjectTester extends ServiceMBeanSupport
040: implements VersionedObjectTesterMBean, MBeanRegistration {
041: // Constants ----------------------------------------------------
042: // Attributes ---------------------------------------------------
043: static Logger log = Logger.getLogger(VersionedObjectTester.class);
044: MBeanServer m_mbeanServer;
045:
046: // Static -------------------------------------------------------
047:
048: // Constructors -------------------------------------------------
049: public VersionedObjectTester() {
050: }
051:
052: // Public -------------------------------------------------------
053:
054: // MBeanRegistration implementation -----------------------------------
055: public ObjectName preRegister(MBeanServer server, ObjectName name)
056: throws Exception {
057: m_mbeanServer = server;
058: return name;
059: }
060:
061: public void postRegister(Boolean registrationDone) {
062: }
063:
064: public void preDeregister() throws Exception {
065: }
066:
067: public void postDeregister() {
068: }
069:
070: protected void startService() throws Exception {
071: }
072:
073: protected void stopService() {
074: }
075:
076: public void testPerField() {
077: try {
078: log.info("TEST PER FIELD VERSIONING");
079: Address address = new Address("Marlborough Street",
080: "Boston", "MA");
081: Person person = new Person("Bill", 32, address);
082: Versioned.makePerFieldVersioned(person);
083:
084: log.info("test optimistic lock");
085: boolean exceptionThrown = false;
086: try {
087: person.testOptimisticLock();
088: } catch (Exception ignored) {
089: exceptionThrown = true;
090: log.info("caught exception correctly: "
091: + ignored.getMessage() + " exception type: "
092: + ignored.getClass().getName());
093: }
094:
095: if (!exceptionThrown)
096: throw new Exception(
097: "Did not catch optimistic lock failure");
098: if (!person.getName().equals("William"))
099: throw new Exception(
100: "optimistic lock failed, field was changed");
101:
102: log.info("test rollback");
103: exceptionThrown = false;
104: try {
105: person.testRollback();
106: } catch (Exception ignored) {
107: exceptionThrown = true;
108: log.info("caught exception correctly: "
109: + ignored.getMessage() + " exception type: "
110: + ignored.getClass().getName());
111: }
112: if (!exceptionThrown)
113: throw new Exception("No rollback happened");
114: if (!person.getName().equals("William"))
115: throw new Exception(
116: "rollback lock failed, field was changed");
117:
118: log.info("test non transactional set");
119: person.setName("Burke");
120: log.info("see if name was reset");
121: if (!person.getName().equals("Burke"))
122: throw new Exception("Failed to setname");
123:
124: log.info("test transactional set");
125: person.setNameTransactional("Bill");
126: if (!person.getName().equals("Bill"))
127: throw new Exception("Failed to setnametransactional");
128:
129: log.info("test 2 transactions, 2 different fields");
130: person.testDifferentFields();
131: if (person.getAge() != 5)
132: throw new Exception(
133: "test 2 transactions, 2 different fields failed");
134: if (!person.getName().equals("William"))
135: throw new Exception(
136: "test 2 transactions, 2 different fields failed");
137:
138: log.info("test optimistic lock with embedded object");
139: exceptionThrown = false;
140: try {
141: person.testOptimisticLockWithAddress();
142: } catch (Exception ignored) {
143: exceptionThrown = true;
144: log.info("caught exception correctly: "
145: + ignored.getMessage() + " exception type: "
146: + ignored.getClass().getName());
147: }
148:
149: if (!exceptionThrown)
150: throw new Exception(
151: "Did not catch optimistic lock failure");
152: if (!person.getAddress().getCity().equals("Rutland"))
153: throw new Exception(
154: "optimistic lock failed, field was changed");
155:
156: log.info("test rollback for embedded object");
157: exceptionThrown = false;
158: try {
159: person.testRollbackForAddress();
160: } catch (Exception ignored) {
161: exceptionThrown = true;
162: log.info("caught exception correctly: "
163: + ignored.getMessage() + " exception type: "
164: + ignored.getClass().getName());
165: }
166:
167: if (!exceptionThrown)
168: throw new Exception(
169: "Did not catch optimistic lock failure");
170: if (!person.getAddress().getCity().equals("Rutland"))
171: throw new Exception(
172: "optimistic lock failed, field was changed");
173:
174: log.info("test 2 fields for embedded object");
175: person.testDifferentFieldsForAddress();
176: if (!person.getAddress().getCity().equals("Rutland"))
177: throw new Exception("field was not changed");
178: if (!person.getAddress().getState().equals("VT"))
179: throw new Exception("field was not changed");
180:
181: log.info("test list optimistic lock");
182: exceptionThrown = false;
183: try {
184: person.testListOptimisticLock();
185: } catch (Exception ignored) {
186: exceptionThrown = true;
187: log.info("caught exception correctly: "
188: + ignored.getMessage() + " exception type: "
189: + ignored.getClass().getName());
190: }
191:
192: if (!exceptionThrown)
193: throw new Exception(
194: "Did not catch optimistic lock failure");
195: ArrayList hobbies = person.getHobbies();
196: if (hobbies.size() != 1)
197: throw new Exception(
198: "optimistic lock failed, unexpected list size:"
199: + hobbies.size());
200: if (!hobbies.get(0).toString().equals("football"))
201: throw new Exception(
202: "optimistic lock failed. unexpected value in list");
203:
204: log.info("test list rollback");
205: exceptionThrown = false;
206: try {
207: person.testListRollback();
208: } catch (Exception ignored) {
209: exceptionThrown = true;
210: log.info("caught exception correctly: "
211: + ignored.getMessage() + " exception type: "
212: + ignored.getClass().getName());
213: }
214:
215: if (!exceptionThrown)
216: throw new Exception(
217: "Did not catch optimistic lock failure");
218: hobbies = person.getHobbies();
219: if (hobbies.size() != 1)
220: throw new Exception(
221: "optimistic lock failed, unexpected list size:"
222: + hobbies.size());
223: if (!hobbies.get(0).toString().equals("football"))
224: throw new Exception(
225: "optimistic lock failed. unexpected value in list");
226:
227: person.addHobby("basketball");
228: if (hobbies.size() != 2)
229: throw new Exception("failed to add hobby");
230:
231: } catch (Throwable ex) {
232: log.error("failed", ex);
233: throw new RuntimeException(ex.getMessage());
234: }
235: }
236:
237: // Inner classes -------------------------------------------------
238: }
|