KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > tableperinheritance > 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.tableperinheritance;
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 import javax.persistence.PersistenceContext;
32
33 /**
34  * Comment
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  * @version $Revision: 37459 $
38  */

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