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                    _log.debug("Undeploying tests for " + hotDeployEvent);
089            }
090    
091            protected List<Class<?>> getAllClassesInIntegrationJar(
092                            HotDeployEvent hotDeployEvent)
093                    throws ClassNotFoundException {
094    
095                    ClassLoader classLoader = hotDeployEvent.getContextClassLoader();
096    
097                    URL url = classLoader.getResource("../lib");
098    
099                    File file = new File(
100                            url.getFile(),
101                            hotDeployEvent.getServletContextName() + "-test-integration.jar");
102    
103                    if (!file.exists()) {
104                            return Collections.emptyList();
105                    }
106    
107                    List<Class<?>> classes = new ArrayList<Class<?>>();
108    
109                    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);
110    
111                    List<String> entries = zipReader.getEntries();
112    
113                    for (String entry : entries) {
114                            if (!entry.endsWith(".class")) {
115                                    continue;
116                            }
117    
118                            String className = entry.replace("/", ".");
119    
120                            className = className.substring(0, className.indexOf(".class"));
121    
122                            Class<?> clazz = classLoader.loadClass(className);
123    
124                            classes.add(clazz);
125                    }
126    
127                    return classes;
128            }
129    
130            protected boolean isTestClass(Class<?> clazz) {
131                    Class<?> declaringClass = ReflectionUtil.getAnnotationDeclaringClass(
132                            RunWith.class, clazz);
133    
134                    if (declaringClass == null) {
135                            return false;
136                    }
137    
138                    RunWith runWith = declaringClass.getAnnotation(RunWith.class);
139    
140                    Class<? extends Runner> value = runWith.value();
141    
142                    String className = clazz.getName();
143    
144                    if (!className.endsWith("Test") ||
145                            !value.equals(LiferayPluginsIntegrationJUnitRunner.class)) {
146    
147                            return false;
148                    }
149    
150                    return true;
151            }
152    
153            protected void runTestClasses(List<Class<?>> classes)
154                    throws RuntimeException {
155    
156                    NumberFormat numberFormat = NumberFormat.getInstance();
157    
158                    numberFormat.setMaximumFractionDigits(3);
159    
160                    for (Class<?> clazz : classes) {
161                            if (!isTestClass(clazz)) {
162                                    continue;
163                            }
164    
165                            double startTime = System.currentTimeMillis();
166    
167                            if (_log.isInfoEnabled()) {
168                                    _log.info("Running " + clazz.getName());
169                            }
170    
171                            Result result = JUnitCore.runClasses(clazz);
172    
173                            if (_log.isInfoEnabled()) {
174                                    double endTime = System.currentTimeMillis();
175    
176                                    StringBundler sb = new StringBundler(9);
177    
178                                    sb.append("Tests run: ");
179                                    sb.append(result.getRunCount());
180                                    sb.append(", Failures: ");
181                                    sb.append(result.getIgnoreCount());
182                                    sb.append(", Errors: ");
183                                    sb.append(result.getFailureCount());
184                                    sb.append(", Time elapsed: ");
185                                    sb.append(
186                                            numberFormat.format((endTime - startTime) / Time.SECOND));
187                                    sb.append(" sec");
188    
189                                    _log.info(sb.toString());
190                            }
191    
192                            for (Failure failure : result.getFailures()) {
193                                    _log.error(failure.toString());
194                            }
195                    }
196            }
197    
198            private static Log _log = LogFactoryUtil.getLog(
199                    PluginIntegrationTestHotDeployListener.class);
200    
201    }