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.ci;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import org.junit.BeforeClass;
023    
024    /**
025     * @author Shuyang Zhou
026     */
027    public abstract class AutoBalanceTestCase {
028    
029            @BeforeClass
030            public static void setUpClass() {
031                    testClassGroupIndex = GetterUtil.getInteger(
032                            System.getProperty("test.class.group.index"), -1);
033    
034                    if (testClassGroupIndex >= 0) {
035                            String[] testClassGroupArray = StringUtil.split(
036                                    System.getProperty("test.class.groups"), CharPool.SPACE);
037    
038                            testClassGroupsSize = testClassGroupArray.length;
039                    }
040    
041                    if (isCIMode()) {
042                            System.out.println(
043                                    "Running in CI mode with " + (testClassGroupIndex + 1) +
044                                            "/" + testClassGroupsSize);
045                    }
046            }
047    
048            protected static boolean isCIMode() {
049                    if ((testClassGroupIndex >= 0) && (testClassGroupsSize > 0)) {
050                            return true;
051                    }
052    
053                    return false;
054            }
055    
056            protected static <T> T[] slice(T[] array) {
057                    int groupSize = array.length / testClassGroupsSize;
058    
059                    if ((array.length % testClassGroupsSize) != 0) {
060                            groupSize++;
061                    }
062    
063                    int start = groupSize * testClassGroupIndex;
064                    int end = start + groupSize;
065    
066                    if (end > array.length) {
067                            end = array.length;
068                    }
069    
070                    return ArrayUtil.subset(array, start, end);
071            }
072    
073            protected static int testClassGroupIndex;
074            protected static int testClassGroupsSize;
075    
076    }