use new directory scanner
[teamcity/dotNetPackagesSupport.git] / nuget-tests / src / jetbrains / buildServer / nuget / tests / util / fsScanner / TestDirectoryScanner.java
1 /*\r
2  * Copyright 2000-2011 JetBrains s.r.o.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 package jetbrains.buildServer.nuget.tests.util.fsScanner;\r
17 \r
18 \r
19 import com.intellij.openapi.util.SystemInfo;\r
20 import jetbrains.buildServer.BaseTestCase;\r
21 import jetbrains.buildServer.nuget.agent.runner.publish.fsScanner.DirectoryScanner;\r
22 import jetbrains.buildServer.util.FileUtil;\r
23 import org.testng.Assert;\r
24 import org.testng.annotations.Test;\r
25 \r
26 import java.io.File;\r
27 import java.io.IOException;\r
28 import java.util.*;\r
29 \r
30 public class TestDirectoryScanner extends BaseTestCase {\r
31   private void AssertScannerResult(String[] fsDescription, String[] includePatterns, String[] excludePatterns, String[] expectedResult) throws IOException {\r
32     File fsp = createTempDir();\r
33 \r
34     CreateDirectories(fsDescription, fsp);\r
35     Collection<File> findFiles = DirectoryScanner.FindFiles(fsp, Arrays.asList(includePatterns), Arrays.asList(excludePatterns));\r
36 \r
37     Set<File> expected = new TreeSet<File>();\r
38     for (String s : expectedResult) {\r
39       expected.add(new File(fsp, s));\r
40     }\r
41     Set<File> actual = new TreeSet<File>();\r
42 \r
43     System.out.println("Found: ");\r
44     for (File file : findFiles) {\r
45       actual.add(file);\r
46       System.out.println("   " + file);\r
47     }\r
48 \r
49     Assert.assertEquals(expected.size(), actual.size());\r
50     final Iterator<File> eIt = expected.iterator();\r
51     final Iterator<File> aIt = actual.iterator();\r
52 \r
53     for (int i = 0; i < expected.size(); i++)\r
54 \r
55     {\r
56       final File eNext = eIt.next();\r
57       final File aNext = aIt.next();\r
58       Assert.assertEquals(PreparePath(eNext), PreparePath(aNext));\r
59     }\r
60   }\r
61 \r
62 \r
63   private static void CreateDirectories(String[] fsDescription, File fsp) {\r
64     for (String f : fsDescription) {\r
65       File path = new File(fsp, f.substring(2));\r
66       if (f.startsWith("f:")) {\r
67         FileUtil.createParentDirs(path);\r
68         FileUtil.writeFile(path, "text");\r
69       } else if (f.startsWith("d:"))\r
70         path.mkdirs();\r
71       else\r
72         Assert.fail("Wrong fsDescription: " + f);\r
73     }\r
74   }\r
75 \r
76 \r
77   @Test\r
78   public void TestSmoke() throws IOException {\r
79     AssertScannerResult(\r
80             new String[]\r
81                     {\r
82                             "d:a",\r
83                             "f:a/a.exe",\r
84                             "f:a/a.dll",\r
85                             "f:a/a.ca",\r
86                     },\r
87             new String[]{"**"},\r
88             new String[]{"**/*.exe"},\r
89             new String[]{"a/a.ca", "a/a.dll"});\r
90   }\r
91 \r
92   @Test\r
93   public void TestEmptyExclude() throws IOException {\r
94     AssertScannerResult(\r
95             new String[]\r
96                     {\r
97                             "d:a",\r
98                             "f:a/a.exe",\r
99                             "f:a/a.dll",\r
100                             "f:a/a.ca",\r
101                     },\r
102             new String[]{"**"},\r
103             new String[]{},\r
104             new String[]{"a/a.ca", "a/a.dll", "a/a.exe"});\r
105   }\r
106 \r
107   @Test\r
108   public void TestEmptyInclude() throws IOException {\r
109     AssertScannerResult(\r
110             new String[]\r
111                     {\r
112                             "d:a",\r
113                             "f:a/a.exe",\r
114                             "f:a/a.dll",\r
115                             "f:a/a.ca",\r
116                     },\r
117             new String[]{},\r
118             new String[]{"**"},\r
119             new String[]{});\r
120   }\r
121 \r
122   @Test\r
123   public void TestBothEmpty() throws IOException {\r
124     AssertScannerResult(\r
125             new String[]\r
126                     {\r
127                             "d:a",\r
128                             "f:a/a.exe",\r
129                             "f:a/a.dll",\r
130                             "f:a/a.ca",\r
131                     },\r
132             new String[]{},\r
133             new String[]{},\r
134             new String[]{});\r
135   }\r
136 \r
137   @Test\r
138   public void TestShouldNotMatchDirectory() throws IOException {\r
139     AssertScannerResult(\r
140             new String[]\r
141                     {\r
142                             "f:a/b/c/d/e/f/g/h/i/p/g/aaa.txt",\r
143                             "d:a/r/c/d/e/f/g/h/i/p/g/aaa.txt",\r
144                     },\r
145             new String[]{"**/*.txt"},\r
146             new String[]{},\r
147             new String[]{"a/b/c/d/e/f/g/h/i/p/g/aaa.txt"});\r
148   }\r
149 \r
150   @Test\r
151   public void TestCaseSensitive() throws IOException {\r
152     if (SystemInfo.isWindows) return;\r
153 \r
154     AssertScannerResult(\r
155             new String[]\r
156                     {\r
157                             "f:a/b/c/d/e/f/g/h/i/p/g/aAa.txt",\r
158                             "f:a/r/e/a/l/f/g/h/i/p/g/aaa.txt",\r
159                     },\r
160             new String[]{"**/*A*.txt"},\r
161             new String[]{},\r
162             new String[]{"a/b/c/d/e/f/g/h/i/p/g/aAa.txt"});\r
163   }\r
164 \r
165   @Test\r
166   public void TestCaseInSensitive() throws IOException {\r
167     if (SystemInfo.isWindows) return;\r
168 \r
169     AssertScannerResult(\r
170             new String[]\r
171                     {\r
172                             "f:a/b/c/d/e/f/g/h/i/p/g/aAa.txt",\r
173                             "f:a/r/e/a/l/f/g/h/i/p/g/aaa.txt",\r
174                     },\r
175             new String[]{"**/*A*.txt"},\r
176             new String[]{},\r
177             new String[]\r
178                     {\r
179                             "a/b/c/d/e/f/g/h/i/p/g/aAa.txt",\r
180                             "a/r/e/a/l/f/g/h/i/p/g/aaa.txt",\r
181                     });\r
182   }\r
183 \r
184 \r
185   @Test\r
186   public void AbsoluteIncludePath() throws IOException {\r
187 \r
188     File fsp = createTempDir();\r
189     File atxtFsp = new File(fsp, "a.txt");\r
190     FileUtil.writeFile(atxtFsp, "text");\r
191 \r
192     Collection<File> files = DirectoryScanner.FindFiles(fsp, Arrays.asList(atxtFsp.getPath()), Collections.<String>emptyList());\r
193     Assert.assertEquals(1, files.size());\r
194     Assert.assertEquals(PreparePath(atxtFsp), PreparePath(files.iterator().next()));\r
195   }\r
196 \r
197   private static String PreparePath(File f) {\r
198     String path = f.getPath();\r
199     path = path.replace('\\', '/');\r
200     if (!SystemInfo.isWindows) {\r
201       return path;\r
202     } else {\r
203       return path.toLowerCase();\r
204     }\r
205   }\r
206 }\r