KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > entityexception > ExceptionTestBean


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.ejb3.test.entityexception;
23
24 import javax.ejb.Remote JavaDoc;
25 import javax.ejb.Stateless JavaDoc;
26 import javax.ejb.TransactionAttribute JavaDoc;
27 import javax.ejb.TransactionAttributeType JavaDoc;
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 /**
36  *
37  * @author <a HREF="mailto:kabir.khan@jboss.org">Kabir Khan</a>
38  * @version $Revision: 42263 $
39  */

40 @Stateless JavaDoc
41 @Remote JavaDoc(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 JavaDoc(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 JavaDoc("TransactionRequiredException not thrown for persist()");
70       }
71       catch(TransactionRequiredException e)
72       {
73       }
74       
75       try
76       {
77          manager.merge(person);
78          throw new RuntimeException JavaDoc("TransactionRequiredException not thrown for merge()");
79       }
80       catch(TransactionRequiredException e)
81       {
82       }
83       
84       try
85       {
86          manager.remove(person);
87          throw new RuntimeException JavaDoc("TransactionRequiredException not thrown for remove()");
88       }
89       catch(TransactionRequiredException e)
90       {
91       }
92
93       try
94       {
95          manager.refresh(person);
96          throw new RuntimeException JavaDoc("TransactionRequiredException not thrown for refresh()");
97       }
98       catch(TransactionRequiredException e)
99       {
100       }
101
102       /*
103       try
104       {
105          manager.contains(person);
106          throw new RuntimeException("TransactionRequiredException not thrown for contains()");
107       }
108       catch(TransactionRequiredException e)
109       {
110       }
111       */

112       
113       try
114       {
115          manager.flush();
116          throw new RuntimeException JavaDoc("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 JavaDoc("IllegalArgumentException not thrown when saving non-entity");
133       }
134       catch(IllegalArgumentException JavaDoc e)
135       {
136       }
137
138       //TODO - IllegalArgumentException should be thrown if entity is in detached state
139
}
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 JavaDoc("IllegalArgumentException not thrown when merging non-entity");
149       }
150       catch(IllegalArgumentException JavaDoc e)
151       {
152       }
153       
154       //TODO - IllegalArgumentException should be thrown if entity is in removed state
155
}
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 JavaDoc("IllegalArgumentException not thrown when merging non-entity");
165       }
166       catch(IllegalArgumentException JavaDoc e)
167       {
168       }
169       
170       //TODO - IllegalArgumentException should be thrown if entity is in detached or removed state
171
}
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 JavaDoc("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 JavaDoc("IllegalArgumentException not thrown");
192       }
193       catch(IllegalArgumentException JavaDoc e)
194       {
195       }
196
197       try
198       {
199          manager.find(Person.class, "abc");
200          throw new RuntimeException JavaDoc("IllegalArgumentException not thrown");
201       }
202       catch(IllegalArgumentException JavaDoc 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          //TODO, according to spec invalid EJBQL should throw IllegalArgumentExceptions
216
//throw new RuntimeException("IllegalArgumentException not thrown");
217
}
218       catch(IllegalArgumentException JavaDoc e)
219       {
220       }
221    }
222    
223    public void testEMRefreshExceptions()
224    {
225       try
226       {
227          manager.refresh(new NonEntity());
228          throw new RuntimeException JavaDoc("IllegalArgumentException not thrown when refreshing a non-entity");
229       }
230       catch(IllegalArgumentException JavaDoc e)
231       {
232       }
233
234       //TODO - IllegalArgumentException should be thrown if argument is not in managed state
235
}
236  
237    public void testEMContainsExceptions()
238    {
239       try
240       {
241          manager.contains(new NonEntity());
242          //TODO - IllegalArgumentException should be thrown
243
//throw new RuntimeException("IllegalArgumentException not thrown for contains a non-entity");
244
}
245       catch(IllegalArgumentException JavaDoc 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 JavaDoc("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 JavaDoc("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 JavaDoc("IllegalArgumentException not thrown for setHint");
285       }
286       catch(IllegalArgumentException JavaDoc 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 JavaDoc("IllegalArgumentException not thrown for setParameter (Wrong name)");
295       }
296       catch(IllegalArgumentException JavaDoc e)
297       {
298       }
299       
300       query.setParameter("name", "Kabir");
301       try
302       {
303          query.setParameter("id", "HELLO");
304          //Acceptable not to throw an exception when using wrong type (query will fail)
305
}
306       catch(IllegalArgumentException JavaDoc 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 JavaDoc("IllegalArgumentException not thrown for setParameter (Wrong index)");
317       }
318       catch(IllegalArgumentException JavaDoc e)
319       {
320       }
321
322       try
323       {
324          query.setParameter(10, "HELLO");
325          throw new RuntimeException JavaDoc("IllegalArgumentException not thrown for setParameter (Wrong index)");
326       }
327       catch(IllegalArgumentException JavaDoc e)
328       {
329       }
330    }
331
332 }
333
Popular Tags