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.cmp2.readonly;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.SQLException;
027: import java.sql.Statement;
028: import java.util.Collection;
029: import java.util.HashSet;
030: import javax.naming.InitialContext;
031: import javax.sql.DataSource;
032: import junit.framework.Test;
033: import org.jboss.test.util.ejb.EJBTestCase;
034: import org.jboss.test.JBossTestCase;
035:
036: public class ReadonlyUnitTestCase extends EJBTestCase {
037:
038: static org.apache.log4j.Category log = org.apache.log4j.Category
039: .getInstance(ReadonlyUnitTestCase.class);
040:
041: public static Test suite() throws Exception {
042: return JBossTestCase.getDeploySetup(ReadonlyUnitTestCase.class,
043: "cmp2-readonly.jar");
044: }
045:
046: public ReadonlyUnitTestCase(String name) {
047: super (name);
048: }
049:
050: private PublisherHome getPublisherHome() {
051: try {
052: InitialContext jndiContext = new InitialContext();
053:
054: return (PublisherHome) jndiContext
055: .lookup("cmp2/readonly/Publisher");
056: } catch (Exception e) {
057: log.debug("failed", e);
058: fail("Exception in getPublisherHome: " + e.getMessage());
059: }
060: return null;
061: }
062:
063: private BookHome getBookHome() {
064: try {
065: InitialContext jndiContext = new InitialContext();
066:
067: return (BookHome) jndiContext.lookup("cmp2/readonly/Book");
068: } catch (Exception e) {
069: log.debug("failed", e);
070: fail("Exception in getBookHome: " + e.getMessage());
071: }
072: return null;
073: }
074:
075: private AuthorHome getAuthorHome() {
076: try {
077: InitialContext jndiContext = new InitialContext();
078:
079: return (AuthorHome) jndiContext
080: .lookup("cmp2/readonly/Author");
081: } catch (Exception e) {
082: log.debug("failed", e);
083: fail("Exception in getAuthorHome: " + e.getMessage());
084: }
085: return null;
086: }
087:
088: private Connection getConnection() {
089: try {
090: InitialContext jndiContext = new InitialContext();
091: DataSource ds = (DataSource) jndiContext
092: .lookup("java:/DefaultDS");
093: return ds.getConnection();
094: } catch (Exception e) {
095: log.debug("failed", e);
096: fail("Exception in getConnection: " + e.getMessage());
097: }
098: return null;
099: }
100:
101: private Publisher oreilly;
102: private Publisher sams;
103: private Book ejb;
104: private Book jms;
105: private Book jmx;
106: private Book jboss;
107: private Author dain;
108:
109: protected void setUp() throws Exception {
110: PublisherHome publisherHome = getPublisherHome();
111: BookHome bookHome = getBookHome();
112: AuthorHome authorHome = getAuthorHome();
113:
114: oreilly = publisherHome.findByName("O'Reilly & Associates");
115: sams = publisherHome.findByName("Sams");
116: ejb = bookHome
117: .findByName("Enterprise Java Beans (3rd Edition)");
118: jms = bookHome.findByName("Java Message Service");
119: jmx = bookHome
120: .findByName("JMX: Managing J2EE with Java Management Extensions");
121: jboss = bookHome
122: .findByName("JBOSS Administration and Development");
123: dain = authorHome.findByName("Dain Sundstrom");
124: }
125:
126: protected void tearDown() {
127: oreilly = null;
128: sams = null;
129: ejb = null;
130: jms = null;
131: jmx = null;
132: jboss = null;
133: }
134:
135: public void testSetUp() throws Exception {
136: Collection oreillyBooks = oreilly.getBooks();
137: assertEquals(2, oreillyBooks.size());
138: assertTrue(oreillyBooks.contains(ejb));
139: assertTrue(oreillyBooks.contains(jms));
140: assertTrue(ejb.getPublisher().isIdentical(oreilly));
141: assertTrue(jms.getPublisher().isIdentical(oreilly));
142:
143: Collection samsBooks = sams.getBooks();
144: assertEquals(2, samsBooks.size());
145: assertTrue(samsBooks.contains(jmx));
146: assertTrue(samsBooks.contains(jboss));
147: assertTrue(jmx.getPublisher().isIdentical(sams));
148: assertTrue(jboss.getPublisher().isIdentical(sams));
149: }
150:
151: public void testReadonlyCMPField() throws Exception {
152: try {
153: oreilly.setName("Stuff");
154: fail("Should have gotten exception from Publisher.setName");
155: } catch (Exception e) {
156: }
157: }
158:
159: public void testReadonlyEntityCMPFieldChange() throws Exception {
160: try {
161: dain.setName("Stuff");
162: fail("Should have gotten exception from Author.setName");
163: } catch (Exception e) {
164: }
165: }
166:
167: public void testReadonlyEntityCreate() throws Exception {
168: try {
169: AuthorHome authorHome = getAuthorHome();
170: authorHome.create(new Integer(44));
171: fail("Should have gotten exception from AuthorHome.create");
172: } catch (Exception e) {
173: }
174: }
175:
176: public void testReadonlySetFK() throws Exception {
177: try {
178: jboss.setPublisher(sams);
179: fail("Should have gotten exception from Book.setPublisher");
180: } catch (Exception e) {
181: }
182: }
183:
184: public void testReadonlySetCollection() throws Exception {
185: try {
186: sams.setBooks(new HashSet());
187: fail("Should have gotten exception from Publisher.setBooks");
188: } catch (Exception e) {
189: }
190: }
191:
192: public void testReadonlyCollectionAdd() throws Exception {
193: try {
194: sams.getBooks().add(jboss);
195: fail("Should have gotten exception from Book.setPublisher");
196: } catch (Exception e) {
197: }
198: }
199:
200: public void testReadonlyCollectionRemove() throws Exception {
201: try {
202: sams.getBooks().remove(ejb);
203: fail("Should have gotten exception from Book.setPublisher");
204: } catch (Exception e) {
205: }
206: }
207:
208: public void setUpEJB() throws Exception {
209: cleanDB();
210:
211: Connection con = null;
212: PreparedStatement ps = null;
213: try {
214: con = getConnection();
215:
216: ps = con
217: .prepareStatement("INSERT INTO PublisherEJB (id, name) "
218: + "VALUES (?,?)");
219:
220: // O'Reilly
221: ps.setInt(1, 1);
222: ps.setString(2, "O'Reilly & Associates");
223: if (ps.executeUpdate() != 1) {
224: fail("Failed to add Publisher to database");
225: }
226:
227: // Sams
228: ps.setInt(1, 2);
229: ps.setString(2, "Sams");
230: if (ps.executeUpdate() != 1) {
231: fail("Failed to add Publisher to database");
232: }
233:
234: ps.close();
235:
236: ps = con
237: .prepareStatement("INSERT INTO Book (id, name, isbn, publisher) "
238: + "VALUES (?,?,?,?)");
239:
240: ps.setInt(1, -1);
241: ps.setString(2, "Enterprise Java Beans (3rd Edition)");
242: ps.setString(3, "0596002262");
243: ps.setInt(4, 1);
244: if (ps.executeUpdate() != 1) {
245: fail("Failed to add Book to database");
246: }
247:
248: ps.setInt(1, -2);
249: ps.setString(2, "Java Message Service");
250: ps.setString(3, "0596000685 ");
251: ps.setInt(4, 1);
252: if (ps.executeUpdate() != 1) {
253: fail("Failed to add Book to database");
254: }
255:
256: ps.setInt(1, -3);
257: ps
258: .setString(2,
259: "JMX: Managing J2EE with Java Management Extensions");
260: ps.setString(3, "0672322889");
261: ps.setInt(4, 2);
262: if (ps.executeUpdate() != 1) {
263: fail("Failed to add Book to database");
264: }
265:
266: ps.setInt(1, -4);
267: ps.setString(2, "JBOSS Administration and Development");
268: ps.setString(3, "0672323478");
269: ps.setInt(4, 2);
270: if (ps.executeUpdate() != 1) {
271: fail("Failed to add Book to database");
272: }
273:
274: ps = con.prepareStatement("INSERT INTO Author (id, name) "
275: + "VALUES (?,?)");
276:
277: // O'Reilly
278: ps.setInt(1, 1);
279: ps.setString(2, "Dain Sundstrom");
280: if (ps.executeUpdate() != 1) {
281: fail("Failed to add Author to database");
282: }
283: } finally {
284: if (ps != null) {
285: try {
286: ps.close();
287: } catch (SQLException e) {
288: log.debug("failed", e);
289: }
290: }
291: if (con != null) {
292: try {
293: con.close();
294: } catch (SQLException e) {
295: log.debug("failed", e);
296: }
297: }
298: }
299: }
300:
301: public void tearDownEJB() throws Exception {
302: cleanDB();
303: }
304:
305: public void cleanDB() throws Exception {
306: Connection con = null;
307: Statement statement = null;
308: try {
309: con = getConnection();
310:
311: statement = con.createStatement();
312:
313: statement.executeUpdate("DELETE FROM Book");
314: statement.executeUpdate("DELETE FROM PublisherEJB");
315: statement.executeUpdate("DELETE FROM Author");
316: } finally {
317: if (statement != null) {
318: try {
319: statement.close();
320: } catch (SQLException e) {
321: log.debug("failed", e);
322: }
323: }
324: if (con != null) {
325: try {
326: con.close();
327: } catch (SQLException e) {
328: log.debug("failed", e);
329: }
330: }
331: }
332: }
333: }
|