Skip to content

Commit

Permalink
- fixed invalid invalid test case analyzer in JTK
Browse files Browse the repository at this point in the history
- summary printed by analisys printer
  • Loading branch information
kcrimson committed May 16, 2010
1 parent 1e3bcce commit 21ece01
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 30 deletions.
Expand Up @@ -2,7 +2,6 @@
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.testdriven.jtk;

import java.io.OutputStream;
Expand All @@ -13,18 +12,27 @@
* @author jpalka
*/
class AnalyzerResultsPrinter {

private final PrintWriter writer;

AnalyzerResultsPrinter(OutputStream output) {
this.writer = new PrintWriter(output);
}

void writeSummary(AnalyzerResults results) {
if(results.getTestCases().length == 0){
if (results.getTestCases().length == 0) {
writer.println("no test cases found");
}

} else {

int testCasesCount = results.getTestCases().length;
int testMethodCount = 0;
for (TestCase testCase : results.getTestCases()) {
testMethodCount += testCase.getTestCaseMethods().length;
}

writer.println("found " + testCasesCount + " test case(s) with " + testMethodCount + " method(s)");
}
writer.flush();
}

}
5 changes: 3 additions & 2 deletions jtk-core/src/main/java/org/testdriven/jtk/JTK.java
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.testdriven.jtk.junit4.JUnit4AnalisysEngine;

public class JTK {
Expand All @@ -23,9 +24,9 @@ public JTK(String sourcedir, String classesdir) throws IOException {
}

public TestCaseAnalyzer createTestCaseAnalyzer() {
AnalisysEngine engine = new JUnit4AnalisysEngine(null);
AnalisysEngine engine = new JUnit4AnalisysEngine(Arrays.asList("org.junit.Assert"));
String[] sourcesDirs = new String[]{sourcesDir.getAbsolutePath()};
String[] classesDirs = new String[]{classesDir.getAbsolutePath()};
return new TestCaseAnalyzer(null, sourcesDirs, classesDirs);
return new TestCaseAnalyzer(engine, sourcesDirs, classesDirs);
}
}
29 changes: 16 additions & 13 deletions jtk-core/src/main/java/org/testdriven/jtk/Main.java
Expand Up @@ -2,18 +2,21 @@

public final class Main {

/**
* Main application entry point
*/
public static void main(final String[] args) throws Exception{
JTKCommandParser parser = new JTKCommandParser(args);
JTKCommandOptions options = parser.parse();
/**
* Main application entry point
*/
public static void main(final String[] args) throws Exception {
JTKCommandParser parser = new JTKCommandParser(args);
JTKCommandOptions options = parser.parse();
final String sources = options.getSources()[0];
System.out.println("Sources " + sources);
final String classes = options.getClasses()[0];
System.out.println("Classes " + classes);
JTK jtk = new JTK(sources, classes);
TestCaseAnalyzer analyzer = jtk.createTestCaseAnalyzer();
AnalyzerResults results = analyzer.analyzeTestCases();

JTK jtk = new JTK(options.getSources()[0],options.getClasses()[0]);
TestCaseAnalyzer analyzer = jtk.createTestCaseAnalyzer();
AnalyzerResults results = analyzer.analyzeTestCases();

AnalyzerResultsPrinter printer = new AnalyzerResultsPrinter(System.out);
printer.writeSummary(results);
}
AnalyzerResultsPrinter printer = new AnalyzerResultsPrinter(System.out);
printer.writeSummary(results);
}
}
19 changes: 12 additions & 7 deletions jtk-core/src/main/java/org/testdriven/jtk/TestCaseAnalyzer.java
Expand Up @@ -63,26 +63,31 @@ public AnalyzerResults analyzeTestCases() throws IOException {

//match test cases sources with class
for (CompilationUnit unit : units) {
for(String baseDir:classesDirs){
for (String baseDir : classesDirs) {
File classFile = unit.getClassFile(baseDir);
if(classFile.exists()){
if (classFile.exists()) {
System.out.println(unit.getSourceFile().getAbsolutePath() + "->" + classFile.getAbsolutePath());
unit.setClassFile(classFile);
break;
}
}
if(unit.getClassFile()==null){
throw new FileNotFoundException("Class file for source "+unit.getSourceFile().getAbsolutePath()+" not found");
if (unit.getClassFile() == null) {
throw new FileNotFoundException("Class file for source " + unit.getSourceFile().getAbsolutePath() + " not found");
}
}

List<TestCase> testCases = new ArrayList<TestCase>();
for(CompilationUnit unit:units){
for (CompilationUnit unit : units) {
TestCase testCase = new TestCase(unit);
final File classFile = unit.getClassFile();
TestCaseMethod[] methods = engine.getTestMethods(new FileInputStream(classFile));
System.out.println(unit.getSourceFile().getAbsolutePath() + "->" + (classFile!=null?classFile.getAbsolutePath():null));
final FileInputStream inputStream = new FileInputStream(classFile);
System.out.println("engine "+engine);
TestCaseMethod[] methods = engine.getTestMethods(inputStream);
testCase.setTestCaseMethods(methods);
testCases.add(testCase);
}

return new AnalyzerResults(testCases);
}

Expand Down
Expand Up @@ -46,7 +46,7 @@ public void setCurrentMethodName(String name) {
methodName = name;
}

public boolean matchesAssetionsFilter(String owner) {
public boolean matchesAssertionsFilter(String owner) {

return hasTestCaseMehtods()
&& assertionsFilter.contains(owner.replace("/", "."));
Expand Down
Expand Up @@ -30,7 +30,7 @@ public AnnotationVisitor visitAnnotation(String name, boolean visible) {
public void visitMethodInsn(int opcode, String owner, String name,
String desc) {

if (analisys.matchesAssetionsFilter(owner)) {
if (analisys.matchesAssertionsFilter(owner)) {

TestCaseAssertion testCaseAssertion = new TestCaseAssertion(owner.replace("/", "."), currentLineNumber);
analisys.addAssertion(testCaseAssertion);
Expand Down
Expand Up @@ -2,14 +2,16 @@
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.testdriven.jtk;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import org.junit.Assert;
import org.junit.Test;
import org.junit.internal.runners.TestMethod;

/**
*
Expand All @@ -18,7 +20,7 @@
public class AnalyzerResultsPrinterTest {

@Test
public void should_print_test_cases_not_found(){
public void should_print_test_cases_not_found() {
//given
AnalyzerResults results = new AnalyzerResults(new ArrayList<TestCase>());
ByteArrayOutputStream output = new ByteArrayOutputStream();
Expand All @@ -31,4 +33,24 @@ public void should_print_test_cases_not_found(){
String summary = new String(output.toByteArray());
Assert.assertEquals("no test cases found\n", summary);
}

@Test
public void should_print_test_cases_summary() {
//given
File baseDir = null;
File sourceFile = null;
TestCase testCase = new TestCase(new CompilationUnit(baseDir,sourceFile));
TestCaseMethod testMethod = new TestCaseMethod("", 0);
testCase.setTestCaseMethods(new TestCaseMethod[]{testMethod});
AnalyzerResults results = new AnalyzerResults(Arrays.asList(testCase));
ByteArrayOutputStream output = new ByteArrayOutputStream();
AnalyzerResultsPrinter printer = new AnalyzerResultsPrinter(output);

//when
printer.writeSummary(results);

//than
String summary = new String(output.toByteArray());
Assert.assertEquals("found 1 test case(s) with 1 method(s)\n", summary);
}
}
Expand Up @@ -27,7 +27,9 @@ public void should_analyze_test_cases() throws Exception {
assertThat(testCases).hasSize(9);
for (TestCase testCase : testCases) {
assertThat(testCase.getUnit()).isNotNull();
assertThat(testCase.getUnit().getClassFile()).isNotNull();
assertThat(testCase.getTestCaseMethods()).isNotEmpty();

}
verify(engine, times(9)).getTestMethods(any(InputStream.class));
}
Expand Down

0 comments on commit 21ece01

Please sign in to comment.