User: dsundstrom
  Date: 01/12/12 12:06:38

  Added:       src/main/org/jboss/test/cmp2/readonly Book.java
                        BookBean.java BookHome.java Publisher.java
                        PublisherBean.java PublisherHome.java
                        ReadonlyUnitTestCase.java
  Log:
  Initial revision of test for readonly cmr and cmp fileds.
  
  Revision  Changes    Path
  1.1                  jbosstest/src/main/org/jboss/test/cmp2/readonly/Book.java
  
  Index: Book.java
  ===================================================================
  package org.jboss.test.cmp2.readonly;
  
  import javax.ejb.EJBLocalObject;
  
  public interface Book extends EJBLocalObject {
     public Integer getId();
     public String getName();
     public void setName(String name);
     public String getIsbn();
     public void setIsbn(String isbn);
        public Publisher getPublisher();
        public void setPublisher(Publisher publisher);
  }
  
  
  
  1.1                  jbosstest/src/main/org/jboss/test/cmp2/readonly/BookBean.java
  
  Index: BookBean.java
  ===================================================================
  package org.jboss.test.cmp2.readonly;
  
  import javax.ejb.EntityBean;
  import javax.ejb.EntityContext;
  
  public abstract class BookBean implements EntityBean {
        transient private EntityContext ctx;
  
        public Integer ejbCreate(Integer id) {
                setId(id);
                return null;
        }
  
        public void ejbPostCreate(Integer id) {
        }
  
        public abstract Integer getId();
        public abstract void setId(Integer id);
  
        public abstract String getName();
        public abstract void setName(String name);
  
        public abstract String getIsbn();
        public abstract void setIsbn(String isbn);
  
        public abstract Publisher getPublisher();
        public abstract void setPublisher(Publisher publisher);
  
        public void setEntityContext(EntityContext ctx) {
                this.ctx = ctx;
        }
  
        public void unsetEntityContext() {
                this.ctx = null;
        }
  
        public void ejbActivate() {
        }
  
        public void ejbPassivate() {
        }
  
        public void ejbLoad() {
        }
  
        public void ejbStore() {
        }
  
        public void ejbRemove() {
        }
  }
  
  
  
  1.1                  jbosstest/src/main/org/jboss/test/cmp2/readonly/BookHome.java
  
  Index: BookHome.java
  ===================================================================
  package org.jboss.test.cmp2.readonly;
  
  import java.util.Collection;
  import javax.ejb.CreateException;
  import javax.ejb.EJBLocalHome;
  import javax.ejb.FinderException;
  
  public interface BookHome extends EJBLocalHome {
        public Book create(Integer id) throws CreateException;
        public Book findByPrimaryKey(Integer id) throws FinderException;
        public Book findByName(String name) throws FinderException;
        public Collection findAll() throws FinderException;
  }
  
  
  
  1.1                  jbosstest/src/main/org/jboss/test/cmp2/readonly/Publisher.java
  
  Index: Publisher.java
  ===================================================================
  package org.jboss.test.cmp2.readonly;
  
  import java.util.Collection;
  import javax.ejb.EJBLocalObject;
  
  public interface Publisher extends EJBLocalObject {
     public Integer getId();
     public String getName();
     public void setName(String name);
        public Collection getBooks();
        public void setBooks(Collection books);
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/cmp2/readonly/PublisherBean.java
  
  Index: PublisherBean.java
  ===================================================================
  package org.jboss.test.cmp2.readonly;
  
  import java.util.Collection;
  import javax.ejb.EntityBean;
  import javax.ejb.EntityContext;
  
  public abstract class PublisherBean implements EntityBean {
        transient private EntityContext ctx;
  
        public Integer ejbCreate(Integer id) {
                setId(id);
                return null;
        }
  
        public void ejbPostCreate(Integer id) {
        }
  
        public abstract Integer getId();
        public abstract void setId(Integer id);
  
        public abstract String getName();
        public abstract void setName(String name);
  
        public abstract Collection getBooks();
        public abstract void setBooks(Collection books);
  
        public void setEntityContext(EntityContext ctx) {
                this.ctx = ctx;
        }
  
        public void unsetEntityContext() {
                this.ctx = null;
        }
  
        public void ejbActivate() {
        }
  
        public void ejbPassivate() {
        }
  
        public void ejbLoad() {
        }
  
        public void ejbStore() {
        }
  
        public void ejbRemove() {
        }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/cmp2/readonly/PublisherHome.java
  
  Index: PublisherHome.java
  ===================================================================
  package org.jboss.test.cmp2.readonly;
  
  import java.util.Collection;
  import javax.ejb.CreateException;
  import javax.ejb.EJBLocalHome;
  import javax.ejb.FinderException;
  
  public interface PublisherHome extends EJBLocalHome {
        public Publisher create(Integer id) throws CreateException;
        public Publisher findByPrimaryKey(Integer id) throws FinderException;
        public Publisher findByName(String name) throws FinderException;
        public Collection findAll() throws FinderException;
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/cmp2/readonly/ReadonlyUnitTestCase.java
  
  Index: ReadonlyUnitTestCase.java
  ===================================================================
  package org.jboss.test.cmp2.readonly;
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.SQLException;
  import java.sql.Statement;
  import java.util.Collection;
  import java.util.HashSet;
  import javax.naming.InitialContext;
  import javax.sql.DataSource;
  import junit.framework.Test;
  import net.sourceforge.junitejb.EJBTestCase;
  import org.jboss.test.JBossTestCase;
  
  public class ReadonlyUnitTestCase extends EJBTestCase {
  
        public static Test suite() throws Exception {
                return JBossTestCase.getJ2eeSetup(
              ReadonlyUnitTestCase.class, "cmp2-readonly.jar");
     }
  
        public ReadonlyUnitTestCase(String name) {
                super(name);
        }
  
        private PublisherHome getPublisherHome() {
                try {
                        InitialContext jndiContext = new InitialContext();
                        
                        return (PublisherHome) 
jndiContext.lookup("cmp2/readonly/Publisher"); 
                } catch(Exception e) {
                        e.printStackTrace();
                        fail("Exception in getPublisherHome: " + e.getMessage());
                }
                return null;
        }
  
        private BookHome getBookHome() {
                try {
                        InitialContext jndiContext = new InitialContext();
                        
                        return (BookHome) jndiContext.lookup("cmp2/readonly/Book");
                } catch(Exception e) {
                        e.printStackTrace();
                        fail("Exception in getBookHome: " + e.getMessage());
                }
                return null;
        }
     
     private Connection getConnection() {
        try {
                        InitialContext jndiContext = new InitialContext();
           DataSource ds = (DataSource) jndiContext.lookup("java:/DefaultDS");
           return ds.getConnection();
                } catch(Exception e) {
                        e.printStackTrace();
                        fail("Exception in getConnection: " + e.getMessage());
                }
                return null;
     }
        
     private Publisher oreilly;
     private Publisher sams;
     private Book ejb;
     private Book jms;
     private Book jmx;
     private Book jboss;
  
     protected void setUp() throws Exception {
        PublisherHome publisherHome = getPublisherHome();
        BookHome bookHome = getBookHome();
  
        oreilly = publisherHome.findByName("O'Reilly & Associates");
        sams = publisherHome.findByName("Sams");
        ejb = bookHome.findByName("Enterprise Java Beans (3rd Edition)");
        jms = bookHome.findByName("Java Message Service");
        jmx = bookHome.findByName(
              "JMX: Managing J2EE with Java Management Extensions");
        jboss = bookHome.findByName("JBOSS Administration and Development");
     }
  
     protected void tearDown() {
        oreilly = null;
        sams = null;
        ejb = null;
        jms = null;
        jmx = null;
        jboss = null;
     }
  
     public void testSetUp() throws Exception {
        Collection oreillyBooks = oreilly.getBooks();
        assertEquals(2, oreillyBooks.size());
        assertTrue(oreillyBooks.contains(ejb));
        assertTrue(oreillyBooks.contains(jms));
        assertTrue(ejb.getPublisher().isIdentical(oreilly));
        assertTrue(jms.getPublisher().isIdentical(oreilly));
  
        Collection samsBooks = sams.getBooks();
        assertEquals(2, samsBooks.size());
        assertTrue(samsBooks.contains(jmx));
        assertTrue(samsBooks.contains(jboss));
        assertTrue(jmx.getPublisher().isIdentical(sams));
        assertTrue(jboss.getPublisher().isIdentical(sams));
     }
  
     public void testReadonlyCMPField() throws Exception {
        try {
           oreilly.setName("Stuff");
           fail("Should have gotten exception from Publisher.setName");
        } catch(Exception e) {
        }
     }
  
     public void testReadonlySetFK() throws Exception {
        try {
           jboss.setPublisher(sams);
           fail("Should have gotten exception from Book.setPublisher");
        } catch(Exception e) {
        }
     }
  
     public void testReadonlySetCollection() throws Exception {
        try {
           sams.setBooks(new HashSet());
           fail("Should have gotten exception from Publisher.setBooks");
        } catch(Exception e) {
        }
     }
     
     public void testReadonlyCollectionAdd() throws Exception {
        try {
           sams.getBooks().add(jboss);
           fail("Should have gotten exception from Book.setPublisher");
        } catch(Exception e) {
        }
     }
     
     public void testReadonlyCollectionRemove() throws Exception {
        try {
           sams.getBooks().remove(ejb);
           fail("Should have gotten exception from Book.setPublisher");
        } catch(Exception e) {
        }
     }
     
        public void setUpEJB() throws Exception {
        cleanDB();
  
        Connection con = null;
        PreparedStatement ps = null;
        try {
           con = getConnection();
           
           ps = con.prepareStatement(
                 "INSERT INTO Publisher (id, name) " +
                 "VALUES (?,?)");
           
           // O'Reilly
           ps.setInt(1, 1);
           ps.setString(2, "O'Reilly & Associates");
           if(ps.executeUpdate() != 1) {
              fail("Failed to add Publisher to database");
           }
  
           // Sams
           ps.setInt(1, 2);
           ps.setString(2, "Sams");
           if(ps.executeUpdate() != 1) {
              fail("Failed to add Publisher to database");
           }
  
           ps.close();
  
           ps = con.prepareStatement(
                 "INSERT INTO Book (id, name, isbn, publisher) " +
                 "VALUES (?,?,?,?)");
  
           ps.setInt(1, -1);
           ps.setString(2, "Enterprise Java Beans (3rd Edition)");
           ps.setString(3, "0596002262");
           ps.setInt(4, 1);
           if(ps.executeUpdate() != 1) {
              fail("Failed to add Book to database");
           }
  
           ps.setInt(1, -2);
           ps.setString(2, "Java Message Service");
           ps.setString(3, "0596000685 ");
           ps.setInt(4, 1);
           if(ps.executeUpdate() != 1) {
              fail("Failed to add Book to database");
           }
  
           ps.setInt(1, -3);
           ps.setString(2, "JMX: Managing J2EE with Java Management Extensions");
           ps.setString(3, "0672322889");
           ps.setInt(4, 2);
           if(ps.executeUpdate() != 1) {
              fail("Failed to add Book to database");
           }
  
           ps.setInt(1, -4);
           ps.setString(2, "JBOSS Administration and Development");
           ps.setString(3, "0672323478");
           ps.setInt(4, 2);
           if(ps.executeUpdate() != 1) {
              fail("Failed to add Book to database");
           }
        } finally {
           if(ps != null) {
              try {
                 ps.close();
              } catch(SQLException e) {
                 e.printStackTrace();
              }
           }
           if(con != null) {
              try {
                 con.close();
              } catch(SQLException e) {
                 e.printStackTrace();
              }
           }
        }
        }
        
        public void tearDownEJB() throws Exception {
        cleanDB();
        }
        
        public void cleanDB() throws Exception {
           Connection con = null;
        Statement statement = null;
        try {
           con = getConnection();
           
           statement = con.createStatement();
  
           statement.executeUpdate("DELETE FROM Book");
           statement.executeUpdate("DELETE FROM Publisher");
        } finally {
           if(statement != null) {
              try {
                 statement.close();
              } catch(SQLException e) {
                 e.printStackTrace();
              }
           }
           if(con != null) {
              try {
                 con.close();
              } catch(SQLException e) {
                 e.printStackTrace();
              }
           }
        }
     }
  }
  
  
  
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to