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.apache.tools.ant.module.api.support;
043:
044: import java.util.Arrays;
045: import java.util.Collections;
046: import java.util.List;
047: import java.util.regex.Pattern;
048: import org.netbeans.junit.NbTestCase;
049: import org.openide.filesystems.FileObject;
050: import org.openide.filesystems.FileUtil;
051: import org.openide.filesystems.XMLFileSystem;
052: import org.openide.loaders.DataObject;
053: import org.openide.nodes.Node;
054: import org.openide.util.Lookup;
055: import org.openide.util.lookup.Lookups;
056: import org.openide.util.lookup.ProxyLookup;
057:
058: // XXX tests needed: runTarget (perhaps)
059:
060: /**
061: * Test functionality of ActionUtils.
062: * @author Jesse Glick
063: */
064: public class ActionUtilsTest extends NbTestCase {
065:
066: public ActionUtilsTest(String name) {
067: super (name);
068: }
069:
070: private FileObject dir, f1, f2, subdir, f3, fx, subdir2, f3a, f4,
071: subsubdir, f5, f5a;
072: private DataObject d1, d2, d3, dx;
073: private Node n1, n2, n3, nx;
074:
075: @Override
076: protected void setUp() throws Exception {
077: super .setUp();
078: clearWorkDir();
079: dir = FileUtil.toFileObject(getWorkDir());
080: f1 = dir.createData("f1.data");
081: f2 = dir.createData("f2");
082: subdir = dir.createFolder("sub");
083: f3 = subdir.createData("f3.data");
084: f3a = subdir.createData("f3a.data");
085: subdir2 = dir.createFolder("subdir2");
086: f4 = subdir2.createData("f3.nondata");
087: subsubdir = subdir2.createFolder("sub");
088: f5 = subdir2.createData("f1.data");
089: f5a = subsubdir.createData("f3.data");
090: fx = new XMLFileSystem().getRoot();
091: d1 = DataObject.find(f1);
092: d2 = DataObject.find(f2);
093: d3 = DataObject.find(f3);
094: dx = DataObject.find(fx);
095: n1 = d1.getNodeDelegate();
096: n2 = d2.getNodeDelegate();
097: n3 = d3.getNodeDelegate();
098: nx = dx.getNodeDelegate();
099: }
100:
101: public void testFindSelectedFiles() throws Exception {
102: assertEquals("one selected file",
103: Collections.singletonList(f1), filesFrom(
104: new Node[] { n1 }, null, null, true));
105: assertEquals("two selected files", Arrays
106: .asList(new FileObject[] { f1, f2 }), filesFrom(
107: new Node[] { n1, n2 }, null, null, true));
108: assertEquals("zero selection", null, filesFrom(new Node[0],
109: null, null, true));
110: assertEquals("not a file selection", null, filesFrom(
111: new Node[] { Node.EMPTY }, null, null, true));
112: assertEquals("not a disk file", null, filesFrom(
113: new Node[] { nx }, null, null, true));
114: assertEquals("order significant", Arrays
115: .asList(new FileObject[] { f2, f1 }), filesFrom(
116: new Node[] { n2, n1 }, null, null, true));
117: assertEquals("one disk file", Collections.singletonList(f1),
118: filesFrom(new Node[] { n1, nx }, null, null, false));
119: assertEquals("one non-disk file", null, filesFrom(new Node[] {
120: n1, nx }, null, null, true));
121: assertEquals("one *.data", Collections.singletonList(f1),
122: filesFrom(new Node[] { n1, n2 }, null, ".data", false));
123: assertEquals("one not *.data", null, filesFrom(new Node[] { n1,
124: n2 }, null, ".data", true));
125: assertEquals("one file in sub/", Collections.singletonList(f3),
126: filesFrom(new Node[] { n1, n3 }, subdir, null, false));
127: assertEquals("one not in sub/", null, filesFrom(new Node[] {
128: n1, n3 }, subdir, null, true));
129: assertEquals("one sub/*.data", Collections.singletonList(f3),
130: filesFrom(new Node[] { n3 }, subdir, ".data", true));
131: assertEquals("duplicates removed (cf. #50644)", Collections
132: .singletonList(f1), filesFrom(new Node[] { n1, n1 },
133: null, null, true));
134: assertEquals("duplicates removed #2 (cf. #50644)", Arrays
135: .asList(new FileObject[] { f1, f2 }), filesFrom(
136: new Node[] { n1, n2, n1 }, null, null, true));
137: }
138:
139: private static Lookup context(Node[] sel) {
140: Lookup[] delegates = new Lookup[sel.length + 1];
141: for (int i = 0; i < sel.length; i++) {
142: delegates[i] = sel[i].getLookup();
143: }
144: delegates[sel.length] = Lookups.fixed((Object[]) sel);
145: return new ProxyLookup(delegates);
146: }
147:
148: private static List<FileObject> filesFrom(Node[] sel,
149: FileObject dir, String suffix, boolean strict) {
150: return files2List(ActionUtils.findSelectedFiles(context(sel),
151: dir, suffix, strict));
152: }
153:
154: public void testAntIncludesList() throws Exception {
155: assertEquals("2 includes", "f1.data,sub/f3.data", ActionUtils
156: .antIncludesList(new FileObject[] { f1, f3 }, dir));
157: assertEquals("1 include", "f1.data", ActionUtils
158: .antIncludesList(new FileObject[] { f1 }, dir));
159: assertEquals("no includes", "", ActionUtils.antIncludesList(
160: new FileObject[0], dir));
161: assertEquals("1 folder include", "sub/**",
162: ActionUtils.antIncludesList(
163: new FileObject[] { subdir }, dir, true));
164: assertEquals("root folder include", "**", ActionUtils
165: .antIncludesList(new FileObject[] { dir }, dir, true));
166: assertEquals("2 folder includes", "sub/**,subdir2/sub/**",
167: ActionUtils.antIncludesList(new FileObject[] { subdir,
168: subsubdir }, dir, true));
169: assertEquals("mixed files and folder includes",
170: "sub/f3.data,subdir2/sub/**", ActionUtils
171: .antIncludesList(new FileObject[] { f3,
172: subsubdir }, dir, true));
173: assertEquals("1 folder include", "sub/*", ActionUtils
174: .antIncludesList(new FileObject[] { subdir }, dir,
175: false));
176: assertEquals("root folder include", "*", ActionUtils
177: .antIncludesList(new FileObject[] { dir }, dir, false));
178: assertEquals("2 folder includes", "sub/*,subdir2/sub/*",
179: ActionUtils.antIncludesList(new FileObject[] { subdir,
180: subsubdir }, dir, false));
181: assertEquals("mixed files and folder includes",
182: "sub/f3.data,subdir2/sub/*", ActionUtils
183: .antIncludesList(new FileObject[] { f3,
184: subsubdir }, dir, false));
185: assertEquals(
186: "antIncludeList(FileObject[], FileObject) delegates to antIncludeList(FileObject[], FileObject, true)",
187: ActionUtils.antIncludesList(
188: new FileObject[] { subdir }, dir), ActionUtils
189: .antIncludesList(new FileObject[] { subdir },
190: dir, true));
191: assertEquals(
192: "antIncludeList(FileObject[], FileObject) delegates to antIncludeList(FileObject[], FileObject, true)",
193: ActionUtils.antIncludesList(new FileObject[] { dir },
194: dir), ActionUtils.antIncludesList(
195: new FileObject[] { dir }, dir, true));
196: }
197:
198: public void testRegexpMapFiles() throws Exception {
199: Pattern fromRx = Pattern.compile("\\.data$");
200: String toSubst = ".nondata";
201: assertEquals("mapped one file", Collections.singletonList(f4),
202: files2List(ActionUtils.regexpMapFiles(new FileObject[] {
203: f3, f3a }, subdir, fromRx, subdir2, toSubst,
204: false)));
205: assertEquals("did not map one file", null,
206: files2List(ActionUtils.regexpMapFiles(new FileObject[] {
207: f3, f3a }, subdir, fromRx, subdir2, toSubst,
208: true)));
209: assertEquals("mapped two file", Arrays.asList(new FileObject[] {
210: f5, f5a }), files2List(ActionUtils.regexpMapFiles(
211: new FileObject[] { f1, f3 }, dir, null, subdir2, null,
212: true)));
213: // XXX test that files which match a regexp, but are substituted to be the same thing, still are OK
214: }
215:
216: private static List<FileObject> files2List(FileObject[] files) {
217: return files != null ? Arrays.asList(files) : null;
218: }
219:
220: }
|