KickJava   Java API By Example, From Geeks To Geeks.

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

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