/*
 * Copyright 2015 the original author or authors.
 *
 * 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
 */

package org.junit.gen5.commons.util;

import java.io.File;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Test;

public class ClasspathScannerTests {

	private ClasspathScanner classpathScanner;

	@Before
	public void init() {
		classpathScanner = new ClasspathScanner(ReflectionUtils::getDefaultClassLoader, ReflectionUtils::loadClass);
	}

	@Test
	public void findAllClassesInThisPackage() throws Exception {
		List<Class<?>> classes = classpathScanner.scanForClassesInPackage("org.junit.gen5.commons", clazz -> true);
		Assert.assertTrue("Should be at least 20 classes", classes.size() >= 20);
		Assert.assertTrue(classes.contains(NestedClassToBeFound.class));
		Assert.assertTrue(classes.contains(MemberClassToBeFound.class));
	}

	@Test
	public void findAllClassesInThisPackageWithFilter() throws Exception {
		Predicate<Class<?>> thisClassOnly = clazz -> clazz == ClasspathScannerTests.class;
		List<Class<?>> classes = classpathScanner.scanForClassesInPackage("org.junit.gen5.commons", thisClassOnly);
		Assert.assertSame(ClasspathScannerTests.class, classes.get(0));
	}

	@Test
	public void isPackage() throws Exception {
		Assert.assertTrue(classpathScanner.isPackage("org.junit.gen5.commons"));
		Assert.assertFalse(classpathScanner.isPackage("org.doesnotexist"));
	}

	@Test
	public void findAllClassesInClasspathRoot() throws Exception {
		Predicate<Class<?>> thisClassOnly = clazz -> clazz == ClasspathScannerTests.class;
		File root = getTestClasspathRoot();
		List<Class<?>> classes = classpathScanner.scanForClassesInClasspathRoot(root, thisClassOnly);
		Assert.assertSame(ClasspathScannerTests.class, classes.get(0));
	}

	@Test
	public void findAllClassesInClasspathRootWithFilter() throws Exception {
		File root = getTestClasspathRoot();
		List<Class<?>> classes = classpathScanner.scanForClassesInClasspathRoot(root, clazz -> true);

		Assert.assertTrue("Should be at least 20 classes", classes.size() >= 20);
		Assert.assertTrue(classes.contains(ClasspathScannerTests.class));
	}

	private File getTestClasspathRoot() {
		String fullClassPath = System.getProperty("java.class.path");
		Optional<String> testRoot = Arrays.stream(fullClassPath.split(File.pathSeparator)).filter(
			s -> s.endsWith("/classes/test")).findFirst();

		Assume.assumeTrue("There should be a test root", testRoot.isPresent());

		return new File(testRoot.get());
	}

	class MemberClassToBeFound {
	}

	static class NestedClassToBeFound {
	}

}

@@ -377,19 +377,15 @@
 ava.
-util.Arrays
+net.URL
 ;%0Aim
@@ -409,35 +409,8 @@
 st;%0A
-import java.util.Optional;%0A
 impo
@@ -472,33 +472,8 @@
 rt;%0A
-import org.junit.Assume;%0A
 impo
@@ -2448,292 +2448,107 @@
 t() 
-%7B%0A%09%09String fullClassPath = System.getProperty(%22java.class.path%22);%0A%09%09Optional%3CString%3E testRoot = Arrays.stream(fullClassPath.split(File.pathSeparator)).filter(%0A%09%09%09s -%3E s.endsWith(%22/classes/test%22)).findFirst();%0A%0A%09%09Assume.assumeTrue(%22There should be a test root%22, testRoot.isPresent());%0A
+throws Exception %7B%0A%09%09URL location = getClass().getProtectionDomain().getCodeSource().getLocation();
 %0A%09%09r
@@ -2566,20 +2566,22 @@
 ile(
-testRoot.get
+location.toURI
 ());

Make ClasspathScannerTests work in Eclipse