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.singletableinheritance;
023:
024: import java.util.Iterator;
025: import java.util.List;
026: import javax.ejb.Remote;
027: import javax.ejb.Stateless;
028: import javax.persistence.EntityManager;
029: import javax.persistence.PersistenceContext;
030:
031: /**
032: * Comment
033: *
034: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
035: * @version $Revision: 60233 $
036: */
037: @Stateless
038: @Remote(EntityTest.class)
039: public class EntityTestBean implements EntityTest {
040: private @PersistenceContext
041: EntityManager manager;
042:
043: private void assertTrue(boolean value) {
044: if (!value)
045: throw new RuntimeException("assert failed");
046: }
047:
048: private void assertFalse(boolean value) {
049: if (value)
050: throw new RuntimeException("assert failed");
051: }
052:
053: private void assertEquals(int x, int y) {
054: if (x != y)
055: throw new RuntimeException("assert failed");
056: }
057:
058: private void assertEquals(String x, String y) {
059: if (x == y)
060: return;
061: if (x == null && y != null)
062: throw new RuntimeException("assert failed");
063: if (x != null && y == null)
064: throw new RuntimeException("assert failed");
065: if (!x.equals(y))
066: throw new RuntimeException("assert failed");
067: }
068:
069: public long[] createBeans() throws Exception {
070: Employee mark = new Employee();
071: mark.setName("Mark");
072: mark.setTitle("internal sales");
073: mark.setSex('M');
074: mark.setAddress("buckhead");
075: mark.setZip("30305");
076: mark.setCountry("USA");
077:
078: Customer joe = new Customer();
079: joe.setName("Joe");
080: joe.setAddress("San Francisco");
081: joe.setZip("XXXXX");
082: joe.setCountry("USA");
083: joe.setComments("Very demanding");
084: joe.setSalesperson(mark);
085:
086: Person yomomma = new Person();
087: yomomma.setName("mum");
088: yomomma.setSex('F');
089:
090: manager.persist(mark);
091: manager.persist(joe);
092: manager.persist(yomomma);
093: long[] ids = { mark.getId(), joe.getId(), yomomma.getId() };
094: return ids;
095: }
096:
097: public void test1() throws Exception {
098: assertEquals(manager.createQuery("from java.io.Serializable")
099: .getResultList().size(), 0);
100:
101: assertEquals(manager.createQuery("from Person").getResultList()
102: .size(), 3);
103: assertEquals(manager.createQuery(
104: "from Person p where p.class = Customer")
105: .getResultList().size(), 1);
106: }
107:
108: public void test2() throws Exception {
109: List customers = manager.createQuery(
110: "from Customer c left join fetch c.salesperson")
111: .getResultList();
112: for (Iterator iter = customers.iterator(); iter.hasNext();) {
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 {
120: List customers = manager.createQuery("from Customer")
121: .getResultList();
122: for (Iterator iter = customers.iterator(); iter.hasNext();) {
123: Customer c = (Customer) iter.next();
124: assertEquals(c.getSalesperson().getName(), "Mark");
125: }
126: assertEquals(customers.size(), 1);
127: }
128:
129: public void test4(long[] ids) throws Exception {
130: Employee mark = manager.find(Employee.class, new Long(ids[0]));
131: Customer joe = (Customer) manager.find(Customer.class,
132: new Long(ids[1]));
133: Person yomomma = manager.find(Person.class, new Long(ids[2]));
134:
135: mark.setZip("30306");
136: assertEquals(manager.createQuery(
137: "from Person p where p.zip = '30306'").getResultList()
138: .size(), 1);
139: manager.remove(mark);
140: manager.remove(joe);
141: manager.remove(yomomma);
142: assertTrue(manager.createQuery("from Person").getResultList()
143: .isEmpty());
144: }
145: }
|