1 22 package org.jboss.ejb3.test.entityexception; 23 24 import javax.ejb.Remote ; 25 import javax.ejb.Stateless ; 26 import javax.ejb.TransactionAttribute ; 27 import javax.ejb.TransactionAttributeType ; 28 import javax.persistence.EntityManager; 29 import javax.persistence.EntityNotFoundException; 30 import javax.persistence.NonUniqueResultException; 31 import javax.persistence.PersistenceContext; 32 import javax.persistence.Query; 33 import javax.persistence.TransactionRequiredException; 34 35 40 @Stateless 41 @Remote (ExceptionTest.class) 42 public class ExceptionTestBean implements ExceptionTest 43 { 44 @PersistenceContext 45 EntityManager manager; 46 47 48 public Person createEntry(Person person) 49 { 50 manager.persist(person); 51 return person; 52 } 53 54 public Person removeEntry(Person person) 55 { 56 manager.remove(person); 57 return person; 58 } 59 60 @TransactionAttribute (TransactionAttributeType.NOT_SUPPORTED) 61 public void testTransactionRequiredException() 62 { 63 System.out.println("*** testEMPersistTransactionRequiredException"); 64 Person person = new Person(100, "No Tx"); 65 66 try 67 { 68 manager.persist(person); 69 throw new RuntimeException ("TransactionRequiredException not thrown for persist()"); 70 } 71 catch(TransactionRequiredException e) 72 { 73 } 74 75 try 76 { 77 manager.merge(person); 78 throw new RuntimeException ("TransactionRequiredException not thrown for merge()"); 79 } 80 catch(TransactionRequiredException e) 81 { 82 } 83 84 try 85 { 86 manager.remove(person); 87 throw new RuntimeException ("TransactionRequiredException not thrown for remove()"); 88 } 89 catch(TransactionRequiredException e) 90 { 91 } 92 93 try 94 { 95 manager.refresh(person); 96 throw new RuntimeException ("TransactionRequiredException not thrown for refresh()"); 97 } 98 catch(TransactionRequiredException e) 99 { 100 } 101 102 112 113 try 114 { 115 manager.flush(); 116 throw new RuntimeException ("TransactionRequiredException not thrown for flush()"); 117 } 118 catch(TransactionRequiredException e) 119 { 120 } 121 122 123 } 124 125 public void testEMPersistExceptions() 126 { 127 System.out.println("*** testEMPersistIllegalArgumentExceptions"); 128 NonEntity nonEntity = new NonEntity(); 129 try 130 { 131 manager.persist(nonEntity); 132 throw new RuntimeException ("IllegalArgumentException not thrown when saving non-entity"); 133 } 134 catch(IllegalArgumentException e) 135 { 136 } 137 138 } 140 141 public void testEMMergeExceptions() 142 { 143 System.out.println("*** testEMMergeIllegalArgumentException"); 144 try 145 { 146 NonEntity nonEntity = new NonEntity(); 147 manager.merge(nonEntity); 148 throw new RuntimeException ("IllegalArgumentException not thrown when merging non-entity"); 149 } 150 catch(IllegalArgumentException e) 151 { 152 } 153 154 } 156 157 public void testEMRemoveIllegalArgumentException() 158 { 159 System.out.println("*** testEMRemoveIllegalArgumentException"); 160 try 161 { 162 NonEntity nonEntity = new NonEntity(); 163 manager.remove(nonEntity); 164 throw new RuntimeException ("IllegalArgumentException not thrown when merging non-entity"); 165 } 166 catch(IllegalArgumentException e) 167 { 168 } 169 170 } 172 173 public boolean testEMFindExceptions() 174 { 175 System.out.println("*** testEMFindExceptions"); 176 manager.find(Person.class, 1); 177 try 178 { 179 180 Person person = manager.getReference(Person.class, 2); 181 person.getId(); 182 throw new RuntimeException ("EntityNotFoundException not thrown: " + person); 183 } 184 catch(EntityNotFoundException e) 185 { 186 } 187 188 try 189 { 190 manager.find(NonEntity.class, 1); 191 throw new RuntimeException ("IllegalArgumentException not thrown"); 192 } 193 catch(IllegalArgumentException e) 194 { 195 } 196 197 try 198 { 199 manager.find(Person.class, "abc"); 200 throw new RuntimeException ("IllegalArgumentException not thrown"); 201 } 202 catch(IllegalArgumentException e) 203 { 204 } 205 206 207 return true; 208 } 209 210 public void testEMCreateQueryExceptions() 211 { 212 try 213 { 214 manager.createQuery("This is all nonsense"); 215 } 218 catch(IllegalArgumentException e) 219 { 220 } 221 } 222 223 public void testEMRefreshExceptions() 224 { 225 try 226 { 227 manager.refresh(new NonEntity()); 228 throw new RuntimeException ("IllegalArgumentException not thrown when refreshing a non-entity"); 229 } 230 catch(IllegalArgumentException e) 231 { 232 } 233 234 } 236 237 public void testEMContainsExceptions() 238 { 239 try 240 { 241 manager.contains(new NonEntity()); 242 } 245 catch(IllegalArgumentException e) 246 { 247 } 248 } 249 250 public void testQuerySingleResultExceptions() 251 { 252 createEntry(new Person(11, "A")); 253 createEntry(new Person(12, "B")); 254 createEntry(new Person(13, "C")); 255 256 Query query = manager.createQuery("from Person"); 257 try 258 { 259 query.getSingleResult(); 260 throw new RuntimeException ("NonUniqueResultException not thrown for getSingleResult"); 261 } 262 catch(NonUniqueResultException e) 263 { 264 } 265 266 Query query2 = manager.createQuery("from Person where id=999"); 267 try 268 { 269 query2.getSingleResult(); 270 throw new RuntimeException ("EntityNotFoundException not thrown for getSingleResult returning no results"); 271 } 272 catch(EntityNotFoundException e) 273 { 274 } 275 } 276 277 public void testQuerySetHintAndParameter() 278 { 279 280 try 281 { 282 Query query = manager.createQuery("from Person"); 283 query.setHint("org.hibernate.timeout", "Not an integer"); 284 throw new RuntimeException ("IllegalArgumentException not thrown for setHint"); 285 } 286 catch(IllegalArgumentException e) 287 { 288 } 289 290 Query query = manager.createQuery("from Person where id=:id and name=:name"); 291 try 292 { 293 query.setParameter("nosuchparam", "Whateverrrr"); 294 throw new RuntimeException ("IllegalArgumentException not thrown for setParameter (Wrong name)"); 295 } 296 catch(IllegalArgumentException e) 297 { 298 } 299 300 query.setParameter("name", "Kabir"); 301 try 302 { 303 query.setParameter("id", "HELLO"); 304 } 306 catch(IllegalArgumentException e) 307 { 308 } 309 310 query = manager.createQuery("from Person where id=? and name=?"); 311 query.setParameter(0, 1); 312 query.setParameter(1, "XXX"); 313 try 314 { 315 query.setParameter(-1, "HELLO"); 316 throw new RuntimeException ("IllegalArgumentException not thrown for setParameter (Wrong index)"); 317 } 318 catch(IllegalArgumentException e) 319 { 320 } 321 322 try 323 { 324 query.setParameter(10, "HELLO"); 325 throw new RuntimeException ("IllegalArgumentException not thrown for setParameter (Wrong index)"); 326 } 327 catch(IllegalArgumentException e) 328 { 329 } 330 } 331 332 } 333 | Popular Tags |