KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > api > support > ActionUtilsTest


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.apache.tools.ant.module.api.support;
21
22 import java.util.Arrays JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.regex.Pattern JavaDoc;
26 import org.netbeans.junit.NbTestCase;
27 import org.openide.filesystems.FileObject;
28 import org.openide.filesystems.FileUtil;
29 import org.openide.filesystems.XMLFileSystem;
30 import org.openide.loaders.DataObject;
31 import org.openide.nodes.Node;
32 import org.openide.util.Lookup;
33 import org.openide.util.lookup.Lookups;
34 import org.openide.util.lookup.ProxyLookup;
35
36 // XXX tests needed: runTarget (perhaps)
37

38 /**
39  * Test functionality of ActionUtils.
40  * @author Jesse Glick
41  */

42 public class ActionUtilsTest extends NbTestCase {
43     
44     public ActionUtilsTest(String JavaDoc name) {
45         super(name);
46     }
47     
48     private FileObject dir, f1, f2, subdir, f3, fx, subdir2, f3a, f4, subsubdir, f5, f5a;
49     private DataObject d1, d2, d3, dx;
50     private Node n1, n2, n3, nx;
51     
52     @Override JavaDoc
53     protected void setUp() throws Exception JavaDoc {
54         super.setUp();
55         clearWorkDir();
56         dir = FileUtil.toFileObject(getWorkDir());
57         f1 = dir.createData("f1.data");
58         f2 = dir.createData("f2");
59         subdir = dir.createFolder("sub");
60         f3 = subdir.createData("f3.data");
61         f3a = subdir.createData("f3a.data");
62         subdir2 = dir.createFolder("subdir2");
63         f4 = subdir2.createData("f3.nondata");
64         subsubdir = subdir2.createFolder("sub");
65         f5 = subdir2.createData("f1.data");
66         f5a = subsubdir.createData("f3.data");
67         fx = new XMLFileSystem().getRoot();
68         d1 = DataObject.find(f1);
69         d2 = DataObject.find(f2);
70         d3 = DataObject.find(f3);
71         dx = DataObject.find(fx);
72         n1 = d1.getNodeDelegate();
73         n2 = d2.getNodeDelegate();
74         n3 = d3.getNodeDelegate();
75         nx = dx.getNodeDelegate();
76     }
77     
78     public void testFindSelectedFiles() throws Exception JavaDoc {
79         assertEquals("one selected file", Collections.singletonList(f1), filesFrom(new Node[] {n1}, null, null, true));
80         assertEquals("two selected files", Arrays.asList(new FileObject[] {f1, f2}), filesFrom(new Node[] {n1, n2}, null, null, true));
81         assertEquals("zero selection", null, filesFrom(new Node[0], null, null, true));
82         assertEquals("not a file selection", null, filesFrom(new Node[] {Node.EMPTY}, null, null, true));
83         assertEquals("not a disk file", null, filesFrom(new Node[] {nx}, null, null, true));
84         assertEquals("order significant", Arrays.asList(new FileObject[] {f2, f1}), filesFrom(new Node[] {n2, n1}, null, null, true));
85         assertEquals("one disk file", Collections.singletonList(f1), filesFrom(new Node[] {n1, nx}, null, null, false));
86         assertEquals("one non-disk file", null, filesFrom(new Node[] {n1, nx}, null, null, true));
87         assertEquals("one *.data", Collections.singletonList(f1), filesFrom(new Node[] {n1, n2}, null, ".data", false));
88         assertEquals("one not *.data", null, filesFrom(new Node[] {n1, n2}, null, ".data", true));
89         assertEquals("one file in sub/", Collections.singletonList(f3), filesFrom(new Node[] {n1, n3}, subdir, null, false));
90         assertEquals("one not in sub/", null, filesFrom(new Node[] {n1, n3}, subdir, null, true));
91         assertEquals("one sub/*.data", Collections.singletonList(f3), filesFrom(new Node[] {n3}, subdir, ".data", true));
92         assertEquals("duplicates removed (cf. #50644)", Collections.singletonList(f1), filesFrom(new Node[] {n1, n1}, null, null, true));
93         assertEquals("duplicates removed #2 (cf. #50644)", Arrays.asList(new FileObject[] {f1, f2}), filesFrom(new Node[] {n1, n2, n1}, null, null, true));
94     }
95     
96     private static Lookup context(Node[] sel) {
97         Lookup[] delegates = new Lookup[sel.length + 1];
98         for (int i = 0; i < sel.length; i++) {
99             delegates[i] = sel[i].getLookup();
100         }
101         delegates[sel.length] = Lookups.fixed((Object JavaDoc[]) sel);
102         return new ProxyLookup(delegates);
103     }
104     
105     private static List JavaDoc<FileObject> filesFrom(Node[] sel, FileObject dir, String JavaDoc suffix, boolean strict) {
106         return files2List(ActionUtils.findSelectedFiles(context(sel), dir, suffix, strict));
107     }
108     
109     public void testAntIncludesList() throws Exception JavaDoc {
110         assertEquals("2 includes", "f1.data,sub/f3.data", ActionUtils.antIncludesList(new FileObject[] {f1, f3}, dir));
111         assertEquals("1 include", "f1.data", ActionUtils.antIncludesList(new FileObject[] {f1}, dir));
112         assertEquals("no includes", "", ActionUtils.antIncludesList(new FileObject[0], dir));
113         assertEquals("1 folder include","sub/**",ActionUtils.antIncludesList(new FileObject[]{subdir}, dir, true));
114         assertEquals("root folder include","**",ActionUtils.antIncludesList(new FileObject[]{dir}, dir, true));
115         assertEquals("2 folder includes","sub/**,subdir2/sub/**",ActionUtils.antIncludesList(new FileObject[]{subdir, subsubdir}, dir, true));
116         assertEquals("mixed files and folder includes","sub/f3.data,subdir2/sub/**",ActionUtils.antIncludesList(new FileObject[]{f3, subsubdir}, dir, true));
117         assertEquals("1 folder include","sub/*",ActionUtils.antIncludesList(new FileObject[]{subdir}, dir, false));
118         assertEquals("root folder include","*",ActionUtils.antIncludesList(new FileObject[]{dir}, dir, false));
119         assertEquals("2 folder includes","sub/*,subdir2/sub/*",ActionUtils.antIncludesList(new FileObject[]{subdir, subsubdir}, dir, false));
120         assertEquals("mixed files and folder includes","sub/f3.data,subdir2/sub/*",ActionUtils.antIncludesList(new FileObject[]{f3, subsubdir}, dir, false));
121         assertEquals("antIncludeList(FileObject[], FileObject) delegates to antIncludeList(FileObject[], FileObject, true)",ActionUtils.antIncludesList(new FileObject[]{subdir}, dir) ,ActionUtils.antIncludesList(new FileObject[]{subdir}, dir, true));
122         assertEquals("antIncludeList(FileObject[], FileObject) delegates to antIncludeList(FileObject[], FileObject, true)",ActionUtils.antIncludesList(new FileObject[]{dir}, dir),ActionUtils.antIncludesList(new FileObject[]{dir}, dir, true));
123     }
124     
125     public void testRegexpMapFiles() throws Exception JavaDoc {
126         Pattern JavaDoc fromRx = Pattern.compile("\\.data$");
127         String JavaDoc toSubst = ".nondata";
128         assertEquals("mapped one file", Collections.singletonList(f4), files2List(
129             ActionUtils.regexpMapFiles(new FileObject[] {f3, f3a}, subdir, fromRx, subdir2, toSubst, false)));
130         assertEquals("did not map one file", null, files2List(
131             ActionUtils.regexpMapFiles(new FileObject[] {f3, f3a}, subdir, fromRx, subdir2, toSubst, true)));
132         assertEquals("mapped two file", Arrays.asList(new FileObject[] {f5, f5a}), files2List(
133             ActionUtils.regexpMapFiles(new FileObject[] {f1, f3}, dir, null, subdir2, null, true)));
134         // XXX test that files which match a regexp, but are substituted to be the same thing, still are OK
135
}
136     
137     private static List JavaDoc<FileObject> files2List(FileObject[] files) {
138         return files != null ? Arrays.asList(files) : null;
139     }
140     
141 }
142
Popular Tags