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