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.nbbuild;
043:
044: import java.io.FileOutputStream;
045: import java.io.FileWriter;
046: import java.util.jar.JarEntry;
047: import java.util.jar.JarOutputStream;
048: import java.util.jar.Manifest;
049: import junit.framework.*;
050: import java.io.File;
051: import java.io.IOException;
052: import java.util.HashSet;
053: import java.util.Set;
054: import java.util.StringTokenizer;
055: import java.util.jar.JarFile;
056: import org.apache.tools.ant.BuildException;
057: import org.apache.tools.ant.types.Parameter;
058: import org.apache.tools.ant.types.selectors.SelectorUtils;
059:
060: import org.netbeans.junit.NbTestCase;
061:
062: /** Check behaviour of ModuleSelector.
063: *
064: * @author Jaroslav Tulach
065: */
066: public class ModuleSelectorTest extends NbTestCase {
067: private ModuleSelector selector;
068:
069: public ModuleSelectorTest(String testName) {
070: super (testName);
071: }
072:
073: protected void setUp() throws Exception {
074: selector = new ModuleSelector();
075: }
076:
077: public void testIsSelectedForNotAModule() throws IOException {
078: File noModule = generateJar(new String[0],
079: ModuleDependenciesTest.createManifest());
080: assertFalse("Not acceptable", selector.isSelected(getWorkDir(),
081: noModule.toString(), noModule));
082: }
083:
084: public void testIncludesAllModulesByDefault() throws Exception {
085: Manifest m = ModuleDependenciesTest.createManifest();
086: m.getMainAttributes().putValue("OpenIDE-Module",
087: "org.my.module");
088: File aModule = generateJar(new String[0], m);
089: assertTrue("Accepted", selector.isSelected(getWorkDir(),
090: aModule.toString(), aModule));
091: }
092:
093: public void testCanExcludeAModule() throws Exception {
094: Parameter p = new Parameter();
095: p.setName("excludeModules");
096: p.setValue("org.my.module");
097: selector.setParameters(new Parameter[] { p });
098:
099: Manifest m = ModuleDependenciesTest.createManifest();
100: m.getMainAttributes().putValue("OpenIDE-Module",
101: "org.my.module");
102: File aModule = generateJar(new String[0], m);
103: assertFalse("Refused", selector.isSelected(getWorkDir(),
104: aModule.toString(), aModule));
105: }
106:
107: public void testCanShowOnlyExcludedModules() throws Exception {
108: Parameter p = new Parameter();
109: p.setName("excluded");
110: p.setValue("true");
111: Parameter p2 = new Parameter();
112: p2.setName("excludeModules");
113: p2.setValue("org.my.module");
114: selector.setParameters(new Parameter[] { p, p2 });
115:
116: Manifest m = ModuleDependenciesTest.createManifest();
117: m.getMainAttributes().putValue("OpenIDE-Module",
118: "org.my.module");
119: File aModule = generateJar(new String[0], m);
120: assertTrue("Now we are accepting only excluded modules",
121: selector.isSelected(getWorkDir(), aModule.toString(),
122: aModule));
123: }
124:
125: public void testIsSelectedForNotAModuleIsStillFalseEvenWeAcceptOnlyExcludedModules()
126: throws IOException {
127: Parameter p = new Parameter();
128: p.setName("excluded");
129: p.setValue("true");
130: Parameter p2 = new Parameter();
131: p2.setName("excludeModules");
132: p2.setValue("org.my.module");
133: selector.setParameters(new Parameter[] { p, p2 });
134:
135: File noModule = generateJar(new String[0],
136: ModuleDependenciesTest.createManifest());
137: assertFalse("Not acceptable", selector.isSelected(getWorkDir(),
138: noModule.toString(), noModule));
139: }
140:
141: public void testCanExcludeACluster() throws Exception {
142: Parameter p = new Parameter();
143: p.setName("includeClusters");
144: p.setValue("nonexistent");
145: selector.setParameters(new Parameter[] { p });
146:
147: Manifest m = ModuleDependenciesTest.createManifest();
148: m.getMainAttributes().putValue("OpenIDE-Module",
149: "org.my.module");
150: File aModule = generateJar(new String[0], m);
151: new File(getWorkDir(), "update_tracking").mkdir();
152: assertFalse("Refused", selector.isSelected(getWorkDir()
153: .getParentFile(), aModule.toString(), aModule));
154: }
155:
156: public void testWhatItDoesOnADirectory() throws Exception {
157: assertFalse("Refused", selector.isSelected(getWorkDir()
158: .getParentFile(), getWorkDir().getName(), getWorkDir()));
159: }
160:
161: public void testNoManifest() throws Exception {
162: File aModule = generateJar(
163: new String[] { "some/fake/entry.txt" }, null);
164: assertFalse("Refused", selector.isSelected(getWorkDir()
165: .getParentFile(), aModule.toString(), aModule));
166: }
167:
168: public void testParsingOfUpdateTrackingFiles() throws Exception {
169: doParsingOfUpdateTrackingFiles(1);
170: }
171:
172: public void testParsingOfUpdateTrackingFilesOnMoreDirs()
173: throws Exception {
174: doParsingOfUpdateTrackingFiles(2);
175: }
176:
177: private void doParsingOfUpdateTrackingFiles(int parents)
178: throws Exception {
179: File updateTracking = new File(getWorkDir(), "update-tracking");
180: updateTracking.mkdirs();
181: assertTrue("Created", updateTracking.isDirectory());
182:
183: Manifest m = ModuleDependenciesTest.createManifest();
184: m.getMainAttributes().putValue("OpenIDE-Module",
185: "org.my.module");
186: File aModule = generateJar(new String[0], m);
187:
188: File trackingFile = new File(updateTracking,
189: "org-my-module.xml");
190: FileWriter w = new FileWriter(trackingFile);
191: w
192: .write("<?xml version='1.0' encoding='UTF-8'?>\n"
193: + "<module codename='org.apache.tools.ant.module/3'>\n"
194: + "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n"
195: + "<file name='ant/bin/ant' crc='1536373800'/>\n"
196: + "<file name='ant/bin/ant.bat' crc='3245456472'/>\n"
197: + "<file name='ant/bin/ant.cmd' crc='3819623376'/>\n"
198: + "<file name='ant/bin/antRun' crc='2103827286'/>\n"
199: + "<file name='ant/bin/antRun.bat' crc='2739687679'/>\n"
200: + "<file name='ant/bin/antRun.pl' crc='3955456526'/>\n"
201: + " </module_version>\n" + "</module>\n");
202: w.close();
203:
204: StringBuffer sb = new StringBuffer();
205: sb.append(trackingFile.getPath());
206:
207: while (--parents > 0) {
208: File x = new File(getWorkDir(), parents + ".xml");
209: FileWriter xw = new FileWriter(x);
210: xw
211: .write("<?xml version='1.0' encoding='UTF-8'?>\n"
212: + "<module codename='"
213: + x
214: + "/3'>\n"
215: + "<module_version specification_version='3.22' origin='installer' last='true' install_time='1124194231878'>\n"
216: + " </module_version>\n" + "</module>\n");
217: xw.close();
218:
219: sb.insert(0, File.pathSeparator);
220: sb.insert(0, x.getPath());
221: }
222:
223: Parameter p = new Parameter();
224: p.setName("updateTrackingFiles");
225: p.setValue(sb.toString());
226: selector.setParameters(new Parameter[] { p });
227:
228: assertTrue("module accepted", selector.isSelected(getWorkDir(),
229: aModule.toString(), aModule));
230: assertTrue("its file as well", selector.isSelected(
231: getWorkDir(), "ant/bin/ant.bat", new File(aModule
232: .getParent(), "ant/bin/ant.bat")));
233: assertTrue("also the tracking file is accepted", selector
234: .isSelected(getWorkDir(), "update-tracking/"
235: + trackingFile.getName(), trackingFile));
236: }
237:
238: private final File createNewJarFile() throws IOException {
239: int i = 0;
240: for (;;) {
241: File f = new File(this .getWorkDir(), i++ + ".jar");
242: if (!f.exists())
243: return f;
244: }
245: }
246:
247: protected final File generateJar(String[] content, Manifest manifest)
248: throws IOException {
249: File f = createNewJarFile();
250:
251: JarOutputStream os;
252: if (manifest != null) {
253: os = new JarOutputStream(new FileOutputStream(f), manifest);
254: } else {
255: os = new JarOutputStream(new FileOutputStream(f));
256: }
257:
258: for (int i = 0; i < content.length; i++) {
259: os.putNextEntry(new JarEntry(content[i]));
260: os.closeEntry();
261: }
262: os.closeEntry();
263: os.close();
264:
265: return f;
266: }
267:
268: }
|