be3dceb03e61d4ac87e76461be1eeb5b1db70cfc
[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.Collection;\r
29 import java.util.Iterator;\r
30 import java.util.Set;\r
31 import java.util.TreeSet;\r
32 \r
33 public class TestDirectoryScanner extends BaseTestCase {\r
34   private void AssertScannerResult(String[] fsDescription, String[] includePatterns, String[] excludePatterns, String[] expectedResult) throws IOException {\r
35     File fsp = createTempDir();\r
36 \r
37     CreateDirectories(fsDescription, fsp);\r
38     Collection<File> findFiles = DirectoryScanner.FindFiles(fsp, includePatterns, excludePatterns);\r
39 \r
40     Set<File> expected = new TreeSet<File>();\r
41     for (String s : expectedResult) {\r
42       expected.add(new File(fsp, s));\r
43     }\r
44     Set<File> actual = new TreeSet<File>();\r
45 \r
46     System.out.println("Found: ");\r
47     for (File file : findFiles) {\r
48       actual.add(file);\r
49       System.out.println("   " + file);\r
50     }\r
51 \r
52     Assert.assertEquals(expected.size(), actual.size());\r
53     final Iterator<File> eIt = expected.iterator();\r
54     final Iterator<File> aIt = actual.iterator();\r
55 \r
56     for (int i = 0; i < expected.size(); i++)\r
57 \r
58     {\r
59       final File eNext = eIt.next();\r
60       final File aNext = aIt.next();\r
61       Assert.assertEquals(PreparePath(eNext), PreparePath(aNext));\r
62     }\r
63   }\r
64 \r
65 \r
66   private static void CreateDirectories(String[] fsDescription, File fsp) {\r
67     for (String f : fsDescription) {\r
68       File path = new File(fsp, f.substring(2));\r
69       if (f.startsWith("f:")) {\r
70         FileUtil.createParentDirs(path);\r
71         FileUtil.writeFile(path, "text");\r
72       } else if (f.startsWith("d:"))\r
73         path.mkdirs();\r
74       else\r
75         Assert.fail("Wrong fsDescription: " + f);\r
76     }\r
77   }\r
78 \r
79 \r
80   @Test\r
81   public void TestSmoke() throws IOException {\r
82     AssertScannerResult(\r
83             new String[]\r
84                     {\r
85                             "d:a",\r
86                             "f:a/a.exe",\r
87                             "f:a/a.dll",\r
88                             "f:a/a.ca",\r
89                     },\r
90             new String[]{"**"},\r
91             new String[]{"**/*.exe"},\r
92             new String[]{"a/a.ca", "a/a.dll"});\r
93   }\r
94 \r
95   @Test\r
96   public void TestEmptyExclude() throws IOException {\r
97     AssertScannerResult(\r
98             new String[]\r
99                     {\r
100                             "d:a",\r
101                             "f:a/a.exe",\r
102                             "f:a/a.dll",\r
103                             "f:a/a.ca",\r
104                     },\r
105             new String[]{"**"},\r
106             new String[]{},\r
107             new String[]{"a/a.ca", "a/a.dll", "a/a.exe"});\r
108   }\r
109 \r
110   @Test\r
111   public void TestEmptyInclude() throws IOException {\r
112     AssertScannerResult(\r
113             new String[]\r
114                     {\r
115                             "d:a",\r
116                             "f:a/a.exe",\r
117                             "f:a/a.dll",\r
118                             "f:a/a.ca",\r
119                     },\r
120             new String[]{},\r
121             new String[]{"**"},\r
122             new String[]{});\r
123   }\r
124 \r
125   @Test\r
126   public void TestBothEmpty() throws IOException {\r
127     AssertScannerResult(\r
128             new String[]\r
129                     {\r
130                             "d:a",\r
131                             "f:a/a.exe",\r
132                             "f:a/a.dll",\r
133                             "f:a/a.ca",\r
134                     },\r
135             new String[]{},\r
136             new String[]{},\r
137             new String[]{});\r
138   }\r
139 \r
140   @Test\r
141   public void TestShouldNotMatchDirectory() throws IOException {\r
142     AssertScannerResult(\r
143             new String[]\r
144                     {\r
145                             "f:a/b/c/d/e/f/g/h/i/p/g/aaa.txt",\r
146                             "d:a/r/c/d/e/f/g/h/i/p/g/aaa.txt",\r
147                     },\r
148             new String[]{"**/*.txt"},\r
149             new String[]{},\r
150             new String[]{"a/b/c/d/e/f/g/h/i/p/g/aaa.txt"});\r
151   }\r
152 \r
153   @Test\r
154   public void TestCaseSensitive() throws IOException {\r
155     if (SystemInfo.isWindows) return;\r
156 \r
157     AssertScannerResult(\r
158             new String[]\r
159                     {\r
160                             "f:a/b/c/d/e/f/g/h/i/p/g/aAa.txt",\r
161                             "f:a/r/e/a/l/f/g/h/i/p/g/aaa.txt",\r
162                     },\r
163             new String[]{"**/*A*.txt"},\r
164             new String[]{},\r
165             new String[]{"a/b/c/d/e/f/g/h/i/p/g/aAa.txt"});\r
166   }\r
167 \r
168   @Test\r
169   public void TestCaseInSensitive() throws IOException {\r
170     if (SystemInfo.isWindows) return;\r
171 \r
172     AssertScannerResult(\r
173             new String[]\r
174                     {\r
175                             "f:a/b/c/d/e/f/g/h/i/p/g/aAa.txt",\r
176                             "f:a/r/e/a/l/f/g/h/i/p/g/aaa.txt",\r
177                     },\r
178             new String[]{"**/*A*.txt"},\r
179             new String[]{},\r
180             new String[]\r
181                     {\r
182                             "a/b/c/d/e/f/g/h/i/p/g/aAa.txt",\r
183                             "a/r/e/a/l/f/g/h/i/p/g/aaa.txt",\r
184                     });\r
185   }\r
186 \r
187 \r
188   @Test\r
189   public void AbsoluteIncludePath() throws IOException {\r
190 \r
191     File fsp = createTempDir();\r
192     File atxtFsp = new File(fsp, "a.txt");\r
193     FileUtil.writeFile(atxtFsp, "text");\r
194 \r
195     Collection<File> files = DirectoryScanner.FindFiles(fsp, new String[]{atxtFsp.getPath()}, new String[0]);\r
196     Assert.assertEquals(1, files.size());\r
197     Assert.assertEquals(PreparePath(atxtFsp), PreparePath(files.iterator().next()));\r
198   }\r
199 \r
200   private static String PreparePath(File f) {\r
201     String path = f.getPath();\r
202     path = path.replace('\\', '/');\r
203     if (!SystemInfo.isWindows) {\r
204       return path;\r
205     } else {\r
206       return path.toLowerCase();\r
207     }\r
208   }\r
209 }\r