1 25 package org.objectweb.easybeans.tests.common.ejbs.stateful.containermanaged.xmldescriptor; 26 27 import static org.testng.Assert.fail; 28 29 import java.sql.SQLException ; 30 31 import javax.ejb.EJB ; 32 import javax.ejb.EJBException ; 33 import javax.ejb.Remote ; 34 import javax.ejb.Stateful ; 35 import javax.naming.NamingException ; 36 import javax.transaction.UserTransaction ; 37 38 import org.objectweb.easybeans.log.JLog; 39 import org.objectweb.easybeans.log.JLogFactory; 40 import org.objectweb.easybeans.tests.common.ejbs.stateless.containermanaged.ItfDatabaseManager; 41 import org.objectweb.easybeans.tests.common.helper.TransactionHelper; 42 43 49 @Stateful 50 @Remote (ItfTransAttributeTester.class) 51 public class SFSBTransAttributeTester implements ItfTransAttributeTester { 52 53 57 @EJB (beanName = "SFSBTransAttributeDefMandatory") 58 private ItfTransAttributeDefinition beanMandatory; 59 60 64 @EJB (beanName = "SFSBTransAttributeDefRequired") 65 private ItfTransAttributeDefinition beanRequired; 66 67 71 @EJB (beanName = "SFSBTransAttributeDefRequiresNew") 72 private ItfTransAttributeDefinition beanRequiresNew; 73 74 78 @EJB (beanName = "SFSBTransAttributeDefSupports") 79 private ItfTransAttributeDefinition beanSupports; 80 81 85 @EJB (beanName = "SFSBTransAttributeDefNotSupported") 86 private ItfTransAttributeDefinition beanNotSupported; 87 88 92 @EJB (beanName = "SFSBTransAttributeDefNever") 93 private ItfTransAttributeDefinition beanNever; 94 95 98 @EJB 99 private ItfDatabaseManager slsbDatabaseManager; 100 101 104 private static JLog logger = JLogFactory.getLog(SFSBTransAttributeTester.class); 105 106 115 private boolean usesClientTransaction(final ItfTransAttributeDefinition bean) throws Exception { 116 UserTransaction utx = TransactionHelper.getUserTransaction(); 117 try { 118 utx.begin(); 119 bean.insertTableCorrectMandatory(DB_NAME, TABLE_NAME); 120 } finally { 121 utx.rollback(); 122 } 123 try { 124 slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME); 125 return false; 126 } catch (SQLException e) { 127 return true; 128 } 129 } 130 131 137 private boolean hasTransaction(final ItfTransAttributeDefinition bean) throws Exception { 138 bean.insertTableErrorMandatory(DB_NAME, TABLE_NAME); 139 try { 140 slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME); 141 return false; 142 } catch (SQLException e) { 143 return true; 144 } 145 } 146 147 152 public void testMandatory() throws Exception { 153 deleteTable(DB_NAME, TABLE_NAME); 155 156 UserTransaction utx = TransactionHelper.getUserTransaction(); 157 try { 158 utx.begin(); 159 beanMandatory.insertTableCorrect(DB_NAME, TABLE_NAME); 160 } finally { 161 utx.rollback(); 162 } 163 try { 164 slsbDatabaseManager.verifyTable(DB_NAME, TABLE_NAME); 165 fail("The deployment descriptor did not override the " 166 + "annotation. The bean did not use the client transaction in mandatory mode."); 167 } catch (SQLException e) { 168 logger.debug("The bean thron an expected exception{0}", e); 169 } 170 171 deleteTable(DB_NAME, TABLE_NAME); 173 174 try { 175 beanMandatory.insertTableCorrect(DB_NAME, TABLE_NAME); 176 } catch (EJBException e) { 177 logger.debug("The bean thron an expected exception{0}", e); 178 fail("The deployment descriptor did not override the annotation. " 179 + "The bean was called withou a transaction context in a mandatory mode and there was not exception."); 180 } 181 } 182 183 188 public void testRequired() throws Exception { 189 deleteTable(DB_NAME, TABLE_NAME); 191 boolean bolClientTransaction = usesClientTransaction(beanRequired); 192 deleteTable(DB_NAME, TABLE_NAME); 194 boolean bolHasTransaction = hasTransaction(beanRequired); 195 if (!(bolClientTransaction && bolHasTransaction)) { 196 fail("The container did not override the transaction attribute for REQUIRED"); 197 } 198 } 199 200 205 public void testRequiresNew() throws Exception { 206 deleteTable(DB_NAME, TABLE_NAME); 208 boolean bolClientTransaction = usesClientTransaction(beanRequiresNew); 209 deleteTable(DB_NAME, TABLE_NAME); 211 boolean bolHasTransaction = hasTransaction(beanRequiresNew); 212 if (!(!bolClientTransaction && bolHasTransaction)) { 213 fail("The container did not override the transaction attribute for REQUIRES_NEW"); 214 } 215 } 216 217 222 public void testSupports() throws Exception { 223 deleteTable(DB_NAME, TABLE_NAME); 225 boolean bolClientTransaction = usesClientTransaction(beanSupports); 226 deleteTable(DB_NAME, TABLE_NAME); 228 boolean bolHasTransaction = hasTransaction(beanSupports); 229 if (!(bolClientTransaction && !bolHasTransaction)) { 230 fail("The container did not override the transaction attribute for SUPPORTS"); 231 } 232 } 233 234 239 public void testNotSupported() throws Exception { 240 deleteTable(DB_NAME, TABLE_NAME); 242 boolean bolClientTransaction = usesClientTransaction(beanNotSupported); 243 deleteTable(DB_NAME, TABLE_NAME); 245 boolean bolHasTransaction = hasTransaction(beanNotSupported); 246 if (bolClientTransaction || bolHasTransaction) { 247 fail("The container did not override the transaction attribute for NOT_SUPPORTED"); 248 } 249 } 250 251 256 public void testNever() throws Exception { 257 deleteTable(DB_NAME, TABLE_NAME); 259 boolean bolClientTransaction = true; 260 try { 261 usesClientTransaction(beanNever); 262 } catch (EJBException e) { 263 bolClientTransaction = false; 264 } 265 deleteTable(DB_NAME, TABLE_NAME); 267 boolean bolHasTransaction = hasTransaction(beanNever); 268 if (bolClientTransaction || bolHasTransaction) { 269 fail("The container did not override the transaction attribute for NEVER"); 270 } 271 } 272 273 278 protected void deleteTable(final String dbName, final String tableName) { 279 try { 281 slsbDatabaseManager.deleteTable(dbName, tableName); 282 } catch (SQLException e) { 283 logger.debug("The table delete threw an error during the execution {0}", e); 284 } catch (NamingException e) { 285 logger.debug("The table delete threw an error during the execution {0}", e); 286 } 287 } 288 289 } 290 | Popular Tags |