/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat Inc. - initial API and implementation
 *******************************************************************************/
package org.eclipse.jdt.ls.core.internal.managers;

import static org.junit.Assert.assertEquals;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Test;

/**
 * @author Fred Bricon
 *
 */
public class BasicFileDetectorTest {

	@Test
	public void testScanBuildFileAtRootExcludingNestedDirs() throws Exception {
		BasicFileDetector detector = new BasicFileDetector(Paths.get("projects/buildfiles"), "buildfile")
				.includeNested(false);
		Collection<Path> dirs = detector.scan(null);
		assertEquals("Found " + dirs, 1, dirs.size());// .metadata is ignored
		assertEquals("projects/buildfiles", dirs.iterator().next().toString());
	}

	@Test
	public void testScanBuildFileAtRootIncludingNestedDirs() throws Exception {
		BasicFileDetector detector = new BasicFileDetector(Paths.get("projects/buildfiles/"), "buildfile");
		Collection<Path> dirs = detector.scan(null);
		assertEquals("Found " + dirs, 6, dirs.size());

		List<String> missingDirs = list("projects/buildfiles", "projects/buildfiles/parent/1_0/0_2_0",
				"projects/buildfiles/parent/1_0/0_2_1", "projects/buildfiles/parent/1_1",
				"projects/buildfiles/parent/1_1/1_2_0", "projects/buildfiles/parent/1_1/1_2_1");
		dirs.stream().map(Path::toString).forEach(missingDirs::remove);
		assertEquals("Directories were not detected" + missingDirs, 0, missingDirs.size());
	}

	@Test
	public void testScanExcludingNestedBuildFilesDepth3() throws Exception {
		BasicFileDetector detector = new BasicFileDetector(Paths.get("projects/buildfiles/parent"), "buildfile")
				.includeNested(false).maxDepth(3);
		Collection<Path> dirs = detector.scan(null);
		assertEquals("Found " + dirs, 3, dirs.size());
		List<String> missingDirs = list("projects/buildfiles/parent/1_1", "projects/buildfiles/parent/1_0/0_2_0",
				"projects/buildfiles/parent/1_0/0_2_1");
		dirs.stream().map(Path::toString).forEach(missingDirs::remove);
		assertEquals("Directories were not detected" + missingDirs, 0, missingDirs.size());
	}

	@Test
	public void testScanNestedBuildFilesDepth2() throws Exception {
		BasicFileDetector detector = new BasicFileDetector(Paths.get("projects/buildfiles/parent"), "buildfile")
				.includeNested(false).maxDepth(2);
		Collection<Path> dirs = detector.scan(null);
		assertEquals("Found " + dirs, 1, dirs.size());
		assertEquals("projects/buildfiles/parent/1_1", dirs.iterator().next().toString());
	}

	@SafeVarargs
	private final <E> List<E> list(E... elements) {
		return new ArrayList<>(Arrays.asList(elements));
	}
}

@@ -780,16 +780,96 @@
 l.List;%0A
+import java.util.stream.Collectors;%0A%0Aimport org.apache.commons.io.FilenameUtils;
 %0Aimport 
@@ -1294,32 +1294,65 @@
 %0A%09%09assertEquals(
+FilenameUtils.separatorsToSystem(
 %22projects/buildf
@@ -1356,16 +1356,17 @@
 ldfiles%22
+)
 , dirs.i
@@ -1703,32 +1703,51 @@
 %3E missingDirs = 
+separatorsToSystem(
 list(%22projects/b
@@ -1955,24 +1955,25 @@
 /1_1/1_2_1%22)
+)
 ;%0A%09%09dirs.str
@@ -2110,32 +2110,204 @@
 rs.size());%0A%09%7D%0A%0A
+%09private List%3CString%3E separatorsToSystem(List%3CString%3E paths) %7B%0A%09%09return paths.stream().map(p -%3E FilenameUtils.separatorsToSystem(p))%0A%09%09%09%09.collect(Collectors.toList());%0A%09%7D%0A%0A
 %09@Test%0A%09public v
@@ -2642,16 +2642,35 @@
 gDirs = 
+separatorsToSystem(
 list(%22pr
@@ -2695,25 +2695,29 @@
 parent/1_1%22,
- 
+%0A%09%09%09%09
 %22projects/bu
@@ -2735,37 +2735,33 @@
 rent/1_0/0_2_0%22,
-%0A%09%09%09%09
+ 
 %22projects/buildf
@@ -2783,16 +2783,17 @@
 /0_2_1%22)
+)
 ;%0A%09%09dirs
@@ -3271,16 +3271,49 @@
 tEquals(
+FilenameUtils.separatorsToSystem(
 %22project
@@ -3336,18 +3336,23 @@
 ent/1_1%22
-, 
+),%0A%09%09%09%09
 dirs.ite

Test failures on Windows