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