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.foedeployer.test;
023:
024: import java.io.IOException;
025: import java.net.InetAddress;
026: import java.rmi.RemoteException;
027: import java.util.Set;
028: import java.util.Collection;
029: import javax.ejb.CreateException;
030: import javax.ejb.Handle;
031: import javax.management.ObjectName;
032: import javax.naming.InitialContext;
033: import javax.naming.NamingException;
034: import javax.rmi.PortableRemoteObject;
035:
036: import junit.extensions.TestSetup;
037: import junit.framework.Test;
038: import junit.framework.TestCase;
039: import junit.framework.TestSuite;
040:
041: import org.jboss.test.JBossTestCase;
042: import org.jboss.test.JBossTestSetup;
043:
044: import org.jboss.test.foedeployer.ejb.simple.SecretManager;
045: import org.jboss.test.foedeployer.ejb.simple.SecretManagerHome;
046: import org.jboss.test.foedeployer.ejb.o2mb.O2MBManager;
047: import org.jboss.test.foedeployer.ejb.o2mb.O2MBManagerHome;
048:
049: /**
050: * Test of relationships conversion
051: *
052: * @author <a href="mailto:loubyansky@hotmail.com">Alex Loubyansky</a>
053: * @version $Revision: 57211 $
054: */
055: public class O2MBConversionTestCase extends JBossTestCase {
056: // Constants -----------------------------------------------------
057: public static final String FOE_DEPLOYER = "foe-deployer-3.2.sar";
058: public static final String FOE_DEPLOYER_NAME = "jboss:service=FoeDeployer";
059: public static final String CONVERTOR_DEPLOYER_QUERY_NAME = "jboss:service=Convertor,*";
060: public static final String APPLICATION = "foe-deployer-o2mb-test";
061: public static final String MANAGER_SESSION_JNDI_NAME = "O2MBManagerEJB.O2MBManagerHome";
062:
063: // Static --------------------------------------------------------
064: /**
065: * Setup the test suite.
066: */
067: public static Test suite() throws Exception {
068: TestSuite suite = new TestSuite();
069: suite.addTest(new TestSuite(O2MBConversionTestCase.class));
070:
071: // Create an initializer for the test suite
072: TestSetup wrapper = new JBossTestSetup(suite) {
073: protected void setUp() throws Exception {
074: super .setUp();
075: }
076:
077: protected void tearDown() throws Exception {
078: super .tearDown();
079: }
080: };
081: return wrapper;
082: }
083:
084: // Constructors --------------------------------------------------
085: public O2MBConversionTestCase(String name) {
086: super (name);
087: }
088:
089: // Public --------------------------------------------------------
090: /**
091: * Test a simple conversion
092: **/
093: public void testSimpleConversion() throws Exception {
094: try {
095: log.debug("+++ testO2MBConversion");
096:
097: // First check if foe-deployer is deployed
098: boolean isInitiallyDeployed = getServer().isRegistered(
099: new ObjectName(FOE_DEPLOYER_NAME));
100: if (!isInitiallyDeployed)
101: deploy(FOE_DEPLOYER);
102:
103: boolean isDeployed = getServer().isRegistered(
104: new ObjectName(FOE_DEPLOYER_NAME));
105: assertTrue("Foe-Deployer is not deployed", isDeployed);
106:
107: // Count number of convertors (must be a list one)
108: int count = getServer()
109: .queryNames(
110: new ObjectName(
111: CONVERTOR_DEPLOYER_QUERY_NAME),
112: null).size();
113: assertTrue("No Convertor found on web server", count > 0);
114:
115: // Deploy the simple application
116: deploy(APPLICATION + ".wlar");
117:
118: // Because the Foe-Deployer copies the converted JAR back to the original place
119: // it has to be deployed from here again
120: deploy(APPLICATION + ".jar");
121:
122: // Access the Session Bean and invoke some methods on it
123: int i;
124: String companyName = "Romashka";
125: String[] employees = { "Ivanov", "Petrov", "Sidorov" };
126:
127: O2MBManager manager = getO2MBManager();
128:
129: log.debug("cleaning the database");
130: manager.removeCompanyIfExists(companyName);
131:
132: log.debug("creating company: " + companyName);
133: manager.createCompany(companyName);
134:
135: // create all employees except the last one
136: for (i = 0; i < employees.length - 1; ++i) {
137: log.debug("creating employee '" + employees[i]
138: + "' for company '" + companyName + "'");
139: manager.createEmployeeForCompany(employees[i],
140: companyName);
141: }
142:
143: // fetch created employees
144: Collection emps = manager
145: .getEmployeesForCompany(companyName);
146: log.debug("employees for company '" + companyName + "': "
147: + emps);
148:
149: log
150: .debug("checking whether employees employed by the company");
151: for (i = 0; i < employees.length - 1; ++i) {
152: assertTrue("Employee '" + employees[i]
153: + "' must have been employed", emps
154: .contains(employees[i]));
155: }
156:
157: log.debug("creating the last employee: "
158: + employees[employees.length - 1]);
159: manager.createEmployee(employees[employees.length - 1]);
160:
161: log.debug("employ '" + employees[employees.length - 1]
162: + "'");
163: manager
164: .employ(employees[employees.length - 1],
165: companyName);
166:
167: // verifying the last employee is employed
168: assertTrue(
169: "Employee '" + employees[employees.length - 1]
170: + "' must have been employed",
171: companyName
172: .equals(manager
173: .getCompanyForEmployee(employees[employees.length - 1])));
174:
175: log
176: .debug("checking whether all employees are employed by the company");
177: emps = manager.getEmployeesForCompany(companyName);
178: for (i = 0; i < employees.length; ++i) {
179: assertTrue("Employee '" + employees[i]
180: + "' must have been employed", emps
181: .contains(employees[i]));
182: }
183:
184: log.debug("removing company: " + companyName);
185: manager.removeCompany(companyName);
186:
187: // Undeploy converted application to clean up
188: undeploy(APPLICATION + ".jar");
189: // undeploy wlar (though it should work without it)
190: undeploy(APPLICATION + ".wlar");
191:
192: // Only undeploy if deployed here
193: if (!isInitiallyDeployed)
194: undeploy(FOE_DEPLOYER);
195: } catch (Exception e) {
196: e.printStackTrace();
197: throw e;
198: }
199: }
200:
201: // Private -------------------------------------------------------
202: private O2MBManager getO2MBManager() throws Exception {
203: log.debug("looking for O2MBManager");
204: Object ref = getInitialContext().lookup(
205: MANAGER_SESSION_JNDI_NAME);
206: O2MBManagerHome home = (O2MBManagerHome) PortableRemoteObject
207: .narrow(ref, O2MBManagerHome.class);
208: return home.create();
209: }
210: }
|