001: /**
002: *
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */package org.apache.openejb.test.entity.cmp;
018:
019: import javax.rmi.PortableRemoteObject;
020: import java.util.Collection;
021: import java.util.Collections;
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: /**
026: * [2] Should be run as the second test suite of the ComplexCmpTestClients
027: *
028: * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
029: * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
030: */
031: public class ComplexHomeIntfcTests extends ComplexCmpTestClient {
032:
033: public ComplexHomeIntfcTests() {
034: super ("ComplexPk.");
035: }
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: Object obj = initialContext
040: .lookup("client/tests/entity/cmp/ComplexCmpHome");
041: ejbHome = (ComplexCmpHome) PortableRemoteObject.narrow(obj,
042: ComplexCmpHome.class);
043: }
044:
045: //===============================
046: // Test home interface methods
047: //
048: public void test01_create() {
049: try {
050: ejbObject = ejbHome.createObject("First Bean");
051: assertNotNull("The EJBObject is null", ejbObject);
052: } catch (Exception e) {
053: fail("Received Exception " + e.getClass() + " : "
054: + e.getMessage());
055: }
056: }
057:
058: public void test02_findByPrimaryKey() {
059: try {
060: ejbPrimaryKey = ejbObject.getPrimaryKey();
061: assertTrue(
062: "Expected (ejbPrimaryKey instanceof ComplexCmpBeanPk) but was instanceof "
063: + ejbPrimaryKey.getClass().getName(),
064: ejbPrimaryKey instanceof ComplexCmpBeanPk);
065: ejbObject = ejbHome
066: .findByPrimaryKey((ComplexCmpBeanPk) ejbPrimaryKey);
067: assertNotNull("The EJBObject is null", ejbObject);
068: } catch (Exception e) {
069: e.printStackTrace();
070: fail("Received Exception " + e.getClass() + " : "
071: + e.getMessage());
072: }
073: }
074:
075: public void test03_findByLastName() {
076: Set<ComplexCmpBeanPk> keys = new HashSet<ComplexCmpBeanPk>();
077: try {
078: ejbObject = ejbHome.createObject("David Blevins");
079: ejbPrimaryKey = ejbObject.getPrimaryKey();
080: assertTrue(
081: "Expected (ejbPrimaryKey instanceof ComplexCmpBeanPk) but was instanceof "
082: + ejbPrimaryKey.getClass().getName(),
083: ejbPrimaryKey instanceof ComplexCmpBeanPk);
084: keys.add((ComplexCmpBeanPk) ejbObject.getPrimaryKey());
085:
086: ejbObject = ejbHome.createObject("Dennis Blevins");
087: ejbPrimaryKey = ejbObject.getPrimaryKey();
088: assertTrue(
089: "Expected (ejbPrimaryKey instanceof ComplexCmpBeanPk) but was instanceof "
090: + ejbPrimaryKey.getClass().getName(),
091: ejbPrimaryKey instanceof ComplexCmpBeanPk);
092: keys.add((ComplexCmpBeanPk) ejbObject.getPrimaryKey());
093:
094: ejbObject = ejbHome.createObject("Claude Blevins");
095: ejbPrimaryKey = ejbObject.getPrimaryKey();
096: assertTrue(
097: "Expected (ejbPrimaryKey instanceof ComplexCmpBeanPk) but was instanceof "
098: + ejbPrimaryKey.getClass().getName(),
099: ejbPrimaryKey instanceof ComplexCmpBeanPk);
100: keys.add((ComplexCmpBeanPk) ejbObject.getPrimaryKey());
101: } catch (Exception e) {
102: fail("Received exception while preparing the test: "
103: + e.getClass() + " : " + e.getMessage());
104: }
105:
106: try {
107: Collection objects = ejbHome.findByLastName("Blevins");
108: Set<ComplexCmpBeanPk> foundKeys = new HashSet<ComplexCmpBeanPk>();
109: assertNotNull("The Collection is null", objects);
110: assertEquals("The Collection is not the right size.", keys
111: .size(), objects.size());
112: for (Object object : objects) {
113: ejbObject = (ComplexCmpObject) PortableRemoteObject
114: .narrow(object, ComplexCmpObject.class);
115:
116: // This could be problematic, it assumes the order of the collection.
117: ComplexCmpBeanPk foundKey = (ComplexCmpBeanPk) ejbObject
118: .getPrimaryKey();
119: assertTrue("Extra ejb found "
120: + ejbObject.getPrimaryKey(), keys
121: .contains(foundKey));
122: foundKeys.add(foundKey);
123: }
124:
125: keys.removeAll(foundKeys);
126: assertEquals("Some keys were not found",
127: Collections.EMPTY_SET, keys);
128: } catch (Exception e) {
129: e.printStackTrace();
130: fail("Received Exception " + e.getClass() + " : "
131: + e.getMessage());
132: }
133: }
134:
135: public void test04_homeMethod() {
136: try {
137: int expected = 8;
138: int actual = ejbHome.sum(5, 3);
139: assertEquals("home method returned wrong result", expected,
140: actual);
141: } catch (Exception e) {
142: fail("Received Exception " + e.getClass() + " : "
143: + e.getMessage());
144: }
145: }
146: //
147: // Test home interface methods
148: //===============================
149:
150: }
|