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.plugins;
016    
017    import com.liferay.portal.kernel.deploy.hot.BaseHotDeployListener;
018    import com.liferay.portal.kernel.deploy.hot.HotDeployEvent;
019    import com.liferay.portal.kernel.deploy.hot.HotDeployException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.ReflectionUtil;
023    import com.liferay.portal.kernel.util.StringBundler;
024    import com.liferay.portal.kernel.util.Time;
025    import com.liferay.portal.kernel.zip.ZipReader;
026    import com.liferay.portal.kernel.zip.ZipReaderFactoryUtil;
027    
028    import java.io.File;
029    
030    import java.net.URL;
031    
032    import java.text.NumberFormat;
033    
034    import java.util.ArrayList;
035    import java.util.Collections;
036    import java.util.List;
037    
038    import org.junit.runner.JUnitCore;
039    import org.junit.runner.Result;
040    import org.junit.runner.RunWith;
041    import org.junit.runner.Runner;
042    import org.junit.runner.notification.Failure;
043    
044    /**
045     * @author Manuel de la Pe??a
046     */
047    public class PluginIntegrationTestHotDeployListener
048            extends BaseHotDeployListener {
049    
050            @Override
051            public void invokeDeploy(HotDeployEvent hotDeployEvent)
052                    throws HotDeployException {
053    
054                    try {
055                            doInvokeDeploy(hotDeployEvent);
056                    }
057                    catch (Throwable t) {
058                            throwHotDeployException(
059                                    hotDeployEvent, "Unable to register tests for ", t);
060                    }
061            }
062    
063            @Override
064            public void invokeUndeploy(HotDeployEvent hotDeployEvent)
065                    throws HotDeployException {
066    
067                    try {
068                            doInvokeUndeploy(hotDeployEvent);
069                    }
070                    catch (Throwable t) {
071                            throwHotDeployException(
072                                    hotDeployEvent, "Unable to register tests for ", t);
073                    }
074            }
075    
076            protected void doInvokeDeploy(HotDeployEvent hotDeployEvent)
077                    throws Exception {
078    
079                    List<Class<?>> testClasses = getAllClassesInIntegrationJar(
080                            hotDeployEvent);
081    
082                    runTestClasses(testClasses);
083            }
084    
085            protected void doInvokeUndeploy(HotDeployEvent hotDeployEvent)
086                    throws Exception {
087    
088                    if (_log.isDebugEnabled()) {
089                            _log.debug("Undeploying tests for " + hotDeployEvent);
090                    }
091            }
092    
093            protected List<Class<?>> getAllClassesInIntegrationJar(
094                            HotDeployEvent hotDeployEvent)
095                    throws ClassNotFoundException {
096    
097                    ClassLoader classLoader = hotDeployEvent.getContextClassLoader();
098    
099                    URL url = classLoader.getResource("../lib");
100    
101                    File file = new File(
102                            url.getFile(),
103                            hotDeployEvent.getServletContextName() + "-test-integration.jar");
104    
105                    if (!file.exists()) {
106                            return Collections.emptyList();
107                    }
108    
109                    List<Class<?>> classes = new ArrayList<Class<?>>();
110    
111                    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);
112    
113                    List<String> entries = zipReader.getEntries();
114    
115                    for (String entry : entries) {
116                            if (!entry.endsWith(".class")) {
117                                    continue;
118                            }
119    
120                            String className = entry.replace("/", ".");
121    
122                            className = className.substring(0, className.indexOf(".class"));
123    
124                            Class<?> clazz = classLoader.loadClass(className);
125    
126                            classes.add(clazz);
127                    }
128    
129                    zipReader.close();
130    
131                    return classes;
132            }
133    
134            protected boolean isTestClass(Class<?> clazz) {
135                    Class<?> declaringClass = ReflectionUtil.getAnnotationDeclaringClass(
136                            RunWith.class, clazz);
137    
138                    if (declaringClass == null) {
139                            return false;
140                    }
141    
142                    RunWith runWith = declaringClass.getAnnotation(RunWith.class);
143    
144                    Class<? extends Runner> value = runWith.value();
145    
146                    String className = clazz.getName();
147    
148                    if (!className.endsWith("Test") ||
149                            !value.equals(LiferayPluginsIntegrationJUnitRunner.class)) {
150    
151                            return false;
152                    }
153    
154                    return true;
155            }
156    
157            protected void runTestClasses(List<Class<?>> classes)
158                    throws RuntimeException {
159    
160                    NumberFormat numberFormat = NumberFormat.getInstance();
161    
162                    numberFormat.setMaximumFractionDigits(3);
163    
164                    for (Class<?> clazz : classes) {
165                            if (!isTestClass(clazz)) {
166                                    continue;
167                            }
168    
169                            double startTime = System.currentTimeMillis();
170    
171                            if (_log.isInfoEnabled()) {
172                                    _log.info("Running " + clazz.getName());
173                            }
174    
175                            Result result = JUnitCore.runClasses(clazz);
176    
177                            if (_log.isInfoEnabled()) {
178                                    double endTime = System.currentTimeMillis();
179    
180                                    StringBundler sb = new StringBundler(9);
181    
182                                    sb.append("Tests run: ");
183                                    sb.append(result.getRunCount());
184                                    sb.append(", Failures: ");
185                                    sb.append(result.getIgnoreCount());
186                                    sb.append(", Errors: ");
187                                    sb.append(result.getFailureCount());
188                                    sb.append(", Time elapsed: ");
189                                    sb.append(
190                                            numberFormat.format((endTime - startTime) / Time.SECOND));
191                                    sb.append(" sec");
192    
193                                    _log.info(sb.toString());
194                            }
195    
196                            for (Failure failure : result.getFailures()) {
197                                    _log.error(failure.toString());
198                            }
199                    }
200            }
201    
202            private static final Log _log = LogFactoryUtil.getLog(
203                    PluginIntegrationTestHotDeployListener.class);
204    
205    }