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.ejb3.test.entityoptimisticlocking.unit;
023:
024: import java.util.Set;
025: import org.jboss.ejb3.test.entity.Address;
026: import org.jboss.ejb3.test.entity.Company;
027: import org.jboss.ejb3.test.entity.Customer;
028: import org.jboss.ejb3.test.entity.EntityTest;
029: import org.jboss.ejb3.test.entity.FieldAddress;
030: import org.jboss.ejb3.test.entity.FieldCompany;
031: import org.jboss.ejb3.test.entity.FieldCustomer;
032: import org.jboss.ejb3.test.entity.FieldFlight;
033: import org.jboss.ejb3.test.entity.FieldTicket;
034: import org.jboss.ejb3.test.entity.Flight;
035: import org.jboss.ejb3.test.entity.Ticket;
036: import org.jboss.test.JBossTestCase;
037: import junit.framework.Test;
038:
039: /**
040: * Sample client for the jboss container.
041: *
042: * @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
043: * @version $Id: EntityUnitTestCase.java 57207 2006-09-26 12:06:13Z dimitris@jboss.org $
044: */
045:
046: public class EntityUnitTestCase extends JBossTestCase {
047: org.apache.log4j.Category log = getLog();
048:
049: static boolean deployed = false;
050: static int test = 0;
051:
052: public EntityUnitTestCase(String name) {
053:
054: super (name);
055:
056: }
057:
058: public void testOneToMany() throws Exception {
059: EntityTest test = (EntityTest) this .getInitialContext().lookup(
060: "EntityTestBean/remote");
061: Customer c = test.oneToManyCreate();
062: assertNotNull(c);
063: assertNotNull(c.getTickets());
064: Set<Ticket> tickets = c.getTickets();
065: assertTrue(tickets.size() > 0);
066:
067: // test find
068: c = test.findCustomerById(c.getId());
069: assertNotNull(c);
070: assertNotNull(c.getTickets());
071: tickets = c.getTickets();
072: assertTrue(tickets.size() > 0);
073:
074: // test 1-1
075: Address address = c.getAddress();
076: assertTrue(address != null);
077: assertTrue(address.getCity().equals("Boston"));
078: }
079:
080: public void testManyToOne() throws Exception {
081: EntityTest test = (EntityTest) this .getInitialContext().lookup(
082: "EntityTestBean/remote");
083: Flight f = test.manyToOneCreate();
084: f = test.findFlightById(f.getId());
085: assertTrue(f.getName().equals("AF0101"));
086: assertTrue(f.getCompany().getName().equals("Air France"));
087:
088: Company c = test.findCompanyById(f.getCompany().getId());
089: assertTrue(c != null);
090: assertTrue(c.getFlights().size() == 1);
091: }
092:
093: public void testManyToMany() throws Exception {
094: EntityTest test = (EntityTest) this .getInitialContext().lookup(
095: "EntityTestBean/remote");
096: test.manyToManyCreate();
097:
098: Flight one = test.findFlightById(new Long(1));
099: assertTrue(one.getCompany().getName().equals("Air France"));
100:
101: Flight two = test.findFlightById(new Long(2));
102: assertTrue(two.getCompany().getName().equals("USAir"));
103:
104: System.out.println("Air France customers");
105: for (Customer c : one.getCustomers()) {
106: System.out.println(c.getName());
107:
108: }
109: System.out.println("USAir customers");
110:
111: for (Customer c : two.getCustomers()) {
112: System.out.println(c.getName());
113: }
114:
115: }
116:
117: public void testFieldOneToMany() throws Exception {
118: EntityTest test = (EntityTest) this .getInitialContext().lookup(
119: "EntityTestBean/remote");
120: FieldCustomer c = test.fieldOneToManyCreate();
121: assertNotNull(c);
122: assertNotNull(c.getTickets());
123: Set<FieldTicket> tickets = c.getTickets();
124: assertTrue(tickets.size() > 0);
125:
126: // test find
127: c = test.fieldFindCustomerById(c.getId());
128: assertNotNull(c);
129: assertNotNull(c.getTickets());
130: tickets = c.getTickets();
131: assertTrue(tickets.size() > 0);
132:
133: // test 1-1
134: FieldAddress address = c.getAddress();
135: assertTrue(address != null);
136: assertTrue(address.getCity().equals("Boston"));
137: }
138:
139: public void testFieldManyToOne() throws Exception {
140: EntityTest test = (EntityTest) this .getInitialContext().lookup(
141: "EntityTestBean/remote");
142: FieldFlight f = test.fieldManyToOneCreate();
143: f = test.fieldFindFlightById(f.getId());
144: assertTrue(f.getName().equals("AF0101"));
145: assertTrue(f.getCompany().getName().equals("Air France"));
146:
147: FieldCompany c = test.fieldFindCompanyById(f.getCompany()
148: .getId());
149: assertTrue(c != null);
150: assertTrue(c.getFlights().size() == 1);
151: }
152:
153: public void testFieldManyToMany() throws Exception {
154: EntityTest test = (EntityTest) this .getInitialContext().lookup(
155: "EntityTestBean/remote");
156: test.fieldManyToManyCreate();
157:
158: FieldFlight one = test.fieldFindFlightById(new Long(1));
159: assertTrue(one.getCompany().getName().equals("Air France"));
160:
161: FieldFlight two = test.fieldFindFlightById(new Long(2));
162: assertTrue(two.getCompany().getName().equals("USAir"));
163:
164: System.out.println("Air France customers");
165: for (FieldCustomer c : one.getCustomers()) {
166: System.out.println(c.getName());
167:
168: }
169: System.out.println("USAir customers");
170:
171: for (FieldCustomer c : two.getCustomers()) {
172: System.out.println(c.getName());
173: }
174:
175: }
176:
177: public void testNamedQueries() throws Exception {
178: EntityTest test = (EntityTest) this .getInitialContext().lookup(
179: "EntityTestBean/remote");
180: test.testNamedQueries();
181: }
182:
183: public void testOutsideTx() throws Exception {
184: EntityTest test = (EntityTest) this .getInitialContext().lookup(
185: "EntityTestBean/remote");
186: test.testOutsideTransaction();
187:
188: }
189:
190: public void testFlush() throws Exception {
191: EntityTest test = (EntityTest) this .getInitialContext().lookup(
192: "EntityTestBean/remote");
193: Customer c = test.createCustomer("Emmanuel");
194: test.changeCustomer(c.getId(), "Bill");
195: Customer c2 = test.loadCustomer(c.getId());
196: assertEquals("Bill", c2.getName());
197: }
198:
199: public static Test suite() throws Exception {
200: return getDeploySetup(EntityUnitTestCase.class,
201: "entityoptimisticlocking-test.jar");
202: }
203:
204: }
|