001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.db.sql.execute.ui.util;
043:
044: import java.io.File;
045: import java.io.FilenameFilter;
046: import java.net.URL;
047: import java.util.ArrayList;
048: import java.util.HashMap;
049: import java.util.List;
050: import org.netbeans.junit.Manager;
051:
052: /**
053: *
054: * @author luke
055: */
056: public class TestCaseDataFactory {
057:
058: public static String DB_SQLCREATE = "dbcreate.sql";
059: public static String DB_SQLSELECT = "dbselect.sql";
060: public static String DB_TEXT = "dbdata.txt";
061: public static String DB_PROP = "dbprop.properties";
062: public static String DB_SQLDEL = "dbdel.sql";
063: public static String DB_JARS = "jar";
064: public static String[] FILES = { DB_SQLCREATE, DB_PROP, DB_SQLDEL,
065: DB_SQLSELECT, DB_TEXT };
066: private List list = new ArrayList();
067: private static TestCaseDataFactory factory;
068:
069: public static TestCaseDataFactory getTestCaseFactory()
070: throws Exception {
071:
072: if (factory == null) {
073:
074: factory = new TestCaseDataFactory();
075: factory.process();
076:
077: }
078: return factory;
079: }
080:
081: private TestCaseDataFactory() throws Exception {
082: }
083:
084: private File getDataDir() {
085:
086: String className = getClass().getName();
087: URL url = this .getClass().getResource(
088: className.substring(className.lastIndexOf('.') + 1)
089: + ".class"); // NOI18N
090: File dataDir = new File(url.getFile()).getParentFile();
091: int index = 0;
092: while ((index = className.indexOf('.', index) + 1) > 0) {
093: dataDir = dataDir.getParentFile();
094: }
095: dataDir = new File(dataDir.getParentFile(), "data"); //NOI18N
096: return Manager.normalizeFile(dataDir);
097:
098: }
099:
100: private void process() throws Exception {
101: File data_dir = getDataDir();
102: HashMap map = new HashMap();
103: String[] dir = data_dir.list();
104: for (int i = 0; i < dir.length; i++) {
105: String dir_name = dir[i];
106: String path = data_dir.getAbsolutePath() + File.separator
107: + dir[i];
108: if (new File(path).isDirectory()) {
109:
110: for (int index = 0; index < FILES.length; index++) {
111: File f = new File(path + File.separator
112: + FILES[index]);
113: if (!f.exists())
114: throw new RuntimeException("File called "
115: + FILES[index] + "in directory "
116: + dir_name + "doesn't exist");
117: map.put(FILES[index], f);
118:
119: }
120: String[] s = new File(path).list(new FilenameFilter() {
121: public boolean accept(File dir, String name) {
122: return name.endsWith(".jar")
123: || name.endsWith(".zip") ? true : false;
124: }
125: });
126:
127: for (int iii = 0; iii < s.length; iii++) {
128: System.out.println(s[iii]);
129: }
130: // if(s.length>1)
131: // throw new RuntimeException("one jar or zip file must existed in directory "+dir_name);
132: if (s.length == 0)
133: throw new RuntimeException(
134: "the driver doesn't extist for test case called: "
135: + dir_name);
136: ArrayList drivers = new ArrayList();
137: for (int myint = 0; myint < s.length; myint++) {
138: File file = new File(path + File.separator
139: + s[myint]);
140: drivers.add(file);
141:
142: }
143: map.put(DB_JARS, drivers.toArray(new File[0]));
144:
145: TestCaseContext context = new TestCaseContext(map,
146: dir_name);
147: list.add(context);
148:
149: }
150: }
151: }
152:
153: public Object[] getTestCaseContext() {
154: return list.toArray();
155: }
156:
157: }
|