You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomee.apache.org by Romain Manni-Bucau <rm...@gmail.com> on 2012/04/19 19:55:56 UTC
Fwd: svn commit: r1328054 - in /openejb/trunk/openejb/container/openejb-junit/src:
main/java/org/apache/openejb/junit/ejbcontainer/ test/java/org/apache/openejb/junit/
test/resources/META-INF/
Hi,
following this commit i would like to discuss about which runner(s) we can
provide and which functionnalities we should provide.
our bind("inject", xxx) hook is fine but a runner is pretty more straight
forward when you write test.
well, let's discuss, i'll be able to hack on it in 2 weeks if some wishes
are done
- Romain
---------- Forwarded message ----------
From: <rm...@apache.org>
Date: 2012/4/19
Subject: svn commit: r1328054 - in
/openejb/trunk/openejb/container/openejb-junit/src:
main/java/org/apache/openejb/junit/ejbcontainer/
test/java/org/apache/openejb/junit/ test/resources/META-INF/
To: commits@openejb.apache.org
Author: rmannibucau
Date: Thu Apr 19 17:50:59 2012
New Revision: 1328054
URL: http://svn.apache.org/viewvc?rev=1328054&view=rev
Log:
adding EJBContainerRunner. It is closed to the OpenEjbRunner but it closes
the container without issue which is very important in a big build. Next
steps can be to see how to merge both runners.
Added:
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/EJBContainerRunner.java
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Properties.java
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Property.java
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/PropertyFile.java
openejb/trunk/openejb/container/openejb-junit/src/test/java/org/apache/openejb/junit/TestWithCdiBean.java
openejb/trunk/openejb/container/openejb-junit/src/test/resources/META-INF/beans.xml
Added:
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/EJBContainerRunner.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/EJBContainerRunner.java?rev=1328054&view=auto
==============================================================================
---
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/EJBContainerRunner.java
(added)
+++
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/EJBContainerRunner.java
Thu Apr 19 17:50:59 2012
@@ -0,0 +1,180 @@
+package org.apache.openejb.junit.ejbcontainer;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.logging.ConsoleHandler;
+import javax.ejb.embeddable.EJBContainer;
+import org.apache.openejb.OpenEJBException;
+import org.apache.openejb.OpenEjbContainer;
+import org.apache.openejb.util.JuliLogStreamFactory;
+import org.apache.openejb.util.SingleLineFormatter;
+import org.junit.Rule;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.BlockJUnit4ClassRunner;
+import org.junit.runners.model.FrameworkMethod;
+import org.junit.runners.model.InitializationError;
+import org.junit.runners.model.Statement;
+
+public class EJBContainerRunner extends BlockJUnit4ClassRunner {
+ static { // logging conf
+ System.setProperty("java.util.logging.manager",
JuliLogStreamFactory.OpenEJBLogManager.class.getName());
+ java.util.logging.Logger logger =
java.util.logging.Logger.getLogger("net");
+ logger.setUseParentHandlers(false);
+ logger.addHandler(new ConsoleHandler());
+ logger.getHandlers()[0].setFormatter(new SingleLineFormatter());
+ }
+
+ private EJBContainer container;
+
+ public EJBContainerRunner(final Class<?> klass) throws
InitializationError {
+ super(klass);
+ }
+
+ @Override
+ protected Statement withBeforeClasses(Statement statement) {
+ final Statement superStatement =
super.withBeforeClasses(statement);
+ return new StartingStatement(superStatement);
+ }
+
+ @Override
+ protected Statement withAfterClasses(Statement statement) {
+ final Statement superStatement = super.withAfterClasses(statement);
+ return new ShutingDownStatement(superStatement);
+ }
+
+ @Override
+ protected List<FrameworkMethod> computeTestMethods() {
+ final List<FrameworkMethod> methods = super.computeTestMethods();
+ Collections.shuffle(methods); // real tests should manage shuffle
ordering
+ return methods;
+ }
+
+ @Override
+ protected List<TestRule> getTestRules(Object target) {
+ final List<TestRule> rules = new ArrayList<TestRule>();
+ rules.add(new InjectRule(target));
+ rules.addAll(getTestClass().getAnnotatedFieldValues(target,
Rule.class, TestRule.class));
+ return rules;
+ }
+
+ private static abstract class DecoratingStatement extends Statement {
+ protected Statement decorated;
+
+ public DecoratingStatement(final Statement statement) {
+ decorated = statement;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ before();
+ try {
+ decorated.evaluate();
+ } finally {
+ after();
+ }
+ }
+
+ protected void before() throws Exception {
+ // no-op
+ }
+
+ protected void after() throws Exception {
+ // no-op
+ }
+ }
+
+ private class StartingStatement extends DecoratingStatement {
+ public StartingStatement(final Statement statement) {
+ super(statement);
+ }
+
+ @Override
+ protected void before() throws Exception {
+ final Class<?> clazz = getTestClass().getJavaClass();
+ final Map<String, String> properties = new HashMap<String,
String>();
+
properties.put(OpenEjbContainer.Provider.OPENEJB_ADDITIONNAL_CALLERS_KEY,
clazz.getName());
+
+ final PropertyFile propertyFile =
clazz.getAnnotation(PropertyFile.class);
+ if (propertyFile != null) {
+ final String path = propertyFile.value();
+ if (!path.isEmpty()) {
+ InputStream is =
clazz.getClassLoader().getResourceAsStream(path);
+ if (is == null) {
+ final File file = new File(path);
+ if (file.exists()) {
+ is = new FileInputStream(file);
+ } else {
+ throw new OpenEJBException("properties
resource '" + path + "' not found");
+ }
+ }
+
+ final java.util.Properties fileProps = new
java.util.Properties();
+ fileProps.load(is);
+ for (Map.Entry<Object, Object> entry :
fileProps.entrySet()) {
+ properties.put(entry.getKey().toString(),
entry.getValue().toString());
+ }
+ }
+ }
+
+ final Properties annotationConfig =
clazz.getAnnotation(Properties.class);
+ if (annotationConfig != null) {
+ for (Property property : annotationConfig.value()) {
+ properties.put(property.key(), property.value());
+ }
+ }
+
+ container = EJBContainer.createEJBContainer(properties);
+ }
+ }
+
+ private class ShutingDownStatement extends DecoratingStatement {
+ public ShutingDownStatement(final Statement statement) {
+ super(statement);
+ }
+
+ @Override
+ protected void after() throws Exception {
+ if (container != null) {
+ container.close();
+ container = null;
+ }
+ }
+ }
+
+ private class InjectRule implements TestRule {
+ private Object test;
+
+ public InjectRule(final Object target) {
+ this.test = target;
+ }
+
+ @Override
+ public Statement apply(final Statement base, final Description
description) {
+ return new InjectStatement(base, test);
+ }
+ }
+
+ private class InjectStatement extends Statement {
+ private Object test;
+ private Statement statement;
+
+ public InjectStatement(final Statement stat, final Object o) {
+ statement = stat;
+ test = o;
+ }
+
+ @Override
+ public void evaluate() throws Throwable {
+ container.getContext().bind("inject", test);
+ statement.evaluate();
+ }
+ }
+}
+
Added:
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Properties.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Properties.java?rev=1328054&view=auto
==============================================================================
---
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Properties.java
(added)
+++
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Properties.java
Thu Apr 19 17:50:59 2012
@@ -0,0 +1,23 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openejb.junit.ejbcontainer;
+
+public @interface Properties {
+ Property[] value() default {};
+
+}
Added:
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Property.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Property.java?rev=1328054&view=auto
==============================================================================
---
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Property.java
(added)
+++
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/Property.java
Thu Apr 19 17:50:59 2012
@@ -0,0 +1,23 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openejb.junit.ejbcontainer;
+
+public @interface Property {
+ String key();
+ String value();
+}
Added:
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/PropertyFile.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/PropertyFile.java?rev=1328054&view=auto
==============================================================================
---
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/PropertyFile.java
(added)
+++
openejb/trunk/openejb/container/openejb-junit/src/main/java/org/apache/openejb/junit/ejbcontainer/PropertyFile.java
Thu Apr 19 17:50:59 2012
@@ -0,0 +1,22 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openejb.junit.ejbcontainer;
+
+public @interface PropertyFile {
+ String value() default "";
+}
Added:
openejb/trunk/openejb/container/openejb-junit/src/test/java/org/apache/openejb/junit/TestWithCdiBean.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-junit/src/test/java/org/apache/openejb/junit/TestWithCdiBean.java?rev=1328054&view=auto
==============================================================================
---
openejb/trunk/openejb/container/openejb-junit/src/test/java/org/apache/openejb/junit/TestWithCdiBean.java
(added)
+++
openejb/trunk/openejb/container/openejb-junit/src/test/java/org/apache/openejb/junit/TestWithCdiBean.java
Thu Apr 19 17:50:59 2012
@@ -0,0 +1,35 @@
+package org.apache.openejb.junit;
+
+import javax.ejb.Stateless;
+import javax.inject.Inject;
+import org.apache.openejb.config.DeploymentFilterable;
+import org.apache.openejb.junit.ejbcontainer.EJBContainerRunner;
+import org.apache.openejb.junit.ejbcontainer.Properties;
+import org.apache.openejb.junit.ejbcontainer.Property;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import static org.junit.Assert.assertNotNull;
+
+@Properties({ // just a small conf to go faster
+ @Property(key = DeploymentFilterable.CLASSPATH_EXCLUDE, value =
"jar:.*")
+})
+@RunWith(EJBContainerRunner.class)
+public class TestWithCdiBean {
+ @Inject
+ private CdiBean cdi;
+
+ @Inject
+ private EjbBean ejb;
+
+ @Test
+ public void checkInjections() {
+ assertNotNull(cdi);
+ assertNotNull(ejb);
+ }
+
+ public static class CdiBean {}
+
+ @Stateless
+ public static class EjbBean {}
+}
Added:
openejb/trunk/openejb/container/openejb-junit/src/test/resources/META-INF/beans.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-junit/src/test/resources/META-INF/beans.xml?rev=1328054&view=auto
==============================================================================
(empty)