KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > entity > unit > EntityUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.entity.unit;
23
24 import java.util.Set JavaDoc;
25 import org.jboss.ejb3.test.entity.Address;
26 import org.jboss.ejb3.test.entity.Company;
27 import org.jboss.ejb3.test.entity.Customer;
28 import org.jboss.ejb3.test.entity.EntityTest;
29 import org.jboss.ejb3.test.entity.FieldAddress;
30 import org.jboss.ejb3.test.entity.FieldCompany;
31 import org.jboss.ejb3.test.entity.FieldCustomer;
32 import org.jboss.ejb3.test.entity.FieldFlight;
33 import org.jboss.ejb3.test.entity.FieldTicket;
34 import org.jboss.ejb3.test.entity.Flight;
35 import org.jboss.ejb3.test.entity.Ticket;
36 import org.jboss.test.JBossTestCase;
37 import junit.framework.Test;
38
39 /**
40  * Sample client for the jboss container.
41  *
42  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
43  * @version $Id: EntityUnitTestCase.java 58110 2006-11-04 08:34:21Z scott.stark@jboss.org $
44  */

45
46 public class EntityUnitTestCase
47 extends JBossTestCase
48 {
49    org.jboss.logging.Logger log = getLog();
50
51    static boolean deployed = false;
52    static int test = 0;
53
54    public EntityUnitTestCase(String JavaDoc name)
55    {
56
57       super(name);
58
59    }
60
61    public void testOneToMany() throws Exception JavaDoc
62    {
63       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
64       Customer c = test.oneToManyCreate();
65       assertNotNull(c);
66       assertNotNull(c.getTickets());
67       Set JavaDoc<Ticket> tickets = c.getTickets();
68       assertTrue(tickets.size() > 0);
69
70       // test find
71
c = test.findCustomerById(c.getId());
72       assertNotNull(c);
73       assertNotNull(c.getTickets());
74       tickets = c.getTickets();
75       assertTrue(tickets.size() > 0);
76
77       // test 1-1
78
Address address = c.getAddress();
79       assertTrue(address != null);
80       assertTrue(address.getCity().equals("Boston"));
81    }
82
83    public void testManyToOne() throws Exception JavaDoc
84    {
85       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
86       Flight f = test.manyToOneCreate();
87       f = test.findFlightById(f.getId());
88       assertTrue(f.getName().equals("AF0101"));
89       assertTrue(f.getCompany().getName().equals("Air France"));
90
91       Company c = test.findCompanyById(f.getCompany().getId());
92       assertTrue(c != null);
93       assertTrue(c.getFlights().size() == 1);
94    }
95
96    public void testManyToMany() throws Exception JavaDoc
97    {
98       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
99       test.manyToManyCreate();
100
101       Flight one = test.findFlightById(new Long JavaDoc(1));
102       assertTrue(one.getCompany().getName().equals("Air France"));
103
104       Flight two = test.findFlightById(new Long JavaDoc(2));
105       assertTrue(two.getCompany().getName().equals("USAir"));
106
107       System.out.println("Air France customers");
108       for (Customer c : one.getCustomers())
109       {
110          System.out.println(c.getName());
111
112       }
113       System.out.println("USAir customers");
114
115       for (Customer c : two.getCustomers())
116       {
117          System.out.println(c.getName());
118       }
119
120    }
121
122    public void testFieldOneToMany() throws Exception JavaDoc
123    {
124       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
125       FieldCustomer c = test.fieldOneToManyCreate();
126       assertNotNull(c);
127       assertNotNull(c.getTickets());
128       Set JavaDoc<FieldTicket> tickets = c.getTickets();
129       assertTrue(tickets.size() > 0);
130
131       // test find
132
c = test.fieldFindCustomerById(c.getId());
133       assertNotNull(c);
134       assertNotNull(c.getTickets());
135       tickets = c.getTickets();
136       assertTrue(tickets.size() > 0);
137
138       // test 1-1
139
FieldAddress address = c.getAddress();
140       assertTrue(address != null);
141       assertTrue(address.getCity().equals("Boston"));
142    }
143
144    public void testFieldManyToOne() throws Exception JavaDoc
145    {
146       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
147       FieldFlight f = test.fieldManyToOneCreate();
148       f = test.fieldFindFlightById(f.getId());
149       assertTrue(f.getName().equals("AF0101"));
150       assertTrue(f.getCompany().getName().equals("Air France"));
151
152       FieldCompany c = test.fieldFindCompanyById(f.getCompany().getId());
153       assertTrue(c != null);
154       assertTrue(c.getFlights().size() == 1);
155    }
156
157    public void testFieldManyToMany() throws Exception JavaDoc
158    {
159       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
160       test.fieldManyToManyCreate();
161
162       FieldFlight one = test.fieldFindFlightById(new Long JavaDoc(1));
163       assertTrue(one.getCompany().getName().equals("Air France"));
164
165       FieldFlight two = test.fieldFindFlightById(new Long JavaDoc(2));
166       assertTrue(two.getCompany().getName().equals("USAir"));
167
168       System.out.println("Air France customers");
169       for (FieldCustomer c : one.getCustomers())
170       {
171          System.out.println(c.getName());
172
173       }
174       System.out.println("USAir customers");
175
176       for (FieldCustomer c : two.getCustomers())
177       {
178          System.out.println(c.getName());
179       }
180
181    }
182
183    public void testNamedQueries() throws Exception JavaDoc
184    {
185       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
186       test.testNamedQueries();
187    }
188
189    public void testOutsideTx() throws Exception JavaDoc
190    {
191       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
192       test.testOutsideTransaction();
193
194    }
195    
196    public void testFlush() throws Exception JavaDoc
197    {
198       EntityTest test = (EntityTest) this.getInitialContext().lookup("EntityTestBean/remote");
199       Customer c = test.createCustomer("Emmanuel");
200       test.changeCustomer(c.getId(), "Bill");
201       Customer c2 = test.loadCustomer(c.getId());
202       assertEquals("Bill", c2.getName());
203    }
204
205    public static Test suite() throws Exception JavaDoc
206    {
207       return getDeploySetup(EntityUnitTestCase.class, "entity-test.jar");
208    }
209
210 }
211
Popular Tags