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.test.bmp.test;
023:
024: import java.util.Iterator;
025:
026: import javax.naming.InitialContext;
027:
028: import junit.framework.Test;
029:
030: import org.jboss.test.JBossTestCase;
031: import org.jboss.test.bmp.interfaces.BMPHelperSession;
032: import org.jboss.test.bmp.interfaces.BMPHelperSessionHome;
033: import org.jboss.test.bmp.interfaces.SimpleBMP;
034: import org.jboss.test.bmp.interfaces.SimpleBMPHome;
035:
036: public class BmpUnitTestCase extends JBossTestCase {
037: public BmpUnitTestCase(String name) {
038: super (name);
039: }
040:
041: public void testBMP() throws Exception {
042: BMPHelperSessionHome sessionHome = (BMPHelperSessionHome) new InitialContext()
043: .lookup("bmp.BMPHelperSession");
044: BMPHelperSession session = sessionHome.create();
045:
046: getLog().debug("looking up table:");
047: boolean exists = session.existsSimpleBeanTable();
048: if (exists) {
049: getLog().debug("table exists.");
050: getLog().debug("delete it...");
051: session.dropSimpleBeanTable();
052: getLog().debug("done.");
053: }
054:
055: getLog().debug("table does not exist.");
056: getLog().debug("create it...");
057: session.createSimpleBeanTable();
058: try {
059: getLog().debug("done.");
060:
061: getLog().debug("start playing with bmp beans.");
062: SimpleBMPHome home = (SimpleBMPHome) new InitialContext()
063: .lookup("bmp.SimpleBMP");
064: getLog().debug("create bean1: 1, Daniel");
065: SimpleBMP b1 = home.create(1, "Daniel");
066: getLog().debug("getName (): " + b1.getName());
067:
068: getLog().debug("create bean2: 2, Robert");
069: b1 = home.createMETHOD(2, "Robert");
070: getLog().debug("getName (): " + b1.getName());
071:
072: try {
073: getLog()
074: .debug(
075: "trying to create one with same primkey: 1, Patrick");
076: b1 = home.create(1, "Patrick");
077: } catch (Exception _e) {
078: getLog().debug(_e.toString());
079: }
080:
081: getLog().debug("create some more dummys:");
082: for (int i = 0; i < 50; ++i)
083: home.create(i + 3, ("Dummy " + i));
084:
085: try {
086: getLog().debug("trying to find Robert again");
087: b1 = home.findByPrimaryKey(new Integer(2));
088: getLog().debug("getName (): " + b1.getName());
089: } catch (Exception _e) {
090: getLog().debug(_e.toString());
091: }
092:
093: try {
094: getLog().debug("trying to find an not existing bean");
095: b1 = home.findByPrimaryKey(new Integer(0));
096: getLog().debug("getName (): " + b1.getName());
097: } catch (Exception _e) {
098: getLog().debug(_e.toString());
099: }
100:
101: getLog().debug("rename Daniel to Maria: 1, Daniel");
102: b1 = home.findByPrimaryKey(new Integer(1));
103: getLog().debug("name old: " + b1.getName());
104: b1.setName("Maria");
105: getLog().debug("name new: " + b1.getName());
106:
107: getLog().debug("find all beans:");
108: Iterator it = home.findAll().iterator();
109: while (it.hasNext()) {
110: getLog().debug(
111: "found:" + ((SimpleBMP) it.next()).getName());
112: }
113:
114: getLog()
115: .debug(
116: "*******Now trying from within the Session bean (to be able to rollback):");
117: getLog().debug(session.doTest());
118: getLog().debug("Getting the name after a rollback:");
119: getLog().debug(session.doTestAfterRollback());
120:
121: getLog().debug("removing all beans:");
122: it = home.findAll().iterator();
123: while (it.hasNext())
124: ((SimpleBMP) it.next()).remove();
125:
126: SimpleBMP b2 = home.create(200, "Dave");
127: assertFalse(
128: "ejbStore() should not be invoked during ejbPostCreate()",
129: b2.isEjbStoreInvoked());
130: } finally {
131: getLog().debug("table exists.");
132: getLog().debug("delete it...");
133: session.dropSimpleBeanTable();
134: getLog().debug("done.");
135: }
136: }
137:
138: public static Test suite() throws Exception {
139: return getDeploySetup(BmpUnitTestCase.class, "bmp.jar");
140: }
141: }
|