1 22 package org.jboss.test.cmp2.readonly; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.Collection ; 29 import java.util.HashSet ; 30 import javax.naming.InitialContext ; 31 import javax.sql.DataSource ; 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 { 42 return JBossTestCase.getDeploySetup( 43 ReadonlyUnitTestCase.class, "cmp2-readonly.jar"); 44 } 45 46 public ReadonlyUnitTestCase(String name) { 47 super(name); 48 } 49 50 private PublisherHome getPublisherHome() { 51 try { 52 InitialContext jndiContext = new InitialContext (); 53 54 return (PublisherHome) jndiContext.lookup("cmp2/readonly/Publisher"); 55 } catch(Exception 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 jndiContext = new InitialContext (); 65 66 return (BookHome) jndiContext.lookup("cmp2/readonly/Book"); 67 } catch(Exception 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 jndiContext = new InitialContext (); 77 78 return (AuthorHome) jndiContext.lookup("cmp2/readonly/Author"); 79 } catch(Exception e) { 80 log.debug("failed", e); 81 fail("Exception in getAuthorHome: " + e.getMessage()); 82 } 83 return null; 84 } 85 86 private Connection getConnection() { 87 try { 88 InitialContext jndiContext = new InitialContext (); 89 DataSource ds = (DataSource ) jndiContext.lookup("java:/DefaultDS"); 90 return ds.getConnection(); 91 } catch(Exception 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 { 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 { 131 Collection 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 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 { 147 try { 148 oreilly.setName("Stuff"); 149 fail("Should have gotten exception from Publisher.setName"); 150 } catch(Exception e) { 151 } 152 } 153 154 public void testReadonlyEntityCMPFieldChange() throws Exception { 155 try { 156 dain.setName("Stuff"); 157 fail("Should have gotten exception from Author.setName"); 158 } catch(Exception e) { 159 } 160 } 161 162 public void testReadonlyEntityCreate() throws Exception { 163 try { 164 AuthorHome authorHome = getAuthorHome(); 165 authorHome.create(new Integer (44)); 166 fail("Should have gotten exception from AuthorHome.create"); 167 } catch(Exception e) { 168 } 169 } 170 171 public void testReadonlySetFK() throws Exception { 172 try { 173 jboss.setPublisher(sams); 174 fail("Should have gotten exception from Book.setPublisher"); 175 } catch(Exception e) { 176 } 177 } 178 179 public void testReadonlySetCollection() throws Exception { 180 try { 181 sams.setBooks(new HashSet ()); 182 fail("Should have gotten exception from Publisher.setBooks"); 183 } catch(Exception e) { 184 } 185 } 186 187 public void testReadonlyCollectionAdd() throws Exception { 188 try { 189 sams.getBooks().add(jboss); 190 fail("Should have gotten exception from Book.setPublisher"); 191 } catch(Exception e) { 192 } 193 } 194 195 public void testReadonlyCollectionRemove() throws Exception { 196 try { 197 sams.getBooks().remove(ejb); 198 fail("Should have gotten exception from Book.setPublisher"); 199 } catch(Exception e) { 200 } 201 } 202 203 public void setUpEJB() throws Exception { 204 cleanDB(); 205 206 Connection con = null; 207 PreparedStatement ps = null; 208 try { 209 con = getConnection(); 210 211 ps = con.prepareStatement( 212 "INSERT INTO PublisherEJB (id, name) " + 213 "VALUES (?,?)"); 214 215 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 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 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 e) { 282 log.debug("failed", e); 283 } 284 } 285 if(con != null) { 286 try { 287 con.close(); 288 } catch(SQLException e) { 289 log.debug("failed", e); 290 } 291 } 292 } 293 } 294 295 public void tearDownEJB() throws Exception { 296 cleanDB(); 297 } 298 299 public void cleanDB() throws Exception { 300 Connection con = null; 301 Statement 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 e) { 315 log.debug("failed", e); 316 } 317 } 318 if(con != null) { 319 try { 320 con.close(); 321 } catch(SQLException e) { 322 log.debug("failed", e); 323 } 324 } 325 } 326 } 327 } 328 329 330 331 | Popular Tags |