001
014
015 package com.liferay.portal.test.runners;
016
017 import com.liferay.portal.deploy.hot.IndexerPostProcessorRegistry;
018 import com.liferay.portal.deploy.hot.SchedulerEntryRegistry;
019 import com.liferay.portal.deploy.hot.ServiceWrapperRegistry;
020 import com.liferay.portal.kernel.util.StringPool;
021 import com.liferay.portal.service.ServiceTestUtil;
022
023 import java.net.MalformedURLException;
024 import java.net.URL;
025 import java.net.URLClassLoader;
026
027 import java.security.CodeSource;
028 import java.security.ProtectionDomain;
029
030 import java.util.Map;
031 import java.util.concurrent.ConcurrentHashMap;
032
033 import javax.naming.Context;
034
035 import org.junit.runners.model.InitializationError;
036
037
040 public class PACLIntegrationJUnitTestRunner
041 extends LiferayIntegrationJUnitTestRunner {
042
043 public static final String RESOURCE_PATH =
044 "com/liferay/portal/security/pacl/test/dependencies";
045
046 public PACLIntegrationJUnitTestRunner(Class<?> clazz)
047 throws InitializationError {
048
049 super(_wrapTestClass(clazz));
050 }
051
052 @Override
053 public void initApplicationContext() {
054 if (_initialized) {
055 return;
056 }
057
058 Class<?> clazz = getClass();
059
060 URL resource = clazz.getResource("pacl-test.properties");
061
062 if (resource != null) {
063 System.setProperty("external-properties", resource.getPath());
064 }
065
066 System.setProperty(
067 Context.INITIAL_CONTEXT_FACTORY,
068 "org.apache.naming.java.javaURLContextFactory");
069
070 super.initApplicationContext();
071
072 ServiceTestUtil.initServices();
073 ServiceTestUtil.initPermissions();
074
075 new IndexerPostProcessorRegistry();
076 new SchedulerEntryRegistry();
077 new ServiceWrapperRegistry();
078
079 _initialized = true;
080 }
081
082 private static Class<?> _wrapTestClass(Class<?> clazz)
083 throws InitializationError {
084
085 try {
086 ProtectionDomain protectionDomain = clazz.getProtectionDomain();
087
088 CodeSource codeSource = protectionDomain.getCodeSource();
089
090 ClassLoader classLoader = new PACLClassLoader(
091 new URL[] {codeSource.getLocation()}, clazz.getClassLoader());
092
093 return Class.forName(clazz.getName(), true, classLoader);
094 }
095 catch (Exception e) {
096 throw new InitializationError(e);
097 }
098 }
099
100 private static final String _PACKAGE_PATH =
101 "com.liferay.portal.security.pacl.test.";
102
103 private static boolean _initialized;
104
105 private static class PACLClassLoader extends URLClassLoader {
106
107 public PACLClassLoader(URL[] urls, ClassLoader parentClassLoader) {
108 super(urls, parentClassLoader);
109 }
110
111 @Override
112 public URL findResource(String name) {
113 if (_urls.containsKey(name)) {
114 return _urls.get(name);
115 }
116
117 URL resource = null;
118
119 if (!name.contains(RESOURCE_PATH)) {
120 String newName = name;
121
122 if (!newName.startsWith(StringPool.SLASH)) {
123 newName = StringPool.SLASH.concat(newName);
124 }
125
126 newName = RESOURCE_PATH.concat(newName);
127
128 resource = super.findResource(newName);
129 }
130
131 if ((resource == null) && !name.contains(RESOURCE_PATH)) {
132 String newName = name;
133
134 if (!newName.startsWith(StringPool.SLASH)) {
135 newName = StringPool.SLASH.concat(newName);
136 }
137
138 newName = RESOURCE_PATH.concat("/WEB-INF/classes").concat(
139 newName);
140
141 resource = super.findResource(newName);
142 }
143
144 if (resource == null) {
145 resource = super.findResource(name);
146 }
147
148 if (resource != null) {
149 _urls.put(name, resource);
150 }
151
152 return resource;
153 }
154
155 @Override
156 public URL getResource(String name) {
157 if (name.equals(
158 "com/liferay/util/bean/PortletBeanLocatorUtil.class")) {
159
160 URL url = findResource("/");
161
162 String path = url.getPath();
163
164 path = path.substring(
165 0, path.length() - RESOURCE_PATH.length() - 1);
166
167 path = path.concat(name);
168
169 try {
170 return new URL("file", null, path);
171 }
172 catch (MalformedURLException murle) {
173 }
174 }
175
176 URL url = findResource(name);
177
178 if (url != null) {
179 return url;
180 }
181
182 return super.getResource(name);
183 }
184
185 @Override
186 public Class<?> loadClass(String name) throws ClassNotFoundException {
187 if (name.startsWith(_PACKAGE_PATH)) {
188 if (_classes.containsKey(name)) {
189 return _classes.get(name);
190 }
191
192 Class<?> clazz = super.findClass(name);
193
194 _classes.put(name, clazz);
195
196 return clazz;
197 }
198
199 return super.loadClass(name);
200 }
201
202 @Override
203 protected synchronized Class<?> loadClass(String name, boolean resolve)
204 throws ClassNotFoundException {
205
206 if (name.startsWith(_PACKAGE_PATH)) {
207 if (_classes.containsKey(name)) {
208 return _classes.get(name);
209 }
210
211 Class<?> clazz = super.findClass(name);
212
213 _classes.put(name, clazz);
214
215 return clazz;
216 }
217
218 return super.loadClass(name, resolve);
219 }
220
221 private final Map<String, Class<?>> _classes =
222 new ConcurrentHashMap<String, Class<?>>();
223 private final Map<String, URL> _urls =
224 new ConcurrentHashMap<String, URL>();
225
226 }
227
228 }