KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > nbbuild > TestDistFilterTest


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.nbbuild;
21
22 import java.io.File JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import junit.framework.*;
28 import org.apache.tools.ant.Project;
29 import org.netbeans.junit.*;
30
31 /**
32  *
33  * @author pzajac
34  */

35 public class TestDistFilterTest extends NbTestCase {
36     private static final String JavaDoc ORG_OPENIDE_UNIT = "unit/platform5/org-openide";
37     private static final String JavaDoc ORG_OPENIDE_FS = "unit/platform5/org-openide-fs";
38     private static final String JavaDoc ORG_OPENIDE_LOADERS = "unit/platform5/org-openide-loaders";
39     private static final String JavaDoc ORG_OPENIDE_LOADERS_QA = "qa-functional/platform5/org-openide-loaders";
40     private static final String JavaDoc ORG_OPENIDE_FS_QA = "qa-functional/platform5/org-openide-fs";
41     private static final String JavaDoc ORG_OPENIDE_NO_LOADERS = "unit/platform5/org-openide-no-loaders";
42     public TestDistFilterTest(java.lang.String JavaDoc testName) {
43         super(testName);
44     }
45     
46     public void testDistFilter() throws IOException JavaDoc {
47         TestDistFilter filter = new TestDistFilter();
48         filter.setTestDistDir(getWorkDir());
49         createModule(ORG_OPENIDE_UNIT,createConfig("unstable","code"));
50         filter.setAttribs("stable");
51         filter.setHarness("junit");
52         Project prj = getProject();
53         filter.setProject(prj);
54         filter.setTesttype("unit");
55         
56         filter.setTestListProperty("list.prop");
57         filter.execute();
58         assertProperty(prj,"list.prop",new String JavaDoc[]{});
59         
60         createModule(ORG_OPENIDE_UNIT,createConfig("stable","code"));
61         
62         filter.setTestListProperty("list.prop1");
63         filter.execute();
64         assertProperty(prj,"list.prop1",new String JavaDoc[]{ORG_OPENIDE_UNIT});
65         
66         createModule(ORG_OPENIDE_FS,createConfig("stable","ide"));
67         filter.setTestListProperty("list.prop2");
68         filter.execute();
69         assertProperty(prj,"list.prop2",new String JavaDoc[]{ORG_OPENIDE_UNIT});
70  
71         filter.setTestListProperty("list.prop3");
72         createModule(ORG_OPENIDE_LOADERS,createConfig("stable","code"));
73         filter.execute();
74         assertProperty(prj,"list.prop3",new String JavaDoc[]{ORG_OPENIDE_UNIT,ORG_OPENIDE_LOADERS});
75
76         createModule(ORG_OPENIDE_FS_QA,createConfig("stable","code"));
77         filter.setTestListProperty("list.prop4");
78         filter.execute();
79         assertProperty(prj,"list.prop4",new String JavaDoc[]{ORG_OPENIDE_UNIT,ORG_OPENIDE_LOADERS});
80
81         filter.setTestListProperty("list.prop5");
82         createModule(ORG_OPENIDE_LOADERS,createConfig("unstable","code"));
83         filter.execute();
84         assertProperty(prj,"list.prop5",new String JavaDoc[]{ORG_OPENIDE_UNIT});
85    
86         filter.setHarness("xtest");
87         filter.setTestListProperty("list.prop6");
88         filter.execute();
89         assertProperty(prj,"list.prop6",new String JavaDoc[]{ORG_OPENIDE_UNIT,ORG_OPENIDE_FS});
90
91         filter.setTestListProperty("list.prop7");
92         filter.setTesttype("qa-functional");
93         filter.execute();
94         assertProperty(prj,"list.prop7",new String JavaDoc[]{ORG_OPENIDE_FS_QA});
95    }
96     
97    public void testAllTestTypes() throws Exception JavaDoc {
98         TestDistFilter filter = new TestDistFilter();
99         filter.setTestDistDir(getWorkDir());
100         Project prj = getProject();
101         filter.setProject(prj);
102         filter.setTesttype("all");
103         filter.setHarness("xtest");
104         createModule(ORG_OPENIDE_UNIT,createConfig("stable","code"));
105         createModule(ORG_OPENIDE_FS_QA,createConfig("stable","code"));
106         filter.setTestListProperty("list.prop");
107         filter.execute();
108         assertProperty(prj,"list.prop",new String JavaDoc[]{ORG_OPENIDE_UNIT,ORG_OPENIDE_FS_QA});
109    }
110    
111    public void testComplexAttribs() throws Exception JavaDoc {
112         TestDistFilter filter = new TestDistFilter();
113         filter.setTestDistDir(getWorkDir());
114         Project prj = getProject();
115         createModule(ORG_OPENIDE_UNIT,createConfig("stable|xx , aa","code"));
116         filter.setProject(prj);
117         filter.setTesttype("all");
118         filter.setHarness("xtest");
119         filter.setAttribs("stable");
120         filter.setTestListProperty("list.prop");
121         filter.execute();
122         assertProperty(prj,"list.prop",new String JavaDoc[]{ORG_OPENIDE_UNIT});
123        
124         filter.setAttribs("xx");
125         filter.setTestListProperty("list.prop2");
126         filter.execute();
127         assertProperty(prj,"list.prop2",new String JavaDoc[]{ORG_OPENIDE_UNIT});
128  
129         filter.setAttribs("aa");
130         filter.setTestListProperty("list.prop3");
131         filter.execute();
132         assertProperty(prj,"list.prop3",new String JavaDoc[]{ORG_OPENIDE_UNIT});
133
134         filter.setAttribs("");
135         filter.setTestListProperty("list.prop4");
136         filter.execute();
137         assertProperty(prj,"list.prop4",new String JavaDoc[]{ORG_OPENIDE_UNIT});
138         
139         filter.setAttribs(null);
140         filter.setTestListProperty("list.prop5");
141         filter.execute();
142         assertProperty(prj,"list.prop5",new String JavaDoc[]{ORG_OPENIDE_UNIT});
143
144         filter.setAttribs("nonsence");
145         filter.setTestListProperty("list.prop6");
146         filter.execute();
147         assertProperty(prj,"list.prop6",new String JavaDoc[]{});
148    }
149    public void testComplexAttribs2 () throws IOException JavaDoc {
150         TestDistFilter filter = new TestDistFilter();
151         filter.setTestDistDir(getWorkDir());
152         Project prj = getProject();
153         filter.setProject(prj);
154         createModule(ORG_OPENIDE_LOADERS_QA,createConfig("validation or full or ds or i18n or dspool","code"));
155         filter.setAttribs("validation");
156         filter.setHarness("xtest");
157         filter.setTesttype("qa-functional");
158         filter.setTestListProperty("list.prop1");
159         filter.execute();
160         assertProperty(prj,"list.prop1",new String JavaDoc[]{ORG_OPENIDE_LOADERS_QA});
161       
162    }
163     public void testRequiredModules() throws IOException JavaDoc {
164         TestDistFilter filter = new TestDistFilter();
165         filter.setTestDistDir(getWorkDir());
166         filter.setAttribs("stable");
167         filter.setHarness("junit");
168         Project prj = getProject();
169         filter.setProject(prj);
170         filter.setTesttype("unit");
171         
172         String JavaDoc FS = "modules/org-openide-filesystems.jar";
173         String JavaDoc LOADERS = "modules/org-openide-loaders.jar";
174         createModule(ORG_OPENIDE_UNIT,createConfig("stable","code"),LOADERS);
175         createModule(ORG_OPENIDE_FS,createConfig("stable","code"),FS);
176         createModule(ORG_OPENIDE_LOADERS,createConfig("stable","code"),FS + ":" + LOADERS );
177         createModule(ORG_OPENIDE_NO_LOADERS,createConfig("stable","code"),FS);
178         filter.setRequiredModules("org-openide-filesystems.jar");
179         filter.setTestListProperty("list.prop");
180         filter.execute();
181         assertProperty(prj,"list.prop",new String JavaDoc[]{ORG_OPENIDE_FS,ORG_OPENIDE_LOADERS,ORG_OPENIDE_NO_LOADERS});
182
183         filter.setRequiredModules(null);
184         filter.setTestListProperty("list.prop1");
185         filter.execute();
186         assertProperty(prj,"list.prop1",new String JavaDoc[]{ORG_OPENIDE_UNIT,ORG_OPENIDE_FS,ORG_OPENIDE_LOADERS,ORG_OPENIDE_NO_LOADERS});
187
188         filter.setRequiredModules("");
189         filter.setTestListProperty("list.prop11");
190         filter.execute();
191         assertProperty(prj,"list.prop11",new String JavaDoc[]{ORG_OPENIDE_UNIT,ORG_OPENIDE_FS,ORG_OPENIDE_LOADERS,ORG_OPENIDE_NO_LOADERS});
192         
193         filter.setRequiredModules("org-openide-loaders.jar");
194         filter.setTestListProperty("list.prop2");
195         filter.execute();
196         assertProperty(prj,"list.prop2",new String JavaDoc[]{ORG_OPENIDE_UNIT,ORG_OPENIDE_LOADERS});
197
198         filter.setRequiredModules("none.jar");
199         filter.setTestListProperty("list.prop3");
200         filter.execute();
201         assertProperty(prj,"list.prop3",new String JavaDoc[]{});
202         
203         filter.setRequiredModules("org-openide-loaders.jar,org-openide-filesystems.jar");
204         filter.setTestListProperty("list.prop4");
205         filter.execute();
206         assertProperty(prj,"list.prop4",new String JavaDoc[]{ORG_OPENIDE_LOADERS,ORG_OPENIDE_UNIT,ORG_OPENIDE_FS,ORG_OPENIDE_NO_LOADERS});
207         
208     }
209    
210     private String JavaDoc createConfig(String JavaDoc attribs,String JavaDoc executor) {
211         return "<?xml version='1.0' encoding='UTF-8'?>\n" +
212                 "<!DOCTYPE mconfig PUBLIC '-//NetBeans//DTD XTest cfg 1.0//EN' 'http://www.netbeans.org/dtds/xtest-cfg-1_0.dtd'>\n" +
213                 "<mconfig name='Configuration of refactoring unit tests'>\n" +
214                     "<testbag testattribs='" + attribs + "' executor='" + executor + "' name='Refactoring unit tests'>\n" +
215                         "<testset dir='unit/src'>\n" +
216                             "<patternset>\n" +
217                                 "<include name='org/netbeans/test/refactoring/encapsulate/EncapsulateTest.class'/>\n" +
218                             "</patternset>\n" +
219                         "</testset>\n" +
220                     "</testbag>\n" +
221                 "</mconfig>\n";
222     }
223
224     private void createModule(String JavaDoc path, String JavaDoc xml) throws IOException JavaDoc {
225         File JavaDoc dir = new File JavaDoc(getWorkDir(),path);
226         dir.mkdirs();
227         File JavaDoc cfg = new File JavaDoc(dir,(path.startsWith("unit")) ? "cfg-unit.xml" : "cfg-qa-functional.xml");
228         PrintStream JavaDoc ps = new PrintStream JavaDoc(cfg);
229         try {
230             ps.print(xml);
231         } finally {
232             ps.close();
233         }
234     }
235
236     private Project getProject() throws IOException JavaDoc {
237         Project project = new Project();
238         project.setBaseDir(getWorkDir());
239         return project;
240     }
241
242     private void assertProperty(Project prj, String JavaDoc propName, String JavaDoc modules[]) throws IOException JavaDoc {
243         String JavaDoc listModules = prj.getProperty(propName);
244         assertNotNull("prop " + propName + " was not defined",listModules);
245         log(" listModules " + listModules);
246         String JavaDoc arrayModules[] = (listModules.length() == 0) ? new String JavaDoc[0] :listModules.split(":");
247         Set JavaDoc set1 = new HashSet JavaDoc();
248         for (int i = 0 ; i < arrayModules.length ; i++) {
249             String JavaDoc module = arrayModules[i];
250             if (module.length() == 1 && i < arrayModules.length + 1) {
251                 // module is e:/dd/dd/ on windows
252
module = module + ":" + arrayModules[++i];
253             }
254             log(i + " = " + module );
255             set1.add(new File JavaDoc(module));
256         }
257         Set JavaDoc set2 = new HashSet JavaDoc();
258         for (int i = 0 ; i < modules.length ; i++) {
259             set2.add(new File JavaDoc(getWorkDir(),modules[i]));
260         }
261         assertEquals("paths length",set2.size(),set1.size());
262         assertEquals("Different paths: ", set2,set1);
263     }
264
265     private void createModule(String JavaDoc path, String JavaDoc xml, String JavaDoc runcp) throws IOException JavaDoc {
266         createModule(path,xml);
267         File JavaDoc dir = new File JavaDoc(getWorkDir(),path);
268         File JavaDoc props = new File JavaDoc(dir,"test.properties");
269         PrintStream JavaDoc ps = new PrintStream JavaDoc(props);
270         try {
271             ps.println("test.unit.run.cp=" + runcp);
272         } finally {
273             ps.close();
274         }
275     }
276 }
277
Popular Tags