package com.dkarv.jdcallgraph.instr;

import com.dkarv.jdcallgraph.instr.JavassistInstr;
import com.dkarv.jdcallgraph.util.config.ConfigUtils;
import javassist.*;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.lang.instrument.Instrumentation;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class JavassistInstrTest {
  @Rule
  public TemporaryFolder tmp = new TemporaryFolder();

  @Test
  public void testGetShortName() {
    String name = "abc.def.ghi.Example";
    CtClass cl = new CtClass(name) {
    };
    Assert.assertEquals("Example", JavassistInstr.getShortName(cl));
    Assert.assertEquals("Example", JavassistInstr.getShortName(name));
  }

  @Test
  public void testTransform() {
    String className = "abc/def/ghi/Example";
    List<Pattern> excludes = new ArrayList<>();
    Pattern pattern = Mockito.mock(Pattern.class);
    excludes.add(pattern);
    Matcher m = Mockito.mock(Matcher.class);
    Mockito.when(pattern.matcher(Mockito.anyString())).thenReturn(m);
    byte[] input = new byte[]{1, 2, 3, 4, 5};
    byte[] output = new byte[]{5, 4, 3, 2, 1};

    JavassistInstr p = Mockito.spy(new JavassistInstr(excludes));
    Mockito.doReturn(output).when(p).enhanceClass(Mockito.any(byte[].class));

    // Ignore class test
    Mockito.when(m.matches()).thenReturn(true);
    byte[] result = p.transform(null, className, null, null, input);
    Assert.assertArrayEquals(input, result);
    Mockito.verify(p, Mockito.never()).enhanceClass(Mockito.<byte[]>any());

    // Do not ignore class test
    Mockito.when(m.matches()).thenReturn(false);
    result = p.transform(null, className, null, null, input);
    Assert.assertArrayEquals(output, result);
    Mockito.verify(p).enhanceClass(Mockito.eq(input));
  }

  @Test
  public void testMakeClass() throws IOException {
    byte[] input = new byte[]{1, 2, 3, 4, 5};
    ClassPool pool = Mockito.mock(ClassPool.class);
    (new JavassistInstr(new ArrayList<Pattern>())).makeClass(pool, input);

    ArgumentCaptor<ByteArrayInputStream> captor = ArgumentCaptor.forClass(ByteArrayInputStream.class);
    Mockito.verify(pool).makeClass(captor.capture());
    ByteArrayInputStream stream = captor.getValue();
    byte[] array = new byte[stream.available()];
    stream.read(array);
    Assert.assertArrayEquals(input, array);
  }


}

@@ -35,59 +35,8 @@
 r;%0A%0A
-import com.dkarv.jdcallgraph.instr.JavassistInstr;%0A
 impo
@@ -76,19 +76,9 @@
 fig.
-ConfigUtils
+*
 ;%0Aim
@@ -116,60 +116,9 @@
 nit.
-Assert;%0Aimport org.junit.Rule;%0Aimport org.junit.Test
+*
 ;%0Aim
@@ -138,31 +138,17 @@
 t.rules.
-TemporaryFolder
+*
 ;%0Aimport
@@ -164,307 +164,48 @@
 ito.
-ArgumentCaptor;%0Aimport org.mockito.Mockito;%0A%0Aimport java.io.ByteArrayInputStream;%0Aimport java.io.File;%0Aimport java.io.IOException;%0Aimport java.lang.instrument.Instrumentation;%0Aimport java.nio.file.NoSuchFileException;%0Aimport java.util.ArrayList;%0Aimport java.util.List;%0Aimport java.util.regex.Matcher
+*;%0A%0Aimport java.io.*;%0Aimport java.util.*
 ;%0Aim
@@ -225,23 +225,17 @@
 l.regex.
-Pattern
+*
 ;%0A%0Apubli
@@ -326,16 +326,104 @@
 der();%0A%0A
+  @Before%0A  public void init() throws IOException %7B%0A    ConfigUtils.replace(true);%0A  %7D%0A%0A
   @Test%0A

Fix unit tests