001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.superbiz.injection.tx;
017:
018: import junit.framework.TestCase;
019:
020: import javax.naming.Context;
021: import javax.naming.InitialContext;
022: import javax.ejb.Stateless;
023: import javax.ejb.TransactionAttribute;
024: import static javax.ejb.TransactionAttributeType.REQUIRES_NEW;
025: import java.util.Properties;
026: import java.util.List;
027: import java.util.concurrent.Callable;
028:
029: /**
030: * @version $Revision: 607077 $ $Date: 2007-12-27 06:55:23 -0800 (Thu, 27 Dec 2007) $
031: */
032: public class MoviesTest extends TestCase {
033: private Context context;
034:
035: protected void setUp() throws Exception {
036: Properties p = new Properties();
037: p.put(Context.INITIAL_CONTEXT_FACTORY,
038: "org.apache.openejb.client.LocalInitialContextFactory");
039: p.put("movieDatabase", "new://Resource?type=DataSource");
040: p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
041: p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
042:
043: p.put("movieDatabaseUnmanaged",
044: "new://Resource?type=DataSource");
045: p.put("movieDatabaseUnmanaged.JdbcDriver",
046: "org.hsqldb.jdbcDriver");
047: p.put("movieDatabaseUnmanaged.JdbcUrl",
048: "jdbc:hsqldb:mem:moviedb");
049: p.put("movieDatabaseUnmanaged.JtaManaged", "false");
050:
051: context = new InitialContext(p);
052: }
053:
054: private void doWork() throws Exception {
055: Movies movies = (Movies) context.lookup("MoviesLocal");
056:
057: movies.addMovie(new Movie("Quentin Tarantino",
058: "Reservoir Dogs", 1992));
059: movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
060: movies
061: .addMovie(new Movie("Joel Coen", "The Big Lebowski",
062: 1998));
063:
064: List<Movie> list = movies.getMovies();
065: assertEquals("List.size()", 3, list.size());
066:
067: for (Movie movie : list) {
068: movies.deleteMovie(movie);
069: }
070:
071: assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
072: }
073:
074: public void testWithTransaction() throws Exception {
075: Caller transactionBean = (Caller) context
076: .lookup("TransactionBeanLocal");
077:
078: transactionBean.call(new Callable() {
079: public Object call() throws Exception {
080: doWork();
081: return null;
082: }
083: });
084: }
085:
086: public void testWithoutTransaction() throws Exception {
087: try {
088: doWork();
089: fail("The Movies bean should be using TransactionAttributeType.MANDATORY");
090: } catch (javax.ejb.EJBTransactionRequiredException e) {
091: // good, our Movies bean is using TransactionAttributeType.MANDATORY as we want
092: }
093: }
094:
095: public static interface Caller {
096: public <V> V call(Callable<V> callable) throws Exception;
097: }
098:
099: /**
100: * This little bit of magic allows our test code to execute in
101: * the scope of a container controlled transaction.
102: *
103: * The src/test/resource/META-INF/ejb-jar.xml will cause this
104: * EJB to be automatically discovered and deployed when
105: * OpenEJB boots up.
106: */
107: @Stateless
108: @TransactionAttribute(REQUIRES_NEW)
109: public static class TransactionBean implements Caller {
110:
111: public <V> V call(Callable<V> callable) throws Exception {
112: return callable.call();
113: }
114:
115: }
116:
117: }
|