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:
023: import javax.ejb.CreateException;
024: import javax.ejb.FinderException;
025: import java.sql.Connection;
026: import java.sql.ResultSet;
027: import java.sql.Statement;
028: import java.sql.SQLException;
029:
030: /**
031: *
032: * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
033: */
034: public class OneToOneTests extends AbstractCMRTest {
035: private PersonLocalHome personLocalHome;
036: private LicenseLocalHome licenseLocalHome;
037:
038: public OneToOneTests() {
039: super ("OneToOne.");
040: }
041:
042: protected void setUp() throws Exception {
043: super .setUp();
044:
045: personLocalHome = (PersonLocalHome) initialContext
046: .lookup("client/tests/entity/cmr/oneToOne/PersonLocal");
047: licenseLocalHome = (LicenseLocalHome) initialContext
048: .lookup("client/tests/entity/cmr/oneToOne/LicenseLocal");
049: }
050:
051: public void test00_AGetBExistingAB() throws Exception {
052: resetDB();
053: beginTransaction();
054: try {
055: PersonLocal person = findPerson(1);
056: assertNotNull("person is null", person);
057: LicenseLocal license = person.getLicense();
058: assertNotNull("license is null", license);
059: assertEquals(new Integer(11), license.getId());
060: assertEquals("value11", license.getNumber());
061: } finally {
062: completeTransaction();
063: }
064: }
065:
066: public void test01_BGetAExistingAB() throws Exception {
067: resetDB();
068: beginTransaction();
069: try {
070: LicenseLocal license = findLicense(11);
071: PersonLocal person = license.getPerson();
072: assertNotNull(person);
073: assertEquals(new Integer(1), person.getId());
074: assertEquals("value1", person.getName());
075: } finally {
076: completeTransaction();
077: }
078: }
079:
080: public void test02_ASetBDropExisting() throws Exception {
081: resetDB();
082: beginTransaction();
083: try {
084: PersonLocal person = findPerson(1);
085: person.setLicense(null);
086: } finally {
087: completeTransaction();
088: }
089:
090: assertUnlinked(1);
091: }
092:
093: public void test03_BSetADropExisting() throws Exception {
094: resetDB();
095: beginTransaction();
096: try {
097: LicenseLocal license = findLicense(11);
098: license.setPerson(null);
099: } finally {
100: completeTransaction();
101: }
102:
103: assertUnlinked(1);
104: }
105:
106: public void test04_ASetBNewAB() throws Exception {
107: resetDB();
108: beginTransaction();
109: try {
110: PersonLocal person = findPerson(2);
111: LicenseLocal license = createLicense(22);
112: person.setLicense(license);
113: } finally {
114: completeTransaction();
115: }
116:
117: assertLinked(2, 22);
118: }
119:
120: public void test05_BSetANewAB() throws Exception {
121: resetDB();
122: beginTransaction();
123: try {
124: PersonLocal person = findPerson(2);
125: LicenseLocal license = createLicense(22);
126: license.setPerson(person);
127: } finally {
128: completeTransaction();
129: }
130:
131: assertLinked(2, 22);
132: }
133:
134: public void test06_ASetBExistingBNewA() throws Exception {
135: resetDB();
136: beginTransaction();
137: try {
138: PersonLocal person = findPerson(2);
139: LicenseLocal license = findLicense(11);
140: person.setLicense(license);
141: } finally {
142: completeTransaction();
143: }
144:
145: assertLinked(2, 11);
146: }
147:
148: public void test07_BSetAExistingBNewA() throws Exception {
149: resetDB();
150: beginTransaction();
151: try {
152: PersonLocal person = createPerson(3);
153: LicenseLocal license = findLicense(11);
154: license.setPerson(person);
155: } finally {
156: completeTransaction();
157: }
158: assertLinked(3, 11);
159: }
160:
161: public void test09_BSetAExistingANewB() throws Exception {
162: resetDB();
163: beginTransaction();
164: try {
165: PersonLocal person = findPerson(1);
166: LicenseLocal license = createLicense(22);
167: license.setPerson(person);
168: } finally {
169: completeTransaction();
170: }
171: assertLinked(1, 22);
172: }
173:
174: public void test10_RemoveRelationships() throws Exception {
175: resetDB();
176: beginTransaction();
177: try {
178: PersonLocal person = findPerson(1);
179: person.remove();
180: } finally {
181: completeTransaction();
182: }
183:
184: Connection c = ds.getConnection();
185: Statement s = c.createStatement();
186: ResultSet rs = s.executeQuery("SELECT COUNT(*) FROM License");
187: assertTrue(rs.next());
188: assertEquals(1, rs.getInt(1));
189: close(rs);
190: rs = s
191: .executeQuery("SELECT COUNT(*) FROM License WHERE person_id = 1");
192: assertTrue(rs.next());
193: assertEquals(0, rs.getInt(1));
194: close(rs);
195: close(s);
196: close(c);
197: }
198:
199: public void test11_CascadeDelete() throws Exception {
200: resetDB();
201: beginTransaction();
202: try {
203: LicenseLocal license = findLicense(11);
204: license.remove();
205: } finally {
206: completeTransaction();
207: }
208:
209: Connection c = ds.getConnection();
210: Statement s = c.createStatement();
211: ResultSet rs = s
212: .executeQuery("SELECT COUNT(*) FROM Person WHERE id = 1");
213: assertTrue(rs.next());
214: assertEquals(0, rs.getInt(1));
215: close(rs);
216: close(s);
217: close(c);
218: }
219:
220: // todo enable these when field to fk is implemented
221: public void Xtest12_CMPMappedToForeignKeyColumn() throws Exception {
222: resetDB();
223: beginTransaction();
224: try {
225: LicenseLocal license = findLicense(11);
226:
227: Integer field3 = license.getPoints();
228: assertEquals(license.getPerson().getPrimaryKey(), field3);
229: } finally {
230: completeTransaction();
231: }
232: }
233:
234: // todo enable these when field to fk is implemented
235: public void Xtest13_SetCMPMappedToForeignKeyColumn()
236: throws Exception {
237: resetDB();
238: beginTransaction();
239: try {
240: LicenseLocal license = findLicense(11);
241:
242: license.setPoints(new Integer(2));
243:
244: PersonLocal person = license.getPerson();
245: assertEquals(new Integer(2), person.getId());
246: assertEquals("value2", person.getName());
247: } finally {
248: completeTransaction();
249: }
250: }
251:
252: private PersonLocal createPerson(int personId)
253: throws CreateException {
254: PersonLocal person = personLocalHome.create(personId);
255: person.setName("value" + personId);
256: return person;
257: }
258:
259: private PersonLocal findPerson(int personId) throws FinderException {
260: return personLocalHome.findByPrimaryKey(personId);
261: }
262:
263: private LicenseLocal createLicense(int licenseId)
264: throws CreateException {
265: LicenseLocal license = licenseLocalHome.create(licenseId);
266: license.setNumber("value" + licenseId);
267: return license;
268: }
269:
270: private LicenseLocal findLicense(int licenseId)
271: throws FinderException {
272: return licenseLocalHome.findByPrimaryKey(licenseId);
273: }
274:
275: private void assertLinked(int personId, int licenseId)
276: throws Exception {
277: Connection c = ds.getConnection();
278: Statement s = c.createStatement();
279: ResultSet rs = s
280: .executeQuery("SELECT name FROM Person WHERE id = "
281: + personId);
282: assertTrue(rs.next());
283: assertEquals("value" + personId, rs.getString("name"));
284: close(rs);
285:
286: rs = s
287: .executeQuery("SELECT id, number FROM License WHERE person_id = "
288: + personId);
289: assertTrue(rs.next());
290: assertEquals(licenseId, rs.getInt("id"));
291: assertEquals("value" + licenseId, rs.getString("number"));
292: close(rs);
293: close(s);
294: close(c);
295: }
296:
297: private void assertUnlinked(int personId) throws Exception {
298: Connection c = ds.getConnection();
299: Statement s = c.createStatement();
300: ResultSet rs = s
301: .executeQuery("SELECT COUNT(*) FROM License WHERE person_id = "
302: + personId);
303: assertTrue(rs.next());
304: assertEquals(0, rs.getInt(1));
305: close(rs);
306: close(s);
307: close(c);
308: }
309:
310: private void resetDB() throws Exception {
311: Connection connection = ds.getConnection();
312: Statement statement = null;
313: try {
314: statement = connection.createStatement();
315:
316: try {
317: statement.execute("DELETE FROM Person");
318: } catch (SQLException ignored) {
319: }
320: try {
321: statement.execute("DELETE FROM License");
322: } catch (SQLException ignored) {
323: }
324: } finally {
325: close(statement);
326: close(connection);
327: }
328:
329: PersonLocal person1 = createPerson(1);
330: createPerson(2);
331:
332: LicenseLocal license = createLicense(11);
333: license.setPerson(person1);
334: }
335:
336: protected void dump() throws Exception {
337: dumpTable(ds, "Person");
338: dumpTable(ds, "License");
339: }
340: }
|