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.test.rule.callback;
016    
017    import com.liferay.portal.kernel.test.rule.callback.BaseTestCallback;
018    import com.liferay.portal.kernel.test.util.TestPropsValues;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    
021    import org.junit.Assert;
022    import org.junit.runner.Description;
023    import org.junit.runner.RunWith;
024    import org.junit.runner.Runner;
025    
026    /**
027     * @author Shuyang Zhou
028     */
029    public class CITimeoutTestCallback extends BaseTestCallback<Long, Object> {
030    
031            public static final CITimeoutTestCallback INSTANCE =
032                    new CITimeoutTestCallback();
033    
034            @Override
035            public void doAfterClass(Description description, Long startTime) {
036                    long testTime = System.currentTimeMillis() - startTime;
037    
038                    if (testTime <= TestPropsValues.CI_TEST_TIMEOUT_TIME) {
039                            return;
040                    }
041    
042                    String message =
043                            description.getClassName() + " spent " + testTime +
044                                    "ms and surpassed the timeout threshold " +
045                                            TestPropsValues.CI_TEST_TIMEOUT_TIME + "ms.";
046    
047                    System.setProperty(_CI_TIMEOUT_TEST_CLASS_MESSAGE, message);
048    
049                    Assert.fail(
050                            message + " Marked it as failed and aborting subsequent tests.");
051            }
052    
053            @Override
054            public Long doBeforeClass(Description description) {
055                    String message = System.getProperty(_CI_TIMEOUT_TEST_CLASS_MESSAGE);
056    
057                    if (message != null) {
058                            Assert.fail(
059                                    "Abort running " + description.getClassName() + " due to : " +
060                                            message);
061                    }
062    
063                    if (!_isArquillianTest(description)) {
064                            return System.currentTimeMillis();
065                    }
066    
067                    String startTimeKey =
068                            _CI_TIMEOUT_ARQUILLIAN_TEST_START_TIME + description.getClassName();
069    
070                    String startTimeValue = System.getProperty(startTimeKey);
071    
072                    if (startTimeValue == null) {
073                            long startTime = System.currentTimeMillis();
074    
075                            System.setProperty(startTimeKey, String.valueOf(startTime));
076    
077                            return startTime;
078                    }
079    
080                    return GetterUtil.getLongStrict(startTimeValue);
081            }
082    
083            private CITimeoutTestCallback() {
084            }
085    
086            private boolean _isArquillianTest(Description description) {
087                    RunWith runWith = description.getAnnotation(RunWith.class);
088    
089                    if (runWith == null) {
090                            return false;
091                    }
092    
093                    Class<? extends Runner> runnerClass = runWith.value();
094    
095                    String runnerClassName = runnerClass.getName();
096    
097                    if (runnerClassName.equals(
098                                    "com.liferay.arquillian.extension.junit.bridge.junit." +
099                                            "Arquillian")) {
100    
101                            return true;
102                    }
103    
104                    return false;
105            }
106    
107            private static final String _CI_TIMEOUT_ARQUILLIAN_TEST_START_TIME =
108                    "CI_TIMEOUT_ARQUILLIAN_TEST_START_TIME_";
109    
110            private static final String _CI_TIMEOUT_TEST_CLASS_MESSAGE =
111                    "CI_TIMEOUT_TEST_CLASS_MESSAGE";
112    
113    }