package com.github.jvmgo.classpath;

import org.junit.Test;
import java.io.File;
import java.util.Arrays;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class EntryTest {

    @Test
    public void create() {
        assertEquals(DirEntry.class, Entry.create(".").getClass());
        assertEquals(CompositeEntry.class,
                Entry.create("foo.jar" + File.pathSeparator + "bar.jar").getClass());
        assertEquals(WildcardEntry.class, Entry.create("foo/bar/*").getClass());
    }

    @Test
    public void dirEntry() throws Exception {
        String className = EntryTest.class.getName().replace('.', '/') + ".class";
        String classesDir = EntryTest.class.getResource("EntryTest.class")
                .getPath().replace(className, "");

        Entry entry = Entry.create(classesDir);
        assertEquals(DirEntry.class, entry.getClass());

        byte[] data = entry.readClass(className);
        assertNotNull(data);
    }

    @Test
    public void zipEntry() throws Exception {
        String[] cp = System.getProperty("java.class.path").split(File.pathSeparator);
        String rtJarPath = Arrays.stream(cp)
                .filter(path -> path.endsWith("/rt.jar"))
                .findFirst()
                .get();

        Entry entry = Entry.create(rtJarPath);
        assertEquals(ZipEntry.class, entry.getClass());

        byte[] data = entry.readClass("java/lang/Object.class");
        assertNotNull(data);
    }

    @Test
    public void compositeEntry() throws Exception {
        String cp = System.getProperty("java.class.path");

        Entry entry = Entry.create(cp);
        assertEquals(CompositeEntry.class, entry.getClass());

        byte[] data = entry.readClass("java/lang/Object.class");
        assertNotNull(data);
    }

    @Test
    public void wildcardEntry() throws Exception {
        String[] cp = System.getProperty("java.class.path").split(File.pathSeparator);
        String rtJarPath = Arrays.stream(cp)
                .filter(path -> path.endsWith("/rt.jar"))
                .findFirst()
                .get()
                .replace("rt.jar", "*");

        Entry entry = Entry.create(rtJarPath);
        assertEquals(WildcardEntry.class, entry.getClass());

        byte[] data = entry.readClass("java/lang/Object.class");
        assertNotNull(data);
    }

}

@@ -1225,34 +1225,42 @@
 path.endsWith(%22/
-rt
+junit-4.12
 .jar%22))%0A        
@@ -1444,39 +1444,37 @@
 .readClass(%22
-java/lang/Objec
+org/junit/Tes
 t.class%22);%0A 
@@ -1768,39 +1768,37 @@
 .readClass(%22
-java/lang/Objec
+org/junit/Tes
 t.class%22);%0A 
@@ -2073,18 +2073,26 @@
 sWith(%22/
-rt
+junit-4.12
 .jar%22))%0A
@@ -2173,18 +2173,27 @@
 ce(%22
-rt
+/junit-4.12
 .jar%22, %22
 *%22);
@@ -2188,16 +2188,17 @@
 .jar%22, %22
+/
 *%22);%0A%0A  
@@ -2347,23 +2347,21 @@
 ss(%22
-java/lang/Objec
+org/junit/Tes
 t.cl

fix unit tests