001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.test.junit;
016    
017    import com.liferay.portal.kernel.concurrent.ConcurrentReferenceKeyHashMap;
018    import com.liferay.portal.kernel.memory.FinalizeManager;
019    import com.liferay.portal.kernel.test.ReflectionTestUtil;
020    
021    import java.io.Serializable;
022    
023    import java.util.Map;
024    
025    import org.junit.runner.Description;
026    import org.junit.runner.JUnitCore;
027    import org.junit.runner.Result;
028    import org.junit.runner.notification.Failure;
029    import org.junit.runner.notification.RunListener;
030    import org.junit.runner.notification.RunNotifier;
031    import org.junit.runners.BlockJUnit4ClassRunner;
032    import org.junit.runners.model.InitializationError;
033    import org.junit.runners.model.TestClass;
034    
035    /**
036     * @author Shuyang Zhou
037     */
038    public class BridgeJUnitTestRunner extends BlockJUnit4ClassRunner {
039    
040            public static RunNotifier getRunNotifier(Class<?> clazz) {
041                    return _runNotifiers.get(clazz);
042            }
043    
044            public static Result runBridgeTests(
045                    BridgeRunListener bridgeRunListener, Class<?>... testClasses) {
046    
047                    JUnitCore junitCore = new JUnitCore();
048    
049                    junitCore.addListener(bridgeRunListener);
050    
051                    return junitCore.run(testClasses);
052            }
053    
054            public BridgeJUnitTestRunner(Class<?> clazz) throws InitializationError {
055                    super(clazz);
056            }
057    
058            @Override
059            public void run(RunNotifier runNotifier) {
060                    TestClass testClass = getTestClass();
061    
062                    _runNotifiers.put(testClass.getJavaClass(), runNotifier);
063    
064                    super.run(runNotifier);
065            }
066    
067            public static class BridgeRunListener
068                    extends RunListener implements Serializable {
069    
070                    public BridgeRunListener(Class<?> testClass) {
071                            this.testClass = testClass;
072                    }
073    
074                    @Override
075                    public void testAssumptionFailure(Failure failure) {
076                            bridge("fireTestAssumptionFailed", failure);
077                    }
078    
079                    @Override
080                    public void testFailure(Failure failure) {
081                            bridge("fireTestFailure", failure);
082                    }
083    
084                    @Override
085                    public void testFinished(Description description) {
086                            bridge("fireTestFinished", description);
087                    }
088    
089                    @Override
090                    public void testIgnored(Description description) {
091                            bridge("fireTestIgnored", description);
092                    }
093    
094                    @Override
095                    public void testRunFinished(Result result) {
096                            bridge("fireTestRunFinished", result);
097                    }
098    
099                    @Override
100                    public void testRunStarted(Description description) {
101                            bridge("fireTestRunStarted", description);
102                    }
103    
104                    @Override
105                    public void testStarted(Description description) {
106                            bridge("fireTestStarted", description);
107                    }
108    
109                    protected void bridge(String methodName, Object argument) {
110                            ReflectionTestUtil.invoke(
111                                    getRunNotifier(testClass), methodName,
112                                    new Class<?>[] {argument.getClass()}, argument);
113                    }
114    
115                    protected final Class<?> testClass;
116    
117                    private static final long serialVersionUID = 1L;
118    
119            }
120    
121            private static final Map<Class<?>, RunNotifier> _runNotifiers =
122                    new ConcurrentReferenceKeyHashMap<>(
123                            FinalizeManager.WEAK_REFERENCE_FACTORY);
124    
125    }