Author: rmannibucau Date: Wed Oct 19 15:14:09 2016 New Revision: 1765660 URL: http://svn.apache.org/viewvc?rev=1765660&view=rev Log: JUnit integration - adding file, it's better then
Added: openwebbeans/microwave/trunk/microwave-junit/ openwebbeans/microwave/trunk/microwave-junit/pom.xml openwebbeans/microwave/trunk/microwave-junit/src/ openwebbeans/microwave/trunk/microwave-junit/src/main/ openwebbeans/microwave/trunk/microwave-junit/src/main/java/ openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/ openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/ openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/ openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/ openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRule.java openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRuleBase.java openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MonoMicrowave.java openwebbeans/microwave/trunk/microwave-junit/src/test/ openwebbeans/microwave/trunk/microwave-junit/src/test/java/ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MicrowaveRuleTest.java openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MonoMicrowaveRuleTest.java openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Endpoint.java openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Injectable.java openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/RsApp.java Added: openwebbeans/microwave/trunk/microwave-junit/pom.xml URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/pom.xml?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/pom.xml (added) +++ openwebbeans/microwave/trunk/microwave-junit/pom.xml Wed Oct 19 15:14:09 2016 @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation=" + http://maven.apache.org/POM/4.0.0 + http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <parent> + <artifactId>microwave</artifactId> + <groupId>org.apache.microwave</groupId> + <version>0.0.1-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + + <artifactId>microwave-junit</artifactId> + <name>Microwave :: JUnit</name> + + <dependencies> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + </dependency> + <dependency> + <groupId>org.apache.microwave</groupId> + <artifactId>microwave-core</artifactId> + <version>${project.version}</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <configuration> + <!-- cause of mono runner/rule --> + <reuseForks>false</reuseForks> + </configuration> + </plugin> + </plugins> + </build> +</project> Added: openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRule.java URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRule.java?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRule.java (added) +++ openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRule.java Wed Oct 19 15:14:09 2016 @@ -0,0 +1,45 @@ +/* + * 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.microwave.junit; + +import org.apache.microwave.Microwave; + +public class MicrowaveRule extends MicrowaveRuleBase { + private final Microwave.Builder configuration; + private final String context; + + public MicrowaveRule() { + this(new Microwave.Builder().randomHttpPort(), ""); + } + + public MicrowaveRule(final Microwave.Builder configuration, final String context) { + this.configuration = configuration; + this.context = context; + } + + @Override + public Microwave.Builder getConfiguration() { + return configuration; + } + + @Override + protected AutoCloseable onStart() { + return new Microwave(configuration).bake(context); + } +} Added: openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRuleBase.java URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRuleBase.java?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRuleBase.java (added) +++ openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MicrowaveRuleBase.java Wed Oct 19 15:14:09 2016 @@ -0,0 +1,42 @@ +/* + * 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.microwave.junit; + +import org.apache.microwave.Microwave; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +public abstract class MicrowaveRuleBase implements TestRule { + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + try (final AutoCloseable closeable = onStart()) { + base.evaluate(); + } + } + }; + } + + public abstract Microwave.Builder getConfiguration(); + + protected abstract AutoCloseable onStart(); +} Added: openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MonoMicrowave.java URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MonoMicrowave.java?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MonoMicrowave.java (added) +++ openwebbeans/microwave/trunk/microwave-junit/src/main/java/org/apache/microwave/junit/MonoMicrowave.java Wed Oct 19 15:14:09 2016 @@ -0,0 +1,143 @@ +/* + * 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.microwave.junit; + +import org.apache.microwave.Microwave; +import org.junit.rules.MethodRule; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; +import java.util.List; +import java.util.ServiceLoader; +import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +// a MicrowaveRule starting a single container, very awesome for forkCount=1, reuseForks=true +public class MonoMicrowave { + private static final AtomicReference<AutoCloseable> CONTAINER = new AtomicReference<>(); + private static final AtomicReference<Microwave.Builder> CONFIGURATION = new AtomicReference<>(); + private static final AutoCloseable NOOP_CLOSEABLE = () -> { + }; + + public static class Runner extends BlockJUnit4ClassRunner { + public Runner(final Class<?> klass) throws InitializationError { + super(klass); + } + + @Override + protected List<MethodRule> rules(final Object test) { + final List<MethodRule> rules = super.rules(test); + rules.add((base, method, target) -> new Statement() { + @Override + public void evaluate() throws Throwable { + doBoot(); + configInjection(test.getClass(), test); + base.evaluate(); + } + + private void configInjection(final Class<?> aClass, final Object test) { + Stream.of(aClass.getDeclaredFields()) + .filter(f -> f.isAnnotationPresent(ConfigurationInject.class)) + .forEach(f -> { + if (!f.isAccessible()) { + f.setAccessible(true); + } + try { + f.set(test, CONFIGURATION.get()); + } catch (final IllegalAccessException e) { + throw new IllegalStateException(e); + } + }); + final Class<?> parent = aClass.getSuperclass(); + if (parent != null && parent != Object.class) { + configInjection(parent, true); + } + } + }); + return rules; + } + + /** + * Only working with the runner + */ + @Target(FIELD) + @Retention(RUNTIME) + public @interface ConfigurationInject { + } + } + + public static class Rule extends MicrowaveRuleBase { + @Override + public Microwave.Builder getConfiguration() { + return CONFIGURATION.get(); + } + + @Override + protected AutoCloseable onStart() { + if (CONTAINER.get() == null) { // yes synchro could be simpler but it does the job, feel free to rewrite it + synchronized (CONTAINER) { + if (CONTAINER.get() == null) { + doBoot(); + } + } + } + return NOOP_CLOSEABLE; + } + } + + private static void doBoot() { + final Microwave.Builder configuration = new Microwave.Builder().randomHttpPort(); + StreamSupport.stream(ServiceLoader.load(Microwave.ConfigurationCustomizer.class).spliterator(), false) + .forEach(c -> c.customize(configuration)); + CONFIGURATION.compareAndSet(null, configuration); + + final Microwave microwave = new Microwave(CONFIGURATION.get()); + if (CONTAINER.compareAndSet(null, microwave)) { + final Configuration runnerConfig = StreamSupport.stream(ServiceLoader.load(Configuration.class).spliterator(), false) + .findAny() + .orElseGet(() -> new Configuration() { + }); + + microwave.bake(runnerConfig.context()); + Runtime.getRuntime().addShutdownHook(new Thread() { + { + setName("Microwave-mono-rue-stopping"); + } + + @Override + public void run() { + microwave.close(); + } + }); + } + } + + public interface Configuration { + default String context() { + return ""; + } + } +} Added: openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MicrowaveRuleTest.java URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MicrowaveRuleTest.java?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MicrowaveRuleTest.java (added) +++ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MicrowaveRuleTest.java Wed Oct 19 15:14:09 2016 @@ -0,0 +1,38 @@ +/* + * 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.microwave.junit; + +import org.apache.commons.io.IOUtils; +import org.junit.ClassRule; +import org.junit.Test; + +import java.io.IOException; +import java.net.URL; + +import static org.junit.Assert.assertEquals; + +public class MicrowaveRuleTest { + @ClassRule + public static final MicrowaveRule RULE = new MicrowaveRule(); + + @Test + public void test() throws IOException { + assertEquals("simple", IOUtils.toString(new URL("http://localhost:" + RULE.getConfiguration().getHttpPort() + "/api/test"))); + } +} Added: openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MonoMicrowaveRuleTest.java URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MonoMicrowaveRuleTest.java?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MonoMicrowaveRuleTest.java (added) +++ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/apache/microwave/junit/MonoMicrowaveRuleTest.java Wed Oct 19 15:14:09 2016 @@ -0,0 +1,45 @@ +/* + * 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.microwave.junit; + +import org.apache.commons.io.IOUtils; +import org.apache.microwave.Microwave; +import org.junit.Test; +import org.junit.runner.RunWith; + +import java.io.IOException; +import java.net.URL; + +import static org.junit.Assert.assertEquals; + +@RunWith(MonoMicrowave.Runner.class) +public class MonoMicrowaveRuleTest { + /* or + @ClassRule + public static final MonoMicrowave.Rule RULE = new MonoMicrowave.Rule(); + */ + + @MonoMicrowave.Runner.ConfigurationInject + private Microwave.Builder config; + + @Test + public void test() throws IOException { + assertEquals("simple", IOUtils.toString(new URL("http://localhost:" + config.getHttpPort() + "/api/test"))); + } +} Added: openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Endpoint.java URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Endpoint.java?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Endpoint.java (added) +++ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Endpoint.java Wed Oct 19 15:14:09 2016 @@ -0,0 +1,66 @@ +/* + * 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.app; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; + +@Path("test") +@ApplicationScoped +public class Endpoint { + @Inject + private Injectable injectable; + + @GET + @Produces(MediaType.TEXT_PLAIN) + public String simple() { + return Boolean.parseBoolean(injectable.injected()) ? "simple" : "fail"; + } + + @GET + @Path("json") + @Produces(MediaType.APPLICATION_JSON) + public Simple json() { + return new Simple("test"); + } + + public static class Simple { + private String name; + + public Simple() { + // no-op + } + + public Simple(final String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + } +} Added: openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Injectable.java URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Injectable.java?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Injectable.java (added) +++ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/Injectable.java Wed Oct 19 15:14:09 2016 @@ -0,0 +1,28 @@ +/* + * 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.app; + +import javax.enterprise.context.ApplicationScoped; + +@ApplicationScoped +public class Injectable { + public String injected() { + return "true"; + } +} Added: openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/RsApp.java URL: http://svn.apache.org/viewvc/openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/RsApp.java?rev=1765660&view=auto ============================================================================== --- openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/RsApp.java (added) +++ openwebbeans/microwave/trunk/microwave-junit/src/test/java/org/app/RsApp.java Wed Oct 19 15:14:09 2016 @@ -0,0 +1,28 @@ +/* + * 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.app; + +import javax.enterprise.context.Dependent; +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@Dependent +@ApplicationPath("api") +public class RsApp extends Application { +}