|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2018 Contributors to the Eclipse Foundation |
| 3 | + * |
| 4 | + * See the NOTICE file(s) distributed with this work for additional |
| 5 | + * information regarding copyright ownership. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * You may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + ******************************************************************************/ |
| 19 | + |
| 20 | +package org.eclipse.microprofile.reactive.streams.tck.arquillian; |
| 21 | + |
| 22 | +import org.eclipse.microprofile.reactive.streams.tck.ReactiveStreamsTck; |
| 23 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 24 | +import org.jboss.arquillian.testng.Arquillian; |
| 25 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 26 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 27 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 28 | +import org.reactivestreams.tck.TestEnvironment; |
| 29 | +import org.testng.IClassListener; |
| 30 | +import org.testng.IMethodInstance; |
| 31 | +import org.testng.IMethodInterceptor; |
| 32 | +import org.testng.IObjectFactory; |
| 33 | +import org.testng.ITestClass; |
| 34 | +import org.testng.ITestContext; |
| 35 | +import org.testng.ITestListener; |
| 36 | +import org.testng.ITestNGListener; |
| 37 | +import org.testng.ITestResult; |
| 38 | +import org.testng.TestNG; |
| 39 | +import org.testng.annotations.Test; |
| 40 | +import org.testng.internal.ObjectFactoryImpl; |
| 41 | + |
| 42 | +import javax.inject.Inject; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.Comparator; |
| 46 | +import java.util.List; |
| 47 | +import java.util.concurrent.atomic.AtomicInteger; |
| 48 | +import java.util.concurrent.atomic.AtomicReference; |
| 49 | + |
| 50 | +/** |
| 51 | + * Test runner for running the TCK in Arquillian. |
| 52 | + * <p> |
| 53 | + * It would be nice if this was able to run the tests properly, and I did get something working, but it was so complex |
| 54 | + * and fragile because Arquillian really isn't that flexible that I decided it simply wasn't worth it, this is much |
| 55 | + * simpler. |
| 56 | + */ |
| 57 | +public class ReactiveStreamsArquillianTck extends Arquillian { |
| 58 | + @Deployment |
| 59 | + public static JavaArchive tckDeployment() { |
| 60 | + return ShrinkWrap.create(JavaArchive.class) |
| 61 | + // Add everything from the TCK |
| 62 | + .addPackages(true, ReactiveStreamsTck.class.getPackage()) |
| 63 | + // And add the reactive streams TCK |
| 64 | + .addPackages(true, TestEnvironment.class.getPackage()) |
| 65 | + // And we need a CDI descriptor |
| 66 | + .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + @Inject |
| 71 | + private ReactiveStreamsCdiTck tck; |
| 72 | + |
| 73 | + @Test |
| 74 | + public void runAllTckTests() throws Throwable { |
| 75 | + TestNG testng = new TestNG(); |
| 76 | + |
| 77 | + ObjectFactoryImpl delegate = new ObjectFactoryImpl(); |
| 78 | + testng.setObjectFactory((IObjectFactory) (constructor, params) -> { |
| 79 | + if (constructor.getDeclaringClass().equals(ReactiveStreamsCdiTck.class)) { |
| 80 | + return tck; |
| 81 | + } |
| 82 | + else { |
| 83 | + return delegate.newInstance(constructor, params); |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + testng.setUseDefaultListeners(false); |
| 88 | + ResultListener resultListener = new ResultListener(); |
| 89 | + testng.addListener((ITestNGListener) resultListener); |
| 90 | + testng.setTestClasses(new Class[]{ ReactiveStreamsCdiTck.class }); |
| 91 | + testng.setMethodInterceptor(new IMethodInterceptor() { |
| 92 | + @Override |
| 93 | + public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestContext context) { |
| 94 | + methods.sort(Comparator.comparing(m -> m.getInstance().getClass().getName())); |
| 95 | + return methods; |
| 96 | + } |
| 97 | + }); |
| 98 | + testng.run(); |
| 99 | + int total = resultListener.success.get() + resultListener.failed.get() + resultListener.skipped.get(); |
| 100 | + System.out.println(String.format("Ran %d tests, %d passed, %d failed, %d skipped.", total, resultListener.success.get(), |
| 101 | + resultListener.failed.get(), resultListener.skipped.get())); |
| 102 | + System.out.println("Failed tests:"); |
| 103 | + resultListener.failures.forEach(result -> { |
| 104 | + System.out.println(result.getInstance().getClass().getName() + "." + result.getMethod().getMethodName()); |
| 105 | + }); |
| 106 | + if (resultListener.failed.get() > 0) { |
| 107 | + if (resultListener.lastFailure.get() != null) { |
| 108 | + throw resultListener.lastFailure.get(); |
| 109 | + } |
| 110 | + else { |
| 111 | + throw new Exception("Tests failed with no exception"); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + private static class ResultListener implements IClassListener, ITestListener { |
| 117 | + private final AtomicInteger success = new AtomicInteger(); |
| 118 | + private final AtomicInteger failed = new AtomicInteger(); |
| 119 | + private final AtomicInteger skipped = new AtomicInteger(); |
| 120 | + private final AtomicReference<Throwable> lastFailure = new AtomicReference<>(); |
| 121 | + private final List<ITestResult> failures = Collections.synchronizedList(new ArrayList<>()); |
| 122 | + |
| 123 | + @Override |
| 124 | + public void onBeforeClass(ITestClass testClass) { |
| 125 | + System.out.println(testClass.getName() + ":"); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void onAfterClass(ITestClass testClass) { |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onTestStart(ITestResult result) { |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public void onTestSuccess(ITestResult result) { |
| 138 | + printResult(result, "SUCCESS"); |
| 139 | + success.incrementAndGet(); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void onTestFailure(ITestResult result) { |
| 144 | + printResult(result, "FAILED"); |
| 145 | + if (result.getThrowable() != null) { |
| 146 | + result.getThrowable().printStackTrace(System.out); |
| 147 | + lastFailure.set(result.getThrowable()); |
| 148 | + } |
| 149 | + failures.add(result); |
| 150 | + failed.incrementAndGet(); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void onTestSkipped(ITestResult result) { |
| 155 | + printResult(result, "SKIPPED"); |
| 156 | + skipped.incrementAndGet(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void onTestFailedButWithinSuccessPercentage(ITestResult result) { |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public void onStart(ITestContext context) { |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public void onFinish(ITestContext context) { |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | + private static void printResult(ITestResult result, String status) { |
| 173 | + String methodName = String.format("%-100s", result.getMethod().getMethodName()).replace(' ', '.'); |
| 174 | + System.out.println(" - " + methodName + "." + status); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments