KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > keygen > test > KeyGenerationUnitTestCase


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.keygen.test;
23
24 import java.util.Collection JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27
28 import junit.framework.Test;
29 import org.jboss.logging.Logger;
30 import org.jboss.test.JBossTestCase;
31 import org.jboss.test.cmp2.keygen.ejb.UnknownPKLocal;
32 import org.jboss.test.cmp2.keygen.ejb.UnknownPKLocalHome;
33 import org.jboss.test.cmp2.keygen.ejb.IntegerPKLocalHome;
34 import org.jboss.test.cmp2.keygen.ejb.UnknownPKHome;
35 import org.jboss.test.cmp2.keygen.ejb.UnknownPK;
36 import org.jboss.test.util.ejb.EJBTestCase;
37
38 /** Tests of the entity-command key generation
39  *
40  * @author <a HREF="mailto:jeremy@boynes.com">Jeremy Boynes</a>
41  * @author Scott.Stark@jboss.org
42  * @version $Revision: 58115 $
43  */

44 public class KeyGenerationUnitTestCase extends EJBTestCase
45 {
46    static final Logger log = Logger.getLogger(KeyGenerationUnitTestCase.class);
47
48    public static Test suite() throws Exception JavaDoc
49    {
50       return JBossTestCase.getDeploySetup(KeyGenerationUnitTestCase.class, "cmp2-keygen.jar");
51    }
52
53    public KeyGenerationUnitTestCase(String JavaDoc name)
54    {
55       super(name);
56    }
57
58    public void testJBAS1249() throws Exception JavaDoc
59    {
60       UnknownPKHome home = getUnknownPKRemoteHome("remote/TestPkSqlEJB");
61       UnknownPK ejb = home.create("kloop");
62       try
63       {
64          ejb.getHandle().getEJBObject();
65       }
66       catch(Exception JavaDoc e)
67       {
68          fail("Handler.getEJBObject() worked");
69          throw e;
70       }
71       finally
72       {
73          ejb.remove();
74       }
75    }
76
77    public void testUUIDKeyGenerator() throws Exception JavaDoc
78    {
79       UnknownPKLocalHome home = getUnknownPKHome("local/TestUUIDKeyGenEJB");
80       UnknownPKLocal ejb1 = home.create("testUUIDKeyGenerator");
81       UnknownPKLocal ejb2 = home.create("testUUIDKeyGenerator");
82       try
83       {
84          UnknownPKLocal ejb1a = home.findByPrimaryKey(ejb1.getPrimaryKey());
85          assertTrue(ejb1.isIdentical(ejb1a));
86          assertTrue(ejb1.isIdentical(ejb2) == false);
87          assertTrue(ejb1.getPrimaryKey().equals(ejb2.getPrimaryKey()) == false);
88       }
89       finally
90       {
91          ejb1.remove();
92          ejb2.remove();
93       }
94    }
95
96    public void testPkSQLKeyGenerator() throws Exception JavaDoc
97    {
98       UnknownPKLocalHome home = getUnknownPKHome("local/TestPkSqlEJB");
99       UnknownPKLocal ejb1 = home.create("testPkSQLKeyGenerator");
100       Thread.sleep(50);
101       UnknownPKLocal ejb2 = home.create("testPkSQLKeyGenerator");
102       try
103       {
104          UnknownPKLocal ejb1a = home.findByPrimaryKey(ejb1.getPrimaryKey());
105          assertTrue(ejb1.isIdentical(ejb1a));
106          assertTrue(ejb1.isIdentical(ejb2) == false);
107          assertTrue(ejb1.getPrimaryKey().equals(ejb2.getPrimaryKey()) == false);
108       }
109       finally
110       {
111          ejb1.remove();
112          ejb2.remove();
113       }
114    }
115
116    public void testHsqldbKeyGenerator() throws Exception JavaDoc
117    {
118       UnknownPKLocalHome home = getUnknownPKHome("local/TestHsqldbEJB");
119       UnknownPKLocal ejb1 = home.create("testHsqldbKeyGenerator");
120       UnknownPKLocal ejb2 = home.create("testHsqldbKeyGenerator");
121       try
122       {
123          UnknownPKLocal ejb1a = home.findByPrimaryKey(ejb1.getPrimaryKey());
124          assertTrue(ejb1.isIdentical(ejb1a));
125          assertTrue(ejb1.isIdentical(ejb2) == false);
126          assertTrue(ejb1.getPrimaryKey().equals(ejb2.getPrimaryKey()) == false);
127       }
128       finally
129       {
130          ejb1.remove();
131          ejb2.remove();
132       }
133    }
134
135    public void testHsqldbIntegerKeyGenerator() throws Exception JavaDoc
136    {
137       Context JavaDoc ctx = new InitialContext JavaDoc();
138       IntegerPKLocalHome home = (IntegerPKLocalHome) ctx.lookup("java:comp/env/local/TestHsqldbIntegerEJB");
139       UnknownPKLocal ejb1 = home.create("testHsqldbIntegerKeyGenerator");
140       UnknownPKLocal ejb2 = home.create("testHsqldbIntegerKeyGenerator");
141       try
142       {
143          Integer JavaDoc key = (Integer JavaDoc) ejb1.getPrimaryKey();
144          UnknownPKLocal ejb1a = home.findByPrimaryKey(key);
145          assertTrue(ejb1.isIdentical(ejb1a));
146          assertTrue(ejb1.isIdentical(ejb2) == false);
147          assertTrue(ejb1.getPrimaryKey().equals(ejb2.getPrimaryKey()) == false);
148       }
149       finally
150       {
151          ejb1.remove();
152          ejb2.remove();
153       }
154    }
155
156    public void testInvalidHsqldbIntegerKeyGenerator() throws Exception JavaDoc
157    {
158       Context JavaDoc ctx = new InitialContext JavaDoc();
159       IntegerPKLocalHome home = (IntegerPKLocalHome) ctx.lookup("java:comp/env/local/InvalidHsqldbIntegerEJB");
160       try
161       {
162          UnknownPKLocal ejb1 = home.create("testInvalidHsqldbIntegerKeyGenerator");
163          Object JavaDoc key = ejb1.getPrimaryKey();
164          assertTrue("InvalidHsqldbIntegerEJB key != null", key != null);
165       }
166       catch(Exception JavaDoc e)
167       {
168          log.debug("create failed as expected", e);
169          // Remove the bean that was inserted into the table
170
Collection JavaDoc beans = home.findAll();
171          UnknownPKLocal ejb1 = (UnknownPKLocal) beans.iterator().next();
172          ejb1.remove();
173       }
174    }
175
176    public void testOtherKeyGenerator() throws Exception JavaDoc
177    {
178       UnknownPKLocalHome home = getUnknownPKHome("local/TestOtherEJB");
179       UnknownPKLocal ejb1 = home.create("testOtherKeyGenerator1");
180       UnknownPKLocal ejb2 = home.create("testOtherKeyGenerator2");
181       try
182       {
183          UnknownPKLocal ejb1a = home.findByPrimaryKey(ejb1.getPrimaryKey());
184          assertTrue(ejb1.isIdentical(ejb1a));
185          assertEquals("testOtherKeyGenerator1", ejb1a.getValue());
186          assertTrue(ejb1.isIdentical(ejb2) == false);
187          assertTrue(ejb1.getPrimaryKey().equals(ejb2.getPrimaryKey()) == false);
188       }
189       finally
190       {
191          ejb1.remove();
192          ejb2.remove();
193       }
194    }
195
196    private UnknownPKLocalHome getUnknownPKHome(String JavaDoc jndiName) throws Exception JavaDoc
197    {
198       return (UnknownPKLocalHome) getHome(jndiName);
199    }
200
201    private UnknownPKHome getUnknownPKRemoteHome(String JavaDoc jndiName) throws Exception JavaDoc
202    {
203       return (UnknownPKHome)getHome(jndiName);
204    }
205
206    private Object JavaDoc getHome(String JavaDoc jndiName) throws Exception JavaDoc
207    {
208       Context JavaDoc ctx = new InitialContext JavaDoc();
209       return ctx.lookup("java:comp/env/"+jndiName);
210    }
211 }
212
Popular Tags