1 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 ; 29 import javax.management.MBeanServer ; 30 import javax.management.ObjectName ; 31 import java.util.ArrayList ; 32 38 public class VersionedObjectTester 39 extends ServiceMBeanSupport 40 implements VersionedObjectTesterMBean, MBeanRegistration 41 { 42 static Logger log = Logger.getLogger(VersionedObjectTester.class); 45 MBeanServer m_mbeanServer; 46 47 49 public VersionedObjectTester() 51 {} 52 53 55 public ObjectName preRegister(MBeanServer server, ObjectName name) 57 throws Exception 58 { 59 m_mbeanServer = server; 60 return name; 61 } 62 63 public void postRegister(Boolean registrationDone) 64 {} 65 public void preDeregister() throws Exception 66 {} 67 public void postDeregister() 68 {} 69 70 protected void startService() 71 throws Exception 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 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 ("Did not catch optimistic lock failure"); 100 if (!person.getName().equals("William")) throw new Exception ("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 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 ("No rollback happened"); 114 if (!person.getName().equals("William")) throw new Exception ("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 ("Failed to setname"); 120 121 log.info("test transactional set"); 122 person.setNameTransactional("Bill"); 123 if (!person.getName().equals("Bill")) throw new Exception ("Failed to setnametransactional"); 124 125 log.info("test 2 transactions, 2 different fields"); 126 person.testDifferentFields(); 127 if (person.getAge() != 5) throw new Exception ("test 2 transactions, 2 different fields failed"); 128 if (!person.getName().equals("William")) throw new Exception ("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 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 ("Did not catch optimistic lock failure"); 144 if (!person.getAddress().getCity().equals("Rutland")) throw new Exception ("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 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 ("Did not catch optimistic lock failure"); 160 if (!person.getAddress().getCity().equals("Rutland")) throw new Exception ("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 ("field was not changed"); 165 if (!person.getAddress().getState().equals("VT")) throw new Exception ("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 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 ("Did not catch optimistic lock failure"); 181 ArrayList hobbies = person.getHobbies(); 182 if (hobbies.size() != 1) throw new Exception ("optimistic lock failed, unexpected list size:" + hobbies.size()); 183 if (!hobbies.get(0).toString().equals("football")) throw new Exception ("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 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 ("Did not catch optimistic lock failure"); 198 hobbies = person.getHobbies(); 199 if (hobbies.size() != 1) throw new Exception ("optimistic lock failed, unexpected list size:" + hobbies.size()); 200 if (!hobbies.get(0).toString().equals("football")) throw new Exception ("optimistic lock failed. unexpected value in list"); 201 202 person.addHobby("basketball"); 203 if (hobbies.size() != 2) throw new Exception ("failed to add hobby"); 204 205 } 206 catch (Throwable ex) 207 { 208 log.error("failed", ex); 209 throw new RuntimeException (ex.getMessage()); 210 } 211 } 212 213 } 215 216 | Popular Tags |