package org.netmelody.menodora.core.test;

import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import org.hamcrest.Matchers;
import org.junit.Test;
import org.netmelody.menodora.core.Context;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.startsWith;

public final class ContextTest {

    @Test public void
    retrievesTheRootFromALooseClassFile() {
        assertThat(new Context(org.netmelody.dummy.JsTests.class).root().getPath(), is(not("")));
    }
    
    @Test public void
    retrievesTheRootFromAClassFileInAJar() throws Exception {
        final Class<?> suiteClass = Class.forName("org.netmelody.dummy2.JsTests", true, createClassLoader());
        final Context context = new Context(suiteClass);
        
        final String path = context.root().getPath();
        assertThat(path, startsWith("jar:"));
        assertThat(path, endsWith("dummyproj2.jar!"));
    }

    @Test public void
    canScanInAJar() throws Exception {
        final Class<?> suiteClass = Class.forName("org.netmelody.dummy2.JsTests", true, createClassLoader());
        final Context context = new Context(suiteClass);
        
        Iterable<String> locate = context.reflectionsExample();
        assertThat(locate, Matchers.containsInAnyOrder("jasmine-1.0.2/jasmine.js",
                                                       "env.js-1.2/env.rhino.1.2.js",
                                                       "menodora-js/fake.scheduler.js",
                                                       "dummyjs/Song.js",
                                                       "dummyjs/Player.js",
                                                       "dummyjstests/SpecHelper.js",
                                                       "dummyjstests/PlayerSpec.js"));
    }

    private static ClassLoader createClassLoader() {
        try {
            final List<URL> urls = new ArrayList<URL>();
            urls.add(ContextTest.class.getResource("/dummyproj2.jar").toURI().toURL());
            return new URLClassLoader(urls.toArray(new URL[urls.size()]));
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
}

@@ -93,89 +93,8 @@
 er;%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0Aimport org.hamcrest.Matchers;
 %0Aimp
@@ -257,16 +257,62 @@
 dsWith;%0A
+import static org.hamcrest.Matchers.hasItems;%0A
 import s
@@ -637,20 +637,16 @@
 ;%0A    %7D%0A
-    
 %0A    @Te
@@ -885,32 +885,24 @@
 uiteClass);%0A
-        
 %0A        fin
@@ -1280,24 +1280,16 @@
 Class);%0A
-        
 %0A       
@@ -1289,16 +1289,22 @@
         
+final 
 Iterable
@@ -1382,427 +1382,90 @@
 te, 
-Matchers.containsInAnyOrder(%22jasmine-1.0.2/jasmine.js%22,%0A                                                       %22env.js-1.2/env.rhino.1.2.js%22,%0A                                                       %22menodora-js/fake.scheduler.js%22,%0A                                                       %22dummyjs/Song.js%22,%0A                                                       %22dummyjs/Player.js%22,%0A                                       
+hasItems(%0A                %22dummyjs/Song.js%22,%0A                %22dummyjs/Player.js%22,%0A
     
@@ -1510,47 +1510,8 @@
 s%22,%0A
-                                       
     
@@ -1616,80 +1616,72 @@
 r() 
-%7B%0A        try %7B%0A            final List%3CURL%3E urls = new ArrayList%3CURL%3E();
+throws Exception %7B%0A        return new URLClassLoader(new URL%5B%5D %7B
 %0A   
@@ -1693,17 +1693,12 @@
     
-urls.add(
+    
 Cont
@@ -1745,181 +1745,8 @@
 ar%22)
-.toURI().toURL());%0A            return new URLClassLoader(urls.toArray(new URL%5Burls.size()%5D));%0A        %7D catch (Exception e) %7B%0A            throw new IllegalStateException(e);
 %0A   
@@ -1751,16 +1751,18 @@
        %7D
+);
 %0A    %7D%0A%7D

Fix the ContextTest so it doesn't care about Java's own JS files.