1 19 20 package org.apache.tools.ant.module.api.support; 21 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import java.util.regex.Pattern ; 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 38 42 public class ActionUtilsTest extends NbTestCase { 43 44 public ActionUtilsTest(String 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 53 protected void setUp() throws Exception { 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 { 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 []) sel); 102 return new ProxyLookup(delegates); 103 } 104 105 private static List <FileObject> filesFrom(Node[] sel, FileObject dir, String suffix, boolean strict) { 106 return files2List(ActionUtils.findSelectedFiles(context(sel), dir, suffix, strict)); 107 } 108 109 public void testAntIncludesList() throws Exception { 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 { 126 Pattern fromRx = Pattern.compile("\\.data$"); 127 String 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 } 136 137 private static List <FileObject> files2List(FileObject[] files) { 138 return files != null ? Arrays.asList(files) : null; 139 } 140 141 } 142 | Popular Tags |