Skip to content

Commit

Permalink
Better test coverage #13
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 13, 2023
1 parent 55f7fa0 commit b500f8d
Show file tree
Hide file tree
Showing 52 changed files with 3,077 additions and 111 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package org.fugerit.java.daogen.base.gen.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.function.SimpleValue;
import org.fugerit.java.core.io.FileIO;

public class ExtractCustomCode {

private ExtractCustomCode() {}

public static String extractCustom( File file, String startTag, String endTag ) {
return SafeFunction.get( () -> extractCustom( FileIO.readString( file ) , startTag, endTag ) );
}

public static String extractCustom( CharSequence text, String startTag, String endTag ) {
return SafeFunction.get( () -> {
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter( buffer, true ) ) {
SimpleValue<Boolean> customCode = new SimpleValue<>( false );
reader.lines().forEach( line -> {
if ( line.contains( startTag ) ) {
customCode.setValue( true );
} else if ( line.contains( endTag ) ) {
customCode.setValue( false );
} else if ( customCode.getValue().booleanValue() ) {
writer.println( line );
}
} );
return buffer.toString();
}
} );
}

public static String addCustomContent( CharSequence text, String startTag, String endTag, String customContent ) {
return SafeFunction.get( () -> {
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter( buffer, true ) ) {
SimpleValue<Boolean> customCode = new SimpleValue<>( false );
reader.lines().forEach( line -> {
if ( line.contains( startTag ) ) {
customCode.setValue( true );
} else if ( line.contains( endTag ) ) {
writer.println( customContent ); // append custom content
customCode.setValue( false );
}
// all lines myst be written anyway
writer.println( line );
} );
return buffer.toString();
}
} );
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package test.org.fugerit.java.daogen.util;

import java.io.File;

import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.io.FileIO;
import org.fugerit.java.core.javagen.SimpleJavaGenerator;
import org.fugerit.java.daogen.base.gen.util.ExtractCustomCode;
import org.junit.Assert;
import org.junit.Test;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestExtractCustomCode {

@Test
public void test001() {
SafeFunction.apply( () -> {
String path = "../fj-daogen-tool/src/test/resources/compare_handler_test/daogen_original/org/fugerit/java/daogen/sample/impl/facade/data/DataEntityAddressFacadeHelper.java";
File file = new File( path );
log.info( "try source : {}", file.getCanonicalPath() );
String content = FileIO.readString( file );
String result = ExtractCustomCode.extractCustom( content , SimpleJavaGenerator.CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_END );
log.info( "result : {}", result );
Assert.assertNotNull( result );
} );

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
Expand All @@ -14,79 +15,107 @@
import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.io.FileIO;
import org.fugerit.java.core.javagen.SimpleJavaGenerator;
import org.fugerit.java.core.lang.helpers.BooleanUtils;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.daogen.base.gen.util.ExtractCustomCode;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class CompareHandler {

private static final String SERIAL_UID_STRING = "serialVersionUID";

public static final String ARG_REPORT = "report";

public static final String ARG_TRY_DELETE_EQUAL = "try-delere-equal";

public static final String ARG_TRY_CORRECT_HELPER = "try-correct-helper";
public static final String ARG_CUSTOM_CODE_START = "custom-code-start";
public static final String ARG_CUSTOM_CODE_END = "custom-code-end";
public static final String ARG_CUSTOM_IMPORT_START = "custom-import-start";
public static final String ARG_CUSTOM_IMPORT_END = "custom-import-end";

public void handleCompare( File baseDir, File file1, File file2, Properties params ) {
SafeFunction.apply( () -> {
try ( StringWriter writer = new StringWriter();
PrintWriter report = new PrintWriter( writer, true ) ) {
String removePath = baseDir == null ? "" : baseDir.getCanonicalPath();
handleCompareWork(removePath, file1, file2, params, report);
log.info( "print report : \n{}", writer.toString() );
String reportFile = params.getProperty( ARG_REPORT );
if ( StringUtils.isNotEmpty( reportFile ) ) {
log.info( "write report file : {}", reportFile );
FileIO.writeString( writer.toString() , new File(reportFile) );
} else {
log.info( "print report : \n{}", writer.toString() );
}
}
} );

}

private String removePath( String path, String removePath ) {
String res = path;
if ( StringUtils.isNotEmpty( removePath ) ) {
if ( path.indexOf( removePath ) == 0 ) {
res = path.substring( removePath.length() );
}
if ( StringUtils.isNotEmpty( removePath ) && path.indexOf( removePath ) == 0) {
res = path.substring( removePath.length() );
}
return res;
}

private void tryCorrectHelperEqual( File file1, File file2, Properties params, PrintWriter report ) {
private void handleRealFileCorrect( File file2, Properties params, PrintWriter report, File realFile ) throws IOException {
report.print( "real file exists, try to correct from helper : "+realFile.getName() );
String customCodeStart = params.getProperty( ARG_CUSTOM_CODE_START, SimpleJavaGenerator.CUSTOM_CODE_START );
String customCodeEnd = params.getProperty( ARG_CUSTOM_CODE_END, SimpleJavaGenerator.CUSTOM_CODE_END );
String customImportStart = params.getProperty( ARG_CUSTOM_IMPORT_START, SimpleJavaGenerator.CUSTOM_IMPORT_START );
String customImportEnd = params.getProperty( ARG_CUSTOM_IMPORT_END, SimpleJavaGenerator.CUSTOM_IMPORT_END );
String contentCode = ExtractCustomCode.extractCustom( file2 , customCodeStart, customCodeEnd);
String contentImport = ExtractCustomCode.extractCustom( file2 , customImportStart, customImportEnd);
log.info( "customCode : {}", contentCode );
log.info( "contentImport : {}", contentImport );
if ( StringUtils.isNotEmpty( contentCode ) || StringUtils.isNotEmpty( contentImport ) ) {
String realFileContent = FileIO.readString( realFile );
if ( StringUtils.isNotEmpty( contentCode ) ) {
realFileContent = ExtractCustomCode.addCustomContent( realFileContent , customCodeStart, customCodeEnd, contentCode );
}
if ( StringUtils.isNotEmpty( contentImport ) ) {
realFileContent = ExtractCustomCode.addCustomContent( realFileContent , customImportStart, customImportEnd, contentImport );
}
FileIO.writeString( realFileContent , realFile );
report.print( " real file customized! "+realFileContent );
}
}

private void tryCorrectHelperEqual( File file2, Properties params, PrintWriter report ) throws IOException {
boolean tryCorrectHelper = BooleanUtils.isTrue( params.getProperty( ARG_TRY_CORRECT_HELPER ) );
if ( tryCorrectHelper ) {
if ( file2.getName().endsWith( "Helper.java" ) ) {
String realFileName = file2.getName().replace( "Helper.java" , ".java" );
File realFile = new File( file2.getParentFile(), realFileName );
if ( realFile.exists() ) {
report.print( "real file exists, try to correct from helper : "+realFileName );
} else {
report.print( "default real file not found : "+realFileName );
}
if ( tryCorrectHelper && file2.getName().endsWith( "Helper.java" ) ) {
String realFileName = file2.getName().replace( "Helper.java" , ".java" );
File realFile = new File( file2.getParentFile(), realFileName );
if ( realFile.exists() ) {
handleRealFileCorrect(file2, params, report, realFile);
} else {
report.print( "default real file not found : "+realFileName );
}
}
}

private void tryDeleteEqual( File file1, File file2, Properties params, PrintWriter report ) {
private void tryDeleteEqual( File file2, Properties params, PrintWriter report ) throws IOException {
boolean tryDeleteEqual = BooleanUtils.isTrue( params.getProperty( ARG_TRY_DELETE_EQUAL ) );
if ( tryDeleteEqual ) {
report.print( " - try delete equal result : "+file2.delete() );
report.print( " - try delete equal result : "+ Files.deleteIfExists( file2.toPath() ) );
}
}

private void compareFile( File file1, File file2, Properties params, PrintWriter report ) throws IOException {
List<String> lines1 = new ArrayList<>();
List<String> lines2 = new ArrayList<>();
try ( BufferedReader reader1 = new BufferedReader( new FileReader( file1 ) );
BufferedReader reader2 = new BufferedReader( new FileReader( file2 ) ) ) {
lines1.addAll( reader1.lines().collect( Collectors.toList() ) );
lines2.addAll( reader2.lines().collect( Collectors.toList() ) );
private void checkDiffResult( File file2, Properties params, PrintWriter report, int diffSize, boolean differentUid ) throws IOException {
if ( diffSize > 0 ) {
report.print( " - diffeent lines : "+diffSize );
} else {
this.tryDeleteEqual(file2, params, report);
}
// check
if ( differentUid ) {
report.print( " - diffeent serialVersionUID" );
}
}

private void checkLines( File file2, Properties params, PrintWriter report, List<String> lines1, List<String> lines2 ) throws IOException {
report.print( "file 1 size : "+lines1.size()+", " );
if ( lines1.size() == lines2.size() ) {
report.print( "file 2 same size! " );
Expand All @@ -96,28 +125,33 @@ private void compareFile( File file1, File file2, Properties params, PrintWriter
String current1 = lines1.get( k );
String current2 = lines2.get( k );
if ( !current1.equals( current2 ) ) {
if ( current1.contains( "serialVersionUID" ) && current2.contains( "serialVersionUID" ) ) {
if ( current1.contains( SERIAL_UID_STRING ) && current2.contains( SERIAL_UID_STRING ) ) {
differentUid = true;
} else {
diffSize++;
}
}
}
if ( diffSize > 0 ) {
report.print( " - diffeent lines : "+diffSize );
} else {
this.tryDeleteEqual(file1, file2, params, report);
}
if ( differentUid ) {
report.print( " - diffeent serialVersionUID" );
}
this.checkDiffResult( file2, params, report, diffSize, differentUid);
} else {
report.print( "file 2 size : "+lines1.size()+" DIFFERENT! " );
this.tryCorrectHelperEqual(file1, file2, params, report);
this.tryCorrectHelperEqual(file2, params, report);
}
report.println();
}

private void compareFile( File file1, File file2, Properties params, PrintWriter report ) throws IOException {
List<String> lines1 = new ArrayList<>();
List<String> lines2 = new ArrayList<>();
try ( BufferedReader reader1 = new BufferedReader( new FileReader( file1 ) );
BufferedReader reader2 = new BufferedReader( new FileReader( file2 ) ) ) {
lines1.addAll( reader1.lines().collect( Collectors.toList() ) );
lines2.addAll( reader2.lines().collect( Collectors.toList() ) );
}
// check
this.checkLines(file2, params, report, lines1, lines2);
}

private void handleCompareWork( String removePath, File file1, File file2, Properties params, PrintWriter report ) throws ConfigException, IOException {
String fileEntry1 = removePath( file1.getCanonicalPath(), removePath );
String fileEntry2 = removePath( file2.getCanonicalPath(), removePath );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
@Slf4j
public class TestCompareHandler {

private static final String SOURCE_TEST = "src/test/resources/compare_handler_test/daogen";
private static final String SOURCE_GENERATED = "src/test/resources/compare_handler_test/daogen_generated";

private static final String SOURCE_ORIGINAL = "src/test/resources/compare_handler_test/daogen_original";

private static void copyHelper( File baseSource, File baseDest, File currentSource ) throws IOException {
String relPath = currentSource.getCanonicalPath().substring( baseSource.getCanonicalPath().length() );
Expand All @@ -36,24 +38,38 @@ private static void copyHelper( File baseSource, File baseDest, File currentSour
}
}

private static int deleteRecurse( File dir ) {
int res = 0;
if ( dir.isDirectory() ) {
for ( File file : dir.listFiles() ) {
res+= deleteRecurse( file );
}
}
if ( dir.delete() ) {
res++;
}
return res;
}

private static void copyHelper( File sourceFile, File testDir ) throws IOException {
if ( !testDir.exists() ) {
copyHelper(sourceFile, testDir, sourceFile );
} else {
log.info( "testDir already exists! {}", testDir );
if ( testDir.exists() ) {
int res = deleteRecurse( testDir );
log.info( "delete result {} : {}", testDir, res );
}
copyHelper(sourceFile, testDir, sourceFile );
}

private static void prepare( File file1, File file2 ) {
SafeFunction.apply( () -> {
File sourceDir = new File( SOURCE_TEST );
File sourceDir = new File( SOURCE_GENERATED );
File sourceDirOriginal = new File( SOURCE_ORIGINAL );
copyHelper( sourceDir, file1 );
copyHelper( sourceDir, file2 );
copyHelper( sourceDirOriginal, file2 );
});
}

@Test
public void test() {
public void test001() {
File baseDir = new File( "target" );
File file1 = new File( baseDir, "daogen1" );
File file2 = new File( baseDir, "daogen2" );
Expand All @@ -62,6 +78,7 @@ public void test() {
Assert.assertTrue( file2.exists() );
CompareHandler handler = new CompareHandler();
Properties params = new Properties();
params.setProperty( CompareHandler.ARG_REPORT , "target/report_compare_handler_001.md" );
params.setProperty( CompareHandler.ARG_TRY_CORRECT_HELPER , BooleanUtils.BOOLEAN_TRUE );
params.setProperty( CompareHandler.ARG_TRY_DELETE_EQUAL , BooleanUtils.BOOLEAN_TRUE );
handler.handleCompare( baseDir, file1, file2, params );
Expand Down
Loading

0 comments on commit b500f8d

Please sign in to comment.