1 22 package org.jboss.test.cmp2.jbas979; 23 24 import javax.naming.InitialContext ; 25 import javax.naming.NamingException ; 26 import javax.transaction.TransactionRolledbackException ; 27 import javax.ejb.EJBException ; 28 import org.jboss.test.JBossTestCase; 29 import org.jboss.test.util.ejb.EJBTestCase; 30 import junit.framework.Test; 31 32 36 public class JBAS979UnitTestCase 37 extends EJBTestCase 38 { 39 private static final String STORE_NOT_FLUSHED_FALSE = "AStoreNotFlushedFalse"; 40 private static final String STORE_NOT_FLUSHED_TRUE = "AStoreNotFlushedTrue"; 41 private static final Integer PK = new Integer (1); 42 43 public static boolean PASSIVATED_IN_AFTER_COMPLETION; 44 public static Exception ERROR_IN_EJB_PASSIVATE; 45 46 public static Test suite() throws Exception 47 { 48 return JBossTestCase.getDeploySetup(JBAS979UnitTestCase.class, "cmp2-jbas979.jar"); 49 } 50 51 public JBAS979UnitTestCase(String methodName) 52 { 53 super(methodName); 54 } 55 56 protected void setUp() throws Exception 57 { 58 } 59 60 protected void tearDown() throws Exception 61 { 62 } 63 64 66 public void testPassivateAfterCommit_storeNotFlushedTrue() throws Exception 67 { 68 passivateAfterCommit(STORE_NOT_FLUSHED_TRUE); 69 } 70 71 public void testPassivateAfterCommit_storeNotFlushedFalse() throws Exception 72 { 73 passivateAfterCommit(STORE_NOT_FLUSHED_FALSE); 74 } 75 76 public void testUpdateAfterFlush_storeNotFlushedTrue() throws Exception 77 { 78 String jndiName = STORE_NOT_FLUSHED_TRUE; 79 Facade facade = getFacadeHome().create(); 80 facade.create(jndiName, PK, "name1"); 81 try 82 { 83 assertEquals("name2", facade.getNameFlushCacheSetName(jndiName, PK, "name2")); 84 } 85 finally 86 { 87 facade.remove(jndiName, PK); 88 } 89 } 90 91 public void testUpdateAfterFlush_storeNotFlushedFalse() throws Exception 92 { 93 String jndiName = STORE_NOT_FLUSHED_FALSE; 94 Facade facade = getFacadeHome().create(); 95 facade.create(jndiName, PK, "name1"); 96 try 97 { 98 facade.getNameFlushCacheSetName(jndiName, PK, "name2"); 99 fail("Flushed modified instances are not stored."); 100 } 101 catch(TransactionRolledbackException expected) 102 { 103 } 104 finally 105 { 106 facade.remove(jndiName, PK); 107 } 108 } 109 110 public void testAgeOutDoesntSchedulePassivationAfterCommit_storeNotFlushedTrue() throws Exception 111 { 112 ageOutDoesntSchedulePassivationAfterCommit(STORE_NOT_FLUSHED_TRUE); 113 } 114 115 public void testAgeOutDoesntSchedulePassivationAfterCommit_storeNotFlushedFalse() throws Exception 116 { 117 ageOutDoesntSchedulePassivationAfterCommit(STORE_NOT_FLUSHED_FALSE); 118 } 119 120 122 private void ageOutDoesntSchedulePassivationAfterCommit(String jndiName) 123 throws Exception 124 { 125 Facade facade = getFacadeHome().create(); 126 facade.create(jndiName, PK, "name1"); 127 try 128 { 129 facade.longTx(jndiName, PK, 5000); 130 if(ERROR_IN_EJB_PASSIVATE != null) 131 { 132 throw new EJBException ("Error in ejbPassivate", ERROR_IN_EJB_PASSIVATE); 133 } 134 135 if(PASSIVATED_IN_AFTER_COMPLETION) 136 { 137 fail("Natural aging out doesn't schedule passivation when transaction ends."); 138 } 139 } 140 finally 141 { 142 facade.remove(jndiName, PK); 143 } 144 } 145 146 private void passivateAfterCommit(String jndiName) 147 throws Exception 148 { 149 Facade facade = getFacadeHome().create(); 150 facade.create(jndiName, PK, "name1"); 151 try 152 { 153 String name1 = facade.getName(jndiName, PK); 154 facade.updateDB(jndiName, PK, "name2"); 155 assertEquals(name1, facade.getNameFlushCacheGetName(jndiName, PK)); 157 assertEquals("name2", facade.getName(jndiName, PK)); 158 } 159 finally 160 { 161 facade.remove(jndiName, PK); 162 } 163 } 164 165 private FacadeHome getFacadeHome() 166 throws NamingException 167 { 168 return (FacadeHome)lookup("Facade"); 169 } 170 171 private Object lookup(String name) throws NamingException 172 { 173 InitialContext ic = null; 174 try 175 { 176 ic = new InitialContext (); 177 return ic.lookup(name); 178 } 179 finally 180 { 181 if(ic != null) 182 { 183 ic.close(); 184 } 185 } 186 } 187 } 188 | Popular Tags |