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 44787c3
Show file tree
Hide file tree
Showing 50 changed files with 2,955 additions and 110 deletions.
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 @@ -22,6 +23,8 @@
@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";
Expand All @@ -34,12 +37,11 @@ public void handleCompare( File baseDir, File file1, File file2, Properties para
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() );
}
}
} );
Expand All @@ -48,45 +50,44 @@ public void handleCompare( File baseDir, File file1, File file2, Properties para

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 tryCorrectHelperEqual( File file2, Properties params, PrintWriter report ) {
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() ) {
report.print( "real file exists, try to correct from helper : "+realFileName );
} 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 file1, 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 +97,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(file1, 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package org.fugerit.java.daogen.sample.def.facade;

import org.fugerit.java.core.db.dao.DAOException;
import org.fugerit.java.core.db.daogen.BasicDaoResult;
import org.fugerit.java.core.db.daogen.DAOContext;
import org.fugerit.java.daogen.sample.def.model.ModelAddress;

// custom import start ( code above here will be overwritten )
// custom import end ( code below here will be overwritten )

/**
* EntityAddressFacadeHelper, version : 1.0.0
*
* author: fugerit
*
* warning!: auto generated object, insert custom code only between comments :
* // custom code start ( code above here will be overwritten )
* // custom code end ( code below here will be overwritten )
*/
public interface EntityAddressFacadeHelper {

// custom code start ( code above here will be overwritten )
// custom code end ( code below here will be overwritten )

/*
* NOTE: It is advised to use a finder for incapsulating search params, except searches for primary key.
*/

/**
* Method to load all the items for entity : ModelAddress
*
* @param context DAOContext
*
* @return search result
* @throws DAOException in case of errors
*/
BasicDaoResult<ModelAddress> loadAll( DAOContext context ) throws DAOException;

/**
* Method to load all the items for entity : ModelAddress
*
* @param context DAOContext
* @param finder the finder incapsulating search params
*
* @return search result
* @throws DAOException in caso di errori
*/
BasicDaoResult<ModelAddress> loadAllByFinder( DAOContext context, AddressFinder finder ) throws DAOException;

/**
* Load method by primary key for entity : ModelAddress
*
* @param context DAO Context
* @param id Address system id
*
* @return The found object or <code>null</code>
* @throws DAOException in case of errors
*/
ModelAddress loadById( DAOContext context, java.math.BigDecimal id ) throws DAOException;

/**
* Method to create an new entity of type : ModelAddress
*
* A new ID should be assigned by this method.
*
* @param context DAO context
* @param model Entity to create
*
* @return The created entity
* @throws DAOException In case of any error.
*/
BasicDaoResult<ModelAddress> create( DAOContext context, ModelAddress model ) throws DAOException;

/**
* Delete method by primary key for entity : ModelAddress
*
* @param context DAO Context
* @param id Address system id
*
* @return Delete result (resultCode=0, delete ok)
* @throws DAOException in case of errors
*/
BasicDaoResult<ModelAddress> deleteById( DAOContext context, java.math.BigDecimal id ) throws DAOException;

/**
* Delete method by primary key for entity : ModelAddress
*
* @param context DAO Context
* @param model entity to update
*
* @return Update result (resultCode=0, update ok)
* @throws DAOException in case of errors
*/
BasicDaoResult<ModelAddress> updateById( DAOContext context, ModelAddress model ) throws DAOException;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.fugerit.java.daogen.sample.def.facade;

import org.fugerit.java.core.db.dao.DAOException;
import org.fugerit.java.core.db.daogen.BasicDaoResult;
import org.fugerit.java.core.db.daogen.DAOContext;
import org.fugerit.java.daogen.sample.def.model.ModelLogData;

// custom import start ( code above here will be overwritten )
// custom import end ( code below here will be overwritten )

/**
* EntityLogDataFacadeHelper, version : 1.0.0
*
* author: fugerit
*
* warning!: auto generated object, insert custom code only between comments :
* // custom code start ( code above here will be overwritten )
* // custom code end ( code below here will be overwritten )
*/
public interface EntityLogDataFacadeHelper {

// custom code start ( code above here will be overwritten )
// custom code end ( code below here will be overwritten )

/*
* NOTE: It is advised to use a finder for incapsulating search params, except searches for primary key.
*/

/**
* Method to load all the items for entity : ModelLogData
*
* @param context DAOContext
*
* @return search result
* @throws DAOException in case of errors
*/
BasicDaoResult<ModelLogData> loadAll( DAOContext context ) throws DAOException;

/**
* Method to load all the items for entity : ModelLogData
*
* @param context DAOContext
* @param finder the finder incapsulating search params
*
* @return search result
* @throws DAOException in caso di errori
*/
BasicDaoResult<ModelLogData> loadAllByFinder( DAOContext context, LogDataFinder finder ) throws DAOException;

}
Loading

0 comments on commit 44787c3

Please sign in to comment.