package com.agile.train.util;

import com.agile.train.dto.SourceFile;
import org.junit.Ignore;
import org.junit.Test;

import java.util.List;

import static junit.framework.TestCase.assertNotNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

public class PinganCodeUtilTest {

    @Test
    public void should_read_source_code_by_file_name_in_jar_correct() throws Exception {
        String sourceCode = PinganCodeUtil.readSourceCodeByFileNameInJar(
                "test-repository/cglib-2.2-sources.jar", "net/sf/cglib/beans/BeanCopier.java");
        assertNotNull(sourceCode);
        assertThat(sourceCode.contains("public class BeanCopier"), is(true));
    }

    @Test
    public void should_search_file_in_repository_by_keyword_return_1_file_when_when_have_1_jar() throws Exception {
        List<SourceFile> result = PinganCodeUtil.searchFileInRepositoryByKeyword("test-repository", "BeanCopier");
        assertThat(result.size(), is(1));
        assertThat(result.get(0).getPath(), is("net/sf/cglib/beans/BeanCopier.java"));
        assertThat(result.get(0).getVersion(), is("2.2"));
    }

    @Test
    @Ignore
    public void should_search_file_in_repository_by_keyword_return_more_files_when_when_have_many_jars() throws Exception {
        List<SourceFile> result = PinganCodeUtil.searchFileInRepositoryByKeyword("test-repository", "Log");
        assertThat(result.size(), is(16));
        assertThat(result.get(0).getPath(), is("org/slf4j/impl/Log4jLoggerAdapter.java"));
        assertThat(result.get(result.size() - 1).getPath(), is("org/apache/commons/logging/LogSource.java"));
        assertThat(result.get(1).getVersion(), is("1.7.5"));
        assertThat(result.get(result.size() - 1).getVersion(), is("1.1.1"));
    }

    @Test
    public void should_test() throws Exception {

        String sourceCode = PinganCodeUtil.readSourceCodeByFileNameInJar(
                "test-repository/cglib-2.2-sources.jar", "net/sf/cglib/beans/BeanCopier.java");
        List<String> result = PinganCodeUtil.findMethodsBySourceCode(sourceCode);
        assertThat(result.size(), is(11));
        assertThat(result.contains("void setSource(Class source)"), is(true));
    }

}

@@ -67,33 +67,8 @@
 le;%0A
-import org.junit.Ignore;%0A
 impo
@@ -1107,24 +1107,101 @@
 is(%222.2%22));%0A
+        assertThat(result.get(0).getJarName(), is(%22cglib-2.2-sources.jar%22));%0A
     %7D%0A%0A    @
@@ -1209,20 +1209,8 @@
 est%0A
-    @Ignore%0A
     
@@ -1492,154 +1492,413 @@
     
-assertThat(result.get(0).getPath(), is(%22org/slf4j/impl/Log4jLoggerAdapter.java%22));%0A        assertThat(result.get(result.size() - 1).getPath(), is(
+for (SourceFile sourceFile : result) %7B%0A            assertSourceFileWhenExist(sourceFile, %22org/slf4j/impl/Log4jLoggerAdapter.java%22,%0A                    %22slf4j-log4j12-1.7.5-sources.jar%22, %221.7.5%22);%0A%0A            assertSourceFileWhenExist(sourceFile, %22org/slf4j/impl/Log4jLoggerAdapter.java%22,%0A                    %22slf4j-log4j12-1.7.5-sources.jar%22, %221.7.5%22);%0A%0A            assertSourceFileWhenExist(sourceFile, 
 %22org
@@ -1932,27 +1932,25 @@
 Source.java%22
-));
+,
 %0A        ass
@@ -1950,135 +1950,65 @@
     
-assertThat(result.get(1).getVersion(), is(%221.7.5%22));%0A        assertThat(result.get(result.size() - 1).getVersion(), is(
+            %22commons-logging-1.1.1-sources.jar%22, 
 %221.1.1%22)
 );%0A 
@@ -2003,19 +2003,28 @@
 %221.1.1%22)
-)
 ;%0A
+        %7D%0A
     %7D%0A%0A 
@@ -2055,20 +2055,57 @@
  should_
-test
+return_method_list_when_given_source_code
 () throw
@@ -2500,10 +2500,392 @@
 %0A    %7D%0A%0A
+    private void assertSourceFileWhenExist(SourceFile sourceFile, String exceptPath,%0A                                           String exceptJarName, String exceptVersion) %7B%0A        if (sourceFile.getPath().equals(exceptPath)) %7B%0A            assertThat(sourceFile.getJarName(), is(exceptJarName));%0A            assertThat(sourceFile.getVersion(), is(exceptVersion));%0A        %7D%0A    %7D%0A%0A
 %7D%0A

refactor test case