package com.yahoo.abicheck;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.google.common.collect.Iterables;
import com.yahoo.abicheck.classtree.ClassFileTree;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.junit.jupiter.api.Test;

public class ClassFileTreeTest {

  @Test
  public void testJarParsing() throws IOException {
    JarFile jarFile = mock(JarFile.class);
    JarEntry jarEntry = new JarEntry("com/yahoo/Test.class");
    InputStream jarEntryStream = mock(InputStream.class);
    when(jarFile.entries()).thenReturn(Collections
        .enumeration(Collections.singleton(jarEntry)));
    when(jarFile.getInputStream(jarEntry)).thenReturn(jarEntryStream);

    ClassFileTree cft = ClassFileTree.fromJar(jarFile);
    ClassFileTree.Package com = Iterables.getOnlyElement(cft.getRootPackages());
    assertThat(com.getFullyQualifiedName(), equalTo("com"));
    assertThat(com.getClassFiles(), empty());
    ClassFileTree.Package yahoo = Iterables.getOnlyElement(com.getSubPackages());
    assertThat(yahoo.getFullyQualifiedName(), equalTo("com.yahoo"));
    assertThat(yahoo.getSubPackages(), empty());
    ClassFileTree.ClassFile testClassFile = Iterables.getOnlyElement(yahoo.getClassFiles());
    assertThat(testClassFile.getName(), equalTo("Test.class"));
    assertThat(testClassFile.getInputStream(), sameInstance(jarEntryStream));
  }
}

@@ -445,16 +445,41 @@
 Stream;%0A
+import java.util.Arrays;%0A
 import j
@@ -747,17 +747,22 @@
 arEntry 
-j
+classJ
 arEntry 
@@ -801,16 +801,71 @@
 lass%22);%0A
+    JarEntry dirJarEntry = new JarEntry(%22com/yahoo/%22);%0A
     Inpu
@@ -941,16 +941,25 @@
 tries())
+%0A        
 .thenRet
@@ -973,25 +973,16 @@
 lections
-%0A        
 .enumera
@@ -990,31 +990,41 @@
 ion(
-Collections.singleton(j
+Arrays.asList(dirJarEntry, classJ
 arEn
@@ -1063,17 +1063,22 @@
 tStream(
-j
+classJ
 arEntry)
@@ -1108,24 +1108,29 @@
 ream);%0A%0A    
+try (
 ClassFileTre
@@ -1167,18 +1167,22 @@
 jarFile)
-;%0A
+) %7B%0A  
     Clas
@@ -1246,32 +1246,34 @@
 ootPackages());%0A
+  
     assertThat(c
@@ -1313,32 +1313,34 @@
 To(%22com%22));%0A    
+  
 assertThat(com.g
@@ -1357,32 +1357,34 @@
 es(), empty());%0A
+  
     ClassFileTre
@@ -1445,32 +1445,34 @@
 ackages());%0A    
+ 
+ 
 assertThat(yahoo
@@ -1516,32 +1516,34 @@
 m.yahoo%22));%0A    
+  
 assertThat(yahoo
@@ -1567,24 +1567,26 @@
 , empty());%0A
+  
     ClassFil
@@ -1662,32 +1662,34 @@
 ssFiles());%0A    
+  
 assertThat(testC
@@ -1728,24 +1728,26 @@
 t.class%22));%0A
+  
     assertTh
@@ -1812,14 +1812,20 @@
 ream));%0A
+    %7D%0A
   %7D%0A%7D%0A

Improve test coverage.