/*
 * Copyright 2015-2017 Canoo Engineering AG.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.canoo.impl.server;

import com.canoo.impl.server.classpathscan.documented.DocumentAnnotatedClass;
import com.canoo.impl.server.util.AnnotatedClassForClasspathScan;
import com.canoo.impl.server.util.AnnotationForClasspathScanTest;
import com.canoo.dp.impl.server.scanner.DefaultClasspathScanner;
import org.testng.annotations.Test;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.lang.annotation.Documented;
import java.util.Set;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertTrue;

public class ClasspathScannerTest {

    @Test
    public void testSimpleScan() {
        //There can't be a class that is annotated with Inject
        final DefaultClasspathScanner scanner = new DefaultClasspathScanner();
        Set<Class<?>> classes = scanner.getTypesAnnotatedWith(Resources.class);
        assertNotNull(classes);
        assertEquals(classes.size(), 0);

        classes = scanner.getTypesAnnotatedWith(AnnotationForClasspathScanTest.class);
        assertNotNull(classes);
        assertEquals(classes.size(), 1);
        assertTrue(classes.contains(AnnotatedClassForClasspathScan.class));
    }

    @Test
    public void testInPackageScan() {
        //There can't be a class that is annotated with Inject
        final DefaultClasspathScanner scanner = new DefaultClasspathScanner("com.canoo.impl.server.classpathscan");
        assertForAnnotation(scanner);
    }

    @Test
    public void testInMultiplePackages() {
        //There can't be a class that is annotated with Inject
        final DefaultClasspathScanner scanner = new DefaultClasspathScanner("com.canoo.impl.server.classpathscan.documented", "com.canoo.impl.server.classpathscan.resource");
        assertForAnnotation(scanner);
    }

    private void assertForAnnotation(DefaultClasspathScanner scanner) {
        final Set<Class<?>> resourceClasses = scanner.getTypesAnnotatedWith(Resource.class);
        assertNotNull(resourceClasses);
        assertEquals(resourceClasses.size(), 1);

        final Set<Class<?>> documentClasses = scanner.getTypesAnnotatedWith(Documented.class);
        assertNotNull(documentClasses);
        assertEquals(documentClasses.size(), 1);
    }

    @Test
    public void testInMultiplePackages2() {
        //There can't be a class that is annotated with Inject
        final DefaultClasspathScanner scanner = new DefaultClasspathScanner("com.canoo.impl.server.classpathscan", "com.canoo.impl.server.classpathscan.documented", "com.canoo.impl.server.classpathscan.resource");
        assertForAnnotation(scanner);
    }

    @Test
    public void testScanOtherPackage() {
        //There can't be a class that is annotated with Inject
        final DefaultClasspathScanner scanner = new DefaultClasspathScanner("com.canoo.impl.server.classpathscan");
        Set<Class<?>> classes = scanner.getTypesAnnotatedWith(Resources.class);
        assertNotNull(classes);
        assertEquals(classes.size(), 0);

        classes = scanner.getTypesAnnotatedWith(AnnotationForClasspathScanTest.class);
        assertNotNull(classes);
        assertEquals(classes.size(), 0);

        classes = scanner.getTypesAnnotatedWith(Documented.class);
        assertNotNull(classes);
        assertEquals(classes.size(), 1);
        assertTrue(classes.contains(DocumentAnnotatedClass.class));
    }

    @Test(expectedExceptions = NullPointerException.class)
    public void testNullArgument() {
        final DefaultClasspathScanner scanner = new DefaultClasspathScanner();
        Set<Class<?>> classes = scanner.getTypesAnnotatedWith(null);
    }

}

@@ -1254,16 +1254,104 @@
 Test %7B%0A%0A
+    private static final String CLASSPATH_SCAN = %22com.canoo.impl.server.classpathscan%22;%0A
     @Tes
@@ -2117,45 +2117,54 @@
 ner(
-%22com.canoo.impl.server.classpath
+CLASSPATH_SCAN);%0A        assertNotNull(
 scan
-%22
+ner
 );%0A 
@@ -2580,16 +2580,22 @@
 otation(
+final 
 DefaultC
@@ -3043,17 +3043,36 @@
 Packages
-2
+WithBasePackageFirst
 () %7B%0A   
@@ -3211,45 +3211,22 @@
 ner(
-%22com.canoo.impl.server.classpathscan%22
+CLASSPATH_SCAN
 , %22c
@@ -3561,45 +3561,22 @@
 ner(
-%22com.canoo.impl.server.classpathscan%22
+CLASSPATH_SCAN
 );%0A 

Fix review comments