001: package net.sourceforge.squirrel_sql.fw.util;
002:
003: import java.io.IOException;
004: import java.net.URL;
005:
006: import net.sourceforge.squirrel_sql.BaseSQuirreLTestCase;
007: import net.sourceforge.squirrel_sql.client.plugin.IPlugin;
008: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
009: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
010: import net.sourceforge.squirrel_sql.test.TestUtil;
011:
012: public class MyURLClassLoaderTest extends BaseSQuirreLTestCase {
013:
014: private static String filePrefix = "file:";
015:
016: private static String COMMONS_CLI_JAR = "squirrel-sql-dist/squirrel-sql/core/dist/lib/commons-cli.jar";
017:
018: private static String DBCOPY_JAR = "squirrel-sql-dist/squirrel-sql/plugins/dbcopy/dist/dbcopy.jar";
019:
020: private static String BOGUS_ZIP_FILE = "squirrel-sql-dist/squirrel-sql/core/dist/log4j.properties";
021:
022: private static String EXTERNAL_DEPENDS_JAR = "plugins/syntax/lib/syntax.jar";
023:
024: private static final String COMMONS_CLI_OPTION_CLASS = "org.apache.commons.cli.Option";
025:
026: private static final String DBCOPY_PLUGIN_CLASS = "net.sourceforge.squirrel_sql.plugins.dbcopy.DBCopyPlugin";
027:
028: /** Logger for this class. */
029: private final static ILogger s_log = LoggerController
030: .createLogger(MyURLClassLoaderTest.class);
031:
032: protected void setUp() throws Exception {
033: super .setUp();
034:
035: }
036:
037: /**
038: * Try to locate the file using different prefixes so that the test can be
039: * run from the ant script, or from in Eclipse.
040: *
041: * @param filename
042: * @return
043: */
044: private String getFilePrefixedFilename(String filename) {
045: String distDir = TestUtil
046: .findAncestorSquirrelSqlDistDirBase("squirrel-sql-dist");
047: if (distDir == null) {
048: throw new IllegalStateException(
049: "Couldn't locate distDir (squirrel-sql-dist)");
050: }
051: String result = filePrefix + distDir + filename;
052: System.out.println("Found file: " + result);
053: return result;
054: }
055:
056: protected void tearDown() throws Exception {
057: super .tearDown();
058: }
059:
060: // Constructor Tests
061:
062: public void testMyURLClassLoaderString() {
063: try {
064: MyURLClassLoader loader = new MyURLClassLoader(".");
065: loader.getAssignableClasses(IPlugin.class, s_log);
066: } catch (IOException e) {
067: fail(e.getMessage());
068: }
069: }
070:
071: public void testMyURLClassLoaderURL() {
072: getIPluginAssignableClasses(getFilePrefixedFilename(COMMONS_CLI_JAR));
073: }
074:
075: public void testMyURLClassLoaderURLArray() {
076: try {
077: URL url = new URL(getFilePrefixedFilename(DBCOPY_JAR));
078: MyURLClassLoader loader = new MyURLClassLoader(
079: new URL[] { url });
080: loader.findClass(DBCOPY_PLUGIN_CLASS);
081: } catch (Exception e) {
082: e.printStackTrace();
083: fail(e.getMessage());
084: }
085: }
086:
087: // Method Tests
088:
089: public void testAddClassLoaderListener() {
090: MyClassLoaderListener listener = new MyClassLoaderListener();
091: MyURLClassLoader loader = getLoader(getFilePrefixedFilename(COMMONS_CLI_JAR));
092: loader.addClassLoaderListener(listener);
093: }
094:
095: public void testRemoveClassLoaderListener() {
096: MyClassLoaderListener listener = new MyClassLoaderListener();
097: MyURLClassLoader loader = getLoader(getFilePrefixedFilename(DBCOPY_JAR));
098: loader.addClassLoaderListener(listener);
099: try {
100: loader.getAssignableClasses(IPlugin.class, s_log);
101: } catch (Exception e) {
102: // fail ??
103: }
104: assertEquals(1, listener.loadingZipFileCount);
105: listener.loadingZipFileCount = 0;
106: loader.removeClassLoaderListener(listener);
107:
108: try {
109: loader.getAssignableClasses(IPlugin.class, s_log);
110: } catch (Exception e) {
111: // fail ??
112: }
113: assertEquals(0, listener.loadingZipFileCount);
114: }
115:
116: @SuppressWarnings("unchecked")
117: public void testGetAssignableClasses() {
118: Class[] classes = getIPluginAssignableClasses(getFilePrefixedFilename(COMMONS_CLI_JAR));
119: assertEquals(0, classes.length);
120:
121: classes = getIPluginAssignableClasses(getFilePrefixedFilename(DBCOPY_JAR));
122: assertEquals(1, classes.length);
123:
124: classes = getIPluginAssignableClasses(getFilePrefixedFilename(BOGUS_ZIP_FILE));
125: assertEquals(0, classes.length);
126:
127: classes = getIPluginAssignableClasses(getFilePrefixedFilename(EXTERNAL_DEPENDS_JAR));
128: assertEquals(0, classes.length);
129:
130: }
131:
132: public void testFindClassString() {
133: MyURLClassLoader loader = getLoader(getFilePrefixedFilename(COMMONS_CLI_JAR));
134: try {
135: loader.findClass(COMMONS_CLI_OPTION_CLASS);
136: } catch (Exception e) {
137: fail(e.getMessage());
138: }
139: }
140:
141: public void testClassHasBeenLoaded() {
142: try {
143: MyURLClassLoader loader = getLoader(getFilePrefixedFilename(COMMONS_CLI_JAR));
144: loader.findClass(COMMONS_CLI_OPTION_CLASS);
145: loader
146: .classHasBeenLoaded(org.apache.commons.cli.Option.class);
147: } catch (Exception e) {
148: fail(e.getMessage());
149: }
150: }
151:
152: // HELPERS
153: @SuppressWarnings("unchecked")
154: private Class[] getIPluginAssignableClasses(String urlToSearch) {
155: MyURLClassLoader loader = getLoader(urlToSearch);
156: return loader.getAssignableClasses(IPlugin.class, s_log);
157: }
158:
159: private MyURLClassLoader getLoader(String urlToSearch) {
160: MyURLClassLoader result = null;
161: try {
162: URL url = new URL(urlToSearch);
163: MyURLClassLoader loader = new MyURLClassLoader(url);
164: return loader;
165: } catch (Exception e) {
166: fail(e.getMessage());
167: }
168: return result;
169: }
170:
171: private static class MyClassLoaderListener implements
172: ClassLoaderListener {
173:
174: public int loadingZipFileCount = 0;
175:
176: /* (non-Javadoc)
177: * @see net.sourceforge.squirrel_sql.fw.util.ClassLoaderListener#finishedLoadingZipFiles()
178: */
179: public void finishedLoadingZipFiles() {
180: // TODO Auto-generated method stub
181:
182: }
183:
184: /* (non-Javadoc)
185: * @see net.sourceforge.squirrel_sql.fw.util.ClassLoaderListener#loadedZipFile(java.lang.String)
186: */
187: public void loadedZipFile(String filename) {
188: loadingZipFileCount++;
189: }
190:
191: }
192: }
|