KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > joininheritance > EntityTestBean


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.joininheritance;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.ejb.Remote JavaDoc;
27 import javax.ejb.Stateless JavaDoc;
28 import javax.persistence.EntityManager;
29 import javax.persistence.PersistenceContext;
30 import javax.persistence.PersistenceContext;
31
32 /**
33  * Comment
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 37459 $
37  */

38 @Stateless JavaDoc
39 @Remote JavaDoc(EntityTest.class)
40 public class EntityTestBean implements EntityTest
41 {
42    private @PersistenceContext EntityManager manager;
43
44    private void assertTrue(boolean value)
45    {
46       if (!value) throw new RuntimeException JavaDoc("assert failed");
47    }
48
49    private void assertFalse(boolean value)
50    {
51       if (value) throw new RuntimeException JavaDoc("assert failed");
52    }
53
54    private void assertEquals(int x, int y)
55    {
56       if (x != y) throw new RuntimeException JavaDoc("assert failed");
57    }
58
59    private void assertEquals(String JavaDoc x, String JavaDoc y)
60    {
61       if (x == y) return;
62       if (x == null && y != null) throw new RuntimeException JavaDoc("assert failed");
63       if (x != null && y == null) throw new RuntimeException JavaDoc("assert failed");
64       if (!x.equals(y)) throw new RuntimeException JavaDoc("assert failed");
65    }
66
67    public long[] createBeans() throws Exception JavaDoc
68    {
69       Employee mark = new Employee();
70       mark.setName("Mark");
71       mark.setTitle("internal sales");
72       mark.setSex('M');
73       mark.setAddress("buckhead");
74       mark.setZip("30305");
75       mark.setCountry("USA");
76
77       Customer joe = new Customer();
78       joe.setName("Joe");
79       joe.setAddress("San Francisco");
80       joe.setZip("XXXXX");
81       joe.setCountry("USA");
82       joe.setComments("Very demanding");
83       joe.setSalesperson(mark);
84
85       Person yomomma = new Person();
86       yomomma.setName("mum");
87       yomomma.setSex('F');
88
89       manager.persist(mark);
90       manager.persist(joe);
91       manager.persist(yomomma);
92       long[] ids = {mark.getId(), joe.getId(), yomomma.getId()};
93       return ids;
94    }
95
96    public void test1() throws Exception JavaDoc
97    {
98       assertEquals(manager.createQuery("from Person").getResultList().size(), 3);
99       assertEquals(manager.createQuery("from Person p where p.class = Customer").getResultList().size(), 1);
100    }
101
102    public void test2() throws Exception JavaDoc
103    {
104       List JavaDoc customers = manager.createQuery("from Customer c left join fetch c.salesperson").getResultList();
105       for (Iterator JavaDoc iter = customers.iterator(); iter.hasNext();)
106       {
107          Customer c = (Customer) iter.next();
108          assertEquals(c.getSalesperson().getName(), "Mark");
109       }
110       assertEquals(customers.size(), 1);
111    }
112
113    public void test3() throws Exception JavaDoc
114    {
115       List JavaDoc customers = manager.createQuery("from Customer").getResultList();
116       for (Iterator JavaDoc iter = customers.iterator(); iter.hasNext();)
117       {
118          Customer c = (Customer) iter.next();
119          assertEquals(c.getSalesperson().getName(), "Mark");
120       }
121       assertEquals(customers.size(), 1);
122    }
123
124    public void test4(long[] ids) throws Exception JavaDoc
125    {
126       Employee mark = manager.find(Employee.class, new Long JavaDoc(ids[0]));
127       Customer joe = (Customer) manager.find(Customer.class, new Long JavaDoc(ids[1]));
128       Person yomomma = manager.find(Person.class, new Long JavaDoc(ids[2]));
129
130       mark.setZip("30306");
131       assertEquals(manager.createQuery("from Person p where p.zip = '30306'").getResultList().size(), 1);
132       manager.remove(mark);
133       manager.remove(joe);
134       manager.remove(yomomma);
135       assertTrue(manager.createQuery("from Person").getResultList().isEmpty());
136    }
137 }
138
Popular Tags