KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > readonly > ReadonlyUnitTestCase


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.cmp2.readonly;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.PreparedStatement JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.Collection JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31 import javax.sql.DataSource JavaDoc;
32 import junit.framework.Test;
33 import net.sourceforge.junitejb.EJBTestCase;
34 import org.jboss.test.JBossTestCase;
35
36 public class ReadonlyUnitTestCase extends EJBTestCase {
37
38        static org.jboss.logging.Logger log =
39        org.jboss.logging.Logger.getLogger(ReadonlyUnitTestCase.class);
40
41     public static Test suite() throws Exception JavaDoc {
42         return JBossTestCase.getDeploySetup(
43             ReadonlyUnitTestCase.class, "cmp2-readonly.jar");
44    }
45
46     public ReadonlyUnitTestCase(String JavaDoc name) {
47         super(name);
48     }
49
50     private PublisherHome getPublisherHome() {
51         try {
52             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
53             
54             return (PublisherHome) jndiContext.lookup("cmp2/readonly/Publisher");
55         } catch(Exception JavaDoc e) {
56             log.debug("failed", e);
57             fail("Exception in getPublisherHome: " + e.getMessage());
58         }
59         return null;
60     }
61
62     private BookHome getBookHome() {
63         try {
64             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
65             
66             return (BookHome) jndiContext.lookup("cmp2/readonly/Book");
67         } catch(Exception JavaDoc e) {
68             log.debug("failed", e);
69             fail("Exception in getBookHome: " + e.getMessage());
70         }
71         return null;
72     }
73  
74     private AuthorHome getAuthorHome() {
75         try {
76             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
77             
78             return (AuthorHome) jndiContext.lookup("cmp2/readonly/Author");
79         } catch(Exception JavaDoc e) {
80             log.debug("failed", e);
81             fail("Exception in getAuthorHome: " + e.getMessage());
82         }
83         return null;
84     }
85    
86    private Connection JavaDoc getConnection() {
87       try {
88             InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
89          DataSource JavaDoc ds = (DataSource JavaDoc) jndiContext.lookup("java:/DefaultDS");
90          return ds.getConnection();
91         } catch(Exception JavaDoc e) {
92             log.debug("failed", e);
93             fail("Exception in getConnection: " + e.getMessage());
94         }
95         return null;
96    }
97       
98    private Publisher oreilly;
99    private Publisher sams;
100    private Book ejb;
101    private Book jms;
102    private Book jmx;
103    private Book jboss;
104    private Author dain;
105
106    protected void setUp() throws Exception JavaDoc {
107       PublisherHome publisherHome = getPublisherHome();
108       BookHome bookHome = getBookHome();
109       AuthorHome authorHome = getAuthorHome();
110
111       oreilly = publisherHome.findByName("O'Reilly & Associates");
112       sams = publisherHome.findByName("Sams");
113       ejb = bookHome.findByName("Enterprise Java Beans (3rd Edition)");
114       jms = bookHome.findByName("Java Message Service");
115       jmx = bookHome.findByName(
116             "JMX: Managing J2EE with Java Management Extensions");
117       jboss = bookHome.findByName("JBOSS Administration and Development");
118       dain = authorHome.findByName("Dain Sundstrom");
119    }
120
121    protected void tearDown() {
122       oreilly = null;
123       sams = null;
124       ejb = null;
125       jms = null;
126       jmx = null;
127       jboss = null;
128    }
129
130    public void testSetUp() throws Exception JavaDoc {
131       Collection JavaDoc oreillyBooks = oreilly.getBooks();
132       assertEquals(2, oreillyBooks.size());
133       assertTrue(oreillyBooks.contains(ejb));
134       assertTrue(oreillyBooks.contains(jms));
135       assertTrue(ejb.getPublisher().isIdentical(oreilly));
136       assertTrue(jms.getPublisher().isIdentical(oreilly));
137
138       Collection JavaDoc samsBooks = sams.getBooks();
139       assertEquals(2, samsBooks.size());
140       assertTrue(samsBooks.contains(jmx));
141       assertTrue(samsBooks.contains(jboss));
142       assertTrue(jmx.getPublisher().isIdentical(sams));
143       assertTrue(jboss.getPublisher().isIdentical(sams));
144    }
145
146    public void testReadonlyCMPField() throws Exception JavaDoc {
147       try {
148          oreilly.setName("Stuff");
149          fail("Should have gotten exception from Publisher.setName");
150       } catch(Exception JavaDoc e) {
151       }
152    }
153
154    public void testReadonlyEntityCMPFieldChange() throws Exception JavaDoc {
155       try {
156          dain.setName("Stuff");
157          fail("Should have gotten exception from Author.setName");
158       } catch(Exception JavaDoc e) {
159       }
160    }
161
162    public void testReadonlyEntityCreate() throws Exception JavaDoc {
163       try {
164          AuthorHome authorHome = getAuthorHome();
165          authorHome.create(new Integer JavaDoc(44));
166          fail("Should have gotten exception from AuthorHome.create");
167       } catch(Exception JavaDoc e) {
168       }
169    }
170
171    public void testReadonlySetFK() throws Exception JavaDoc {
172       try {
173          jboss.setPublisher(sams);
174          fail("Should have gotten exception from Book.setPublisher");
175       } catch(Exception JavaDoc e) {
176       }
177    }
178
179    public void testReadonlySetCollection() throws Exception JavaDoc {
180       try {
181          sams.setBooks(new HashSet JavaDoc());
182          fail("Should have gotten exception from Publisher.setBooks");
183       } catch(Exception JavaDoc e) {
184       }
185    }
186    
187    public void testReadonlyCollectionAdd() throws Exception JavaDoc {
188       try {
189          sams.getBooks().add(jboss);
190          fail("Should have gotten exception from Book.setPublisher");
191       } catch(Exception JavaDoc e) {
192       }
193    }
194    
195    public void testReadonlyCollectionRemove() throws Exception JavaDoc {
196       try {
197          sams.getBooks().remove(ejb);
198          fail("Should have gotten exception from Book.setPublisher");
199       } catch(Exception JavaDoc e) {
200       }
201    }
202    
203     public void setUpEJB() throws Exception JavaDoc {
204       cleanDB();
205
206       Connection JavaDoc con = null;
207       PreparedStatement JavaDoc ps = null;
208       try {
209          con = getConnection();
210          
211          ps = con.prepareStatement(
212                "INSERT INTO PublisherEJB (id, name) " +
213                "VALUES (?,?)");
214          
215          // O'Reilly
216
ps.setInt(1, 1);
217          ps.setString(2, "O'Reilly & Associates");
218          if(ps.executeUpdate() != 1) {
219             fail("Failed to add Publisher to database");
220          }
221
222          // Sams
223
ps.setInt(1, 2);
224          ps.setString(2, "Sams");
225          if(ps.executeUpdate() != 1) {
226             fail("Failed to add Publisher to database");
227          }
228
229          ps.close();
230
231          ps = con.prepareStatement(
232                "INSERT INTO Book (id, name, isbn, publisher) " +
233                "VALUES (?,?,?,?)");
234
235          ps.setInt(1, -1);
236          ps.setString(2, "Enterprise Java Beans (3rd Edition)");
237          ps.setString(3, "0596002262");
238          ps.setInt(4, 1);
239          if(ps.executeUpdate() != 1) {
240             fail("Failed to add Book to database");
241          }
242
243          ps.setInt(1, -2);
244          ps.setString(2, "Java Message Service");
245          ps.setString(3, "0596000685 ");
246          ps.setInt(4, 1);
247          if(ps.executeUpdate() != 1) {
248             fail("Failed to add Book to database");
249          }
250
251          ps.setInt(1, -3);
252          ps.setString(2, "JMX: Managing J2EE with Java Management Extensions");
253          ps.setString(3, "0672322889");
254          ps.setInt(4, 2);
255          if(ps.executeUpdate() != 1) {
256             fail("Failed to add Book to database");
257          }
258
259          ps.setInt(1, -4);
260          ps.setString(2, "JBOSS Administration and Development");
261          ps.setString(3, "0672323478");
262          ps.setInt(4, 2);
263          if(ps.executeUpdate() != 1) {
264             fail("Failed to add Book to database");
265          }
266
267          ps = con.prepareStatement(
268                "INSERT INTO Author (id, name) " +
269                "VALUES (?,?)");
270          
271          // O'Reilly
272
ps.setInt(1, 1);
273          ps.setString(2, "Dain Sundstrom");
274          if(ps.executeUpdate() != 1) {
275             fail("Failed to add Author to database");
276          }
277       } finally {
278          if(ps != null) {
279             try {
280                ps.close();
281             } catch(SQLException JavaDoc e) {
282                log.debug("failed", e);
283             }
284          }
285          if(con != null) {
286             try {
287                con.close();
288             } catch(SQLException JavaDoc e) {
289                log.debug("failed", e);
290             }
291          }
292       }
293     }
294     
295     public void tearDownEJB() throws Exception JavaDoc {
296       cleanDB();
297     }
298     
299     public void cleanDB() throws Exception JavaDoc {
300        Connection JavaDoc con = null;
301       Statement JavaDoc statement = null;
302       try {
303          con = getConnection();
304          
305          statement = con.createStatement();
306
307          statement.executeUpdate("DELETE FROM Book");
308          statement.executeUpdate("DELETE FROM PublisherEJB");
309          statement.executeUpdate("DELETE FROM Author");
310       } finally {
311          if(statement != null) {
312             try {
313                statement.close();
314             } catch(SQLException JavaDoc e) {
315                log.debug("failed", e);
316             }
317          }
318          if(con != null) {
319             try {
320                con.close();
321             } catch(SQLException JavaDoc e) {
322                log.debug("failed", e);
323             }
324          }
325       }
326    }
327 }
328
329
330
331
Popular Tags