From c40e4dd0bc95a95fd97f0f2c4673f2a12432c48b Mon Sep 17 00:00:00 2001 From: "John D. Ament" Date: Sun, 3 Apr 2016 21:03:47 -0400 Subject: [PATCH] Backport Microscoped to leverage Java 6 only. Added tests and ported existing tests to work outside of TomEE. Updated the domain tests to not rely on /etc/hosts entries but instead trick out localhost and 127.0.0.1. --- microscoped-core/pom.xml | 12 ---- .../org/tomitribe/microscoped/core/Scope.java | 23 +++++-- .../microscoped/core/ScopeContext.java | 29 ++++++--- microscoped-domain/pom.xml | 18 ++---- .../domain/DomainScopedExtension.java | 3 +- .../java/org/supertribe/domain/AppPath.java | 24 +++++++ .../supertribe/domain/DomainScopedTest.java | 62 +++++++++---------- microscoped-header/pom.xml | 12 ---- .../header/HeaderScopedExtension.java | 2 +- .../header/HeaderScopedFilter.java | 2 - .../java/org/supertribe/header/AppPath.java | 24 +++++++ .../supertribe/header/HeaderScopedTest.java | 58 ++++++++--------- microscoped-method/pom.xml | 26 ++------ .../method/MethodScopedExtension.java | 2 +- .../java/org/supertribe/MethodScopedTest.java | 28 +++++---- microscoped-timer/pom.xml | 12 ---- .../timer/TimerScopedExtension.java | 2 +- .../org/supertribe/timer/ColorService.java | 5 -- .../test/java/org/supertribe/timer/Count.java | 12 ---- .../test/java/org/supertribe/timer/Log.java | 10 +-- .../org/supertribe/timer/TimerScopedTest.java | 38 +++--------- pom.xml | 58 ++++++++++++++++- 22 files changed, 235 insertions(+), 227 deletions(-) create mode 100644 microscoped-domain/src/test/java/org/supertribe/domain/AppPath.java create mode 100644 microscoped-header/src/test/java/org/supertribe/header/AppPath.java diff --git a/microscoped-core/pom.xml b/microscoped-core/pom.xml index 8056603..4550ea7 100644 --- a/microscoped-core/pom.xml +++ b/microscoped-core/pom.xml @@ -36,18 +36,6 @@ ${openejb.javaee.api} provided - - org.apache.openejb - arquillian-tomee-embedded - ${tomee.version} - test - - - org.apache.openejb - tomee-jaxrs - ${tomee.version} - test - junit junit diff --git a/microscoped-core/src/main/java/org/tomitribe/microscoped/core/Scope.java b/microscoped-core/src/main/java/org/tomitribe/microscoped/core/Scope.java index eebca4c..c1513f8 100644 --- a/microscoped-core/src/main/java/org/tomitribe/microscoped/core/Scope.java +++ b/microscoped-core/src/main/java/org/tomitribe/microscoped/core/Scope.java @@ -21,10 +21,11 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +@SuppressWarnings("unchecked") class Scope { - private final Instance NOTHING = new Instance<>(null, null, null); + private final Instance NOTHING = new Instance(null, null, null); - private final Map, Instance> instances = new ConcurrentHashMap<>(); + private final Map, Instance> instances = new ConcurrentHashMap, Instance>(); private final Key key; @@ -43,7 +44,12 @@ public Key getKey() { * @return existing or newly created bean instance, never null */ public T get(final Contextual contextual, final CreationalContext creationalContext) { - return (T) instances.computeIfAbsent(contextual, c -> new Instance<>(contextual, creationalContext)).get(); + Instance instance = instances.get(contextual); + if(instance == null) { + instances.put(contextual, new Instance(contextual, creationalContext)); + instance = instances.get(contextual); + } + return instance.get(); } /** @@ -53,7 +59,12 @@ public T get(final Contextual contextual, final CreationalContext crea * @return existing the bean instance or null */ public T get(final Contextual contextual) { - return (T) instances.getOrDefault(contextual, NOTHING).get(); + Instance instance = instances.get(contextual); + if(instance == null){ + return (T) NOTHING.get(); + } else { + return instance.get(); + } } /** @@ -61,7 +72,9 @@ public T get(final Contextual contextual) { */ public void destroy() { // TODO We really should ensure no more instances can be added during or after this - instances.values().stream().forEach(Instance::destroy); + for(Instance instance : instances.values()) { + instance.destroy(); + } instances.clear(); } diff --git a/microscoped-core/src/main/java/org/tomitribe/microscoped/core/ScopeContext.java b/microscoped-core/src/main/java/org/tomitribe/microscoped/core/ScopeContext.java index 3d63cae..ea23391 100644 --- a/microscoped-core/src/main/java/org/tomitribe/microscoped/core/ScopeContext.java +++ b/microscoped-core/src/main/java/org/tomitribe/microscoped/core/ScopeContext.java @@ -26,7 +26,7 @@ public class ScopeContext implements Context { - private final Map scopes = new ConcurrentHashMap<>(); + private final Map scopes = new ConcurrentHashMap(); /** * The use of an AtomicReference inside the ThreadLocal is a clever way to be able to @@ -38,7 +38,7 @@ public class ScopeContext implements Context { * * Similar to if Map.Entry had a 'setValue(Object)' method */ - private final ThreadLocal>> active = ThreadLocal.withInitial(this::inactive); + private final ThreadLocal>> active = new ThreadLocal>>(); private final Class scopeAnnotation; @@ -55,7 +55,11 @@ public ScopeContext(final Class scopeAnnotation) { * @return returns the previous "center of the world" so it can be reassociated when this scope exits */ public Key enter(Key key) { - final Scope scope = scopes.computeIfAbsent(key, k -> new Scope(key)); + Scope scope = scopes.get(key); + if(scope == null) { + scope = new Scope(key); + scopes.put(key, scope); + } return scope().getAndSet(scope).getKey(); } @@ -85,14 +89,16 @@ public void exit(Key previous) { * @param key the key of the scope */ public void destroy(Key key) { - scopes.computeIfPresent(key, (k, scope) -> { + Scope scope = scopes.get(key); + if(scope != null) { scope.destroy(); - return null; - }); + } } public void destroyAll() { - scopes.values().stream().forEach(Scope::destroy); + for(Scope scope : scopes.values()) { + scope.destroy(); + } scopes.clear(); } @@ -103,10 +109,15 @@ public T get(Contextual contextual, CreationalContext creationalContex @Override public T get(Contextual contextual) { - return scope().get().get(contextual); + AtomicReference> scope = scope(); + Scope keyScope = scope.get(); + return keyScope.get(contextual); } private AtomicReference> scope() { + if(active.get() == null) { + active.set(inactive()); + } return active.get(); } @@ -138,6 +149,6 @@ public void destroy() { }; private AtomicReference> inactive() { - return new AtomicReference<>(inactiveScope); + return new AtomicReference>(inactiveScope); } } diff --git a/microscoped-domain/pom.xml b/microscoped-domain/pom.xml index fc2bb2b..f0d48ec 100644 --- a/microscoped-domain/pom.xml +++ b/microscoped-domain/pom.xml @@ -41,24 +41,18 @@ ${openejb.javaee.api} provided - - org.apache.openejb - arquillian-tomee-embedded - ${tomee.version} - test - - - org.apache.openejb - tomee-jaxrs - ${tomee.version} - test - junit junit 4.12 test + + org.apache.httpcomponents + httpclient + 4.4.1 + test + diff --git a/microscoped-domain/src/main/java/org/tomitribe/microscoped/domain/DomainScopedExtension.java b/microscoped-domain/src/main/java/org/tomitribe/microscoped/domain/DomainScopedExtension.java index 49b0078..564655f 100644 --- a/microscoped-domain/src/main/java/org/tomitribe/microscoped/domain/DomainScopedExtension.java +++ b/microscoped-domain/src/main/java/org/tomitribe/microscoped/domain/DomainScopedExtension.java @@ -23,7 +23,6 @@ import javax.enterprise.inject.spi.BeforeBeanDiscovery; import javax.enterprise.inject.spi.Extension; - public class DomainScopedExtension implements Extension { public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) { @@ -33,7 +32,7 @@ public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) { public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) { - abd.addContext(new ScopeContext<>(DomainScoped.class)); + abd.addContext(new ScopeContext(DomainScoped.class)); } } \ No newline at end of file diff --git a/microscoped-domain/src/test/java/org/supertribe/domain/AppPath.java b/microscoped-domain/src/test/java/org/supertribe/domain/AppPath.java new file mode 100644 index 0000000..3ca825c --- /dev/null +++ b/microscoped-domain/src/test/java/org/supertribe/domain/AppPath.java @@ -0,0 +1,24 @@ +/* + * 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.supertribe.domain; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/") +public class AppPath extends Application { +} diff --git a/microscoped-domain/src/test/java/org/supertribe/domain/DomainScopedTest.java b/microscoped-domain/src/test/java/org/supertribe/domain/DomainScopedTest.java index 0359169..8d6f97a 100644 --- a/microscoped-domain/src/test/java/org/supertribe/domain/DomainScopedTest.java +++ b/microscoped-domain/src/test/java/org/supertribe/domain/DomainScopedTest.java @@ -16,16 +16,19 @@ */ package org.supertribe.domain; -import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset; -import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.tomitribe.microscoped.core.ScopeContext; @@ -33,21 +36,11 @@ import javax.enterprise.inject.spi.Extension; import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; /** * - * This test requires some DNS hacking. - * Edit your /etc/hosts to contain the following entries: - * - *
- * $ cat /etc/hosts | egrep 'orange|red'
- * 127.0.0.1	orange
- * 127.0.0.1	red
- * 
- * - * Then comment out the @Ignore + * This test relies on 127.0.0.1 being different from localhost */ @RunWith(Arquillian.class) public class DomainScopedTest extends Assert { @@ -59,37 +52,40 @@ public static WebArchive createDeployment() { .addPackage(ScopeContext.class.getPackage()) .addPackage(DomainScopedExtension.class.getPackage()) .addPackage(SimpleService.class.getPackage()) + .addPackages(true, "org.apache.http") .addAsWebInfResource(new ClassLoaderAsset("META-INF/beans.xml"), "classes/META-INF/beans.xml") - .addAsWebInfResource(new StringAsset(DomainScopedExtension.class.getName()), - "classes/META-INF/services/" + Extension.class.getName() - ); + .addAsServiceProviderAndClasses(Extension.class, DomainScopedExtension.class); } @ArquillianResource private URL webappUrl; @Test - @Ignore("Comment this out to run the test") public void test() throws Exception { - assertDomain("orange", 1); - assertDomain("orange", 2); - assertDomain("red", 1); - assertDomain("red", 2); - assertDomain("red", 3); - assertDomain("orange", 3); - assertDomain("orange", 4); - assertDomain("red", 4); + String localhost = "localhost"; + String realHost = "127.0.0.1"; + assertDomain(localhost, 1); + assertDomain(localhost, 2); + assertDomain(realHost, 1); + assertDomain(realHost, 2); + assertDomain(realHost, 3); + assertDomain(localhost, 3); + assertDomain(localhost, 4); + assertDomain(realHost, 4); } - private void assertDomain(String orange, int i) throws URISyntaxException { - final URI uri = setDomain(orange, webappUrl.toURI()); - final WebClient webClient = WebClient.create(uri); + private void assertDomain(String domain, int i) throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + CloseableHttpResponse response = httpclient.execute(new HttpGet(createUri(domain))); + HttpEntity entity = response.getEntity(); + String s = EntityUtils.toString(entity); + + final String expected = String.format("%s domain, %s invocations", domain, i); - final String result = webClient.path("domain").get(String.class); - assertEquals(orange + " domain, " + i + " invocations", result); + assertEquals(expected, s); } - private static URI setDomain(String domain, URI uri) throws URISyntaxException { - return new URI(uri.getScheme(), uri.getUserInfo(), domain, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); + private URI createUri(String domain) throws Exception{ + return new URL(webappUrl.getProtocol(), domain, webappUrl.getPort(), webappUrl.getFile()+"domain").toURI(); } } diff --git a/microscoped-header/pom.xml b/microscoped-header/pom.xml index 847fed1..f1114f9 100644 --- a/microscoped-header/pom.xml +++ b/microscoped-header/pom.xml @@ -41,18 +41,6 @@ ${openejb.javaee.api} provided - - org.apache.openejb - arquillian-tomee-embedded - ${tomee.version} - test - - - org.apache.openejb - tomee-jaxrs - ${tomee.version} - test - junit junit diff --git a/microscoped-header/src/main/java/org/tomitribe/microscoped/header/HeaderScopedExtension.java b/microscoped-header/src/main/java/org/tomitribe/microscoped/header/HeaderScopedExtension.java index 6cbcaaa..02d056e 100644 --- a/microscoped-header/src/main/java/org/tomitribe/microscoped/header/HeaderScopedExtension.java +++ b/microscoped-header/src/main/java/org/tomitribe/microscoped/header/HeaderScopedExtension.java @@ -33,7 +33,7 @@ public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) { public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) { - abd.addContext(new ScopeContext<>(HeaderScoped.class)); + abd.addContext(new ScopeContext(HeaderScoped.class)); } } \ No newline at end of file diff --git a/microscoped-header/src/main/java/org/tomitribe/microscoped/header/HeaderScopedFilter.java b/microscoped-header/src/main/java/org/tomitribe/microscoped/header/HeaderScopedFilter.java index aa04787..c66af5a 100644 --- a/microscoped-header/src/main/java/org/tomitribe/microscoped/header/HeaderScopedFilter.java +++ b/microscoped-header/src/main/java/org/tomitribe/microscoped/header/HeaderScopedFilter.java @@ -45,7 +45,6 @@ public class HeaderScopedFilter implements javax.servlet.Filter { public void doFilter(final ServletRequest servletRequest, final ServletResponse servletResponse, final FilterChain filterChain) throws IOException, ServletException { final String value = getKey((HttpServletRequest) servletRequest); - final ScopeContext context = (ScopeContext) beanManager.getContext(HeaderScoped.class); // set the name and value in the thread for others to see @@ -63,7 +62,6 @@ public void doFilter(final ServletRequest servletRequest, final ServletResponse private String getKey(final HttpServletRequest request) { final String header = request.getHeader(config.getHeaderName()); - if (header != null) { return header; } else { diff --git a/microscoped-header/src/test/java/org/supertribe/header/AppPath.java b/microscoped-header/src/test/java/org/supertribe/header/AppPath.java new file mode 100644 index 0000000..80e0431 --- /dev/null +++ b/microscoped-header/src/test/java/org/supertribe/header/AppPath.java @@ -0,0 +1,24 @@ +/* + * 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.supertribe.header; + +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@ApplicationPath("/") +public class AppPath extends Application{ +} diff --git a/microscoped-header/src/test/java/org/supertribe/header/HeaderScopedTest.java b/microscoped-header/src/test/java/org/supertribe/header/HeaderScopedTest.java index 6a3e971..a776363 100644 --- a/microscoped-header/src/test/java/org/supertribe/header/HeaderScopedTest.java +++ b/microscoped-header/src/test/java/org/supertribe/header/HeaderScopedTest.java @@ -16,13 +16,17 @@ */ package org.supertribe.header; -import org.apache.cxf.jaxrs.client.WebClient; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset; -import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.WebArchive; import org.junit.Assert; import org.junit.Test; @@ -31,8 +35,6 @@ import org.tomitribe.microscoped.header.HeaderScopedExtension; import javax.enterprise.inject.spi.Extension; -import java.net.URI; -import java.net.URISyntaxException; import java.net.URL; @RunWith(Arquillian.class) @@ -45,13 +47,9 @@ public static WebArchive createDeployment() { .addPackage(ScopeContext.class.getPackage()) .addPackage(HeaderScopedExtension.class.getPackage()) .addPackage(SimpleService.class.getPackage()) + .addPackages(true, "org.apache.http") .addAsWebInfResource(new ClassLoaderAsset("META-INF/beans.xml"), "classes/META-INF/beans.xml") - .addAsWebInfResource(new StringAsset(HeaderScopedExtension.class.getName()), - "classes/META-INF/services/" + Extension.class.getName() - ) - .addAsWebInfResource(new StringAsset(HeaderScopedConfigExtension.class.getName()), - "classes/META-INF/services/" + Extension.class.getName() - ) + .addAsServiceProviderAndClasses(Extension.class, HeaderScopedConfigExtension.class, HeaderScopedExtension.class) ; } @@ -60,33 +58,27 @@ public static WebArchive createDeployment() { @Test public void test() throws Exception { - assertHeader("1.0", 1); - assertHeader("1.0", 2); - - assertHeader("1.1", 1); - assertHeader("1.1", 2); - assertHeader("1.1", 3); - - assertHeader("1.0", 3); - assertHeader("1.0", 4); - - assertHeader("1.1", 4); + assertVersion("1.0", 1); + assertVersion("1.0", 2); + assertVersion("1.1", 1); + assertVersion("1.1", 2); + assertVersion("1.1", 3); + assertVersion("1.1", 4); + assertVersion("1.0", 3); + assertVersion("1.0", 4); } - private void assertHeader(String version, int i) throws URISyntaxException { - final URI uri = webappUrl.toURI(); - - final String result = WebClient.create(uri) - .header("version", version) - .path("domain") - .get(String.class); + private void assertVersion(String version, int i) throws Exception { + CloseableHttpClient httpclient = HttpClients.createDefault(); + final String uri = webappUrl.toURI() + "domain"; + HttpGet get = new HttpGet(uri); + get.addHeader("version", version); + CloseableHttpResponse response = httpclient.execute(get); + HttpEntity entity = response.getEntity(); + String s = EntityUtils.toString(entity); final String expected = String.format("version=%s , %s invocations", version, i); - assertEquals(expected, result); - } - - private static URI setDomain(String domain, URI uri) throws URISyntaxException { - return new URI(uri.getScheme(), uri.getUserInfo(), domain, uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment()); + assertEquals(expected, s); } } diff --git a/microscoped-method/pom.xml b/microscoped-method/pom.xml index 1f5d533..0c40d62 100644 --- a/microscoped-method/pom.xml +++ b/microscoped-method/pom.xml @@ -28,37 +28,23 @@ microscoped-method jar - org.tomitribe microscoped-core 0.4-SNAPSHOT - - org.apache.openejb - javaee-api - ${openejb.javaee.api} - provided - - - org.apache.openejb - arquillian-tomee-embedded - ${tomee.version} - test - - - org.apache.openejb - tomee-jaxrs - ${tomee.version} - test - junit junit 4.12 test + + org.apache.openejb + javaee-api + ${openejb.javaee.api} + provided + - diff --git a/microscoped-method/src/main/java/org/tomitribe/microscoped/method/MethodScopedExtension.java b/microscoped-method/src/main/java/org/tomitribe/microscoped/method/MethodScopedExtension.java index a28f004..f793a96 100644 --- a/microscoped-method/src/main/java/org/tomitribe/microscoped/method/MethodScopedExtension.java +++ b/microscoped-method/src/main/java/org/tomitribe/microscoped/method/MethodScopedExtension.java @@ -33,7 +33,7 @@ public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) { public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) { - abd.addContext(new ScopeContext<>(MethodScoped.class)); + abd.addContext(new ScopeContext(MethodScoped.class)); } } \ No newline at end of file diff --git a/microscoped-method/src/test/java/org/supertribe/MethodScopedTest.java b/microscoped-method/src/test/java/org/supertribe/MethodScopedTest.java index d5187fc..bbd7160 100644 --- a/microscoped-method/src/test/java/org/supertribe/MethodScopedTest.java +++ b/microscoped-method/src/test/java/org/supertribe/MethodScopedTest.java @@ -16,10 +16,9 @@ */ package org.supertribe; -import org.apache.cxf.jaxrs.client.WebClient; +import javax.inject.Inject; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset; import org.jboss.shrinkwrap.api.asset.StringAsset; @@ -31,8 +30,6 @@ import org.tomitribe.microscoped.method.MethodScopedExtension; import javax.enterprise.inject.spi.Extension; -import java.net.URISyntaxException; -import java.net.URL; @RunWith(Arquillian.class) public class MethodScopedTest extends Assert { @@ -50,12 +47,11 @@ public static WebArchive createDeployment() { ); } - @ArquillianResource - private URL webappUrl; + @Inject + private ColorService colorService; @Test public void test() throws Exception { - assertRequest("color/red", "red, 1 invocations"); assertRequest("color/red", "red, 2 invocations"); assertRequest("color/red", "red, 3 invocations"); @@ -65,14 +61,20 @@ public void test() throws Exception { assertRequest("color/red", "red, 4 invocations"); assertRequest("color/blue", "blue, 1 invocations"); assertRequest("color/blue", "blue, 2 invocations"); - } - private void assertRequest(String path, String expected) throws URISyntaxException { - final WebClient webClient = WebClient.create(webappUrl.toURI()); - final String s = webClient.path(path).get(String.class); - assertEquals(expected, s); + private void assertRequest(String path, String expected) { + String actualMessage = invoke(path); + assertEquals(expected, actualMessage); } - + private String invoke(String path) { + if(path.endsWith("red")) { + return colorService.red(); + } else if(path.endsWith("green")) { + return colorService.green(); + } else { + return colorService.blue(); + } + } } diff --git a/microscoped-timer/pom.xml b/microscoped-timer/pom.xml index 1c095bf..b86b918 100644 --- a/microscoped-timer/pom.xml +++ b/microscoped-timer/pom.xml @@ -41,18 +41,6 @@ ${openejb.javaee.api} provided - - org.apache.openejb - arquillian-tomee-embedded - ${tomee.version} - test - - - org.apache.openejb - tomee-jaxrs - ${tomee.version} - test - junit junit diff --git a/microscoped-timer/src/main/java/org/tomitribe/microscoped/timer/TimerScopedExtension.java b/microscoped-timer/src/main/java/org/tomitribe/microscoped/timer/TimerScopedExtension.java index 802d290..df13ced 100644 --- a/microscoped-timer/src/main/java/org/tomitribe/microscoped/timer/TimerScopedExtension.java +++ b/microscoped-timer/src/main/java/org/tomitribe/microscoped/timer/TimerScopedExtension.java @@ -33,7 +33,7 @@ public void beforeBeanDiscovery(@Observes BeforeBeanDiscovery bbd) { public void afterBeanDiscovery(@Observes AfterBeanDiscovery abd) { - abd.addContext(new ScopeContext<>(TimerScoped.class)); + abd.addContext(new ScopeContext(TimerScoped.class)); } } \ No newline at end of file diff --git a/microscoped-timer/src/test/java/org/supertribe/timer/ColorService.java b/microscoped-timer/src/test/java/org/supertribe/timer/ColorService.java index 775fe6a..26a31b1 100644 --- a/microscoped-timer/src/test/java/org/supertribe/timer/ColorService.java +++ b/microscoped-timer/src/test/java/org/supertribe/timer/ColorService.java @@ -30,15 +30,11 @@ import javax.ejb.TimerService; import javax.inject.Inject; import javax.interceptor.Interceptors; -import javax.ws.rs.GET; -import javax.ws.rs.Path; import static javax.ejb.LockType.READ; @Lock(READ) @Singleton -@Path("/color") -//@TimerScopeEnabled @Interceptors(TimerScopedInterceptor.class) public class ColorService { @@ -62,7 +58,6 @@ public void construct() { } } - @GET public Log getLog() { return log; } diff --git a/microscoped-timer/src/test/java/org/supertribe/timer/Count.java b/microscoped-timer/src/test/java/org/supertribe/timer/Count.java index 87c2dd2..d2ad4b1 100644 --- a/microscoped-timer/src/test/java/org/supertribe/timer/Count.java +++ b/microscoped-timer/src/test/java/org/supertribe/timer/Count.java @@ -25,19 +25,7 @@ public class Count { private final AtomicInteger count = new AtomicInteger(); - public int get() { - return count.get(); - } - public int add() { return count.incrementAndGet(); } - - public boolean compareAndSet(int expect, int update) { - return count.compareAndSet(expect, update); - } - - public int remove() { - return count.decrementAndGet(); - } } diff --git a/microscoped-timer/src/test/java/org/supertribe/timer/Log.java b/microscoped-timer/src/test/java/org/supertribe/timer/Log.java index 973431e..bdfd680 100644 --- a/microscoped-timer/src/test/java/org/supertribe/timer/Log.java +++ b/microscoped-timer/src/test/java/org/supertribe/timer/Log.java @@ -17,21 +17,13 @@ package org.supertribe.timer; import javax.enterprise.inject.Typed; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlRootElement; import java.util.ArrayList; import java.util.List; @Typed -@XmlRootElement -@XmlAccessorType(XmlAccessType.FIELD) public class Log { - final List lines = new ArrayList(); - - public Log() { - } + private final List lines = new ArrayList(); public List getLines() { return lines; diff --git a/microscoped-timer/src/test/java/org/supertribe/timer/TimerScopedTest.java b/microscoped-timer/src/test/java/org/supertribe/timer/TimerScopedTest.java index a79b4fe..7aca85e 100644 --- a/microscoped-timer/src/test/java/org/supertribe/timer/TimerScopedTest.java +++ b/microscoped-timer/src/test/java/org/supertribe/timer/TimerScopedTest.java @@ -16,30 +16,23 @@ */ package org.supertribe.timer; -import org.apache.cxf.jaxrs.client.WebClient; import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.junit.Arquillian; -import org.jboss.arquillian.test.api.ArquillianResource; import org.jboss.shrinkwrap.api.ShrinkWrap; import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset; -import org.jboss.shrinkwrap.api.asset.StringAsset; import org.jboss.shrinkwrap.api.spec.WebArchive; -import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.tomitribe.microscoped.core.ScopeContext; import org.tomitribe.microscoped.timer.TimerScopedExtension; import javax.enterprise.inject.spi.Extension; -import javax.ws.rs.core.MediaType; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.URL; -import java.util.Collection; +import javax.inject.Inject; import java.util.List; import java.util.concurrent.TimeUnit; +import static org.junit.Assert.assertTrue; + /** * Arquillian will start the container, deploy all @Deployment bundles, then run all the @Test methods. * @@ -52,7 +45,7 @@ * */ @RunWith(Arquillian.class) -public class TimerScopedTest extends Assert { +public class TimerScopedTest { /** * ShrinkWrap is used to create a war file on the fly. @@ -71,33 +64,18 @@ public static WebArchive createDeployment() { .addPackage(TimerScopedExtension.class.getPackage()) .addPackage(ColorService.class.getPackage()) .addAsWebInfResource(new ClassLoaderAsset("META-INF/beans.xml"), "classes/META-INF/beans.xml") - .addAsWebInfResource(new StringAsset(TimerScopedExtension.class.getName()), - "classes/META-INF/services/" + Extension.class.getName() - ); + .addAsServiceProviderAndClasses(Extension.class, TimerScopedExtension.class); } - /** - * This URL will contain the following URL data - * - * - http://:// - * - * This allows the test itself to be agnostic of server information or even - * the name of the webapp - * - */ - @ArquillianResource - private URL webappUrl; - + @Inject + private ColorService colorService; @Test public void test() throws Exception { Thread.sleep(TimeUnit.SECONDS.toMillis(7)); - final WebClient webClient = WebClient.create(webappUrl.toURI()); - webClient.accept(MediaType.APPLICATION_JSON); - - final List lines = webClient.path("color").get(Log.class).getLines(); + final List lines = colorService.getLog().getLines(); assertTrue(lines.contains("red, 1")); assertTrue(lines.contains("red, 2")); diff --git a/pom.xml b/pom.xml index 5296bf4..0427b40 100644 --- a/pom.xml +++ b/pom.xml @@ -59,8 +59,8 @@ maven-compiler-plugin 3.2 - 1.8 - 1.8 + 1.6 + 1.6 @@ -89,6 +89,13 @@ + + org.jboss.arquillian + arquillian-bom + 1.1.10.Final + import + pom + junit junit @@ -104,5 +111,50 @@ - + + + org.apache.httpcomponents + httpclient + 4.4.1 + test + + + + + tomee + + true + + + + org.apache.openejb + arquillian-tomee-embedded + ${tomee.version} + test + + + org.apache.openejb + tomee-jaxrs + ${tomee.version} + test + + + + + wildfly + + + org.wildfly.arquillian + wildfly-arquillian-container-managed + 1.0.0.Final + test + + + org.jboss.arquillian.junit + arquillian-junit-container + test + + + +