This is an automated email from the ASF dual-hosted git repository.

dblevins pushed a commit to branch markdown-to-asciidoc
in repository https://gitbox.apache.org/repos/asf/tomee-site-generator.git

commit 1798878a7d5d21324c81db7be22e12373c2fadb6
Author: David Jencks <[email protected]>
AuthorDate: Sun Feb 16 12:45:37 2020 -0800

    Add three pages from tomee-site that are referred to but not currently 
published. Git rev 3bc5480f5e94a43346b5d0a15c6cdfed0eaa6915 in 
djencks/tomee-site
---
 .../getting-started-with-eclipse-and-webby.adoc    |  39 +++++
 .../pages/testcase-with-testbean-inner-class.adoc  |  71 ++++++++
 .../ROOT/pages/unit-testing-transactions.adoc      | 192 +++++++++++++++++++++
 3 files changed, 302 insertions(+)

diff --git 
a/tomee/modules/ROOT/pages/getting-started-with-eclipse-and-webby.adoc 
b/tomee/modules/ROOT/pages/getting-started-with-eclipse-and-webby.adoc
new file mode 100644
index 0000000..f79f4a6
--- /dev/null
+++ b/tomee/modules/ROOT/pages/getting-started-with-eclipse-and-webby.adoc
@@ -0,0 +1,39 @@
+= TomEE and Eclipse using Webby
+
+If you want to use Eclipse and WTP (the classic way), please read this 
xref:tomee-and-eclipse.adoc[docs].
+
+If you intends to run a Maven WAR project, you can adopt 
link:https://github.com/tesla/m2eclipse-webby[m2eclipse-webby], because it will 
be more efficient. In this case, follow this docs.
+
+== Prerequisite
+Download and install both Apache TomEE (1.7.2 minimum) and Eclipse IDE (not 
JavaEE version, because Webby doesn't work if 
link:https://www.eclipse.org/m2e-wtp[m2eclipse-WTP] is present).
+
+=== m2eclipse
+The next thing you need is link:http://www.eclipse.org/m2e[m2eclipse] plugin.
+If you doesn't have it in your eclipse package, install it like another plugin.
+
+=== Webby
+0. Then you need to install 
link:https://github.com/tesla/m2eclipse-webby[m2eclipse-webby]
+1. In eclipse, select menu "Help" / "Install new software", and type this URL 
: 
https://repository.takari.io/content/sites/m2e.extras/m2eclipse-webby/0.2.2/N/LATEST/
+2. Select Web Application Launcher for M2Eclipse / Webby Core
+3. Click Finish and continue the classic installation way.
+4. Restart eclipse.
+
+== Setting up eclipse project
+1. Create new Maven project or import an existing one (only War project are 
accepted with Webby).
+2. Add new Debug configuration (Right click on the project / Debug As / Debug 
Configuration)
+3. Select Launch Type "Webby", right click "New"
+4. Browse your project if it not present
+5. Type an optionnal context root
+6. Select container "tomee1x" and select "Installed" as type (the only way who 
works right now)
+7. Browse your tomEE installation in "Home" section
+8. Optionnal : change port and timeout values.
+9. Click "Apply", and "Debug" to start TomEE.
+
+== Webby capabilities
+* Webby manage code hot replacement if you don't change method signature and 
so on... If other case you need to restart it.
+* Webby manage hot jsp replacement too.
+* If you want to see Webby status Click menu "Widows" / "Show View" / "Other" 
/ "Webby" / "Webapps". You can start and stop webby in this panel.
+* If you need to start 2 or more Webby in the same time, you need to change 
port to at least 3 numbers more. Example : 8080 and 8084 (other value are 
reserved by Webby automatically).
+* If you have an error "timeout ..." during Webby start, you can change the 
Webby timeout in debug configurations (Note : this error will not stop Webby, 
it's just a warning).
+* In opposite to m2eclipse-WTP, Webby will not "publish" all of your project 
dependencies. It makes just a link to your Maven local repositories. You can 
see the entire list of dependencies in Webby start console.
+* If you want to "publish" you code in Webby, just start it and it works.
diff --git a/tomee/modules/ROOT/pages/testcase-with-testbean-inner-class.adoc 
b/tomee/modules/ROOT/pages/testcase-with-testbean-inner-class.adoc
new file mode 100644
index 0000000..c9c1cca
--- /dev/null
+++ b/tomee/modules/ROOT/pages/testcase-with-testbean-inner-class.adoc
@@ -0,0 +1,71 @@
+Title: TestCase with TestBean inner-class The use of an inner-class annotated 
as an EJB can get you all the resources you may need to run your test case such 
as a UserTransaction, EntityManager, JMS Topic or Queue, DataSource, references 
to other EJBs, etc.
+
+Simply add a _META-INF/ejb-jar.xml_ to your _test_ _sources_ and start 
annotating your inner class as needed.
+
+The test logic can be placed inside the inner-class EJB or you can setup the 
EJB to pass the required resources to the outer class.
+
+....
+import junit.framework.TestCase;
+
+import javax.ejb.Stateless;
+import javax.ejb.TransactionManagement;
+import static javax.ejb.TransactionManagementType.BEAN;
+import javax.annotation.Resource;
+import javax.transaction.UserTransaction;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.EntityManager;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+
+public class MyTest extends TestCase {
+
+   private EntityManager entityManager;
+   private UserTransaction userTransaction;
+
+   @Override
+   protected void setUp() throws Exception {
+       Properties p = new Properties();
+       p.put(Context.INITIAL_CONTEXT_FACTORY, 
"org.apache.openejb.client.LocalInitialContextFactory");
+       // set any other properties
+
+       InitialContext initialContext = new InitialContext(p);
+       TestLocal testLocal = (TestLocal) 
initialContext.lookup("TestBeanLocal");
+
+       entityManager = testLocal.getEntityManager();
+       userTransaction = testLocal.getUserTransaction();
+   }
+
+
+   /**
+    * The src/test/resource/META-INF/ejb-jar.xml will cause this
+    * EJB to be automatically discovered and deployed when
+    * OpenEJB boots up.
+    */
+   @Stateless
+   @TransactionManagement(BEAN)
+   public static class TestBean implements TestLocal {
+
+       @Resource
+       private UserTransaction userTransaction;
+
+       @PersistenceContext(type = PersistenceContextType.EXTENDED)
+       private EntityManager entityManager;
+
+       public UserTransaction getUserTransaction() {
+          return userTransaction;
+       }
+
+       public EntityManager getEntityManager() {
+          return entityManager;
+       }
+   }
+
+   public static interface TestLocal {
+       EntityManager getEntityManager();
+       UserTransaction getUserTransaction();
+   }
+
+}
+....
diff --git a/tomee/modules/ROOT/pages/unit-testing-transactions.adoc 
b/tomee/modules/ROOT/pages/unit-testing-transactions.adoc
new file mode 100644
index 0000000..d3e7ba3
--- /dev/null
+++ b/tomee/modules/ROOT/pages/unit-testing-transactions.adoc
@@ -0,0 +1,192 @@
+:doctype: book
+
+Title: Unit Testing Transactions
+
++++<a name="UnitTestingTransactions-Basicsetup">++++++</a>+++
+
+= Basic setup
+
+Add the following interface and bean to your test sources (they could even be 
inner classes of a test case):
+
++++<a name="UnitTestingTransactions-Businessinterface">++++++</a>+++
+
+== Business interface
+
+ public interface Caller {
+     public <V> V call(Callable<V> callable) throws Exception;
+ }
+
++++<a name="UnitTestingTransactions-BeanImplementation(s)">++++++</a>+++
+
+== Bean Implementation(s)
+
+....
+import java.util.concurrent.Callable;
+
+@Stateless
+@TransactionAttribute(REQUIRES_NEW)
+public class TransactionBean implements Caller {
+    public <V> V call(Callable<V> callable) throws Exception {
+       return callable.call();
+    }
+}
+....
+
++++<a name="UnitTestingTransactions-Havethemdiscovered">++++++</a>+++
+
+== Have them discovered
+
+In src/test/resources/ (or related) create an META-INF/ejb-jar.xml containing 
the text "+++<ejb-jar>++++++</ejb-jar>+++"
+
++++<a name="UnitTestingTransactions-Whatweaccomplished">++++++</a>+++
+
+== What we accomplished
+
+Essentially what we've done is added an ejb that will be picked up as part of 
your test code and deployed.
+You can then look it up and use it to execute test code with any particular 
transaction or security constraints that you want.
+The above bean specifies REQUIRES_NEW;
+functionally the same as REQUIRED as the test case itself won't have a 
transaction, but a little cleaner and more explicit.
+
+You could also annotate the bean with @RunAs("manager") for example and test 
that your security restrictions are how you like them.
+You can have as many of these test beans in your test code as you like, each 
with it's own transaction and security constraints allowing you to write some 
incredibly thorough tests.
+
+You do not need to use java.util.concurrent.Callable, any similar interface of 
your creation could work just as well.
+You may want something with return type void, for example, to eliminate 
useless 'return null' statements.
+
++++<a name="UnitTestingTransactions-Usage">++++++</a>+++
+
+= Usage
+
+There are a number of style choices for using the above bean, specifically 
around the creation of the Callable object you pass in, and it all really 
depends on what looks nice to you.
+
+In the examples below, the Movies bean being tested is simply a thin layer 
around JPA that allows us to use enforce various transaction semantics.
+
+....
+import javax.ejb.Stateful;
+import javax.ejb.TransactionAttribute;
+import static javax.ejb.TransactionAttributeType.MANDATORY;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.Query;
+import java.util.List;
+
+@Stateful(name = "Movies")
+@TransactionAttribute(MANDATORY)
+public class MoviesImpl implements Movies {
+
+    @PersistenceContext(unitName = "movie-unit", type = 
PersistenceContextType.TRANSACTION)
+    private EntityManager entityManager;
+
+    public void addMovie(Movie movie) throws Exception {
+       entityManager.persist(movie);
+    }
+
+    public void deleteMovie(Movie movie) throws Exception {
+       entityManager.remove(movie);
+    }
+
+    public List<Movie> getMovies() throws Exception {
+       Query query = entityManager.createQuery("SELECT m from Movie asm");
+    return query.getResultList();
+    }
+}
+....
+
+Test code below.
+
++++<a name="UnitTestingTransactions-Pureinlined">++++++</a>+++
+
+== Pure inlined
+
+The Callable can be created right in the test method itself.
+
+....
+public class MoviesTest extends TestCase {
+    private Context context;
+
+    protected void setUp() throws Exception {
+       // initialize jndi context as usual
+    }
+
+    public void test() throws Exception {
+       Caller transactionBean = (Caller)
+    context.lookup("TransactionBeanLocal");
+
+       transactionBean.call(new Callable() {
+           public Object call() throws Exception {
+               Movies movies = (Movies) context.lookup("MoviesLocal");
+
+               movies.addMovie(new Movie("Quentin Tarantino", "Reservoir 
Dogs", 1992));
+               movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
+               movies.addMovie(new Movie("Joel Coen", "The Big 
Lebowski",1998));
+
+               List<Movie> list = movies.getMovies();
+               assertEquals("List.size()", 3, list.size());
+
+               for (Movie movie : list) {
+                   movies.deleteMovie(movie);
+               }
+
+               assertEquals("Movies.getMovies()", 0,movies.getMovies().size());
+
+               return null;
+           }
+       });
+    }
+}
+....
+
++++<a 
name="UnitTestingTransactions-Sametestcode,differenttransactionscenarios">++++++</a>+++
+
+== Same test code, different transaction scenarios
+
+Maybe you'd like to test how things behave with and without a transaction to 
guarantee everyone is doing the right thing in all situations.
+Negative testing is often a very good way to stomp out dangerous bugs.
+
+....
+public class MoviesTest extends TestCase {
+    private Context context;
+
+    protected void setUp() throws Exception {
+       // initialize jndi context as usual
+    }
+
+    private void doWork() throws Exception {
+             Movies movies = (Movies) context.lookup("MoviesLocal");
+
+             movies.addMovie(new Movie("Quentin Tarantino", "Reservoir 
Dogs",1992));
+          movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
+             movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
+
+             List<Movie> list = movies.getMovies();
+             assertEquals("List.size()", 3, list.size());
+
+             for (Movie movie : list) {
+                 movies.deleteMovie(movie);
+             }
+
+             assertEquals("Movies.getMovies()", 0, movies.getMovies().size());
+    }
+
+    public void testWithTransaction() throws Exception {
+             Caller transactionBean = 
(Caller)context.lookup("TransactionBeanLocal");
+
+             transactionBean.call(new Callable(){
+                   public Object call() throws Exception {
+                         doWork();
+                         return null;
+                   }
+             });
+    }
+
+    public void testWithoutTransaction() throws Exception {
+             try {
+                   doWork();
+                   fail("The Movies bean should be using 
TransactionAttributeType.MANDATORY");
+             } catch (javax.transaction.TransactionRequiredException e) {
+                   // good, our Movies bean is using 
TransactionAttributeType.MANDATORY as we want
+             }
+    }
+}
+....

Reply via email to