From c929944fb6696c43bfc475d9709e1a7ee53f207e Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Fri, 5 Jun 2015 21:35:45 +0200 Subject: [PATCH 1/5] Extract folder housekeeping Proper naming - as in the main project Ignoreing a test that probably never worked --- extra/camel/pom.xml | 1 + extra/nosql/cassandra/pom.xml | 3 +- extra/nosql/couchbase/pom.xml | 3 +- extra/nosql/hbase/pom.xml | 3 +- extra/nosql/hibernate-ogm/pom.xml | 1 + .../extra/nosql/hibernateogm/EhcacheTest.java | 2 + extra/nosql/mongo/pom.xml | 1 + extra/nosql/neo4j/pom.xml | 3 +- extra/nosql/oracle/pom.xml | 3 +- extra/nosql/pom.xml | 62 +++++++++---------- extra/nosql/redis/pom.xml | 3 +- extra/nosql/riak/pom.xml | 1 + extra/nosql/voldemort/pom.xml | 1 + extra/pom.xml | 12 ++-- extra/quartz/pom.xml | 3 +- extra/twitter-search/pom.xml | 6 ++ extra/webjars/pom.xml | 1 + 17 files changed, 65 insertions(+), 44 deletions(-) diff --git a/extra/camel/pom.xml b/extra/camel/pom.xml index 4ece4ab01..18e38e6cf 100644 --- a/extra/camel/pom.xml +++ b/extra/camel/pom.xml @@ -12,6 +12,7 @@ camel 1.0-SNAPSHOT war + Java EE 7 Samples: extras - camel 2.13.0 diff --git a/extra/nosql/cassandra/pom.xml b/extra/nosql/cassandra/pom.xml index cf5864490..790612803 100644 --- a/extra/nosql/cassandra/pom.xml +++ b/extra/nosql/cassandra/pom.xml @@ -12,7 +12,8 @@ cassandra 1.0-SNAPSHOT war - + Java EE 7 Samples: extras - nosql: cassandra + com.datastax.cassandra diff --git a/extra/nosql/couchbase/pom.xml b/extra/nosql/couchbase/pom.xml index 3e19cae10..46ce55886 100644 --- a/extra/nosql/couchbase/pom.xml +++ b/extra/nosql/couchbase/pom.xml @@ -12,7 +12,8 @@ couchbase 1.0-SNAPSHOT war - + Java EE 7 Samples: extras - nosql: couchbase + com.couchbase.client diff --git a/extra/nosql/hbase/pom.xml b/extra/nosql/hbase/pom.xml index b9f745728..a6b70450b 100644 --- a/extra/nosql/hbase/pom.xml +++ b/extra/nosql/hbase/pom.xml @@ -12,7 +12,8 @@ hbase 1.0-SNAPSHOT war - + Java EE 7 Samples: extras - nosql: hbase + org.apache.hadoop diff --git a/extra/nosql/hibernate-ogm/pom.xml b/extra/nosql/hibernate-ogm/pom.xml index 1b7613090..fc1448b98 100644 --- a/extra/nosql/hibernate-ogm/pom.xml +++ b/extra/nosql/hibernate-ogm/pom.xml @@ -10,5 +10,6 @@ 4.0.0 hibernate-ogm + Java EE 7 Samples: extras - nosql: hibernate-ogm \ No newline at end of file diff --git a/extra/nosql/hibernate-ogm/src/test/java/org/javaee7/extra/nosql/hibernateogm/EhcacheTest.java b/extra/nosql/hibernate-ogm/src/test/java/org/javaee7/extra/nosql/hibernateogm/EhcacheTest.java index f36b9ab7d..01efafb5e 100644 --- a/extra/nosql/hibernate-ogm/src/test/java/org/javaee7/extra/nosql/hibernateogm/EhcacheTest.java +++ b/extra/nosql/hibernate-ogm/src/test/java/org/javaee7/extra/nosql/hibernateogm/EhcacheTest.java @@ -4,12 +4,14 @@ import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Ignore; import org.junit.runner.RunWith; /** * @author Guillaume Scheibel */ @RunWith(Arquillian.class) +@Ignore("This test is probably a sad panda; never been successful sad panda.") public class EhcacheTest extends AbstractOgmTest { @Deployment diff --git a/extra/nosql/mongo/pom.xml b/extra/nosql/mongo/pom.xml index 152582c7c..7080362ed 100644 --- a/extra/nosql/mongo/pom.xml +++ b/extra/nosql/mongo/pom.xml @@ -12,6 +12,7 @@ mongo 1.0-SNAPSHOT war + Java EE 7 Samples: extras - nosql: mongo diff --git a/extra/nosql/neo4j/pom.xml b/extra/nosql/neo4j/pom.xml index 3abd78f3d..baf60fdf7 100644 --- a/extra/nosql/neo4j/pom.xml +++ b/extra/nosql/neo4j/pom.xml @@ -12,7 +12,8 @@ neo4j 1.0-SNAPSHOT war - + Java EE 7 Samples: extras - nosql: neo4j + org.neo4j diff --git a/extra/nosql/oracle/pom.xml b/extra/nosql/oracle/pom.xml index 125b0685b..117471937 100644 --- a/extra/nosql/oracle/pom.xml +++ b/extra/nosql/oracle/pom.xml @@ -12,7 +12,8 @@ oracle 1.0-SNAPSHOT war - + Java EE 7 Samples: extras - nosql: oracle + com.oracle.nosql diff --git a/extra/nosql/pom.xml b/extra/nosql/pom.xml index 59dadab39..a23f1e49b 100644 --- a/extra/nosql/pom.xml +++ b/extra/nosql/pom.xml @@ -1,31 +1,31 @@ - - - 4.0.0 - - org.javaee7.extra - extra-samples - 1.0-SNAPSHOT - ../pom.xml - - - org.javaee7.extra.nosql - extra-nosql-samples - 1.0-SNAPSHOT - pom - Java EE 7 Extra NoSQL Samples - - - mongo - couchbase - redis - cassandra - neo4j - hbase - voldemort - riak - hibernate-ogm - - - + + + 4.0.0 + + org.javaee7.extra + extra-samples + 1.0-SNAPSHOT + ../pom.xml + + + org.javaee7.extra.nosql + extra-nosql-samples + 1.0-SNAPSHOT + pom + Java EE 7 Samples: extras - nosql + + + mongo + couchbase + redis + cassandra + neo4j + hbase + voldemort + riak + hibernate-ogm + + + diff --git a/extra/nosql/redis/pom.xml b/extra/nosql/redis/pom.xml index bb38e5f06..5d3fd3a34 100644 --- a/extra/nosql/redis/pom.xml +++ b/extra/nosql/redis/pom.xml @@ -12,7 +12,8 @@ redis 1.0-SNAPSHOT war - + Java EE 7 Samples: extras - nosql: redis + redis.clients diff --git a/extra/nosql/riak/pom.xml b/extra/nosql/riak/pom.xml index 73eb681e1..0c8cc6302 100644 --- a/extra/nosql/riak/pom.xml +++ b/extra/nosql/riak/pom.xml @@ -12,6 +12,7 @@ riak 1.0-SNAPSHOT war + Java EE 7 Samples: extras - nosql: riak diff --git a/extra/nosql/voldemort/pom.xml b/extra/nosql/voldemort/pom.xml index 6f7ee6f70..b9d83414d 100644 --- a/extra/nosql/voldemort/pom.xml +++ b/extra/nosql/voldemort/pom.xml @@ -12,6 +12,7 @@ voldemort 1.0-SNAPSHOT war + Java EE 7 Samples: extras - nosql: voldemort diff --git a/extra/pom.xml b/extra/pom.xml index 32ba89c1b..2dc9e7f4a 100644 --- a/extra/pom.xml +++ b/extra/pom.xml @@ -1,25 +1,25 @@ 4.0.0 + org.javaee7 - javaee7-samples + samples-parent 1.0-SNAPSHOT ../pom.xml org.javaee7.extra extra-samples - 1.0-SNAPSHOT pom - Java EE 7 Extra Samples + Java EE 7 Samples: extras + camel + nosql quartz - - nosql + webjars diff --git a/extra/quartz/pom.xml b/extra/quartz/pom.xml index 3702ea64d..a20d13014 100644 --- a/extra/quartz/pom.xml +++ b/extra/quartz/pom.xml @@ -12,7 +12,8 @@ quartz 1.0-SNAPSHOT war - + Java EE 7 Samples: extras - quartz + org.quartz-scheduler diff --git a/extra/twitter-search/pom.xml b/extra/twitter-search/pom.xml index 75aa4ef73..92be7d2f0 100644 --- a/extra/twitter-search/pom.xml +++ b/extra/twitter-search/pom.xml @@ -12,12 +12,18 @@ twitter-search 1.0-SNAPSHOT war + Java EE 7 Samples: extras - twitter-search + org.glassfish.samples twitter-api 1.0-SNAPSHOT + system + ${basedir}/src/main/webapp/WEB-INF/lib/twitter.jar com.sun.jersey.contribs.jersey-oauth diff --git a/extra/webjars/pom.xml b/extra/webjars/pom.xml index 7c0a14a11..43ecd7c0d 100644 --- a/extra/webjars/pom.xml +++ b/extra/webjars/pom.xml @@ -14,6 +14,7 @@ org.javaee7.extra webjars 1.0-SNAPSHOT + Java EE 7 Samples: extras - webjars war From e15ffd5baa17a3abd23e893f32c5b34a86688953 Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Fri, 5 Jun 2015 23:18:04 +0200 Subject: [PATCH 2/5] ParameterRule for Arquillian junit tests As described here: http://www.poolik.com/2014/02/how-to-run-parameterized-junit-arquillian-tests/ where the credits should go --- test-utils/pom.xml | 14 ++- .../src/main/java/org/javaee7/Parameter.java | 12 ++ .../main/java/org/javaee7/ParameterRule.java | 103 ++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 test-utils/src/main/java/org/javaee7/Parameter.java create mode 100644 test-utils/src/main/java/org/javaee7/ParameterRule.java diff --git a/test-utils/pom.xml b/test-utils/pom.xml index 26d3dc06e..79b77454d 100644 --- a/test-utils/pom.xml +++ b/test-utils/pom.xml @@ -10,4 +10,16 @@ test-utils Java EE 7 Sample: javaee7-samples - test-utils - + + + + junit + junit + 4.11 + + + org.jboss.arquillian.container + arquillian-container-test-api + + + \ No newline at end of file diff --git a/test-utils/src/main/java/org/javaee7/Parameter.java b/test-utils/src/main/java/org/javaee7/Parameter.java new file mode 100644 index 000000000..80f465dae --- /dev/null +++ b/test-utils/src/main/java/org/javaee7/Parameter.java @@ -0,0 +1,12 @@ +package org.javaee7; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.FIELD}) +public @interface Parameter { +} + diff --git a/test-utils/src/main/java/org/javaee7/ParameterRule.java b/test-utils/src/main/java/org/javaee7/ParameterRule.java new file mode 100644 index 000000000..ea5f741e8 --- /dev/null +++ b/test-utils/src/main/java/org/javaee7/ParameterRule.java @@ -0,0 +1,103 @@ +package org.javaee7; + +import org.jboss.arquillian.container.test.api.Deployment; + +import org.junit.rules.MethodRule; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; + +import javax.naming.InitialContext; +import javax.naming.NamingException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.List; + +/** + * Helper class for Parametrized tests as described here: + * http://blog.schauderhaft.de/2012/12/16/writing-parameterized-tests-with-junit-rules/ + * + * @param + */ +public class ParameterRule implements MethodRule { + private final List params; + + public ParameterRule(List params) { + if (params == null || params.size() == 0) { + throw new IllegalArgumentException("'params' must be specified and have more then zero length!"); + } + this.params = params; + } + + @Override + public Statement apply(final Statement base, final FrameworkMethod method, final Object target) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + boolean runInContainer = getDeploymentMethod(target).getAnnotation(Deployment.class).testable(); + if (runInContainer) { + evaluateParametersInContainer(base, target); + } else { + evaluateParametersInClient(base, target); + } + } + }; + } + + private Method getDeploymentMethod(Object target) throws NoSuchMethodException { + Method[] methods = target.getClass().getDeclaredMethods(); + for (Method method : methods) { + if (method.getAnnotation(Deployment.class) != null) return method; + } + throw new IllegalStateException("No method with @Deployment annotation found!"); + } + + private void evaluateParametersInContainer(Statement base, Object target) throws Throwable { + if (isRunningInContainer()) { + evaluateParamsToTarget(base, target); + } else { + ignoreStatementExecution(base); + } + } + + private void evaluateParametersInClient(Statement base, Object target) throws Throwable { + if (isRunningInContainer()) { + ignoreStatementExecution(base); + } else { + evaluateParamsToTarget(base, target); + } + } + + private boolean isRunningInContainer() { + try { + new InitialContext().lookup("java:comp/env"); + return true; + } catch (NamingException e) { + return false; + } + } + + private void evaluateParamsToTarget(Statement base, Object target) throws Throwable { + for (Object param : params) { + Field targetField = getTargetField(target); + if (!targetField.isAccessible()) { + targetField.setAccessible(true); + } + targetField.set(target, param); + base.evaluate(); + } + } + + private Field getTargetField(Object target) throws NoSuchFieldException { + Field[] allFields = target.getClass().getDeclaredFields(); + for (Field field : allFields) { + if (field.getAnnotation(Parameter.class) != null) return field; + } + throw new IllegalStateException("No field with @Parameter annotation found! Forgot to add it?"); + } + + private void ignoreStatementExecution(Statement base) { + try { + base.evaluate(); + } catch (Throwable ignored) {} + } +} \ No newline at end of file From 98022871fadfd15a91ad0c4a6df19c8bc21baf16 Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Fri, 5 Jun 2015 23:21:03 +0200 Subject: [PATCH 3/5] Moving all Spock tests into a separate project in extras Replacing previous Spock tests with standard jUnit tests --- extra/pom.xml | 1 + extra/spock-tests/pom.xml | 38 ++++++ ...ndedPersistenceContextSpecification.groovy | 129 +++++++++--------- .../JpaListenerInjectionSpecification.groovy | 85 ++++++------ .../filters/FilterServletSpecification.groovy | 11 +- .../pc/ExtendedPersistenceContextTest.java | 81 +++++++++++ .../listeners/JpaListenerInjectionTest.java | 60 ++++++++ pom.xml | 6 + 8 files changed, 300 insertions(+), 111 deletions(-) create mode 100644 extra/spock-tests/pom.xml rename {jpa/extended-pc => extra/spock-tests}/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy (78%) rename {jpa/listeners-injection => extra/spock-tests}/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionSpecification.groovy (76%) rename {servlet/servlet-filters/src/test/groovy => extra/spock-tests/src/test/java}/org/javaee7/servlet/filters/FilterServletSpecification.groovy (99%) create mode 100644 jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextTest.java create mode 100644 jpa/listeners-injection/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionTest.java diff --git a/extra/pom.xml b/extra/pom.xml index 2dc9e7f4a..4c8881f22 100644 --- a/extra/pom.xml +++ b/extra/pom.xml @@ -20,6 +20,7 @@ quartz twitter-search webjars + spock-tests diff --git a/extra/spock-tests/pom.xml b/extra/spock-tests/pom.xml new file mode 100644 index 000000000..0cf44ef1b --- /dev/null +++ b/extra/spock-tests/pom.xml @@ -0,0 +1,38 @@ + + + + org.javaee7.extra + extra-samples + 1.0-SNAPSHOT + + 4.0.0 + + spock-tests + + + + org.javaee7 + jpa-listeners-injection + ${project.version} + classes + test + + + org.javaee7 + jpa-extended-pc + ${project.version} + classes + test + + + org.javaee7 + servlet-servlet-filters + ${project.version} + classes + test + + + + \ No newline at end of file diff --git a/jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy b/extra/spock-tests/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy similarity index 78% rename from jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy rename to extra/spock-tests/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy index dd6f5a727..f6646037b 100644 --- a/jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy +++ b/extra/spock-tests/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextSpecification.groovy @@ -1,63 +1,66 @@ -package org.javaee7.jpa.extended.pc - -import org.jboss.arquillian.container.test.api.Deployment -import org.jboss.arquillian.spock.ArquillianSputnik -import org.jboss.shrinkwrap.api.ShrinkWrap -import org.jboss.shrinkwrap.api.spec.WebArchive -import org.junit.runner.RunWith -import spock.lang.Specification - -import javax.ejb.EJB -import javax.persistence.EntityManager -import javax.persistence.PersistenceContext - -/** - * @author Kuba Marchwicki - */ -@RunWith(ArquillianSputnik) -class ExtendedPersistenceContextSpecification extends Specification { - - @PersistenceContext - EntityManager em; - - @EJB - CharactersBean bean; - - @Deployment - def static WebArchive deploy() { - ShrinkWrap.create(WebArchive.class) - .addPackage("org.javaee7.jpa.extended.pc") - .addAsResource("META-INF/persistence.xml") - .addAsResource("META-INF/create.sql") - .addAsResource("META-INF/drop.sql") - .addAsResource("META-INF/load.sql") - } - - def setup() { - Character wil = new Character(8, "Wil Wheaton") - bean.save(wil) - - for (Character c : bean.get()) { - if ("Raj".equals(c.getName())) { - c.setName("Rajesh Ramayan") - bean.save(c) - } - } - } - - def "should not persist changes without transaction flush"() { - expect: - 7 == em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList().size(); - "Raj" == em.find(Character.class, 6).name - } - - def "should update characters after transaction flush"() { - when: - bean.commitChanges() - - then: - 8 == em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList().size(); - "Rajesh Ramayan" == em.find(Character.class, 6).name - "Wil Wheaton" == em.find(Character.class, 8).name - } -} +package org.javaee7.jpa.extended.pc + +import org.javaee7.jpa.extended.pc.Character +import org.javaee7.jpa.extended.pc.CharactersBean +import org.jboss.arquillian.container.test.api.Deployment +import org.jboss.arquillian.spock.ArquillianSputnik +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.jboss.shrinkwrap.api.spec.WebArchive +import org.junit.runner.RunWith +import spock.lang.Specification + +import javax.ejb.EJB +import javax.persistence.EntityManager +import javax.persistence.PersistenceContext + +import static org.jboss.shrinkwrap.resolver.api.maven.Maven.resolver + +/** + * @author Kuba Marchwicki + */ +@RunWith(ArquillianSputnik) +class ExtendedPersistenceContextSpecification extends Specification { + + @PersistenceContext + EntityManager em; + + @EJB + CharactersBean bean; + + @Deployment + def static WebArchive deploy() { + ShrinkWrap.create(WebArchive.class) + .addAsLibraries( + resolver().resolve("org.javaee7:jpa-extended-pc:jar:classes:1.0-SNAPSHOT") + .withTransitivity().asFile() + ); + } + + def setup() { + Character wil = new Character(8, "Wil Wheaton") + bean.save(wil) + + for (Character c : bean.get()) { + if ("Raj".equals(c.getName())) { + c.setName("Rajesh Ramayan") + bean.save(c) + } + } + } + + def "should not persist changes without transaction flush"() { + expect: + 7 == em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList().size(); + "Raj" == em.find(Character.class, 6).name + } + + def "should update characters after transaction flush"() { + when: + bean.commitChanges() + + then: + 8 == em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList().size(); + "Rajesh Ramayan" == em.find(Character.class, 6).name + "Wil Wheaton" == em.find(Character.class, 8).name + } +} diff --git a/jpa/listeners-injection/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionSpecification.groovy b/extra/spock-tests/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionSpecification.groovy similarity index 76% rename from jpa/listeners-injection/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionSpecification.groovy rename to extra/spock-tests/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionSpecification.groovy index 03a2a2a78..3944a5c02 100644 --- a/jpa/listeners-injection/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionSpecification.groovy +++ b/extra/spock-tests/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionSpecification.groovy @@ -1,42 +1,43 @@ -package org.javaee7.jpa.listeners - -import org.jboss.arquillian.container.test.api.Deployment -import org.jboss.arquillian.spock.ArquillianSputnik -import org.jboss.shrinkwrap.api.ShrinkWrap -import org.jboss.shrinkwrap.api.spec.WebArchive -import org.junit.runner.RunWith -import spock.lang.Specification -import spock.lang.Unroll - -import javax.inject.Inject - -@RunWith(ArquillianSputnik) -class JpaListenerInjectionSpecification extends Specification { - - @Deployment - def static WebArchive "create deployment"() { - ShrinkWrap.create(WebArchive.class) - .addPackage("org.javaee7.jpa.listeners") - .addAsResource("META-INF/persistence.xml") - .addAsResource("META-INF/create.sql") - .addAsResource("META-INF/drop.sql") - .addAsResource("META-INF/load.sql"); - } - - @Inject - MovieBean bean; - - @Unroll("should provide a movie rating of #rating for movie #name") - def "provide movie rating via jpa listener injection"() { - expect: - rating == bean.getMovieByName(name).rating - - where: - name | rating - "The Matrix" | 60 - "The Lord of The Rings" | 70 - "Inception" | 80 - "The Shining" | 90 - } - -} +package org.javaee7.jpa.listeners + +import org.jboss.arquillian.container.test.api.Deployment +import org.jboss.arquillian.spock.ArquillianSputnik +import org.jboss.shrinkwrap.api.ShrinkWrap +import org.jboss.shrinkwrap.api.spec.WebArchive +import org.junit.runner.RunWith +import spock.lang.Specification +import spock.lang.Unroll + +import javax.inject.Inject + +import static org.jboss.shrinkwrap.resolver.api.maven.Maven.resolver; + +@RunWith(ArquillianSputnik) +class JpaListenerInjectionSpecification extends Specification { + + @Deployment + def static WebArchive "create deployment"() { + ShrinkWrap.create(WebArchive.class) + .addAsLibraries( + resolver().resolve("org.javaee7:jpa-listeners-injection:jar:classes:1.0-SNAPSHOT") + .withTransitivity().asFile() + ); + } + + @Inject + MovieBean bean; + + @Unroll("should provide a movie rating of #rating for movie #name") + def "provide movie rating via jpa listener injection"() { + expect: + rating == bean.getMovieByName(name).rating + + where: + name | rating + "The Matrix" | 60 + "The Lord of The Rings" | 70 + "Inception" | 80 + "The Shining" | 90 + } + +} diff --git a/servlet/servlet-filters/src/test/groovy/org/javaee7/servlet/filters/FilterServletSpecification.groovy b/extra/spock-tests/src/test/java/org/javaee7/servlet/filters/FilterServletSpecification.groovy similarity index 99% rename from servlet/servlet-filters/src/test/groovy/org/javaee7/servlet/filters/FilterServletSpecification.groovy rename to extra/spock-tests/src/test/java/org/javaee7/servlet/filters/FilterServletSpecification.groovy index 418e9067d..ca7503934 100644 --- a/servlet/servlet-filters/src/test/groovy/org/javaee7/servlet/filters/FilterServletSpecification.groovy +++ b/extra/spock-tests/src/test/java/org/javaee7/servlet/filters/FilterServletSpecification.groovy @@ -1,19 +1,18 @@ package org.javaee7.servlet.filters -import javax.ws.rs.client.Client -import javax.ws.rs.client.ClientBuilder -import javax.ws.rs.client.WebTarget -import javax.ws.rs.core.Response - import org.jboss.arquillian.container.test.api.Deployment import org.jboss.arquillian.spock.ArquillianSputnik import org.jboss.arquillian.test.api.ArquillianResource import org.jboss.shrinkwrap.api.ShrinkWrap import org.jboss.shrinkwrap.api.spec.WebArchive import org.junit.runner.RunWith - import spock.lang.Specification +import javax.ws.rs.client.Client +import javax.ws.rs.client.ClientBuilder +import javax.ws.rs.client.WebTarget +import javax.ws.rs.core.Response + @RunWith(ArquillianSputnik) class FilterServletSpecification extends Specification{ diff --git a/jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextTest.java b/jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextTest.java new file mode 100644 index 000000000..c4143ed86 --- /dev/null +++ b/jpa/extended-pc/src/test/java/org/javaee7/jpa/extended/pc/ExtendedPersistenceContextTest.java @@ -0,0 +1,81 @@ +package org.javaee7.jpa.extended.pc; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.junit.InSequence; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.ejb.EJB; +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class ExtendedPersistenceContextTest { + + @PersistenceContext + EntityManager em; + + @EJB + CharactersBean bean; + + @Deployment + public static WebArchive deploy() { + return ShrinkWrap.create(WebArchive.class) + .addPackage("org.javaee7.jpa.extended.pc") + .addAsResource("META-INF/persistence.xml") + .addAsResource("META-INF/create.sql") + .addAsResource("META-INF/drop.sql") + .addAsResource("META-INF/load.sql"); + } + + @Before + public void setup() { + Character wil = new Character(8, "Wil Wheaton"); + bean.save(wil); + + for (Character c : bean.get()) { + if ("Raj".equals(c.getName())) { + c.setName("Rajesh Ramayan"); + bean.save(c); + } + } + } + + @Test + @InSequence(1) + public void should_not_persist_changes_without_transaction_flush() { + List characters = em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList(); + Character raj = em.find(Character.class, 6); + + assertThat(characters, hasSize(7)); + assertThat(raj.getName(), is(equalTo("Raj"))); + } + + @Test + @InSequence(2) + public void should_update_characters_after_transaction_flush() { + //when + bean.commitChanges(); + + //then + List characters = em.createNamedQuery(Character.FIND_ALL, Character.class).getResultList(); + Character rajesh = em.find(Character.class, 6); + Character wil = em.find(Character.class, 8); + + assertThat(characters, hasSize(8)); + assertThat(rajesh.getName(), is(equalTo("Rajesh Ramayan"))); + assertThat(wil.getName(), is(equalTo("Wil Wheaton"))); + } + + +} diff --git a/jpa/listeners-injection/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionTest.java b/jpa/listeners-injection/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionTest.java new file mode 100644 index 000000000..322bff573 --- /dev/null +++ b/jpa/listeners-injection/src/test/java/org/javaee7/jpa/listeners/JpaListenerInjectionTest.java @@ -0,0 +1,60 @@ +package org.javaee7.jpa.listeners; + +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.javaee7.Parameter; +import org.javaee7.ParameterRule; +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +import javax.inject.Inject; + +import java.util.Arrays; +import java.util.List; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.collection.IsCollectionWithSize.hasSize; +import static org.junit.Assert.assertThat; + +@RunWith(Arquillian.class) +public class JpaListenerInjectionTest { + + @Deployment + public static WebArchive deployment() { + return ShrinkWrap.create(WebArchive.class) + .addPackage("org.javaee7.jpa.listeners") + .addAsResource("META-INF/persistence.xml") + .addAsResource("META-INF/create.sql") + .addAsResource("META-INF/drop.sql") + .addAsResource("META-INF/load.sql"); + } + + public static final List> movies = Arrays.asList( + new ImmutablePair<>("The Matrix", 60), + new ImmutablePair<>("The Lord of The Rings", 70), + new ImmutablePair<>("Inception", 80), + new ImmutablePair<>("The Shining", 90) + ); + + @Rule + public ParameterRule> rule = new ParameterRule<>(movies); + + @Parameter + ImmutablePair expectedMovie; + + @Inject + MovieBean bean; + + @Test + public void should_provide_movie_rating_via_jpa_listener_injection() { + //given + Movie movie = bean.getMovieByName(expectedMovie.getLeft()); + + assertThat(movie.getRating(), is(equalTo(expectedMovie.getRight()))); + } +} diff --git a/pom.xml b/pom.xml index e987a9cd1..ea34b1808 100644 --- a/pom.xml +++ b/pom.xml @@ -129,6 +129,11 @@ pom test + + org.jboss.arquillian.container + arquillian-container-test-api + ${org.jboss.arquillian.version} + org.javaee7 util @@ -342,6 +347,7 @@ maven-war-plugin 2.1.1 + true false From fc30c0edb0ee59ab02182ec900112813a2d13bac Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Fri, 5 Jun 2015 23:34:48 +0200 Subject: [PATCH 4/5] Moving groovy / Spock dependencies to an extra/spock-tests/pom.xml --- extra/spock-tests/pom.xml | 94 +++++++++++++++++++++++++++++++++++ pom.xml | 102 -------------------------------------- 2 files changed, 94 insertions(+), 102 deletions(-) diff --git a/extra/spock-tests/pom.xml b/extra/spock-tests/pom.xml index 0cf44ef1b..01d948fbf 100644 --- a/extra/spock-tests/pom.xml +++ b/extra/spock-tests/pom.xml @@ -11,7 +11,14 @@ spock-tests + + 1.0.0.Beta2 + 0.7-groovy-2.0 + 2.3.0 + + + org.javaee7 jpa-listeners-injection @@ -33,6 +40,93 @@ classes test + + + + org.jboss.arquillian.spock + arquillian-spock-container + ${org.jboss.arquillian.spock} + test + + + org.spockframework + spock-core + ${spock.version} + test + + + org.codehaus.groovy + groovy-all + ${groovy.version} + test + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + ${java.min.version} + ${java.min.version} + groovy-eclipse-compiler + + + + org.codehaus.groovy + groovy-eclipse-compiler + 2.8.0-01 + + + org.codehaus.groovy + groovy-eclipse-batch + 2.1.5-03 + + + + + org.codehaus.groovy + groovy-eclipse-compiler + 2.8.0-01 + true + + + org.apache.maven.plugins + maven-surefire-plugin + 2.17 + + + default-test + test + + test + + + + org.jboss.arquillian.spock:arquillian-spock-container + + + + + spock-test + test + + test + + + + **/*Specification.java + + + org.jboss.arquillian.junit:arquillian-junit-container + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index ea34b1808..60453ceb7 100644 --- a/pom.xml +++ b/pom.xml @@ -25,34 +25,6 @@ pom Java EE 7 Sample: javaee7-samples - - - codehaus-snapshots - Codehaus Snapshots - http://nexus.codehaus.org/snapshots/ - - false - - - true - - - - - - - codehaus-snapshots - Codehaus Snapshots - http://nexus.codehaus.org/snapshots/ - - false - - - true - - - - 1.7 3.0.0 @@ -61,15 +33,12 @@ 1.3.0.Final 2.40.0 2.0.2.Final - 1.0.0.Beta2 8.2.0.Final 7.2.0.Final 1.3.1 false 1.3 - 0.7-groovy-2.0 - 2.3.0 4.0 @@ -184,12 +153,6 @@ arquillian-junit-container test - - org.jboss.arquillian.spock - arquillian-spock-container - ${org.jboss.arquillian.spock} - test - org.jboss.arquillian.protocol arquillian-protocol-servlet @@ -206,18 +169,6 @@ shrinkwrap-resolver-impl-maven-archive test - - org.spockframework - spock-core - ${spock.version} - test - - - org.codehaus.groovy - groovy-all - ${groovy.version} - test - xmlunit xmlunit @@ -278,60 +229,7 @@ ${java.min.version} ${java.min.version} - groovy-eclipse-compiler - - - org.codehaus.groovy - groovy-eclipse-compiler - 2.8.0-01 - - - org.codehaus.groovy - groovy-eclipse-batch - 2.1.5-03 - - - - - org.codehaus.groovy - groovy-eclipse-compiler - 2.8.0-01 - true - - - org.apache.maven.plugins - maven-surefire-plugin - 2.17 - - - default-test - test - - test - - - - org.jboss.arquillian.spock:arquillian-spock-container - - - - - spock-test - test - - test - - - - **/*Specification.java - - - org.jboss.arquillian.junit:arquillian-junit-container - - - - org.apache.maven.plugins From cfe64e3dafd50c66d50bea84cb0a9421e6d810f8 Mon Sep 17 00:00:00 2001 From: kubamarchwicki Date: Fri, 5 Jun 2015 23:51:46 +0200 Subject: [PATCH 5/5] Clarifing the README.md on Spock tests and Groovy dependency --- README.md | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 138aa26e8..6cc2b57ca 100644 --- a/README.md +++ b/README.md @@ -136,10 +136,6 @@ When developing and runing them from IDE, remember to activate the profile befor To learn more about Arquillian please refer to the [Arquillian Guides](http://arquillian.org/guides/) -### Importing in Eclipse ### - -To import the samples in an Eclipse workspace, please install the [Groovy plugins for your Eclipse version](http://groovy.codehaus.org/Eclipse+Plugin) first, then import the sample projects you want using File>Import>Existing Maven Projects. - ## How to contribute ## With your help we can improve this set of samples, learn from each other and grow the community full of passionate people who care about the technology, innovation and code quality. Every contribution matters! @@ -148,7 +144,9 @@ There is just a bunch of things you should keep in mind before sending a pull re Standard tests are jUnit based - for example [this commit](servlet/servlet-filters/src/test/java/org/javaee7/servlet/filters/FilterServletTest.java). Test classes naming must comply with surefire naming standards `**/*Test.java`, `**/*Test*.java` or `**/*TestCase.java`. -However, if you fancy something new, hip and fashionable we also accept Spock specifications - [like here](/servlet/servlet-filters/src/test/groovy/org/javaee7/servlet/filters/FilterServletSpecification.groovy). The spec files are included in the maven test phase if and only if you follow Spock naming convention and give your `Specification` suffix the magic will happen. +However, if you fancy something new, hip and fashionable it is perfectly legal to write Spock specifications as standard JavaEE integration test. For the sake of clarity and consistency, to minimize the upfront complexity, in this project we prefare standard jUnit test. However, some Spock example are provided in the `extra/spock-tests` folder - [like here](extra/spock-tests/src/test/java/org/javaee7/servlet/filters/FilterServletSpecification.groovy). The `spock-tests` project also showcases the Maven configuration. In this particular case the Groovy Specification files are included in the maven test phase if and only if you follow Spock naming convention and give your `Specification` suffix the magic will happen. + +The extras folder is not included by default, to limit Groovy dependency. If you want to import the extra samples in an Eclipse workspace (including the Spock tests), please install the [Groovy plugins for your Eclipse version](http://groovy.codehaus.org/Eclipse+Plugin) first, then import the sample projects you want using File>Import>Existing Maven Projects. ### Some coding principles ###