Author: vyemialyanchyk
Date: 2010-02-08 07:38:38 -0500 (Mon, 08 Feb 2010)
New Revision: 20172
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/tests/DriverDeleteTest.java
Log:
import delete fix
Modified:
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/tests/DriverDeleteTest.java
===================================================================
---
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/tests/DriverDeleteTest.java 2010-02-08
09:37:50 UTC (rev 20171)
+++
trunk/hibernatetools/tests/org.hibernate.eclipse.console.test/src/org/hibernate/eclipse/console/test/utils/tests/DriverDeleteTest.java 2010-02-08
12:38:38 UTC (rev 20172)
@@ -15,7 +15,6 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
-import java.io.PrintWriter;
import java.io.Writer;
import java.lang.ref.WeakReference;
import java.lang.reflect.Field;
@@ -25,22 +24,30 @@
import java.sql.Driver;
import java.security.PrivilegedAction;
import java.sql.DriverManager;
-import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
+import java.util.Locale;
+import java.util.PropertyResourceBundle;
+import java.util.ResourceBundle;
import java.util.Vector;
+import java.util.jar.JarFile;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.apt.core.internal.JarClassLoader;
+import org.hibernate.console.ConsoleConfigClassLoader;
import org.hibernate.console.execution.DefaultExecutionContext;
import org.hibernate.console.execution.ExecutionContext;
import org.hibernate.eclipse.console.test.HibernateConsoleTestPlugin;
import org.hibernate.eclipse.console.test.utils.GarbageCollectionUtil;
import org.hibernate.util.ReflectHelper;
+//import sun.reflect.FieldAccessor;
+
import junit.framework.TestCase;
/**
@@ -59,6 +66,7 @@
public static final String PUT_PATH = "res"; //$NON-NLS-1$
private WeakReference<ExecutionContext> executionContext = null;
+ private ClassLoader prevClassLoader = null;
protected ClassLoader getParentClassLoader() {
return Thread.currentThread().getContextClassLoader();
@@ -204,6 +212,61 @@
return urlClassLoader;
}
+ public ConsoleConfigClassLoader createJarClassLoader2() {
+ final URL[] customClassPathURLs = getCustomClassPathURLs();
+ ConsoleConfigClassLoader urlClassLoader = AccessController.doPrivileged(new
PrivilegedAction<ConsoleConfigClassLoader>() {
+ public ConsoleConfigClassLoader run() {
+ return new ConsoleConfigClassLoader(customClassPathURLs, getParentClassLoader()) {
+ public InputStream getResourceAsStream(String name) {
+ InputStream is = super.getResourceAsStream(name);
+ return is;
+ }
+
+ public URL findResource(final String name) {
+ URL res = super.findResource(name);
+ return res;
+ }
+
+ public Enumeration<URL> findResources(final String name) throws IOException
{
+ Enumeration<URL> res = super.findResources(name);
+ return res;
+ }
+
+ protected Class<?> findClass(String name) throws ClassNotFoundException {
+ Class<?> res = null;
+ try {
+ res = super.findClass(name);
+ } catch (ClassNotFoundException cnfe) {
+ throw cnfe;
+ }
+ return res;
+ }
+
+ protected synchronized Class<?> loadClass(String name, boolean resolve) throws
ClassNotFoundException {
+ Class<?> res = null;
+ try {
+ res = super.loadClass(name, resolve);
+ } catch (ClassNotFoundException cnfe) {
+ throw cnfe;
+ }
+ return res;
+ }
+
+ public Class<?> loadClass(String name) throws ClassNotFoundException {
+ Class<?> res = null;
+ try {
+ res = super.loadClass(name);
+ } catch (ClassNotFoundException cnfe) {
+ throw cnfe;
+ }
+ return res;
+ }
+ };
+ }
+ });
+ return urlClassLoader;
+ }
+
public class StringWriter extends Writer {
public String res = new String();
@@ -246,12 +309,20 @@
drivers = null;
}
- @SuppressWarnings("unchecked")
+ static Object testStaticObj;
+
+ @SuppressWarnings({"unchecked", "unused", "nls"})
public void initExecutionContext() {
/**/
//URLClassLoader urlClassLoader = createClassLoader();
- JarClassLoader urlClassLoader = createJarClassLoader();
- /**/
+ //JarClassLoader urlClassLoader = createJarClassLoader();
+ final ConsoleConfigClassLoader urlClassLoader = createJarClassLoader2();
+ /** /
+ prevClassLoader = Thread.currentThread().getContextClassLoader();
+ Thread.currentThread().setContextClassLoader(urlClassLoader);
+ ClassLoader prevClassLoader22 = Thread.currentThread().getContextClassLoader();
+ //
+ Class<Driver> driverClass22 = null;
Class<Driver> driverClass = null;
try {
driverClass =
(Class<Driver>)urlClassLoader.loadClass("com.mysql.jdbc.Driver");
//$NON-NLS-1$
@@ -261,21 +332,36 @@
assertNotNull(driverClass);
/**/
int numRedDrivers = 0;
- StringWriter wr = new StringWriter();
- PrintWriter pw = new PrintWriter(wr);
- DriverManager.setLogWriter(pw);
+ //StringWriter wr = new StringWriter();
+ //PrintWriter pw = new PrintWriter(wr);
+ //DriverManager.setLogWriter(pw);
Driver driver = null;
- /**/
+ /** /
+ Connection connection = null;
+ String test = ""; //$NON-NLS-1$
try {
driver = driverClass.newInstance();
- DriverManager.registerDriver(driver);
+ //DriverManager.registerDriver(driver);
+ //driverClass = ReflectHelper.classForName("com.mysql.jdbc.Driver");
//$NON-NLS-1$
+ //driver = driverClass.newInstance();
+ //DriverManager.registerDriver(driver);
//DriverManager.deregisterDriver(driver);
java.util.Enumeration<Driver> drEnum = DriverManager.getDrivers();
while (drEnum.hasMoreElements()) {
driver = drEnum.nextElement();
- DriverManager.deregisterDriver(driver);
+// DriverManager.deregisterDriver(driver);
numRedDrivers++;
}
+ java.util.Properties info = new java.util.Properties();
+ info.put("user", "root");
+ info.put("password", "p@ssw0rd2");
+ connection = driver.connect("jdbc:mysql://localhost:3306/jpa", info);
+ //connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/jpa", "root",
"p@ssw0rd");
+ test = connection.getCatalog();
+ connection.close();
+ DriverManager.deregisterDriver(driver);
+ //} catch (ClassNotFoundException e) {
+ // e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (InstantiationException e) {
@@ -283,18 +369,23 @@
} catch (IllegalAccessException e) {
e.printStackTrace();
}
+ System.out.print(test);
+ System.out.print(numRedDrivers);
+ /** /
+ System.out.print(wr.res);
/**/
- System.out.print(wr.res);
- /** /
DefaultExecutionContext dec = new DefaultExecutionContext(getName(), urlClassLoader);
executionContext = new WeakReference<ExecutionContext>(dec);
- /** /
+ /**/
ExecutionContext.Command command = new ExecutionContext.Command() {
public Object execute() {
try {
Class<Driver> driverClass = null;
+ //Class.forName("com.mysql.jdbc.Driver"); //$NON-NLS-1$
//if (driverClass != null) {
- //driverClass = ReflectHelper.classForName("com.mysql.jdbc.Driver");
//$NON-NLS-1$
+ driverClass = ReflectHelper.classForName("com.mysql.jdbc.Driver");
//$NON-NLS-1$
+ Driver driver2 = driverClass.newInstance();
+ //DriverManager.registerDriver(driver2);
ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
if (contextClassLoader != null) {
driverClass =
(Class<Driver>)contextClassLoader.loadClass("com.mysql.jdbc.Driver");
//$NON-NLS-1$
@@ -303,13 +394,184 @@
//driverClass.newInstance();
//DriverManager.registerDriver(driverClass.newInstance());
//}
+ java.util.Properties info = new java.util.Properties();
+ info.put("user", "root");
+ info.put("password", "p@ssw0rd2");
+
+ /** /
+ try {
+ Connection connection =
driver2.connect("jdbc:mysql://localhost:3306/jpa", info);
+ //Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/jpa", "root",
"p@ssw0rd"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
+ //String test = connection.getCatalog();
+
+ //System.out.println(test);
+ //System.out.println(test);
+ connection.close();
+ } catch (SQLException e) {
+ e.printStackTrace();
+ }
+ /**/
+
+ Object obj = null;
+ Field f = null;
+
+ /**/
+ Class<com.mysql.jdbc.Connection> connClass =
ReflectHelper.classForName("com.mysql.jdbc.Connection"); //$NON-NLS-1$
+ f = connClass.getDeclaredField("cancelTimer");
+ f.setAccessible(true);
+ java.util.Timer timer = (java.util.Timer)f.get(null);
+ if (timer != null) {
+ timer.cancel();
+ timer.purge();
+ }
+ f.set(null, null);
+
+ Class<com.mysql.jdbc.LoadBalancingConnectionProxy>
classLoadBalancingConnectionProxyClass =
ReflectHelper.classForName("com.mysql.jdbc.LoadBalancingConnectionProxy");
//$NON-NLS-1$
+ f =
classLoadBalancingConnectionProxyClass.getDeclaredField("getLocalTimeMethod");
+ f.setAccessible(true);
+ obj = f.get(null);
+ f.set(null, null);
+
+ Class<com.mysql.jdbc.StandardSocketFactory> classStandardSocketFactory =
ReflectHelper.classForName("com.mysql.jdbc.StandardSocketFactory");
//$NON-NLS-1$
+ f = classStandardSocketFactory.getDeclaredField("setTraficClassMethod");
+ f.setAccessible(true);
+ obj = f.get(null);
+ f.set(null, null);
+
+ Class<com.mysql.jdbc.StringUtils> classStringUtils =
ReflectHelper.classForName("com.mysql.jdbc.StringUtils"); //$NON-NLS-1$
+ f = classStringUtils.getDeclaredField("toPlainStringMethod");
+ f.setAccessible(true);
+ obj = f.get(null);
+ f.set(null, null);
+ /**/
+
+ Class<com.mysql.jdbc.Util> classUtil =
ReflectHelper.classForName("com.mysql.jdbc.Util"); //$NON-NLS-1$
+ f = classUtil.getDeclaredField("systemNanoTimeMethod");
+ f.setAccessible(true);
+ obj = f.get(null);
+ f.set(null, null);
+ /** /
+ //
+ f = classUtil.getDeclaredField("DEFAULT_TIMEZONE");
+ f.setAccessible(true);
+ obj = f.get(null);
+ setStaticFinalField(f, null);
+ //
+ f = classUtil.getDeclaredField("enclosingInstance");
+ f.setAccessible(true);
+ obj = f.get(null);
+ f.set(null, null);
+ /** /
+ testStaticObj = Locale.getDefault();
+ /**/
+ ResourceBundle temp =
ResourceBundle.getBundle("com.mysql.jdbc.LocalizedErrorMessages",
Locale.getDefault(),
+ urlClassLoader);
+ /**/
+ final String resName =
"com.mysql.jdbc.LocalizedErrorMessages".replace('.', '/') +
".properties";
+ InputStream stream =
(InputStream)java.security.AccessController.doPrivileged(
+ new java.security.PrivilegedAction() {
+ public Object run() {
+ if (urlClassLoader != null) {
+ return urlClassLoader.getResourceAsStream(resName);
+ } else {
+ return ClassLoader.getSystemResourceAsStream(resName);
+ }
+ }
+ }
+ );
+
+ if (stream != null) {
+ // make sure it is buffered
+ stream = new java.io.BufferedInputStream(stream);
+ java.util.PropertyResourceBundle prb = new PropertyResourceBundle(stream);
+ stream.close();
+ }
+ //
+ /**/
+ //
+ Class<com.mysql.jdbc.Messages> classMessages =
ReflectHelper.classForName("com.mysql.jdbc.Messages"); //$NON-NLS-1$
+ f = classMessages.getDeclaredField("BUNDLE_NAME");
+ f.setAccessible(true);
+ obj = f.get(null);
+ setStaticFinalField(f, null);
+ //
+ f = classMessages.getDeclaredField("RESOURCE_BUNDLE");
+ f.setAccessible(true);
+ obj = f.get(null);
+ setStaticFinalField(f, null);
+ /**/
+ //Class<java.util.ResourceBundle> classResourceBundle =
ReflectHelper.classForName("java.util.ResourceBundle"); //$NON-NLS-1$
+ //Class<java.util.PropertyResourceBundle> classPropertyResourceBundle =
(Class<java.util.PropertyResourceBundle>)obj.getClass();
+ Class<java.util.PropertyResourceBundle> classPropertyResourceBundle =
(Class<java.util.PropertyResourceBundle>)temp.getClass();
+ Class<java.util.ResourceBundle> classResourceBundle =
(Class<ResourceBundle>)classPropertyResourceBundle.getSuperclass(); //$NON-NLS-1$
+ f = classResourceBundle.getDeclaredField("cacheKey");
+ f.setAccessible(true);
+ obj = f.get(null);
+ setStaticFinalField(f, null);
+ //
+ f = classResourceBundle.getDeclaredField("underConstruction");
+ f.setAccessible(true);
+ obj = f.get(null);
+ setStaticFinalField(f, null);
+ //
+ f = classResourceBundle.getDeclaredField("NOT_FOUND");
+ f.setAccessible(true);
+ obj = f.get(null);
+ setStaticFinalField(f, null);
+ //
+ f = classResourceBundle.getDeclaredField("cacheList");
+ f.setAccessible(true);
+ obj = f.get(null);
+ setStaticFinalField(f, null);
+ //
+ f = classResourceBundle.getDeclaredField("referenceQueue");
+ f.setAccessible(true);
+ obj = f.get(null);
+ setStaticFinalField(f, null);
+ /**/
+
+ /**/
+ //Class<sun.net.www.protocol.jar.JarURLConnection> classJarURLConnection =
ReflectHelper.classForName("sun.net.www.protocol.jar.JarURLConnection");
//$NON-NLS-1$
+ //f = classJarURLConnection.getDeclaredField("factory");
+ //f.setAccessible(true);
+ //obj = f.get(null);
+ //f.set(null, null);
+ //
+ Class classJarFileFactory = obj.getClass();
+ f = classJarFileFactory.getDeclaredField("fileCache");
+ f.setAccessible(true);
+ obj = f.get(null);
+ HashMap fileCache = (HashMap)obj;
+ f.set(null, null);
+ //
+ f = classJarFileFactory.getDeclaredField("urlCache");
+ f.setAccessible(true);
+ obj = f.get(null);
+ HashMap urlCache = (HashMap)obj;
+ f.set(null, null);
+ //
+ Iterator it = urlCache.keySet().iterator();
+ while (it.hasNext()) {
+ JarFile jarFile = (JarFile)it.next();
+ if (jarFile.getName().equals(DRIVER_TEST_NAME)) {
+ jarFile.close();
+ }
+ }
+ /**/
+ //DriverManager.deregisterDriver(driver2);
//}
//contextClassLoader = null;
//driverClass = null;
- //} catch (InstantiationException e) {
- // e.printStackTrace();
- //} catch (IllegalAccessException e) {
- // e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } catch (SecurityException e) {
+ e.printStackTrace();
+ } catch (NoSuchFieldException e) {
+ e.printStackTrace();
+ } catch (InstantiationException e) {
+ e.printStackTrace();
+ } catch (IllegalAccessException e) {
+ e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
//} catch (SQLException e) {
@@ -333,7 +595,26 @@
urlClassLoader.close();
}
+ private static final String MODIFIERS_FIELD = "modifiers";
+
+ public static void setStaticFinalField(Field field, Object value)
+ throws NoSuchFieldException, IllegalAccessException {
+ /** /
+ field.setAccessible(true);
+ Field modifiersField = Field.class.getDeclaredField(MODIFIERS_FIELD);
+ modifiersField.setAccessible(true);
+ int modifiers = modifiersField.getInt(field);
+ modifiers &= ~Modifier.FINAL;
+ modifiersField.setInt(field, modifiers);
+ sun.reflect.ReflectionFactory reflection =
+ sun.reflect.ReflectionFactory.getReflectionFactory();
+ FieldAccessor fa = reflection.newFieldAccessor(field, false);
+ fa.set(null, value);
+ /**/
+ }
+
public void cleanupExecutionContext() {
+ /** /
if (executionContext != null && executionContext.get() != null) {
executionContext.get().execute(new ExecutionContext.Command() {
@@ -356,7 +637,12 @@
});
executionContext = null;
}
+ /**/
//
+ if (prevClassLoader != null) {
+ Thread.currentThread().setContextClassLoader(prevClassLoader);
+ }
+ //
GarbageCollectionUtil.forceCollectGarbage();
}