001
014
015 package com.liferay.portal.verify.test;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.test.ReflectionTestUtil;
019 import com.liferay.portal.kernel.util.ObjectValuePair;
020 import com.liferay.portal.kernel.util.ProxyUtil;
021 import com.liferay.portal.verify.VerifyException;
022 import com.liferay.portal.verify.VerifyProcess;
023
024 import java.lang.reflect.InvocationHandler;
025 import java.lang.reflect.InvocationTargetException;
026 import java.lang.reflect.Method;
027
028 import java.sql.Connection;
029
030 import java.util.Queue;
031 import java.util.concurrent.ConcurrentLinkedQueue;
032
033 import javax.naming.NamingException;
034
035 import javax.sql.DataSource;
036
037 import org.junit.After;
038 import org.junit.Before;
039 import org.junit.Test;
040
041
046 public abstract class BaseVerifyProcessTestCase {
047
048 @Before
049 public void setUp() throws Exception {
050 _pacl = ReflectionTestUtil.getFieldValue(DataAccess.class, "_pacl");
051
052 ReflectionTestUtil.setFieldValue(
053 DataAccess.class, "_pacl", new PACLWrapper(_pacl));
054 }
055
056 @After
057 public void tearDown() throws Exception {
058 if (_pacl == null) {
059 throw new NullPointerException();
060 }
061
062 ReflectionTestUtil.setFieldValue(DataAccess.class, "_pacl", _pacl);
063 }
064
065 @Test
066 public void testVerify() throws Exception {
067 Exception exception = null;
068
069 try {
070 doVerify();
071 }
072 catch (VerifyException ve) {
073 exception = ve;
074 }
075 finally {
076 for (ObjectValuePair<Connection, Exception> objectValuePair :
077 _objectValuePairs) {
078
079 Connection connection = objectValuePair.getKey();
080
081 if (!connection.isClosed()) {
082 if (exception == null) {
083 exception = objectValuePair.getValue();
084 }
085 else {
086 exception.addSuppressed(objectValuePair.getValue());
087 }
088 }
089 }
090
091 if (exception != null) {
092 throw exception;
093 }
094 }
095 }
096
097 protected void doVerify() throws VerifyException {
098 VerifyProcess verifyProcess = getVerifyProcess();
099
100 verifyProcess.verify();
101 }
102
103 protected abstract VerifyProcess getVerifyProcess();
104
105 private final Queue<ObjectValuePair<Connection, Exception>>
106 _objectValuePairs = new ConcurrentLinkedQueue<>();
107 private DataAccess.PACL _pacl;
108
109 private class DataSourceInvocationHandler implements InvocationHandler {
110
111 @Override
112 public Object invoke(Object proxy, Method method, Object[] args)
113 throws Throwable {
114
115 try {
116 Object result = method.invoke(_instance, args);
117
118 if (result instanceof Connection) {
119 _objectValuePairs.add(
120 new ObjectValuePair<>(
121 (Connection)result,
122 new Exception("Caught an unclosed exception")));
123 }
124
125 return result;
126 }
127 catch (InvocationTargetException ite) {
128 throw ite.getTargetException();
129 }
130 }
131
132 private DataSourceInvocationHandler(Object instance) {
133 _instance = instance;
134 }
135
136 private final Object _instance;
137
138 }
139
140 private class PACLWrapper implements DataAccess.PACL {
141
142 @Override
143 public DataSource getDataSource() {
144 return _wrapDataSource(_pacl.getDataSource());
145 }
146
147 @Override
148 public DataSource getDataSource(String location)
149 throws NamingException {
150
151 return _wrapDataSource(_pacl.getDataSource(location));
152 }
153
154 private PACLWrapper(DataAccess.PACL pacl) {
155 _pacl = pacl;
156 }
157
158 private DataSource _wrapDataSource(DataSource dataSource) {
159 return (DataSource)ProxyUtil.newProxyInstance(
160 ClassLoader.getSystemClassLoader(),
161 new Class<?>[] {DataSource.class},
162 new DataSourceInvocationHandler(dataSource));
163 }
164
165 private final DataAccess.PACL _pacl;
166
167 }
168
169 }