KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > sql > execute > ui > util > TestCaseDataFactory


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.sql.execute.ui.util;
21
22 import java.io.File JavaDoc;
23 import java.io.FilenameFilter JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.List JavaDoc;
28 import org.netbeans.junit.Manager;
29
30 /**
31  *
32  * @author luke
33  */

34 public class TestCaseDataFactory {
35    
36     public static String JavaDoc DB_SQLCREATE="dbcreate.sql";
37     public static String JavaDoc DB_SQLSELECT="dbselect.sql";
38     public static String JavaDoc DB_TEXT= "dbdata.txt";
39     public static String JavaDoc DB_PROP= "dbprop.properties";
40     public static String JavaDoc DB_SQLDEL="dbdel.sql";
41     public static String JavaDoc DB_JARS="jar";
42     public static String JavaDoc[] FILES={DB_SQLCREATE,DB_PROP,DB_SQLDEL,DB_SQLSELECT,DB_TEXT};
43     private List JavaDoc list=new ArrayList JavaDoc();
44     private static TestCaseDataFactory factory;
45     
46     public static TestCaseDataFactory getTestCaseFactory() throws Exception JavaDoc{
47         
48         if(factory==null){
49           
50           factory=new TestCaseDataFactory();
51           factory.process();
52
53         }
54         return factory;
55     }
56     
57     private TestCaseDataFactory() throws Exception JavaDoc {
58     }
59     
60     private File JavaDoc getDataDir() {
61        
62         
63         String JavaDoc className = getClass().getName();
64         URL JavaDoc url = this.getClass().getResource(className.substring(className.lastIndexOf('.')+1)+".class"); // NOI18N
65
File JavaDoc dataDir = new File JavaDoc(url.getFile()).getParentFile();
66         int index = 0;
67         while((index = className.indexOf('.', index)+1) > 0) {
68                 dataDir = dataDir.getParentFile();
69         }
70         dataDir = new File JavaDoc(dataDir.getParentFile(), "data"); //NOI18N
71
return Manager.normalizeFile(dataDir);
72         
73     }
74     
75     private void process() throws Exception JavaDoc{
76        File JavaDoc data_dir=getDataDir();
77        HashMap JavaDoc map=new HashMap JavaDoc();
78        String JavaDoc[] dir=data_dir.list();
79        for(int i=0;i<dir.length;i++){
80            String JavaDoc dir_name=dir[i];
81            String JavaDoc path=data_dir.getAbsolutePath()+File.separator+dir[i];
82            if(new File JavaDoc(path).isDirectory()){
83                 
84                 for(int index=0;index<FILES.length;index++){
85                     File JavaDoc f=new File JavaDoc(path+File.separator+FILES[index]);
86                     if(!f.exists())
87                         throw new RuntimeException JavaDoc("File called "+FILES[index] +"in directory "+dir_name+"doesn't exist");
88                     map.put(FILES[index],f);
89                     
90                 }
91                 String JavaDoc[] s=new File JavaDoc(path).list(new FilenameFilter JavaDoc() {
92                     public boolean accept(File JavaDoc dir, String JavaDoc name) {
93                          return name.endsWith(".jar") || name.endsWith(".zip") ? true : false;
94                     }
95                 });
96                     
97                 for(int iii=0;iii<s.length;iii++){
98                     System.out.println(s[iii]);
99                 }
100             // if(s.length>1)
101
// throw new RuntimeException("one jar or zip file must existed in directory "+dir_name);
102
if(s.length==0)
103                     throw new RuntimeException JavaDoc("the driver doesn't extist for test case called: "+dir_name);
104                 ArrayList JavaDoc drivers=new ArrayList JavaDoc();
105                 for(int myint=0;myint<s.length;myint++){
106                    File JavaDoc file=new File JavaDoc(path+File.separator+s[myint]);
107                    drivers.add(file);
108                    
109                 }
110                 map.put(DB_JARS,drivers.toArray(new File JavaDoc[0]));
111                   
112                 TestCaseContext context=new TestCaseContext(map,dir_name);
113                 list.add(context);
114                 
115            }
116        }
117     }
118     
119     public Object JavaDoc[] getTestCaseContext(){
120            return list.toArray();
121     }
122     
123 }
124
Popular Tags