You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2016/12/04 11:01:44 UTC
svn commit: r1772522 [42/45] - in /tomee/site/trunk: content/ content/admin/
content/admin/cluster/ content/admin/configuration/ content/advanced/
content/advanced/applicationcomposer/ content/advanced/client/
content/advanced/setup/ content/advanced/s...
Added: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/struts.adoc
URL: http://svn.apache.org/viewvc/tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/struts.adoc?rev=1772522&view=auto
==============================================================================
--- tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/struts.adoc (added)
+++ tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/struts.adoc Sun Dec 4 11:01:40 2016
@@ -0,0 +1,439 @@
+= Struts
+:jbake-date: 2016-09-06
+:jbake-type: page
+:jbake-tomeepdf:
+:jbake-status: published
+
+Example struts can be browsed at https://github.com/apache/tomee/tree/master/examples/struts
+
+
+*Help us document this example! Click the blue pencil icon in the upper right to edit this page.*
+
+== AddUser
+
+
+[source,java]
+----
+package org.superbiz.struts;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+
+
+public class AddUser {
+
+ private int id;
+ private String firstName;
+ private String lastName;
+ private String errorMessage;
+
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ public void setErrorMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String execute() {
+
+ try {
+ UserService service = null;
+ Properties props = new Properties();
+ props.put(Context.INITIAL_CONTEXT_FACTORY,
+ "org.apache.openejb.core.LocalInitialContextFactory");
+ Context ctx = new InitialContext(props);
+ service = (UserService) ctx.lookup("UserServiceImplLocal");
+ service.add(new User(id, firstName, lastName));
+ } catch (Exception e) {
+ this.errorMessage = e.getMessage();
+ return "failure";
+ }
+
+ return "success";
+ }
+}
+----
+
+
+== AddUserForm
+
+
+[source,java]
+----
+package org.superbiz.struts;
+
+import com.opensymphony.xwork2.ActionSupport;
+
+
+public class AddUserForm extends ActionSupport {
+}
+----
+
+
+== FindUser
+
+
+[source,java]
+----
+package org.superbiz.struts;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+
+public class FindUser {
+
+ private int id;
+ private String errorMessage;
+ private User user;
+
+ public User getUser() {
+ return user;
+ }
+
+ public void setUser(User user) {
+ this.user = user;
+ }
+
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ public void setErrorMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String execute() {
+
+ try {
+ UserService service = null;
+ Properties props = new Properties();
+ props.put(Context.INITIAL_CONTEXT_FACTORY,
+ "org.apache.openejb.core.LocalInitialContextFactory");
+ Context ctx = new InitialContext(props);
+ service = (UserService) ctx.lookup("UserServiceImplLocal");
+ this.user = service.find(id);
+ } catch (Exception e) {
+ this.errorMessage = e.getMessage();
+ return "failure";
+ }
+
+ return "success";
+ }
+}
+----
+
+
+== FindUserForm
+
+
+[source,java]
+----
+package org.superbiz.struts;
+
+import com.opensymphony.xwork2.ActionSupport;
+
+
+public class FindUserForm extends ActionSupport {
+}
+----
+
+
+== ListAllUsers
+
+
+[source,java]
+----
+package org.superbiz.struts;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.List;
+import java.util.Properties;
+
+public class ListAllUsers {
+
+ private int id;
+ private String errorMessage;
+ private List<User> users;
+
+ public List<User> getUsers() {
+ return users;
+ }
+
+ public void setUsers(List<User> users) {
+ this.users = users;
+ }
+
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ public void setErrorMessage(String errorMessage) {
+ this.errorMessage = errorMessage;
+ }
+
+ public int getId() {
+ return id;
+ }
+
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ public String execute() {
+
+ try {
+ UserService service = null;
+ Properties props = new Properties();
+ props.put(Context.INITIAL_CONTEXT_FACTORY,
+ "org.apache.openejb.core.LocalInitialContextFactory");
+ Context ctx = new InitialContext(props);
+ service = (UserService) ctx.lookup("UserServiceImplLocal");
+ this.users = service.findAll();
+ } catch (Exception e) {
+ this.errorMessage = e.getMessage();
+ return "failure";
+ }
+
+ return "success";
+ }
+}
+----
+
+
+== User
+
+
+[source,java]
+----
+package org.superbiz.struts;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import java.io.Serializable;
+
+@Entity
+@Table(name = "USER")
+public class User implements Serializable {
+ private long id;
+ private String firstName;
+ private String lastName;
+
+ public User(long id, String firstName, String lastName) {
+ super();
+ this.id = id;
+ this.firstName = firstName;
+ this.lastName = lastName;
+ }
+
+ public User() {
+ }
+
+ @Id
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public void setFirstName(String firstName) {
+ this.firstName = firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public void setLastName(String lastName) {
+ this.lastName = lastName;
+ }
+}
+----
+
+
+== UserService
+
+
+[source,java]
+----
+package org.superbiz.struts;
+
+import java.util.List;
+
+public interface UserService {
+ public void add(User user);
+
+ public User find(int id);
+
+ public List<User> findAll();
+}
+----
+
+
+== UserServiceImpl
+
+
+[source,java]
+----
+package org.superbiz.struts;
+
+import javax.ejb.Stateless;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import java.util.List;
+
+@Stateless
+public class UserServiceImpl implements UserService {
+
+ @PersistenceContext(unitName = "user")
+ private EntityManager manager;
+
+ public void add(User user) {
+ manager.persist(user);
+ }
+
+ public User find(int id) {
+ return manager.find(User.class, id);
+ }
+
+ public List<User> findAll() {
+ return manager.createQuery("select u from User u").getResultList();
+ }
+}
+----
+
+
+== persistence.xml
+
+
+[source,xml]
+----
+</persistence-unit>
+
+ -->
+</persistence>
+
+## struts.xml
+
+<struts>
+ <constant name="struts.devMode" value="true"></constant>
+ <package name="default" namespace="/" extends="struts-default">
+ <action name="addUserForm" class="org.superbiz.struts.AddUserForm">
+ <result>/addUserForm.jsp</result>
+ </action>
+ <action name="addUser" class="org.superbiz.struts.AddUser">
+ <result name="success">/addedUser.jsp</result>
+ <result name='failure'>/addUserForm.jsp</result>
+ </action>
+ <action name="findUserForm" class="org.superbiz.struts.FindUserForm">
+ <result>/findUserForm.jsp</result>
+ </action>
+ <action name="findUser" class="org.superbiz.struts.FindUser">
+ <result name='success'>/displayUser.jsp</result>
+ <result name='failure'>/findUserForm.jsp</result>
+ </action>
+ <action name="listAllUsers" class="org.superbiz.struts.ListAllUsers">
+ <result>/displayUsers.jsp</result>
+ </action>
+
+ </package>
+</struts>
+
+## decorators.xml
+
+<decorators defaultdir="/decorators">
+ <decorator name="main" page="layout.jsp">
+ <pattern>/*</pattern>
+ </decorator>
+</decorators>
+
+## web.xml
+
+<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
+ version="2.5">
+ <display-name>Learn EJB3 and Struts2</display-name>
+ <filter>
+ <filter-name>struts2</filter-name>
+ <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
+ <init-param>
+ <param-name>actionPackages</param-name>
+ <param-value>com.lq</param-value>
+ </init-param>
+ </filter>
+ <filter>
+ <filter-name>struts-cleanup</filter-name>
+ <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
+ </filter>
+ <filter>
+ <filter-name>sitemesh</filter-name>
+ <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class>
+ </filter>
+ <filter-mapping>
+ <filter-name>struts-cleanup</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+ <filter-mapping>
+ <filter-name>sitemesh</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+ <filter-mapping>
+ <filter-name>struts2</filter-name>
+ <url-pattern>/*</url-pattern>
+ </filter-mapping>
+ <welcome-file-list>
+ <welcome-file>index.jsp</welcome-file>
+ </welcome-file-list>
+ <jsp-config>
+ <jsp-property-group>
+ <description>JSP configuration of all the JSP's</description>
+ <url-pattern>*.jsp</url-pattern>
+ <include-prelude>/prelude.jspf</include-prelude>
+ </jsp-property-group>
+ </jsp-config>
+</web-app>
+
+
Propchange: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/struts.adoc
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/telephone-stateful.adoc
URL: http://svn.apache.org/viewvc/tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/telephone-stateful.adoc?rev=1772522&view=auto
==============================================================================
--- tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/telephone-stateful.adoc (added)
+++ tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/telephone-stateful.adoc Sun Dec 4 11:01:40 2016
@@ -0,0 +1,248 @@
+= Telephone Stateful
+:jbake-date: 2016-09-06
+:jbake-type: page
+:jbake-tomeepdf:
+:jbake-status: published
+
+Example telephone-stateful can be browsed at https://github.com/apache/tomee/tree/master/examples/telephone-stateful
+
+
+*Help us document this example! Click the blue pencil icon in the upper right to edit this page.*
+
+This example shows how to use OpenEJB's remoting capabilities in an embedded scenario.
+
+The basic recipe is the same for a standard embedded scenario but with these added
+ingreditents:
+
+ * `openejb.embedded.remotable` property
+ * `openejb-ejbd` jar
+
+While creating the InitialContext, pass in the openejb.embedded.remotable property with
+the value of "true". When this is seen by the LocalInitialContextFactory, it will boot up
+the Server ServiceManager in the VM which will in turn look for ServerServices in the
+classpath.
+
+Provided you have the openejb-ejbd jar in your classpath along with it's dependencies
+(openejb-server, openejb-client, openejb-core), then those services will be brought online
+and remote clients will be able to connect into your vm and invoke beans.
+
+If you want to add more ServerServices such as the http version of the ejbd protocol you'd
+simply add the openejb-httpejbd jar to your classpath. A number of ServerServices are
+available currently:
+
+ * openejb-ejbd
+ * openejb-http
+ * openejb-telnet
+ * openejb-derbynet
+ * openejb-hsql
+ * openejb-activemq
+
+
+== Telephone
+
+
+[source,java]
+----
+package org.superbiz.telephone;
+
+public interface Telephone {
+
+ void speak(String words);
+
+ String listen();
+}
+----
+
+
+== TelephoneBean
+
+
+[source,java]
+----
+package org.superbiz.telephone;
+
+import javax.ejb.Remote;
+import javax.ejb.Stateful;
+import java.util.ArrayList;
+import java.util.List;
+
+@Remote
+@Stateful
+public class TelephoneBean implements Telephone {
+
+ private static final String[] answers = {
+ "How nice.",
+ "Oh, of course.",
+ "Interesting.",
+ "Really?",
+ "No.",
+ "Definitely.",
+ "I wondered about that.",
+ "Good idea.",
+ "You don't say!",
+ };
+
+ private List<String> conversation = new ArrayList<String>();
+
+ public void speak(String words) {
+ conversation.add(words);
+ }
+
+ public String listen() {
+ if (conversation.size() == 0) {
+ return "Nothing has been said";
+ }
+
+ String lastThingSaid = conversation.get(conversation.size() - 1);
+ return answers[Math.abs(lastThingSaid.hashCode()) % answers.length];
+ }
+}
+----
+
+
+== TelephoneTest
+
+
+[source,java]
+----
+package org.superbiz.telephone;
+
+import junit.framework.TestCase;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+
+/**
+ * @version $Rev: 1090810 $ $Date: 2011-04-10 07:49:26 -0700 (Sun, 10 Apr 2011) $
+ */
+public class TelephoneTest extends TestCase {
+
+ protected void setUp() throws Exception {
+ Properties properties = new Properties();
+ properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
+ properties.setProperty("openejb.embedded.remotable", "true");
+ // Uncomment these properties to change the defaults
+ //properties.setProperty("ejbd.port", "4202");
+ //properties.setProperty("ejbd.bind", "localhost");
+ //properties.setProperty("ejbd.threads", "200");
+ //properties.setProperty("ejbd.disabled", "false");
+ //properties.setProperty("ejbd.only_from", "127.0.0.1,192.168.1.1");
+
+ new InitialContext(properties);
+ }
+
+ /**
+ * Lookup the Telephone bean via its remote interface but using the LocalInitialContextFactory
+ *
+ * @throws Exception
+ */
+ public void testTalkOverLocalNetwork() throws Exception {
+
+ Properties properties = new Properties();
+ properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
+ InitialContext localContext = new InitialContext(properties);
+
+ Telephone telephone = (Telephone) localContext.lookup("TelephoneBeanRemote");
+
+ telephone.speak("Did you know I am talking directly through the embedded container?");
+
+ assertEquals("Interesting.", telephone.listen());
+
+
+ telephone.speak("Yep, I'm using the bean's remote interface but since the ejb container is embedded " +
+ "in the same vm I'm just using the LocalInitialContextFactory.");
+
+ assertEquals("Really?", telephone.listen());
+
+
+ telephone.speak("Right, you really only have to use the RemoteInitialContextFactory if you're in a different vm.");
+
+ assertEquals("Oh, of course.", telephone.listen());
+ }
+
+ /**
+ * Lookup the Telephone bean via its remote interface using the RemoteInitialContextFactory
+ *
+ * @throws Exception
+ */
+ public void testTalkOverRemoteNetwork() throws Exception {
+ Properties properties = new Properties();
+ properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
+ properties.setProperty(Context.PROVIDER_URL, "ejbd://localhost:4201");
+ InitialContext remoteContext = new InitialContext(properties);
+
+ Telephone telephone = (Telephone) remoteContext.lookup("TelephoneBeanRemote");
+
+ telephone.speak("Is this a local call?");
+
+ assertEquals("No.", telephone.listen());
+
+
+ telephone.speak("This would be a lot cooler if I was connecting from another VM then, huh?");
+
+ assertEquals("I wondered about that.", telephone.listen());
+
+
+ telephone.speak("I suppose I should hangup and call back over the LocalInitialContextFactory.");
+
+ assertEquals("Good idea.", telephone.listen());
+
+
+ telephone.speak("I'll remember this though in case I ever have to call you accross a network.");
+
+ assertEquals("Definitely.", telephone.listen());
+ }
+}
+----
+
+
+= Running
+
+
+
+[source]
+----
+-------------------------------------------------------
+ T E S T S
+-------------------------------------------------------
+Running org.superbiz.telephone.TelephoneTest
+Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06
+http://tomee.apache.org/
+INFO - openejb.home = /Users/dblevins/examples/telephone-stateful
+INFO - openejb.base = /Users/dblevins/examples/telephone-stateful
+INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
+INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
+INFO - Found EjbModule in classpath: /Users/dblevins/examples/telephone-stateful/target/classes
+INFO - Beginning load: /Users/dblevins/examples/telephone-stateful/target/classes
+INFO - Configuring enterprise application: /Users/dblevins/examples/telephone-stateful/classpath.ear
+INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
+INFO - Auto-creating a container for bean TelephoneBean: Container(type=STATEFUL, id=Default Stateful Container)
+INFO - Enterprise application "/Users/dblevins/examples/telephone-stateful/classpath.ear" loaded.
+INFO - Assembling app: /Users/dblevins/examples/telephone-stateful/classpath.ear
+INFO - Jndi(name=TelephoneBeanRemote) --> Ejb(deployment-id=TelephoneBean)
+INFO - Jndi(name=global/classpath.ear/telephone-stateful/TelephoneBean!org.superbiz.telephone.Telephone) --> Ejb(deployment-id=TelephoneBean)
+INFO - Jndi(name=global/classpath.ear/telephone-stateful/TelephoneBean) --> Ejb(deployment-id=TelephoneBean)
+INFO - Created Ejb(deployment-id=TelephoneBean, ejb-name=TelephoneBean, container=Default Stateful Container)
+INFO - Started Ejb(deployment-id=TelephoneBean, ejb-name=TelephoneBean, container=Default Stateful Container)
+INFO - Deployed Application(path=/Users/dblevins/examples/telephone-stateful/classpath.ear)
+INFO - Initializing network services
+INFO - Creating ServerService(id=admin)
+INFO - Creating ServerService(id=ejbd)
+INFO - Creating ServerService(id=ejbds)
+INFO - Initializing network services
+ ** Starting Services **
+ NAME IP PORT
+ admin thread 127.0.0.1 4200
+ ejbd 127.0.0.1 4201
+ ejbd 127.0.0.1 4203
+-------
+Ready!
+Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.448 sec
+
+Results :
+
+Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
+----
+
+
Propchange: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/telephone-stateful.adoc
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testcase-injection.adoc
URL: http://svn.apache.org/viewvc/tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testcase-injection.adoc?rev=1772522&view=auto
==============================================================================
--- tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testcase-injection.adoc (added)
+++ tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testcase-injection.adoc Sun Dec 4 11:01:40 2016
@@ -0,0 +1,240 @@
+= Testcase Injection
+:jbake-date: 2016-09-06
+:jbake-type: page
+:jbake-tomeepdf:
+:jbake-status: published
+
+Example testcase-injection can be browsed at https://github.com/apache/tomee/tree/master/examples/testcase-injection
+
+
+*Help us document this example! Click the blue pencil icon in the upper right to edit this page.*
+
+== Movie
+
+
+[source,java]
+----
+package org.superbiz.testinjection;
+
+import javax.persistence.Entity;
+
+@Entity
+public class Movie {
+
+ private String director;
+ private String title;
+ private int year;
+
+ public Movie() {
+ }
+
+ public Movie(String director, String title, int year) {
+ this.director = director;
+ this.title = title;
+ this.year = year;
+ }
+
+ public String getDirector() {
+ return director;
+ }
+
+ public void setDirector(String director) {
+ this.director = director;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public int getYear() {
+ return year;
+ }
+
+ public void setYear(int year) {
+ this.year = year;
+ }
+
+}
+----
+
+
+== Movies
+
+
+[source,java]
+----
+package org.superbiz.testinjection;
+
+import javax.ejb.Stateful;
+import javax.ejb.TransactionAttribute;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.Query;
+import java.util.List;
+
+import static javax.ejb.TransactionAttributeType.MANDATORY;
+
+//START SNIPPET: code
+@Stateful
+@TransactionAttribute(MANDATORY)
+public class 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 as m");
+ return query.getResultList();
+ }
+}
+----
+
+
+== persistence.xml
+
+
+[source,xml]
+----
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
+
+ <persistence-unit name="movie-unit">
+ <jta-data-source>movieDatabase</jta-data-source>
+ <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
+ <class>org.superbiz.testinjection.Movie</class>
+
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+</persistence>
+----
+
+
+== MoviesTest
+
+
+[source,java]
+----
+package org.superbiz.testinjection;
+
+import junit.framework.TestCase;
+
+import javax.annotation.Resource;
+import javax.ejb.EJB;
+import javax.ejb.embeddable.EJBContainer;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.transaction.UserTransaction;
+import java.util.List;
+import java.util.Properties;
+
+//START SNIPPET: code
+public class MoviesTest extends TestCase {
+
+ @EJB
+ private Movies movies;
+
+ @Resource
+ private UserTransaction userTransaction;
+
+ @PersistenceContext
+ private EntityManager entityManager;
+
+ public void setUp() throws Exception {
+ Properties p = new Properties();
+ p.put("movieDatabase", "new://Resource?type=DataSource");
+ p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+ EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
+ }
+
+ public void test() throws Exception {
+
+ userTransaction.begin();
+
+ try {
+ entityManager.persist(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
+ entityManager.persist(new Movie("Joel Coen", "Fargo", 1996));
+ entityManager.persist(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());
+ } finally {
+ userTransaction.commit();
+ }
+
+ }
+}
+----
+
+
+= Running
+
+
+
+[source]
+----
+-------------------------------------------------------
+ T E S T S
+-------------------------------------------------------
+Running org.superbiz.testinjection.MoviesTest
+Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06
+http://tomee.apache.org/
+INFO - openejb.home = /Users/dblevins/examples/testcase-injection
+INFO - openejb.base = /Users/dblevins/examples/testcase-injection
+INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
+INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
+INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
+INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database)
+INFO - Found EjbModule in classpath: /Users/dblevins/examples/testcase-injection/target/classes
+INFO - Beginning load: /Users/dblevins/examples/testcase-injection/target/classes
+INFO - Configuring enterprise application: /Users/dblevins/examples/testcase-injection
+WARN - Method 'lookup' is not available for 'javax.annotation.Resource'. Probably using an older Runtime.
+INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
+INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful Container)
+INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
+INFO - Auto-creating a container for bean org.superbiz.testinjection.MoviesTest: Container(type=MANAGED, id=Default Managed Container)
+INFO - Configuring PersistenceUnit(name=movie-unit)
+INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'.
+INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase)
+INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged'
+INFO - Enterprise application "/Users/dblevins/examples/testcase-injection" loaded.
+INFO - Assembling app: /Users/dblevins/examples/testcase-injection
+INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 408ms
+INFO - Jndi(name="java:global/testcase-injection/Movies!org.superbiz.testinjection.Movies")
+INFO - Jndi(name="java:global/testcase-injection/Movies")
+INFO - Jndi(name="java:global/EjbModule1583515396/org.superbiz.testinjection.MoviesTest!org.superbiz.testinjection.MoviesTest")
+INFO - Jndi(name="java:global/EjbModule1583515396/org.superbiz.testinjection.MoviesTest")
+INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Created Ejb(deployment-id=org.superbiz.testinjection.MoviesTest, ejb-name=org.superbiz.testinjection.MoviesTest, container=Default Managed Container)
+INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Started Ejb(deployment-id=org.superbiz.testinjection.MoviesTest, ejb-name=org.superbiz.testinjection.MoviesTest, container=Default Managed Container)
+INFO - Deployed Application(path=/Users/dblevins/examples/testcase-injection)
+Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.24 sec
+
+Results :
+
+Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
+----
+
+
Propchange: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testcase-injection.adoc
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-2.adoc
URL: http://svn.apache.org/viewvc/tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-2.adoc?rev=1772522&view=auto
==============================================================================
--- tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-2.adoc (added)
+++ tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-2.adoc Sun Dec 4 11:01:40 2016
@@ -0,0 +1,306 @@
+= Testing Security 2
+:jbake-date: 2016-09-06
+:jbake-type: page
+:jbake-tomeepdf:
+:jbake-status: published
+
+Example testing-security-2 can be browsed at https://github.com/apache/tomee/tree/master/examples/testing-security-2
+
+
+*Help us document this example! Click the blue pencil icon in the upper right to edit this page.*
+
+== Movie
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import javax.persistence.Entity;
+
+@Entity
+public class Movie {
+
+ private String director;
+ private String title;
+ private int year;
+
+ public Movie() {
+ }
+
+ public Movie(String director, String title, int year) {
+ this.director = director;
+ this.title = title;
+ this.year = year;
+ }
+
+ public String getDirector() {
+ return director;
+ }
+
+ public void setDirector(String director) {
+ this.director = director;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public int getYear() {
+ return year;
+ }
+
+ public void setYear(int year) {
+ this.year = year;
+ }
+
+}
+----
+
+
+== Movies
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+//START SNIPPET: code
+
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
+import javax.ejb.Stateful;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.Query;
+import java.util.List;
+
+@Stateful
+public class Movies {
+
+ @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
+ private EntityManager entityManager;
+
+ @RolesAllowed({"Employee", "Manager"})
+ public void addMovie(Movie movie) throws Exception {
+ entityManager.persist(movie);
+ }
+
+ @RolesAllowed({"Manager"})
+ public void deleteMovie(Movie movie) throws Exception {
+ entityManager.remove(movie);
+ }
+
+ @PermitAll
+ @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+ public List<Movie> getMovies() throws Exception {
+ Query query = entityManager.createQuery("SELECT m from Movie as m");
+ return query.getResultList();
+ }
+}
+----
+
+
+== persistence.xml
+
+
+[source,xml]
+----
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
+
+ <persistence-unit name="movie-unit">
+ <jta-data-source>movieDatabase</jta-data-source>
+ <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
+ <class>org.superbiz.injection.secure.Movie</class>
+
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+</persistence>
+----
+
+
+== MovieTest
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import junit.framework.TestCase;
+
+import javax.ejb.EJB;
+import javax.ejb.EJBAccessException;
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.List;
+import java.util.Properties;
+
+//START SNIPPET: code
+public class MovieTest extends TestCase {
+
+ @EJB
+ private Movies movies;
+
+ protected void setUp() throws Exception {
+
+ // Uncomment this line to set the login/logout functionality on Debug
+ //System.setProperty("log4j.category.OpenEJB.security", "debug");
+
+ Properties p = new Properties();
+ p.put("movieDatabase", "new://Resource?type=DataSource");
+ p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+ EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
+ }
+
+ public void testAsManager() throws Exception {
+ Properties p = new Properties();
+ p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
+ p.put(Context.SECURITY_PRINCIPAL, "jane");
+ p.put(Context.SECURITY_CREDENTIALS, "waterfall");
+
+ InitialContext context = new InitialContext(p);
+
+ try {
+ 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());
+ } finally {
+ context.close();
+ }
+ }
+
+ public void testAsEmployee() throws Exception {
+ Properties p = new Properties();
+ p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
+ p.put(Context.SECURITY_PRINCIPAL, "joe");
+ p.put(Context.SECURITY_CREDENTIALS, "cool");
+
+ InitialContext context = new InitialContext(p);
+
+ try {
+ 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) {
+ try {
+ movies.deleteMovie(movie);
+ fail("Employees should not be allowed to delete");
+ } catch (EJBAccessException e) {
+ // Good, Employees cannot delete things
+ }
+ }
+
+ // The list should still be three movies long
+ assertEquals("Movies.getMovies()", 3, movies.getMovies().size());
+ } finally {
+ context.close();
+ }
+ }
+
+ public void testUnauthenticated() throws Exception {
+ try {
+ movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
+ fail("Unauthenticated users should not be able to add movies");
+ } catch (EJBAccessException e) {
+ // Good, guests cannot add things
+ }
+
+ try {
+ movies.deleteMovie(null);
+ fail("Unauthenticated users should not be allowed to delete");
+ } catch (EJBAccessException e) {
+ // Good, Unauthenticated users cannot delete things
+ }
+
+ try {
+ // Read access should be allowed
+
+ List<Movie> list = movies.getMovies();
+ } catch (EJBAccessException e) {
+ fail("Read access should be allowed");
+ }
+ }
+}
+----
+
+
+= Running
+
+
+
+[source]
+----
+-------------------------------------------------------
+ T E S T S
+-------------------------------------------------------
+Running org.superbiz.injection.secure.MovieTest
+Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06
+http://tomee.apache.org/
+INFO - openejb.home = /Users/dblevins/examples/testing-security-2
+INFO - openejb.base = /Users/dblevins/examples/testing-security-2
+INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
+INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
+INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
+INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database)
+INFO - Found EjbModule in classpath: /Users/dblevins/examples/testing-security-2/target/classes
+INFO - Beginning load: /Users/dblevins/examples/testing-security-2/target/classes
+INFO - Configuring enterprise application: /Users/dblevins/examples/testing-security-2
+INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
+INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful Container)
+INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
+INFO - Auto-creating a container for bean org.superbiz.injection.secure.MovieTest: Container(type=MANAGED, id=Default Managed Container)
+INFO - Configuring PersistenceUnit(name=movie-unit)
+INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'.
+INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase)
+INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged'
+INFO - Enterprise application "/Users/dblevins/examples/testing-security-2" loaded.
+INFO - Assembling app: /Users/dblevins/examples/testing-security-2
+INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 413ms
+INFO - Jndi(name="java:global/testing-security-2/Movies!org.superbiz.injection.secure.Movies")
+INFO - Jndi(name="java:global/testing-security-2/Movies")
+INFO - Jndi(name="java:global/EjbModule1634151355/org.superbiz.injection.secure.MovieTest!org.superbiz.injection.secure.MovieTest")
+INFO - Jndi(name="java:global/EjbModule1634151355/org.superbiz.injection.secure.MovieTest")
+INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Created Ejb(deployment-id=org.superbiz.injection.secure.MovieTest, ejb-name=org.superbiz.injection.secure.MovieTest, container=Default Managed Container)
+INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Started Ejb(deployment-id=org.superbiz.injection.secure.MovieTest, ejb-name=org.superbiz.injection.secure.MovieTest, container=Default Managed Container)
+INFO - Deployed Application(path=/Users/dblevins/examples/testing-security-2)
+INFO - Logging in
+INFO - Logging out
+INFO - EJBContainer already initialized. Call ejbContainer.close() to allow reinitialization
+INFO - Logging in
+INFO - Logging out
+INFO - EJBContainer already initialized. Call ejbContainer.close() to allow reinitialization
+Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.546 sec
+
+Results :
+
+Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
+----
+
+
Propchange: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-2.adoc
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-3.adoc
URL: http://svn.apache.org/viewvc/tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-3.adoc?rev=1772522&view=auto
==============================================================================
--- tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-3.adoc (added)
+++ tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-3.adoc Sun Dec 4 11:01:40 2016
@@ -0,0 +1,381 @@
+= Testing Security 3
+:jbake-date: 2016-09-06
+:jbake-type: page
+:jbake-tomeepdf:
+:jbake-status: published
+
+Example testing-security-3 can be browsed at https://github.com/apache/tomee/tree/master/examples/testing-security-3
+
+
+*Help us document this example! Click the blue pencil icon in the upper right to edit this page.*
+
+== Movie
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import javax.persistence.Entity;
+
+@Entity
+public class Movie {
+
+ private String director;
+ private String title;
+ private int year;
+
+ public Movie() {
+ }
+
+ public Movie(String director, String title, int year) {
+ this.director = director;
+ this.title = title;
+ this.year = year;
+ }
+
+ public String getDirector() {
+ return director;
+ }
+
+ public void setDirector(String director) {
+ this.director = director;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public int getYear() {
+ return year;
+ }
+
+ public void setYear(int year) {
+ this.year = year;
+ }
+
+}
+----
+
+
+== Movies
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+//START SNIPPET: code
+
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
+import javax.ejb.Stateful;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.Query;
+import java.util.List;
+
+@Stateful
+public class Movies {
+
+ @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
+ private EntityManager entityManager;
+
+ @RolesAllowed({"Employee", "Manager"})
+ public void addMovie(Movie movie) throws Exception {
+ entityManager.persist(movie);
+ }
+
+ @RolesAllowed({"Manager"})
+ public void deleteMovie(Movie movie) throws Exception {
+ entityManager.remove(movie);
+ }
+
+ @PermitAll
+ @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+ public List<Movie> getMovies() throws Exception {
+ Query query = entityManager.createQuery("SELECT m from Movie as m");
+ return query.getResultList();
+ }
+}
+----
+
+
+== MyLoginProvider
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import org.apache.openejb.core.security.jaas.LoginProvider;
+
+import javax.security.auth.login.FailedLoginException;
+import java.util.Arrays;
+import java.util.List;
+
+public class MyLoginProvider implements LoginProvider {
+
+
+ @Override
+ public List<String> authenticate(String user, String password) throws FailedLoginException {
+ if ("paul".equals(user) && "michelle".equals(password)) {
+ return Arrays.asList("Manager", "rockstar", "beatle");
+ }
+
+ if ("eddie".equals(user) && "jump".equals(password)) {
+ return Arrays.asList("Employee", "rockstar", "vanhalen");
+ }
+
+ throw new FailedLoginException("Bad user or password!");
+ }
+}
+----
+
+
+== org.apache.openejb.core.security.jaas.LoginProvider
+
+ org.superbiz.injection.secure.MyLoginProvider
+
+== persistence.xml
+
+
+[source,xml]
+----
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
+
+ <persistence-unit name="movie-unit">
+ <jta-data-source>movieDatabase</jta-data-source>
+ <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
+ <class>org.superbiz.injection.secure.Movie</class>
+
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+</persistence>
+----
+
+
+== MovieTest
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import junit.framework.TestCase;
+
+import javax.ejb.EJB;
+import javax.ejb.EJBAccessException;
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.util.List;
+import java.util.Properties;
+
+public class MovieTest extends TestCase {
+
+ @EJB
+ private Movies movies;
+
+ private Context getContext(String user, String pass) throws NamingException {
+ Properties p = new Properties();
+ p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.core.LocalInitialContextFactory");
+ p.setProperty("openejb.authentication.realmName", "ServiceProviderLogin");
+ p.put(Context.SECURITY_PRINCIPAL, user);
+ p.put(Context.SECURITY_CREDENTIALS, pass);
+
+ return new InitialContext(p);
+ }
+
+ protected void setUp() throws Exception {
+ Properties p = new Properties();
+ p.put("movieDatabase", "new://Resource?type=DataSource");
+ p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+ EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
+ }
+
+ public void testAsManager() throws Exception {
+ final Context context = getContext("paul", "michelle");
+
+ try {
+ 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());
+ } finally {
+ context.close();
+ }
+ }
+
+ public void testAsEmployee() throws Exception {
+ final Context context = getContext("eddie", "jump");
+
+ try {
+ 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) {
+ try {
+ movies.deleteMovie(movie);
+ fail("Employees should not be allowed to delete");
+ } catch (EJBAccessException e) {
+ // Good, Employees cannot delete things
+ }
+ }
+
+ // The list should still be three movies long
+ assertEquals("Movies.getMovies()", 3, movies.getMovies().size());
+ } finally {
+ context.close();
+ }
+ }
+
+ public void testUnauthenticated() throws Exception {
+ try {
+ movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
+ fail("Unauthenticated users should not be able to add movies");
+ } catch (EJBAccessException e) {
+ // Good, guests cannot add things
+ }
+
+ try {
+ movies.deleteMovie(null);
+ fail("Unauthenticated users should not be allowed to delete");
+ } catch (EJBAccessException e) {
+ // Good, Unauthenticated users cannot delete things
+ }
+
+ try {
+ // Read access should be allowed
+
+ List<Movie> list = movies.getMovies();
+
+ } catch (EJBAccessException e) {
+ fail("Read access should be allowed");
+ }
+
+ }
+
+ public void testLoginFailure() throws NamingException {
+ try {
+ getContext("eddie", "panama");
+ fail("supposed to have a login failure here");
+ } catch (javax.naming.AuthenticationException e) {
+ //expected
+ }
+
+ try {
+ getContext("jimmy", "foxylady");
+ fail("supposed to have a login failure here");
+ } catch (javax.naming.AuthenticationException e) {
+ //expected
+ }
+ }
+}
+----
+
+
+= Running
+
+
+
+[source]
+----
+-------------------------------------------------------
+ T E S T S
+-------------------------------------------------------
+Running org.superbiz.injection.secure.MovieTest
+INFO - ********************************************************************************
+INFO - OpenEJB http://tomee.apache.org/
+INFO - Startup: Fri Jul 20 08:42:53 EDT 2012
+INFO - Copyright 1999-2012 (C) Apache OpenEJB Project, All Rights Reserved.
+INFO - Version: 4.1.0
+INFO - Build date: 20120720
+INFO - Build time: 08:33
+INFO - ********************************************************************************
+INFO - openejb.home = /home/boto/dev/ws/openejb_trunk/openejb/examples/testing-security-3
+INFO - openejb.base = /home/boto/dev/ws/openejb_trunk/openejb/examples/testing-security-3
+INFO - Created new singletonService org.apache.openejb.cdi.ThreadSingletonServiceImpl@38ee6681
+INFO - Succeeded in installing singleton service
+INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
+INFO - Cannot find the configuration file [conf/openejb.xml]. Will attempt to create one for the beans deployed.
+INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
+INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
+INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database)
+INFO - Creating TransactionManager(id=Default Transaction Manager)
+INFO - Creating SecurityService(id=Default Security Service)
+INFO - Creating Resource(id=movieDatabase)
+INFO - Beginning load: /home/boto/dev/ws/openejb_trunk/openejb/examples/testing-security-3/target/classes
+INFO - Configuring enterprise application: /home/boto/dev/ws/openejb_trunk/openejb/examples/testing-security-3
+INFO - Auto-deploying ejb Movies: EjbDeployment(deployment-id=Movies)
+INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
+INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful Container)
+INFO - Creating Container(id=Default Stateful Container)
+INFO - Using directory /tmp for stateful session passivation
+INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
+INFO - Auto-creating a container for bean org.superbiz.injection.secure.MovieTest: Container(type=MANAGED, id=Default Managed Container)
+INFO - Creating Container(id=Default Managed Container)
+INFO - Using directory /tmp for stateful session passivation
+INFO - Configuring PersistenceUnit(name=movie-unit)
+INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'.
+INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase)
+INFO - Creating Resource(id=movieDatabaseNonJta)
+INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged'
+INFO - Enterprise application "/home/boto/dev/ws/openejb_trunk/openejb/examples/testing-security-3" loaded.
+INFO - Assembling app: /home/boto/dev/ws/openejb_trunk/openejb/examples/testing-security-3
+SEVERE - JAVA AGENT NOT INSTALLED. The JPA Persistence Provider requested installation of a ClassFileTransformer which requires a JavaAgent. See http://tomee.apache.org/3.0/javaagent.html
+INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 268ms
+INFO - Jndi(name="java:global/testing-security-3/Movies!org.superbiz.injection.secure.Movies")
+INFO - Jndi(name="java:global/testing-security-3/Movies")
+INFO - Existing thread singleton service in SystemInstance() org.apache.openejb.cdi.ThreadSingletonServiceImpl@38ee6681
+INFO - OpenWebBeans Container is starting...
+INFO - Adding OpenWebBeansPlugin : [CdiPlugin]
+INFO - All injection points are validated successfully.
+INFO - OpenWebBeans Container has started, it took 170 ms.
+INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Deployed Application(path=/home/boto/dev/ws/openejb_trunk/openejb/examples/testing-security-3)
+20-Jul-2012 8:42:55 AM null openjpa.Runtime
+INFO: Starting OpenJPA 2.2.0
+20-Jul-2012 8:42:56 AM null openjpa.jdbc.JDBC
+INFO: Using dictionary class "org.apache.openjpa.jdbc.sql.HSQLDictionary" (HSQL Database Engine 2.2.8 ,HSQL Database Engine Driver 2.2.8).
+20-Jul-2012 8:42:57 AM null openjpa.Enhance
+INFO: Creating subclass and redefining methods for "[class org.superbiz.injection.secure.Movie]". This means that your application will be less efficient than it would if you ran the OpenJPA enhancer.
+INFO - Logging in
+INFO - Logging out
+INFO - EJBContainer already initialized. Call ejbContainer.close() to allow reinitialization
+INFO - Logging in
+INFO - Logging out
+INFO - EJBContainer already initialized. Call ejbContainer.close() to allow reinitialization
+Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 6.069 sec
+
+Results :
+
+Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
+----
+
+
Propchange: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-3.adoc
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-4.adoc
URL: http://svn.apache.org/viewvc/tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-4.adoc?rev=1772522&view=auto
==============================================================================
--- tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-4.adoc (added)
+++ tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-4.adoc Sun Dec 4 11:01:40 2016
@@ -0,0 +1,9 @@
+= testing-security-4
+:jbake-date: 2016-09-06
+:jbake-type: page
+:jbake-tomeepdf:
+:jbake-status: published
+
+Example testing-security-4 can be browsed at https://github.com/apache/tomee/tree/master/examples/testing-security-4
+
+No README.md yet, be the first to contribute one!
Propchange: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-4.adoc
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-meta.adoc
URL: http://svn.apache.org/viewvc/tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-meta.adoc?rev=1772522&view=auto
==============================================================================
--- tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-meta.adoc (added)
+++ tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-meta.adoc Sun Dec 4 11:01:40 2016
@@ -0,0 +1,517 @@
+= Testing Security Meta
+:jbake-date: 2016-09-06
+:jbake-type: page
+:jbake-tomeepdf:
+:jbake-status: published
+
+Example testing-security-meta can be browsed at https://github.com/apache/tomee/tree/master/examples/testing-security-meta
+
+
+*Help us document this example! Click the blue pencil icon in the upper right to edit this page.*
+
+== AddPermission
+
+
+[source,java]
+----
+package org.superbiz.injection.secure.api;
+
+import javax.annotation.security.RolesAllowed;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+@Metatype
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+
+public @interface AddPermission {
+ public static interface $ {
+
+ @AddPermission
+ @RolesAllowed({"Employee", "Manager"})
+ public void method();
+ }
+}
+----
+
+
+== DeletePermission
+
+
+[source,java]
+----
+package org.superbiz.injection.secure.api;
+
+import javax.annotation.security.RolesAllowed;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+@Metatype
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+
+public @interface DeletePermission {
+ public static interface $ {
+
+ @DeletePermission
+ @RolesAllowed("Manager")
+ public void method();
+ }
+}
+----
+
+
+== Metatype
+
+
+[source,java]
+----
+package org.superbiz.injection.secure.api;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+@Metatype
+@Target(ElementType.ANNOTATION_TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface Metatype {
+}
+----
+
+
+== MovieUnit
+
+
+[source,java]
+----
+package org.superbiz.injection.secure.api;
+
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+@Metatype
+@Target({ElementType.METHOD, ElementType.FIELD})
+@Retention(RetentionPolicy.RUNTIME)
+
+@PersistenceContext(name = "movie-unit", unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
+public @interface MovieUnit {
+}
+----
+
+
+== ReadPermission
+
+
+[source,java]
+----
+package org.superbiz.injection.secure.api;
+
+import javax.annotation.security.PermitAll;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+@Metatype
+@Target(ElementType.METHOD)
+@Retention(RetentionPolicy.RUNTIME)
+
+public @interface ReadPermission {
+ public static interface $ {
+
+ @ReadPermission
+ @PermitAll
+ @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+ public void method();
+ }
+}
+----
+
+
+== RunAsEmployee
+
+
+[source,java]
+----
+package org.superbiz.injection.secure.api;
+
+import javax.annotation.security.RunAs;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+@Metatype
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+
+@RunAs("Employee")
+public @interface RunAsEmployee {
+}
+----
+
+
+== RunAsManager
+
+
+[source,java]
+----
+package org.superbiz.injection.secure.api;
+
+import javax.annotation.security.RunAs;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+
+@Metatype
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+
+@RunAs("Manager")
+public @interface RunAsManager {
+}
+----
+
+
+== Movie
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import javax.persistence.Entity;
+
+@Entity
+public class Movie {
+
+ private String director;
+ private String title;
+ private int year;
+
+ public Movie() {
+ }
+
+ public Movie(String director, String title, int year) {
+ this.director = director;
+ this.title = title;
+ this.year = year;
+ }
+
+ public String getDirector() {
+ return director;
+ }
+
+ public void setDirector(String director) {
+ this.director = director;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public int getYear() {
+ return year;
+ }
+
+ public void setYear(int year) {
+ this.year = year;
+ }
+
+}
+----
+
+
+== Movies
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+//START SNIPPET: code
+
+import org.superbiz.injection.secure.api.AddPermission;
+import org.superbiz.injection.secure.api.DeletePermission;
+import org.superbiz.injection.secure.api.MovieUnit;
+import org.superbiz.injection.secure.api.ReadPermission;
+
+import javax.ejb.Stateful;
+import javax.persistence.EntityManager;
+import javax.persistence.Query;
+import java.util.List;
+
+@Stateful
+public class Movies {
+
+ @MovieUnit
+ private EntityManager entityManager;
+
+ @AddPermission
+ public void addMovie(Movie movie) throws Exception {
+ entityManager.persist(movie);
+ }
+
+ @DeletePermission
+ public void deleteMovie(Movie movie) throws Exception {
+ entityManager.remove(movie);
+ }
+
+ @ReadPermission
+ public List<Movie> getMovies() throws Exception {
+ Query query = entityManager.createQuery("SELECT m from Movie as m");
+ return query.getResultList();
+ }
+}
+----
+
+
+== persistence.xml
+
+
+[source,xml]
+----
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
+
+ <persistence-unit name="movie-unit">
+ <jta-data-source>movieDatabase</jta-data-source>
+ <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
+ <class>org.superbiz.injection.secure.Movie</class>
+
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+</persistence>
+----
+
+
+== MovieTest
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import junit.framework.TestCase;
+import org.superbiz.injection.secure.api.RunAsEmployee;
+import org.superbiz.injection.secure.api.RunAsManager;
+
+import javax.ejb.EJB;
+import javax.ejb.EJBAccessException;
+import javax.ejb.Stateless;
+import javax.ejb.embeddable.EJBContainer;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.Callable;
+
+//START SNIPPET: code
+
+public class MovieTest extends TestCase {
+
+ @EJB
+ private Movies movies;
+
+ @EJB(beanName = "ManagerBean")
+ private Caller manager;
+
+ @EJB(beanName = "EmployeeBean")
+ private Caller employee;
+
+ protected void setUp() throws Exception {
+ Properties p = new Properties();
+ p.put("movieDatabase", "new://Resource?type=DataSource");
+ p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+ EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
+ }
+
+ public void testAsManager() throws Exception {
+ manager.call(new Callable() {
+ public Object call() throws Exception {
+
+ 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;
+ }
+ });
+ }
+
+ public void testAsEmployee() throws Exception {
+ employee.call(new Callable() {
+ public Object call() throws Exception {
+
+ 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) {
+ try {
+ movies.deleteMovie(movie);
+ fail("Employees should not be allowed to delete");
+ } catch (EJBAccessException e) {
+ // Good, Employees cannot delete things
+ }
+ }
+
+ // The list should still be three movies long
+ assertEquals("Movies.getMovies()", 3, movies.getMovies().size());
+ return null;
+ }
+ });
+ }
+
+ public void testUnauthenticated() throws Exception {
+ try {
+ movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
+ fail("Unauthenticated users should not be able to add movies");
+ } catch (EJBAccessException e) {
+ // Good, guests cannot add things
+ }
+
+ try {
+ movies.deleteMovie(null);
+ fail("Unauthenticated users should not be allowed to delete");
+ } catch (EJBAccessException e) {
+ // Good, Unauthenticated users cannot delete things
+ }
+
+ try {
+ // Read access should be allowed
+
+ List<Movie> list = movies.getMovies();
+ } catch (EJBAccessException e) {
+ fail("Read access should be allowed");
+ }
+ }
+
+ public interface Caller {
+ public <V> V call(Callable<V> callable) throws Exception;
+ }
+
+ /**
+ * This little bit of magic allows our test code to execute in
+ * the desired security scope.
+ */
+
+ @Stateless
+ @RunAsManager
+ public static class ManagerBean implements Caller {
+
+ public <V> V call(Callable<V> callable) throws Exception {
+ return callable.call();
+ }
+ }
+
+ @Stateless
+ @RunAsEmployee
+ public static class EmployeeBean implements Caller {
+
+ public <V> V call(Callable<V> callable) throws Exception {
+ return callable.call();
+ }
+ }
+}
+----
+
+
+= Running
+
+
+
+[source]
+----
+-------------------------------------------------------
+ T E S T S
+-------------------------------------------------------
+Running org.superbiz.injection.secure.MovieTest
+Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06
+http://tomee.apache.org/
+INFO - openejb.home = /Users/dblevins/examples/testing-security-meta
+INFO - openejb.base = /Users/dblevins/examples/testing-security-meta
+INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
+INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
+INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
+INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database)
+INFO - Found EjbModule in classpath: /Users/dblevins/examples/testing-security-meta/target/classes
+INFO - Found EjbModule in classpath: /Users/dblevins/examples/testing-security-meta/target/test-classes
+INFO - Beginning load: /Users/dblevins/examples/testing-security-meta/target/classes
+INFO - Beginning load: /Users/dblevins/examples/testing-security-meta/target/test-classes
+INFO - Configuring enterprise application: /Users/dblevins/examples/testing-security-meta
+INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
+INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful Container)
+INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
+INFO - Auto-creating a container for bean ManagerBean: Container(type=STATELESS, id=Default Stateless Container)
+INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
+INFO - Auto-creating a container for bean org.superbiz.injection.secure.MovieTest: Container(type=MANAGED, id=Default Managed Container)
+INFO - Configuring PersistenceUnit(name=movie-unit)
+INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'.
+INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase)
+INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged'
+INFO - Enterprise application "/Users/dblevins/examples/testing-security-meta" loaded.
+INFO - Assembling app: /Users/dblevins/examples/testing-security-meta
+INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 419ms
+INFO - Jndi(name="java:global/testing-security-meta/Movies!org.superbiz.injection.secure.Movies")
+INFO - Jndi(name="java:global/testing-security-meta/Movies")
+INFO - Jndi(name="java:global/testing-security-meta/ManagerBean!org.superbiz.injection.secure.MovieTest$Caller")
+INFO - Jndi(name="java:global/testing-security-meta/ManagerBean")
+INFO - Jndi(name="java:global/testing-security-meta/EmployeeBean!org.superbiz.injection.secure.MovieTest$Caller")
+INFO - Jndi(name="java:global/testing-security-meta/EmployeeBean")
+INFO - Jndi(name="java:global/EjbModule53489605/org.superbiz.injection.secure.MovieTest!org.superbiz.injection.secure.MovieTest")
+INFO - Jndi(name="java:global/EjbModule53489605/org.superbiz.injection.secure.MovieTest")
+INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Created Ejb(deployment-id=ManagerBean, ejb-name=ManagerBean, container=Default Stateless Container)
+INFO - Created Ejb(deployment-id=EmployeeBean, ejb-name=EmployeeBean, container=Default Stateless Container)
+INFO - Created Ejb(deployment-id=org.superbiz.injection.secure.MovieTest, ejb-name=org.superbiz.injection.secure.MovieTest, container=Default Managed Container)
+INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Started Ejb(deployment-id=ManagerBean, ejb-name=ManagerBean, container=Default Stateless Container)
+INFO - Started Ejb(deployment-id=EmployeeBean, ejb-name=EmployeeBean, container=Default Stateless Container)
+INFO - Started Ejb(deployment-id=org.superbiz.injection.secure.MovieTest, ejb-name=org.superbiz.injection.secure.MovieTest, container=Default Managed Container)
+INFO - Deployed Application(path=/Users/dblevins/examples/testing-security-meta)
+INFO - EJBContainer already initialized. Call ejbContainer.close() to allow reinitialization
+INFO - EJBContainer already initialized. Call ejbContainer.close() to allow reinitialization
+Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.754 sec
+
+Results :
+
+Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
+----
+
+
Propchange: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security-meta.adoc
------------------------------------------------------------------------------
svn:executable = *
Added: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security.adoc
URL: http://svn.apache.org/viewvc/tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security.adoc?rev=1772522&view=auto
==============================================================================
--- tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security.adoc (added)
+++ tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security.adoc Sun Dec 4 11:01:40 2016
@@ -0,0 +1,336 @@
+= Testing Security
+:jbake-date: 2016-09-06
+:jbake-type: page
+:jbake-tomeepdf:
+:jbake-status: published
+
+Example testing-security can be browsed at https://github.com/apache/tomee/tree/master/examples/testing-security
+
+
+*Help us document this example! Click the blue pencil icon in the upper right to edit this page.*
+
+== Movie
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import javax.persistence.Entity;
+
+@Entity
+public class Movie {
+
+ private String director;
+ private String title;
+ private int year;
+
+ public Movie() {
+ }
+
+ public Movie(String director, String title, int year) {
+ this.director = director;
+ this.title = title;
+ this.year = year;
+ }
+
+ public String getDirector() {
+ return director;
+ }
+
+ public void setDirector(String director) {
+ this.director = director;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public int getYear() {
+ return year;
+ }
+
+ public void setYear(int year) {
+ this.year = year;
+ }
+
+}
+----
+
+
+== Movies
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+//START SNIPPET: code
+
+import javax.annotation.security.PermitAll;
+import javax.annotation.security.RolesAllowed;
+import javax.ejb.Stateful;
+import javax.ejb.TransactionAttribute;
+import javax.ejb.TransactionAttributeType;
+import javax.persistence.EntityManager;
+import javax.persistence.PersistenceContext;
+import javax.persistence.PersistenceContextType;
+import javax.persistence.Query;
+import java.util.List;
+
+@Stateful
+public class Movies {
+
+ @PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
+ private EntityManager entityManager;
+
+ @RolesAllowed({"Employee", "Manager"})
+ public void addMovie(Movie movie) throws Exception {
+ entityManager.persist(movie);
+ }
+
+ @RolesAllowed({"Manager"})
+ public void deleteMovie(Movie movie) throws Exception {
+ entityManager.remove(movie);
+ }
+
+ @PermitAll
+ @TransactionAttribute(TransactionAttributeType.SUPPORTS)
+ public List<Movie> getMovies() throws Exception {
+ Query query = entityManager.createQuery("SELECT m from Movie as m");
+ return query.getResultList();
+ }
+}
+----
+
+
+== persistence.xml
+
+
+[source,xml]
+----
+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
+
+ <persistence-unit name="movie-unit">
+ <jta-data-source>movieDatabase</jta-data-source>
+ <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
+ <class>org.superbiz.injection.secure.Movie</class>
+
+ <properties>
+ <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
+ </properties>
+ </persistence-unit>
+</persistence>
+----
+
+
+== MovieTest
+
+
+[source,java]
+----
+package org.superbiz.injection.secure;
+
+import junit.framework.TestCase;
+
+import javax.annotation.security.RunAs;
+import javax.ejb.EJB;
+import javax.ejb.EJBAccessException;
+import javax.ejb.Stateless;
+import javax.ejb.embeddable.EJBContainer;
+import java.util.List;
+import java.util.Properties;
+import java.util.concurrent.Callable;
+
+//START SNIPPET: code
+
+public class MovieTest extends TestCase {
+
+ @EJB
+ private Movies movies;
+
+ @EJB(name = "ManagerBean")
+ private Caller manager;
+
+ @EJB(name = "EmployeeBean")
+ private Caller employee;
+
+ protected void setUp() throws Exception {
+ Properties p = new Properties();
+ p.put("movieDatabase", "new://Resource?type=DataSource");
+ p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
+ p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
+
+ EJBContainer.createEJBContainer(p).getContext().bind("inject", this);
+ }
+
+ public void testAsManager() throws Exception {
+ manager.call(new Callable() {
+ public Object call() throws Exception {
+
+ 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;
+ }
+ });
+ }
+
+ public void testAsEmployee() throws Exception {
+ employee.call(new Callable() {
+ public Object call() throws Exception {
+
+ 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) {
+ try {
+ movies.deleteMovie(movie);
+ fail("Employees should not be allowed to delete");
+ } catch (EJBAccessException e) {
+ // Good, Employees cannot delete things
+ }
+ }
+
+ // The list should still be three movies long
+ assertEquals("Movies.getMovies()", 3, movies.getMovies().size());
+ return null;
+ }
+ });
+ }
+
+ public void testUnauthenticated() throws Exception {
+ try {
+ movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
+ fail("Unauthenticated users should not be able to add movies");
+ } catch (EJBAccessException e) {
+ // Good, guests cannot add things
+ }
+
+ try {
+ movies.deleteMovie(null);
+ fail("Unauthenticated users should not be allowed to delete");
+ } catch (EJBAccessException e) {
+ // Good, Unauthenticated users cannot delete things
+ }
+
+ try {
+ // Read access should be allowed
+
+ List<Movie> list = movies.getMovies();
+ } catch (EJBAccessException e) {
+ fail("Read access should be allowed");
+ }
+ }
+
+
+ public static interface Caller {
+ public <V> V call(Callable<V> callable) throws Exception;
+ }
+
+ /**
+ * This little bit of magic allows our test code to execute in
+ * the desired security scope.
+ */
+
+ @Stateless
+ @RunAs("Manager")
+ public static class ManagerBean implements Caller {
+
+ public <V> V call(Callable<V> callable) throws Exception {
+ return callable.call();
+ }
+ }
+
+ @Stateless
+ @RunAs("Employee")
+ public static class EmployeeBean implements Caller {
+
+ public <V> V call(Callable<V> callable) throws Exception {
+ return callable.call();
+ }
+ }
+}
+----
+
+
+= Running
+
+
+
+[source]
+----
+-------------------------------------------------------
+ T E S T S
+-------------------------------------------------------
+Running org.superbiz.injection.secure.MovieTest
+Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06
+http://tomee.apache.org/
+INFO - openejb.home = /Users/dblevins/examples/testing-security
+INFO - openejb.base = /Users/dblevins/examples/testing-security
+INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
+INFO - Configuring Service(id=Default Security Service, type=SecurityService, provider-id=Default Security Service)
+INFO - Configuring Service(id=Default Transaction Manager, type=TransactionManager, provider-id=Default Transaction Manager)
+INFO - Configuring Service(id=movieDatabase, type=Resource, provider-id=Default JDBC Database)
+INFO - Found EjbModule in classpath: /Users/dblevins/examples/testing-security/target/classes
+INFO - Found EjbModule in classpath: /Users/dblevins/examples/testing-security/target/test-classes
+INFO - Beginning load: /Users/dblevins/examples/testing-security/target/classes
+INFO - Beginning load: /Users/dblevins/examples/testing-security/target/test-classes
+INFO - Configuring enterprise application: /Users/dblevins/examples/testing-security
+INFO - Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
+INFO - Auto-creating a container for bean Movies: Container(type=STATEFUL, id=Default Stateful Container)
+INFO - Configuring Service(id=Default Stateless Container, type=Container, provider-id=Default Stateless Container)
+INFO - Auto-creating a container for bean ManagerBean: Container(type=STATELESS, id=Default Stateless Container)
+INFO - Configuring Service(id=Default Managed Container, type=Container, provider-id=Default Managed Container)
+INFO - Auto-creating a container for bean org.superbiz.injection.secure.MovieTest: Container(type=MANAGED, id=Default Managed Container)
+INFO - Configuring PersistenceUnit(name=movie-unit)
+INFO - Auto-creating a Resource with id 'movieDatabaseNonJta' of type 'DataSource for 'movie-unit'.
+INFO - Configuring Service(id=movieDatabaseNonJta, type=Resource, provider-id=movieDatabase)
+INFO - Adjusting PersistenceUnit movie-unit <non-jta-data-source> to Resource ID 'movieDatabaseNonJta' from 'movieDatabaseUnmanaged'
+INFO - Enterprise application "/Users/dblevins/examples/testing-security" loaded.
+INFO - Assembling app: /Users/dblevins/examples/testing-security
+INFO - PersistenceUnit(name=movie-unit, provider=org.apache.openjpa.persistence.PersistenceProviderImpl) - provider time 405ms
+INFO - Jndi(name="java:global/testing-security/Movies!org.superbiz.injection.secure.Movies")
+INFO - Jndi(name="java:global/testing-security/Movies")
+INFO - Jndi(name="java:global/testing-security/ManagerBean!org.superbiz.injection.secure.MovieTest$Caller")
+INFO - Jndi(name="java:global/testing-security/ManagerBean")
+INFO - Jndi(name="java:global/testing-security/EmployeeBean!org.superbiz.injection.secure.MovieTest$Caller")
+INFO - Jndi(name="java:global/testing-security/EmployeeBean")
+INFO - Jndi(name="java:global/EjbModule26174809/org.superbiz.injection.secure.MovieTest!org.superbiz.injection.secure.MovieTest")
+INFO - Jndi(name="java:global/EjbModule26174809/org.superbiz.injection.secure.MovieTest")
+INFO - Created Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Created Ejb(deployment-id=ManagerBean, ejb-name=ManagerBean, container=Default Stateless Container)
+INFO - Created Ejb(deployment-id=EmployeeBean, ejb-name=EmployeeBean, container=Default Stateless Container)
+INFO - Created Ejb(deployment-id=org.superbiz.injection.secure.MovieTest, ejb-name=org.superbiz.injection.secure.MovieTest, container=Default Managed Container)
+INFO - Started Ejb(deployment-id=Movies, ejb-name=Movies, container=Default Stateful Container)
+INFO - Started Ejb(deployment-id=ManagerBean, ejb-name=ManagerBean, container=Default Stateless Container)
+INFO - Started Ejb(deployment-id=EmployeeBean, ejb-name=EmployeeBean, container=Default Stateless Container)
+INFO - Started Ejb(deployment-id=org.superbiz.injection.secure.MovieTest, ejb-name=org.superbiz.injection.secure.MovieTest, container=Default Managed Container)
+INFO - Deployed Application(path=/Users/dblevins/examples/testing-security)
+INFO - EJBContainer already initialized. Call ejbContainer.close() to allow reinitialization
+INFO - EJBContainer already initialized. Call ejbContainer.close() to allow reinitialization
+Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.574 sec
+
+Results :
+
+Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
+----
+
+
Propchange: tomee/site/trunk/generators/site-tomee-ng/src/main/jbake/content/examples/testing-security.adoc
------------------------------------------------------------------------------
svn:executable = *