KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
23 import java.io.FileWriter JavaDoc;
24 import java.util.jar.JarEntry JavaDoc;
25 import java.util.jar.JarOutputStream JavaDoc;
26 import java.util.jar.Manifest JavaDoc;
27 import junit.framework.*;
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Set JavaDoc;
32 import java.util.StringTokenizer JavaDoc;
33 import java.util.jar.JarFile JavaDoc;
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.types.Parameter;
36 import org.apache.tools.ant.types.selectors.SelectorUtils;
37
38 import org.netbeans.junit.NbTestCase;
39
40 /** Check behaviour of ModuleSelector.
41  *
42  * @author Jaroslav Tulach
43  */

44 public class ModuleSelectorTest extends NbTestCase {
45     private ModuleSelector selector;
46     
47     public ModuleSelectorTest(String JavaDoc testName) {
48         super(testName);
49     }
50
51     protected void setUp() throws Exception JavaDoc {
52         selector = new ModuleSelector();
53     }
54
55     public void testIsSelectedForNotAModule() throws IOException JavaDoc {
56         File JavaDoc noModule = generateJar(new String JavaDoc[0], ModuleDependenciesTest.createManifest ());
57         assertFalse("Not acceptable", selector.isSelected(getWorkDir(), noModule.toString(), noModule));
58     }
59
60     public void testIncludesAllModulesByDefault() throws Exception JavaDoc {
61         Manifest JavaDoc m = ModuleDependenciesTest.createManifest ();
62         m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module");
63         File JavaDoc aModule = generateJar(new String JavaDoc[0], m);
64         assertTrue("Accepted", selector.isSelected(getWorkDir(), aModule.toString(), aModule));
65     }
66     
67     public void testCanExcludeAModule() throws Exception JavaDoc {
68         Parameter p = new Parameter();
69         p.setName("excludeModules");
70         p.setValue("org.my.module");
71         selector.setParameters(new Parameter[] { p });
72         
73         Manifest JavaDoc m = ModuleDependenciesTest.createManifest ();
74         m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module");
75         File JavaDoc aModule = generateJar(new String JavaDoc[0], m);
76         assertFalse("Refused", selector.isSelected(getWorkDir(), aModule.toString(), aModule));
77     }
78     
79     public void testCanShowOnlyExcludedModules() throws Exception JavaDoc {
80         Parameter p = new Parameter();
81         p.setName("excluded");
82         p.setValue("true");
83         Parameter p2 = new Parameter();
84         p2.setName("excludeModules");
85         p2.setValue("org.my.module");
86         selector.setParameters(new Parameter[] { p, p2 });
87         
88         Manifest JavaDoc m = ModuleDependenciesTest.createManifest ();
89         m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module");
90         File JavaDoc aModule = generateJar(new String JavaDoc[0], m);
91         assertTrue("Now we are accepting only excluded modules", selector.isSelected(getWorkDir(), aModule.toString(), aModule));
92     }
93     
94     public void testIsSelectedForNotAModuleIsStillFalseEvenWeAcceptOnlyExcludedModules() throws IOException JavaDoc {
95         Parameter p = new Parameter();
96         p.setName("excluded");
97         p.setValue("true");
98         Parameter p2 = new Parameter();
99         p2.setName("excludeModules");
100         p2.setValue("org.my.module");
101         selector.setParameters(new Parameter[] { p, p2 });
102         
103         
104         File JavaDoc noModule = generateJar(new String JavaDoc[0], ModuleDependenciesTest.createManifest ());
105         assertFalse("Not acceptable", selector.isSelected(getWorkDir(), noModule.toString(), noModule));
106     }
107     
108     public void testCanExcludeACluster() throws Exception JavaDoc {
109         Parameter p = new Parameter();
110         p.setName("includeClusters");
111         p.setValue("nonexistent");
112         selector.setParameters(new Parameter[] { p });
113         
114         Manifest JavaDoc m = ModuleDependenciesTest.createManifest ();
115         m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module");
116         File JavaDoc aModule = generateJar(new String JavaDoc[0], m);
117         new File JavaDoc(getWorkDir(), "update_tracking").mkdir();
118         assertFalse("Refused", selector.isSelected(getWorkDir().getParentFile(), aModule.toString(), aModule));
119     }
120     
121     public void testWhatItDoesOnADirectory() throws Exception JavaDoc {
122         assertFalse("Refused", selector.isSelected(getWorkDir().getParentFile(), getWorkDir().getName(), getWorkDir()));
123     }
124     
125     public void testNoManifest() throws Exception JavaDoc {
126         File JavaDoc aModule = generateJar(new String JavaDoc[] { "some/fake/entry.txt" }, null);
127         assertFalse("Refused", selector.isSelected(getWorkDir().getParentFile(), aModule.toString(), aModule));
128     }
129
130     public void testParsingOfUpdateTrackingFiles() throws Exception JavaDoc {
131         doParsingOfUpdateTrackingFiles(1);
132     }
133     
134     public void testParsingOfUpdateTrackingFilesOnMoreDirs() throws Exception JavaDoc {
135         doParsingOfUpdateTrackingFiles(2);
136     }
137     
138     
139     private void doParsingOfUpdateTrackingFiles(int parents) throws Exception JavaDoc {
140         File JavaDoc updateTracking = new File JavaDoc(getWorkDir(), "update-tracking");
141         updateTracking.mkdirs();
142         assertTrue("Created", updateTracking.isDirectory());
143         
144         Manifest JavaDoc m = ModuleDependenciesTest.createManifest ();
145         m.getMainAttributes().putValue("OpenIDE-Module", "org.my.module");
146         File JavaDoc aModule = generateJar(new String JavaDoc[0], m);
147         
148         File JavaDoc trackingFile = new File JavaDoc(updateTracking, "org-my-module.xml");
149         FileWriter JavaDoc w = new FileWriter JavaDoc(trackingFile);
150         w.write(
151 "<?xml version='1.0' encoding='UTF-8'?>\n" +
152 "<module codename='org.apache.tools.ant.module/3'>\n" +
153     "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n" +
154         "<file name='ant/bin/ant' crc='1536373800'/>\n" +
155         "<file name='ant/bin/ant.bat' crc='3245456472'/>\n" +
156         "<file name='ant/bin/ant.cmd' crc='3819623376'/>\n" +
157         "<file name='ant/bin/antRun' crc='2103827286'/>\n" +
158         "<file name='ant/bin/antRun.bat' crc='2739687679'/>\n" +
159         "<file name='ant/bin/antRun.pl' crc='3955456526'/>\n" +
160 " </module_version>\n" +
161 "</module>\n"
162         );
163         w.close();
164
165         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
166         sb.append(trackingFile.getPath());
167         
168         while (--parents > 0) {
169             File JavaDoc x = new File JavaDoc(getWorkDir(), parents + ".xml");
170             FileWriter JavaDoc xw = new FileWriter JavaDoc(x);
171             xw.write(
172     "<?xml version='1.0' encoding='UTF-8'?>\n" +
173     "<module codename='" + x + "/3'>\n" +
174         "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n" +
175     " </module_version>\n" +
176     "</module>\n"
177             );
178             xw.close();
179             
180             sb.insert(0, File.pathSeparator);
181             sb.insert(0, x.getPath());
182         }
183         
184         Parameter p = new Parameter();
185         p.setName("updateTrackingFiles");
186         p.setValue(sb.toString());
187         selector.setParameters(new Parameter[] { p });
188         
189         assertTrue("module accepted", selector.isSelected(getWorkDir(), aModule.toString(), aModule));
190         assertTrue("its file as well", selector.isSelected(getWorkDir(), "ant/bin/ant.bat", new File JavaDoc(aModule.getParent(), "ant/bin/ant.bat")));
191         assertTrue("also the tracking file is accepted", selector.isSelected(getWorkDir(), "update-tracking/" + trackingFile.getName(), trackingFile));
192     }
193     
194     private final File JavaDoc createNewJarFile () throws IOException JavaDoc {
195         int i = 0;
196         for (;;) {
197             File JavaDoc f = new File JavaDoc (this.getWorkDir(), i++ + ".jar");
198             if (!f.exists ()) return f;
199         }
200     }
201
202     protected final File JavaDoc generateJar (String JavaDoc[] content, Manifest JavaDoc manifest) throws IOException JavaDoc {
203         File JavaDoc f = createNewJarFile ();
204         
205         JarOutputStream JavaDoc os;
206         if (manifest != null) {
207             os = new JarOutputStream JavaDoc (new FileOutputStream JavaDoc (f), manifest);
208         } else {
209             os = new JarOutputStream JavaDoc (new FileOutputStream JavaDoc (f));
210         }
211         
212         for (int i = 0; i < content.length; i++) {
213             os.putNextEntry(new JarEntry JavaDoc (content[i]));
214             os.closeEntry();
215         }
216         os.closeEntry ();
217         os.close();
218         
219         return f;
220     }
221     
222 }
223
Popular Tags