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.jbas979;
023:
024: import javax.naming.InitialContext;
025: import javax.naming.NamingException;
026: import javax.transaction.TransactionRolledbackException;
027: import javax.ejb.EJBException;
028: import org.jboss.test.JBossTestCase;
029: import org.jboss.test.util.ejb.EJBTestCase;
030: import junit.framework.Test;
031:
032: /**
033: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
034: * @version <tt>$Revision: 57211 $</tt>
035: */
036: public class JBAS979UnitTestCase extends EJBTestCase {
037: private static final String STORE_NOT_FLUSHED_FALSE = "AStoreNotFlushedFalse";
038: private static final String STORE_NOT_FLUSHED_TRUE = "AStoreNotFlushedTrue";
039: private static final Integer PK = new Integer(1);
040:
041: public static boolean PASSIVATED_IN_AFTER_COMPLETION;
042: public static Exception ERROR_IN_EJB_PASSIVATE;
043:
044: public static Test suite() throws Exception {
045: return JBossTestCase.getDeploySetup(JBAS979UnitTestCase.class,
046: "cmp2-jbas979.jar");
047: }
048:
049: public JBAS979UnitTestCase(String methodName) {
050: super (methodName);
051: }
052:
053: protected void setUp() throws Exception {
054: }
055:
056: protected void tearDown() throws Exception {
057: }
058:
059: // tests
060:
061: public void testPassivateAfterCommit_storeNotFlushedTrue()
062: throws Exception {
063: passivateAfterCommit(STORE_NOT_FLUSHED_TRUE);
064: }
065:
066: public void testPassivateAfterCommit_storeNotFlushedFalse()
067: throws Exception {
068: passivateAfterCommit(STORE_NOT_FLUSHED_FALSE);
069: }
070:
071: public void testUpdateAfterFlush_storeNotFlushedTrue()
072: throws Exception {
073: String jndiName = STORE_NOT_FLUSHED_TRUE;
074: Facade facade = getFacadeHome().create();
075: facade.create(jndiName, PK, "name1");
076: try {
077: assertEquals("name2", facade.getNameFlushCacheSetName(
078: jndiName, PK, "name2"));
079: } finally {
080: facade.remove(jndiName, PK);
081: }
082: }
083:
084: public void testUpdateAfterFlush_storeNotFlushedFalse()
085: throws Exception {
086: String jndiName = STORE_NOT_FLUSHED_FALSE;
087: Facade facade = getFacadeHome().create();
088: facade.create(jndiName, PK, "name1");
089: try {
090: facade.getNameFlushCacheSetName(jndiName, PK, "name2");
091: fail("Flushed modified instances are not stored.");
092: } catch (TransactionRolledbackException expected) {
093: } finally {
094: facade.remove(jndiName, PK);
095: }
096: }
097:
098: public void testAgeOutDoesntSchedulePassivationAfterCommit_storeNotFlushedTrue()
099: throws Exception {
100: ageOutDoesntSchedulePassivationAfterCommit(STORE_NOT_FLUSHED_TRUE);
101: }
102:
103: public void testAgeOutDoesntSchedulePassivationAfterCommit_storeNotFlushedFalse()
104: throws Exception {
105: ageOutDoesntSchedulePassivationAfterCommit(STORE_NOT_FLUSHED_FALSE);
106: }
107:
108: // Private
109:
110: private void ageOutDoesntSchedulePassivationAfterCommit(
111: String jndiName) throws Exception {
112: Facade facade = getFacadeHome().create();
113: facade.create(jndiName, PK, "name1");
114: try {
115: facade.longTx(jndiName, PK, 5000);
116: if (ERROR_IN_EJB_PASSIVATE != null) {
117: throw new EJBException("Error in ejbPassivate",
118: ERROR_IN_EJB_PASSIVATE);
119: }
120:
121: if (PASSIVATED_IN_AFTER_COMPLETION) {
122: fail("Natural aging out doesn't schedule passivation when transaction ends.");
123: }
124: } finally {
125: facade.remove(jndiName, PK);
126: }
127: }
128:
129: private void passivateAfterCommit(String jndiName) throws Exception {
130: Facade facade = getFacadeHome().create();
131: facade.create(jndiName, PK, "name1");
132: try {
133: String name1 = facade.getName(jndiName, PK);
134: facade.updateDB(jndiName, PK, "name2");
135: // commit option A
136: assertEquals(name1, facade.getNameFlushCacheGetName(
137: jndiName, PK));
138: assertEquals("name2", facade.getName(jndiName, PK));
139: } finally {
140: facade.remove(jndiName, PK);
141: }
142: }
143:
144: private FacadeHome getFacadeHome() throws NamingException {
145: return (FacadeHome) lookup("Facade");
146: }
147:
148: private Object lookup(String name) throws NamingException {
149: InitialContext ic = null;
150: try {
151: ic = new InitialContext();
152: return ic.lookup(name);
153: } finally {
154: if (ic != null) {
155: ic.close();
156: }
157: }
158: }
159: }
|