You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by rm...@apache.org on 2017/10/10 23:01:57 UTC
svn commit: r1811778 - in
/openwebbeans/meecrowave/trunk/meecrowave-junit/src:
main/java/org/apache/meecrowave/junit/ test/java/org/apache/meecrowave/junit/
Author: rmannibucau
Date: Tue Oct 10 23:01:57 2017
New Revision: 1811778
URL: http://svn.apache.org/viewvc?rev=1811778&view=rev
Log:
refactoring meecrowave scope handling to ensure it is usable with both kind of rules
Added:
openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/InjectRule.java
openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java
Modified:
openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java
Added: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/InjectRule.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/InjectRule.java?rev=1811778&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/InjectRule.java (added)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/InjectRule.java Tue Oct 10 23:01:57 2017
@@ -0,0 +1,55 @@
+/*
+ * 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.meecrowave.junit;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.CDI;
+
+import org.apache.meecrowave.Meecrowave;
+import org.apache.meecrowave.testing.Injector;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public class InjectRule implements TestRule {
+ private final Object instance;
+
+ public InjectRule(final Object instance) {
+ this.instance = instance;
+ }
+
+ @Override
+ public Statement apply(final Statement base, final Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ CreationalContext<?> creationalContext = null;
+ try {
+ creationalContext = Injector.inject(instance);
+ Injector.injectConfig(CDI.current().select(Meecrowave.Builder.class).get(), instance);
+ base.evaluate();
+ } finally {
+ if (creationalContext != null) {
+ creationalContext.release();
+ }
+ }
+ }
+ };
+ }
+}
Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java?rev=1811778&r1=1811777&r2=1811778&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java (original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/MonoMeecrowave.java Tue Oct 10 23:01:57 2017
@@ -18,21 +18,18 @@
*/
package org.apache.meecrowave.junit;
+import java.util.List;
+
+import javax.enterprise.context.spi.CreationalContext;
+
import org.apache.meecrowave.Meecrowave;
import org.apache.meecrowave.testing.Injector;
import org.apache.meecrowave.testing.MonoBase;
-import org.apache.webbeans.config.WebBeansContext;
-import org.apache.webbeans.spi.ContextsService;
import org.junit.rules.MethodRule;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.Statement;
-import javax.enterprise.context.RequestScoped;
-import javax.enterprise.context.SessionScoped;
-import javax.enterprise.context.spi.CreationalContext;
-import java.util.List;
-
/**
* A MeecrowaveRule starting a single container.
* Very awesome for forkCount=1, reuseForks=true
@@ -56,18 +53,13 @@ public class MonoMeecrowave {
@Override
public void evaluate() throws Throwable {
BASE.startIfNeeded();
- ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
configInjection(test.getClass(), test);
final CreationalContext<?> creationalContext = Injector.inject(test);
try {
- contextsService.startContext(RequestScoped.class, null);
- contextsService.startContext(SessionScoped.class, null);
base.evaluate();
} finally {
creationalContext.release();
- contextsService.endContext(SessionScoped.class, null);
- contextsService.endContext(RequestScoped.class, null);
}
}
Added: openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java?rev=1811778&view=auto
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java (added)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/main/java/org/apache/meecrowave/junit/ScopeRule.java Tue Oct 10 23:01:57 2017
@@ -0,0 +1,63 @@
+/*
+ * 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.meecrowave.junit;
+
+import static java.util.Arrays.asList;
+
+import java.lang.annotation.Annotation;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Stream;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+
+import org.apache.webbeans.config.WebBeansContext;
+import org.apache.webbeans.spi.ContextsService;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+public class ScopeRule implements TestRule {
+ private final List<Class<? extends Annotation>> scopes;
+
+ public ScopeRule(final Class<? extends Annotation>... scopes) {
+ this.scopes = new ArrayList<>(asList(scopes));
+ }
+
+ @Override
+ public Statement apply(final Statement base, final Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ final ContextsService contextsService = WebBeansContext.currentInstance().getContextsService();
+ try {
+ scopes.forEach(s -> contextsService.startContext(s, null));
+ base.evaluate();
+ } finally {
+ Collections.reverse(scopes);
+ scopes.forEach(s -> contextsService.endContext(s, null));
+ Collections.reverse(scopes);
+ }
+ }
+ };
+ }
+}
Modified: openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java
URL: http://svn.apache.org/viewvc/openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java?rev=1811778&r1=1811777&r2=1811778&view=diff
==============================================================================
--- openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java (original)
+++ openwebbeans/meecrowave/trunk/meecrowave-junit/src/test/java/org/apache/meecrowave/junit/MonoMeecrowaveRuleTest.java Tue Oct 10 23:01:57 2017
@@ -18,33 +18,36 @@
*/
package org.apache.meecrowave.junit;
-import org.apache.meecrowave.Meecrowave;
-import org.apache.meecrowave.io.IO;
-import org.apache.meecrowave.testing.ConfigurationInject;
-import org.app.MyAppClass;
-import org.app.MyReqClass;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
-import javax.inject.Inject;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.fail;
+import javax.enterprise.context.RequestScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.inject.Inject;
+
+import org.apache.meecrowave.io.IO;
+import org.app.MyAppClass;
+import org.app.MyReqClass;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TestRule;
-@RunWith(MonoMeecrowave.Runner.class)
public class MonoMeecrowaveRuleTest {
- /* or
@ClassRule
public static final MonoMeecrowave.Rule RULE = new MonoMeecrowave.Rule();
- */
- private static int count = 0;
+ @Rule
+ public final TestRule scopeRule = new ScopeRule(RequestScoped.class, SessionScoped.class);
- @ConfigurationInject
- private Meecrowave.Builder config;
+ @Rule
+ public final TestRule injectRule = new InjectRule(this);
+
+ private static int count = 0;
private @Inject MyAppClass appClass;
private @Inject MyReqClass reqClass;
@@ -52,14 +55,14 @@ public class MonoMeecrowaveRuleTest {
@Test
public void test() throws IOException {
- assertEquals("simple", slurp(new URL("http://localhost:" + config.getHttpPort() + "/api/test")));
+ assertEquals("simple", slurp(new URL("http://localhost:" + RULE.getConfiguration().getHttpPort() + "/api/test")));
testScopes();
}
@Test
public void anotherTest() throws IOException {
- assertEquals("simple", slurp(new URL("http://localhost:" + config.getHttpPort() + "/api/test")));
+ assertEquals("simple", slurp(new URL("http://localhost:" + RULE.getConfiguration().getHttpPort() + "/api/test")));
testScopes();
}