Skip to content

Commit c2d1b72

Browse files
committed
adding a test case for accept files
1 parent f0dd85b commit c2d1b72

File tree

1 file changed

+381
-0
lines changed

1 file changed

+381
-0
lines changed
Lines changed: 381 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,381 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.util;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.util.ArrayList;
30+
import java.util.Arrays;
31+
import java.util.List;
32+
import java.util.Set;
33+
import java.util.TreeSet;
34+
import org.junit.After;
35+
import org.junit.Assert;
36+
import org.junit.Before;
37+
import org.junit.Test;
38+
import org.opensolaris.opengrok.configuration.Project;
39+
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
40+
import org.opensolaris.opengrok.index.IgnoredNames;
41+
42+
/**
43+
*
44+
* @author Krystof Tulinger
45+
*/
46+
public class AcceptHelperTest {
47+
48+
private File tempDir;
49+
50+
@Before
51+
public void setUp() throws IOException {
52+
tempDir = File.createTempFile("temp", Long.toString(System.currentTimeMillis()));
53+
if (!tempDir.delete()) {
54+
throw new IOException("Could not delete temporary file to create a directory: " + tempDir.getAbsolutePath());
55+
}
56+
if (!tempDir.mkdir()) {
57+
throw new IOException("Could not create a temporary directory: " + tempDir.getAbsolutePath());
58+
}
59+
}
60+
61+
@After
62+
public void tearDown() throws IOException {
63+
IOUtils.removeRecursive(tempDir.toPath());
64+
}
65+
66+
protected void runAcceptTests(File sourceRoot, File[] tests, boolean accept) throws IOException {
67+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
68+
IgnoredNames ignoredNames = env.getIgnoredNames();
69+
70+
createSourceRoot(sourceRoot);
71+
String oldSourceRoot = env.getSourceRootPath();
72+
env.setSourceRoot(sourceRoot.getAbsolutePath());
73+
74+
IgnoredNames newIgnored = new IgnoredNames();
75+
newIgnored.add("f:inside.c");
76+
newIgnored.add("d:foo");
77+
newIgnored.add("d:subdir");
78+
newIgnored.add("d:ignoredDir");
79+
newIgnored.add("*/ignored/*");
80+
newIgnored.add("foolink");
81+
env.setIgnoredNames(newIgnored);
82+
83+
for (File test : tests) {
84+
Assert.assertEquals(accept, AcceptHelper.accept(test));
85+
}
86+
87+
env.setIgnoredNames(ignoredNames);
88+
if (oldSourceRoot != null) {
89+
env.setSourceRoot(oldSourceRoot);
90+
}
91+
}
92+
93+
/**
94+
* Test of accept method, of class AcceptHelper.
95+
*/
96+
@Test
97+
public void testAcceptIgnored() throws IOException {
98+
File sourceRoot = new File(tempDir.getAbsolutePath(), "source");
99+
100+
File ignored[] = new File[]{
101+
createFile(sourceRoot + File.separator + "inside.c"),
102+
createFile(sourceRoot + File.separator + "subdir" + File.separator + "inside.c"),
103+
createFile(sourceRoot + File.separator + "subdir" + File.separator + "subdir" + File.separator + "inside.c"),
104+
createFile(sourceRoot + File.separator + "ignored" + File.separator + "main.c"),
105+
createFile(sourceRoot + File.separator + "ignored" + File.separator + "random.c"),
106+
createDirectory(sourceRoot + File.separator + "ignoredDir"),
107+
createDirectory(sourceRoot + File.separator + "subdir" + File.separator + "ignoredDir")
108+
};
109+
110+
createFile(sourceRoot + File.separator + "main.c");
111+
createFile(sourceRoot + File.separator + "random.c");
112+
createFile(sourceRoot + File.separator + "subdir" + File.separator + "main.c");
113+
createFile(sourceRoot + File.separator + "subdir" + File.separator + "random.c");
114+
createSymlink(sourceRoot + File.separator + "subdir", sourceRoot + File.separator + "link");
115+
createSymlink(sourceRoot + File.separator + "subdir", sourceRoot + File.separator + "subdir" + File.separator + "link");
116+
createSymlink(sourceRoot.getAbsolutePath(), sourceRoot + File.separator + "foolink");
117+
createSymlink(sourceRoot.getAbsolutePath(), sourceRoot + File.separator + "subdir" + File.separator + "foolink");
118+
119+
runAcceptTests(sourceRoot, ignored, false);
120+
IOUtils.removeRecursive(sourceRoot.toPath());
121+
}
122+
123+
/**
124+
* Test of accept method, of class AcceptHelper.
125+
*/
126+
@Test
127+
public void testAcceptAccepted() throws IOException {
128+
129+
File sourceRoot = new File(tempDir.getAbsolutePath(), "source");
130+
131+
createFile(sourceRoot + File.separator + "inside.c");
132+
createFile(sourceRoot + File.separator + "subdir" + File.separator + "inside.c");
133+
createFile(sourceRoot + File.separator + "subdir" + File.separator + "subdir" + File.separator + "inside.c");
134+
createFile(sourceRoot + File.separator + "ignored" + File.separator + "main.c");
135+
createFile(sourceRoot + File.separator + "ignored" + File.separator + "random.c");
136+
createDirectory(sourceRoot + File.separator + "ignoredDir");
137+
createDirectory(sourceRoot + File.separator + "subdir" + File.separator + "ignoredDir");
138+
139+
File accepted[] = new File[]{
140+
createFile(sourceRoot + File.separator + "main.c"),
141+
createFile(sourceRoot + File.separator + "random.c"),
142+
createFile(sourceRoot + File.separator + "subdir" + File.separator + "main.c"),
143+
createFile(sourceRoot + File.separator + "subdir" + File.separator + "random.c"),
144+
createSymlink(sourceRoot + File.separator + "subdir", sourceRoot + File.separator + "link"),
145+
createSymlink(sourceRoot + File.separator + "subdir", sourceRoot + File.separator + "subdir" + File.separator + "link"),
146+
createSymlink(sourceRoot.getAbsolutePath(), sourceRoot + File.separator + "foolink"),
147+
createSymlink(sourceRoot.getAbsolutePath(), sourceRoot + File.separator + "subdir" + File.separator + "foolink")
148+
};
149+
150+
runAcceptTests(sourceRoot, accepted, true);
151+
IOUtils.removeRecursive(sourceRoot.toPath());
152+
}
153+
154+
/**
155+
* Test of acceptSymlink method, of class AcceptHelper.
156+
*/
157+
@Test
158+
public void testAcceptSymlink() throws IOException {
159+
File sourceRoot = createSourceRoot(new File(tempDir.getAbsolutePath(), "source/root"));
160+
File symlinkRoot = sourceRoot.getParentFile();
161+
162+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
163+
Set<String> allowedSymlinks = env.getAllowedSymlinks();
164+
165+
File t1 = createFile(symlinkRoot + File.separator + "file1.c");
166+
File t2 = createFile(symlinkRoot + File.separator + "file2.c");
167+
File t3 = createFile(symlinkRoot + File.separator + "file3.c");
168+
169+
env.setAllowedSymlinks(new TreeSet<>(Arrays.asList(new String[]{
170+
sourceRoot + File.separator + "link1",
171+
sourceRoot + File.separator + "link2",
172+
sourceRoot + File.separator + "link3"
173+
})));
174+
175+
File[] tests = new File[]{
176+
createSymlink(t1.getAbsolutePath(), sourceRoot + File.separator + "link1"),
177+
createSymlink(t2.getAbsolutePath(), sourceRoot + File.separator + "link2"),
178+
createSymlink(t3.getAbsolutePath(), sourceRoot + File.separator + "link3")
179+
};
180+
runSymlinkTests(sourceRoot, tests, true);
181+
182+
env.setAllowedSymlinks(new TreeSet<>());
183+
runSymlinkTests(sourceRoot, tests, false);
184+
185+
env.setAllowedSymlinks(allowedSymlinks);
186+
IOUtils.removeRecursive(sourceRoot.toPath());
187+
}
188+
189+
private void runSymlinkTests(File sourceRoot, File[] tests, boolean expected) throws IOException {
190+
createSourceRoot(sourceRoot);
191+
192+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
193+
String oldSourceRoot = env.getSourceRootPath();
194+
env.setSourceRoot(sourceRoot.getAbsolutePath());
195+
196+
for (File file : tests) {
197+
Assert.assertEquals(expected, AcceptHelper.acceptSymlink(null, file));
198+
}
199+
200+
if (oldSourceRoot != null) {
201+
env.setSourceRoot(oldSourceRoot);
202+
}
203+
}
204+
205+
/**
206+
*
207+
* @param sourceRoot
208+
* @param tests
209+
* @throws IOException
210+
*/
211+
protected void runLocalTests(File sourceRoot, Object[][] tests) throws IOException {
212+
createSourceRoot(sourceRoot);
213+
214+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
215+
List<Project> projects = env.getProjects();
216+
String oldSourceRoot = env.getSourceRootPath();
217+
env.setProjects(new ArrayList<>());
218+
env.setSourceRoot(sourceRoot.getAbsolutePath());
219+
220+
for (Object[] test : tests) {
221+
Project project = (Project) test[0];
222+
if (project != null && !env.getProjects().contains(project)) {
223+
env.getProjects().add(project);
224+
}
225+
File file = (File) test[1];
226+
Boolean expected = (Boolean) test[2];
227+
Assert.assertEquals(expected.booleanValue(), AcceptHelper.isLocal(project, file.getCanonicalPath()));
228+
}
229+
230+
env.setProjects(projects);
231+
if (oldSourceRoot != null) {
232+
env.setSourceRoot(oldSourceRoot);
233+
}
234+
}
235+
236+
protected File createSourceRoot(File sourceRoot) throws IOException {
237+
if (!sourceRoot.exists() && !sourceRoot.mkdirs()) {
238+
throw new IOException("Could not create a testing source root: " + sourceRoot.getAbsolutePath());
239+
}
240+
return sourceRoot;
241+
}
242+
243+
/**
244+
* Test of isLocal method, of class AcceptHelper.
245+
*
246+
* @throws java.io.IOException
247+
*/
248+
@Test
249+
public void testIsLocalWithoutProjects() throws IOException {
250+
File sourceRoot = new File(tempDir.getAbsolutePath(), "source");
251+
Object[][] tests = new Object[][]{
252+
{null, createFile(sourceRoot + File.separator + "inside.c"), true},
253+
{null, createFile(sourceRoot + File.separator + "subdir" + File.separator + "inside.c"), true},
254+
{null, createFile(sourceRoot + File.separator + "subdir" + File.separator + "subdir" + File.separator + "inside.c"), true},
255+
{null, createSymlink(sourceRoot + File.separator + "subdir", sourceRoot + File.separator + "link"), true},
256+
{null, createSymlink(sourceRoot + File.separator + "subdir", sourceRoot + File.separator + "subdir" + File.separator + "link"), true},
257+
{null, createSymlink(sourceRoot.getAbsolutePath(), sourceRoot + File.separator + "foolink"), true},
258+
{null, createSymlink(sourceRoot.getAbsolutePath(), sourceRoot + File.separator + "subdir" + File.separator + "foolink"), true}
259+
};
260+
runLocalTests(sourceRoot, tests);
261+
IOUtils.removeRecursive(sourceRoot.toPath());
262+
}
263+
264+
/**
265+
* Test of isLocal method, of class AcceptHelper.
266+
*
267+
* @throws java.io.IOException
268+
*/
269+
@Test
270+
public void testIsLocalWithProjects() throws IOException {
271+
File sourceRoot = new File(tempDir.getAbsolutePath(), "source/subroot");
272+
Object[][] tests = new Object[][]{
273+
{createProject("/project1"), createFile(sourceRoot + File.separator + "project1" + File.separator + "inside.c"), true},
274+
{createProject("/project1"), createFile(sourceRoot + File.separator + "project1" + File.separator + "subdir" + File.separator + "inside.c"), true},
275+
{createProject("/project1"), createFile(sourceRoot + File.separator + "project1" + File.separator + "subdir" + File.separator + "subdir" + File.separator + "inside.c"), true},
276+
{createProject("/project1"), createSymlink(sourceRoot + File.separator + "project1" + File.separator + "subdir", sourceRoot + File.separator + "project1" + File.separator + "link"), true},
277+
{createProject("/project1"), createSymlink(sourceRoot + File.separator + "project1" + File.separator + "subdir", sourceRoot + File.separator + "project1" + File.separator + "subdir" + File.separator + "link"), true},
278+
{createProject("/project1"), createSymlink(sourceRoot.getAbsolutePath() + File.separator + "project1", sourceRoot + File.separator + "project1" + File.separator + "foolink"), true},
279+
{createProject("/project1"), createSymlink(sourceRoot.getAbsolutePath() + File.separator + "project1", sourceRoot + File.separator + "project1" + File.separator + "subdir" + File.separator + "foolink"), true},
280+
{createProject("/project2"), createFile(sourceRoot + File.separator + "project2" + File.separator + "inside.c"), true},
281+
{createProject("/project2"), createFile(sourceRoot + File.separator + "project2" + File.separator + "subdir" + File.separator + "inside.c"), true},
282+
{createProject("/project2"), createFile(sourceRoot + File.separator + "project2" + File.separator + "subdir" + File.separator + "subdir" + File.separator + "inside.c"), true},
283+
{createProject("/project2"), createSymlink(sourceRoot + File.separator + "project2" + File.separator + "subdir", sourceRoot + File.separator + "project2" + File.separator + "link"), true},
284+
{createProject("/project2"), createSymlink(sourceRoot + File.separator + "project2" + File.separator + "subdir", sourceRoot + File.separator + "project2" + File.separator + "subdir" + File.separator + "link"), true},
285+
{createProject("/project2"), createSymlink(sourceRoot.getAbsolutePath() + File.separator + "project2", sourceRoot + File.separator + "project2" + File.separator + "foolink"), true},
286+
{createProject("/project2"), createSymlink(sourceRoot.getAbsolutePath() + File.separator + "project2", sourceRoot + File.separator + "project2" + File.separator + "subdir" + File.separator + "foolink"), true}
287+
};
288+
runLocalTests(sourceRoot, tests);
289+
}
290+
291+
/**
292+
* Test of isLocal method, of class AcceptHelper.
293+
*
294+
* @throws java.io.IOException
295+
*/
296+
@Test
297+
public void testIsNotLocalWithProjects() throws IOException {
298+
File sourceRoot = new File(tempDir.getAbsolutePath(), "source/subroot");
299+
Object[][] tests = new Object[][]{
300+
{createProject("/project1"), createFile(sourceRoot + File.separator + "inside.c"), false},
301+
{createProject("/project1"), createFile(sourceRoot + File.separator + "subdir" + File.separator + "inside.c"), false},
302+
{createProject("/project1"), createFile(sourceRoot + File.separator + "subdir" + File.separator + "subdir" + File.separator + "inside.c"), false},
303+
{createProject("/project1"), createSymlink(sourceRoot + File.separator + "subdir", sourceRoot + File.separator + "link"), false},
304+
{createProject("/project1"), createSymlink(sourceRoot + File.separator + "subdir", sourceRoot + File.separator + "subdir" + File.separator + "link"), false},
305+
{createProject("/project1"), createSymlink(sourceRoot.getAbsolutePath(), sourceRoot + File.separator + "foolink"), false},
306+
{createProject("/project1"), createSymlink(sourceRoot.getAbsolutePath(), sourceRoot + File.separator + "subdir" + File.separator + "foolink"), false},
307+
{createProject("/project1"), createFile(sourceRoot.getParentFile() + File.separator + "inside.c"), false},
308+
{createProject("/project1"), createFile(sourceRoot.getParentFile() + File.separator + "subdir" + File.separator + "inside.c"), false},
309+
{createProject("/project1"), createFile(sourceRoot.getParentFile() + File.separator + "subdir" + File.separator + "subdir" + File.separator + "inside.c"), false},
310+
{createProject("/project1"), createSymlink(sourceRoot.getParentFile() + File.separator + "subdir", sourceRoot.getParentFile() + File.separator + "link"), false},
311+
{createProject("/project1"), createSymlink(sourceRoot.getParentFile() + File.separator + "subdir", sourceRoot.getParentFile() + File.separator + "subdir" + File.separator + "link"), false},
312+
{createProject("/project1"), createSymlink(sourceRoot.getParentFile().getAbsolutePath(), sourceRoot.getParentFile() + File.separator + "foolink"), false},
313+
{createProject("/project1"), createSymlink(sourceRoot.getParentFile().getAbsolutePath(), sourceRoot.getParentFile() + File.separator + "subdir" + File.separator + "foolink"), false},
314+
{createProject("/project1"), createFile(sourceRoot + File.separator + "project2" + File.separator + "subdir" + File.separator + "main.c"), false},
315+
{createProject("/project2"), createFile(sourceRoot + File.separator + "project1" + File.separator + "main.c"), false},
316+
{createProject("/project1"), createSymlink(sourceRoot + File.separator + "project2", sourceRoot + File.separator + "project1" + File.separator + "external"), false}
317+
};
318+
runLocalTests(sourceRoot, tests);
319+
}
320+
321+
/**
322+
* Test of isLocal method, of class AcceptHelper.
323+
*
324+
* @throws java.io.IOException
325+
*/
326+
@Test
327+
public void testIsNotLocalWithoutProjects() throws IOException {
328+
File sourceRoot = new File(tempDir.getAbsolutePath(), "source/subroot");
329+
Object[][] tests = new Object[][]{
330+
{null, createFile(sourceRoot.getParentFile() + File.separator + "inside.c"), false},
331+
{null, createFile(sourceRoot.getParentFile() + File.separator + "subdir" + File.separator + "inside.c"), false},
332+
{null, createFile(sourceRoot.getParentFile() + File.separator + "subdir" + File.separator + "subdir" + File.separator + "inside.c"), false},
333+
{null, createSymlink(sourceRoot.getParentFile() + File.separator + "subdir", sourceRoot.getParentFile() + File.separator + "link"), false},
334+
{null, createSymlink(sourceRoot.getParentFile() + File.separator + "subdir", sourceRoot.getParentFile() + File.separator + "subdir" + File.separator + "link"), false},
335+
{null, createSymlink(sourceRoot.getParentFile().getAbsolutePath(), sourceRoot.getParentFile() + File.separator + "foolink"), false},
336+
{null, createSymlink(sourceRoot.getParentFile().getAbsolutePath(), sourceRoot.getParentFile() + File.separator + "subdir" + File.separator + "foolink"), false}
337+
};
338+
runLocalTests(sourceRoot, tests);
339+
}
340+
341+
protected Project createProject(String path) {
342+
Project p = new Project();
343+
p.setDescription(path);
344+
p.setPath(path);
345+
return p;
346+
}
347+
348+
protected File createSymlink(String target, String name) throws IOException {
349+
Path link = Files.createSymbolicLink(
350+
new File(name).toPath(),
351+
new File(target).toPath());
352+
return link.toFile();
353+
}
354+
355+
protected File createFile(String path) throws IOException {
356+
return createFile(path, false);
357+
}
358+
359+
protected File createDirectory(String path) throws IOException {
360+
return createFile(path, true);
361+
}
362+
363+
protected File createFile(String path, boolean directory) throws IOException {
364+
File test = new File(path);
365+
if (!test.exists()) {
366+
if (!test.getParentFile().exists() && !test.getParentFile().mkdirs()) {
367+
throw new IOException("Could not create the path: " + test.getParentFile().getAbsolutePath());
368+
}
369+
if (directory) {
370+
if (!test.mkdir()) {
371+
throw new IOException("Could not create the directory: " + test.getAbsolutePath());
372+
}
373+
} else {
374+
if (!test.createNewFile()) {
375+
throw new IOException("Could not create the file: " + test.getAbsolutePath());
376+
}
377+
}
378+
}
379+
return test;
380+
}
381+
}

0 commit comments

Comments
 (0)