KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > jbas979 > JBAS979UnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.jbas979;
23
24 import javax.naming.InitialContext JavaDoc;
25 import javax.naming.NamingException JavaDoc;
26 import javax.transaction.TransactionRolledbackException JavaDoc;
27 import javax.ejb.EJBException JavaDoc;
28 import org.jboss.test.JBossTestCase;
29 import org.jboss.test.util.ejb.EJBTestCase;
30 import junit.framework.Test;
31
32 /**
33  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
34  * @version <tt>$Revision: 44013 $</tt>
35  */

36 public class JBAS979UnitTestCase
37    extends EJBTestCase
38 {
39    private static final String JavaDoc STORE_NOT_FLUSHED_FALSE = "AStoreNotFlushedFalse";
40    private static final String JavaDoc STORE_NOT_FLUSHED_TRUE = "AStoreNotFlushedTrue";
41    private static final Integer JavaDoc PK = new Integer JavaDoc(1);
42
43    public static boolean PASSIVATED_IN_AFTER_COMPLETION;
44    public static Exception JavaDoc ERROR_IN_EJB_PASSIVATE;
45
46    public static Test suite() throws Exception JavaDoc
47    {
48       return JBossTestCase.getDeploySetup(JBAS979UnitTestCase.class, "cmp2-jbas979.jar");
49    }
50
51    public JBAS979UnitTestCase(String JavaDoc methodName)
52    {
53       super(methodName);
54    }
55
56    protected void setUp() throws Exception JavaDoc
57    {
58    }
59
60    protected void tearDown() throws Exception JavaDoc
61    {
62    }
63
64    // tests
65

66    public void testPassivateAfterCommit_storeNotFlushedTrue() throws Exception JavaDoc
67    {
68       passivateAfterCommit(STORE_NOT_FLUSHED_TRUE);
69    }
70
71    public void testPassivateAfterCommit_storeNotFlushedFalse() throws Exception JavaDoc
72    {
73       passivateAfterCommit(STORE_NOT_FLUSHED_FALSE);
74    }
75
76    public void testUpdateAfterFlush_storeNotFlushedTrue() throws Exception JavaDoc
77    {
78       String JavaDoc 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 JavaDoc
92    {
93       String JavaDoc 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 JavaDoc expected)
102       {
103       }
104       finally
105       {
106          facade.remove(jndiName, PK);
107       }
108    }
109
110    public void testAgeOutDoesntSchedulePassivationAfterCommit_storeNotFlushedTrue() throws Exception JavaDoc
111    {
112       ageOutDoesntSchedulePassivationAfterCommit(STORE_NOT_FLUSHED_TRUE);
113    }
114
115    public void testAgeOutDoesntSchedulePassivationAfterCommit_storeNotFlushedFalse() throws Exception JavaDoc
116    {
117       ageOutDoesntSchedulePassivationAfterCommit(STORE_NOT_FLUSHED_FALSE);
118    }
119
120    // Private
121

122    private void ageOutDoesntSchedulePassivationAfterCommit(String JavaDoc jndiName)
123       throws Exception JavaDoc
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 JavaDoc("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 JavaDoc jndiName)
147       throws Exception JavaDoc
148    {
149       Facade facade = getFacadeHome().create();
150       facade.create(jndiName, PK, "name1");
151       try
152       {
153          String JavaDoc name1 = facade.getName(jndiName, PK);
154          facade.updateDB(jndiName, PK, "name2");
155          // commit option A
156
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 JavaDoc
167    {
168       return (FacadeHome)lookup("Facade");
169    }
170
171    private Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc
172    {
173       InitialContext JavaDoc ic = null;
174       try
175       {
176          ic = new InitialContext JavaDoc();
177          return ic.lookup(name);
178       }
179       finally
180       {
181          if(ic != null)
182          {
183             ic.close();
184          }
185       }
186    }
187 }
188
Popular Tags