001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.test.entity.cmr;
017:
018: import org.apache.openejb.test.entity.cmr.onetoone.PersonLocal;
019: import org.apache.openejb.test.entity.cmr.onetoone.PersonLocalHome;
020: import org.apache.openejb.test.entity.cmr.onetoone.LicenseLocal;
021: import org.apache.openejb.test.entity.cmr.onetoone.LicenseLocalHome;
022: import org.apache.openejb.test.entity.cmr.onetoone.PersonPk;
023: import org.apache.openejb.test.entity.cmr.onetoone.LicensePk;
024:
025: import javax.ejb.CreateException;
026: import javax.ejb.FinderException;
027: import java.sql.Connection;
028: import java.sql.ResultSet;
029: import java.sql.Statement;
030: import java.sql.SQLException;
031:
032: /**
033: *
034: * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
035: */
036: public class OneToOneComplexPkTests extends AbstractCMRTest {
037: private PersonLocalHome personLocalHome;
038: private LicenseLocalHome licenseLocalHome;
039:
040: public OneToOneComplexPkTests() {
041: super ("OneToOneCompound.");
042: }
043:
044: protected void setUp() throws Exception {
045: super .setUp();
046:
047: personLocalHome = (PersonLocalHome) initialContext
048: .lookup("client/tests/entity/cmr/oneToOne/ComplexPersonLocal");
049: licenseLocalHome = (LicenseLocalHome) initialContext
050: .lookup("client/tests/entity/cmr/oneToOne/ComplexLicenseLocal");
051: }
052:
053: public void test00_AGetBExistingAB() throws Exception {
054: resetDB();
055: beginTransaction();
056: try {
057: PersonLocal person = findPerson(1);
058: assertNotNull("person is null", person);
059: LicenseLocal license = person.getLicense();
060: assertNotNull("license is null", license);
061: assertEquals(new Integer(11), license.getId());
062: assertEquals("value11", license.getNumber());
063: } finally {
064: completeTransaction();
065: }
066: }
067:
068: public void test01_BGetAExistingAB() throws Exception {
069: resetDB();
070: beginTransaction();
071: try {
072: LicenseLocal license = findLicense(11);
073: PersonLocal person = license.getPerson();
074: assertNotNull(person);
075: assertEquals(new Integer(1), person.getId());
076: assertEquals("value1", person.getName());
077: } finally {
078: completeTransaction();
079: }
080: }
081:
082: public void test02_ASetBDropExisting() throws Exception {
083: resetDB();
084: beginTransaction();
085: try {
086: PersonLocal person = findPerson(1);
087: person.setLicense(null);
088: } finally {
089: completeTransaction();
090: }
091:
092: assertUnlinked(1);
093: }
094:
095: public void test03_BSetADropExisting() throws Exception {
096: resetDB();
097: beginTransaction();
098: try {
099: LicenseLocal license = findLicense(11);
100: license.setPerson(null);
101: } finally {
102: completeTransaction();
103: }
104:
105: assertUnlinked(1);
106: }
107:
108: public void test04_ASetBNewAB() throws Exception {
109: resetDB();
110: beginTransaction();
111: try {
112: PersonLocal person = findPerson(2);
113: LicenseLocal license = createLicense(22);
114: person.setLicense(license);
115: } finally {
116: completeTransaction();
117: }
118:
119: assertLinked(2, 22);
120: }
121:
122: public void test05_BSetANewAB() throws Exception {
123: resetDB();
124: beginTransaction();
125: try {
126: PersonLocal person = findPerson(2);
127: LicenseLocal license = createLicense(22);
128: license.setPerson(person);
129: } finally {
130: completeTransaction();
131: }
132:
133: assertLinked(2, 22);
134: }
135:
136: public void test06_ASetBExistingBNewA() throws Exception {
137: resetDB();
138: beginTransaction();
139: try {
140: PersonLocal person = findPerson(2);
141: LicenseLocal license = findLicense(11);
142: person.setLicense(license);
143: } finally {
144: completeTransaction();
145: }
146:
147: assertLinked(2, 11);
148: }
149:
150: public void test07_BSetAExistingBNewA() throws Exception {
151: resetDB();
152: beginTransaction();
153: try {
154: PersonLocal person = createPerson(3);
155: LicenseLocal license = findLicense(11);
156: license.setPerson(person);
157: } finally {
158: completeTransaction();
159: }
160: assertLinked(3, 11);
161: }
162:
163: public void test09_BSetAExistingANewB() throws Exception {
164: resetDB();
165: beginTransaction();
166: try {
167: PersonLocal person = findPerson(1);
168: LicenseLocal license = createLicense(22);
169: license.setPerson(person);
170: } finally {
171: completeTransaction();
172: }
173: assertLinked(1, 22);
174: }
175:
176: public void test10_RemoveRelationships() throws Exception {
177: resetDB();
178: beginTransaction();
179: try {
180: PersonLocal person = findPerson(1);
181: person.remove();
182: } finally {
183: completeTransaction();
184: }
185:
186: Connection c = ds.getConnection();
187: Statement s = c.createStatement();
188: ResultSet rs = s
189: .executeQuery("SELECT COUNT(*) FROM ComplexLicense");
190: assertTrue(rs.next());
191: assertEquals(1, rs.getInt(1));
192: close(rs);
193: rs = s
194: .executeQuery("SELECT COUNT(*) FROM ComplexLicense WHERE person_id = 1");
195: assertTrue(rs.next());
196: assertEquals(0, rs.getInt(1));
197: close(rs);
198: close(s);
199: close(c);
200: }
201:
202: public void test11_CascadeDelete() throws Exception {
203: resetDB();
204: beginTransaction();
205: try {
206: LicenseLocal license = findLicense(11);
207: license.remove();
208: } finally {
209: completeTransaction();
210: }
211:
212: Connection c = ds.getConnection();
213: Statement s = c.createStatement();
214: ResultSet rs = s
215: .executeQuery("SELECT COUNT(*) FROM ComplexPerson WHERE id = 1");
216: assertTrue(rs.next());
217: assertEquals(0, rs.getInt(1));
218: close(rs);
219: close(s);
220: close(c);
221: }
222:
223: // todo enable these when field to fk is implemented
224: public void Xtest12_CMPMappedToForeignKeyColumn() throws Exception {
225: resetDB();
226: beginTransaction();
227: try {
228: LicenseLocal license = findLicense(11);
229:
230: Integer field3 = license.getPoints();
231: assertEquals(license.getPerson().getPrimaryKey(), field3);
232: } finally {
233: completeTransaction();
234: }
235: }
236:
237: // todo enable these when field to fk is implemented
238: public void Xtest13_SetCMPMappedToForeignKeyColumn()
239: throws Exception {
240: resetDB();
241: beginTransaction();
242: try {
243: LicenseLocal license = findLicense(11);
244:
245: license.setPoints(new Integer(2));
246:
247: PersonLocal person = license.getPerson();
248: assertEquals(new Integer(2), person.getId());
249: assertEquals("value2", person.getName());
250: } finally {
251: completeTransaction();
252: }
253: }
254:
255: private PersonLocal createPerson(int personId)
256: throws CreateException {
257: PersonLocal person = personLocalHome.create(new PersonPk(
258: personId, "value" + personId));
259: return person;
260: }
261:
262: private PersonLocal findPerson(int personId) throws FinderException {
263: return personLocalHome.findByPrimaryKey(new PersonPk(personId,
264: "value" + personId));
265: }
266:
267: private LicenseLocal createLicense(int licenseId)
268: throws CreateException {
269: LicenseLocal license = licenseLocalHome.create(new LicensePk(
270: licenseId, "value" + licenseId));
271: return license;
272: }
273:
274: private LicenseLocal findLicense(int licenseId)
275: throws FinderException {
276: return licenseLocalHome.findByPrimaryKey(new LicensePk(
277: licenseId, "value" + licenseId));
278: }
279:
280: private void assertLinked(int personId, int licenseId)
281: throws Exception {
282: Connection c = ds.getConnection();
283: Statement s = c.createStatement();
284: ResultSet rs = s
285: .executeQuery("SELECT name FROM ComplexPerson WHERE id = "
286: + personId);
287: assertTrue(rs.next());
288: assertEquals("value" + personId, rs.getString("name"));
289: close(rs);
290:
291: rs = s
292: .executeQuery("SELECT id, number FROM ComplexLicense WHERE person_id = "
293: + personId);
294: assertTrue(rs.next());
295: assertEquals(licenseId, rs.getInt("id"));
296: assertEquals("value" + licenseId, rs.getString("number"));
297: close(rs);
298: close(s);
299: close(c);
300: }
301:
302: private void assertUnlinked(int personId) throws Exception {
303: Connection c = ds.getConnection();
304: Statement s = c.createStatement();
305: ResultSet rs = s
306: .executeQuery("SELECT COUNT(*) FROM ComplexLicense WHERE person_id = "
307: + personId);
308: assertTrue(rs.next());
309: assertEquals(0, rs.getInt(1));
310: close(rs);
311: close(s);
312: close(c);
313: }
314:
315: private void resetDB() throws Exception {
316: Connection connection = ds.getConnection();
317: Statement statement = null;
318: try {
319: statement = connection.createStatement();
320:
321: try {
322: statement.execute("DELETE FROM ComplexPerson");
323: } catch (SQLException ignored) {
324: }
325: try {
326: statement.execute("DELETE FROM ComplexLicense");
327: } catch (SQLException ignored) {
328: }
329: } finally {
330: close(statement);
331: close(connection);
332: }
333:
334: PersonLocal person1 = createPerson(1);
335: createPerson(2);
336:
337: LicenseLocal license = createLicense(11);
338: license.setPerson(person1);
339: }
340:
341: protected void dump() throws Exception {
342: dumpTable(ds, "ComplexPerson");
343: dumpTable(ds, "ComplexLicense");
344: }
345: }
|