001    /**
002     * Copyright (c) 2000-2013 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                    return classes;
130            }
131    
132            protected boolean isTestClass(Class<?> clazz) {
133                    Class<?> declaringClass = ReflectionUtil.getAnnotationDeclaringClass(
134                            RunWith.class, clazz);
135    
136                    if (declaringClass == null) {
137                            return false;
138                    }
139    
140                    RunWith runWith = declaringClass.getAnnotation(RunWith.class);
141    
142                    Class<? extends Runner> value = runWith.value();
143    
144                    String className = clazz.getName();
145    
146                    if (!className.endsWith("Test") ||
147                            !value.equals(LiferayPluginsIntegrationJUnitRunner.class)) {
148    
149                            return false;
150                    }
151    
152                    return true;
153            }
154    
155            protected void runTestClasses(List<Class<?>> classes)
156                    throws RuntimeException {
157    
158                    NumberFormat numberFormat = NumberFormat.getInstance();
159    
160                    numberFormat.setMaximumFractionDigits(3);
161    
162                    for (Class<?> clazz : classes) {
163                            if (!isTestClass(clazz)) {
164                                    continue;
165                            }
166    
167                            double startTime = System.currentTimeMillis();
168    
169                            if (_log.isInfoEnabled()) {
170                                    _log.info("Running " + clazz.getName());
171                            }
172    
173                            Result result = JUnitCore.runClasses(clazz);
174    
175                            if (_log.isInfoEnabled()) {
176                                    double endTime = System.currentTimeMillis();
177    
178                                    StringBundler sb = new StringBundler(9);
179    
180                                    sb.append("Tests run: ");
181                                    sb.append(result.getRunCount());
182                                    sb.append(", Failures: ");
183                                    sb.append(result.getIgnoreCount());
184                                    sb.append(", Errors: ");
185                                    sb.append(result.getFailureCount());
186                                    sb.append(", Time elapsed: ");
187                                    sb.append(
188                                            numberFormat.format((endTime - startTime) / Time.SECOND));
189                                    sb.append(" sec");
190    
191                                    _log.info(sb.toString());
192                            }
193    
194                            for (Failure failure : result.getFailures()) {
195                                    _log.error(failure.toString());
196                            }
197                    }
198            }
199    
200            private static Log _log = LogFactoryUtil.getLog(
201                    PluginIntegrationTestHotDeployListener.class);
202    
203    }