Skip to content

Commit

Permalink
Test coverage for tool compare #13
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 13, 2023
1 parent d78652b commit 73f5a9c
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

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

import lombok.Getter;
import lombok.Setter;

public class ExtractCustomCode {

private ExtractCustomCode() {}
Expand Down Expand Up @@ -63,20 +66,22 @@ public static String addCustomContent( CharSequence text, String startTag, Strin
} );
}

private static String addWithCondition( CharSequence text, String customContent, Function<LineCursor, Boolean> condition ) {
private static String addWithCondition( CharSequence text, String customContent, Predicate<LineCursor> condition ) {
return SafeFunction.get( () -> {
try ( BufferedReader reader = new BufferedReader( new StringReader( text.toString() ) );
StringWriter buffer = new StringWriter();
PrintWriter writer = new PrintWriter( buffer, true ) ) {
List<String> lines = reader.lines().collect( Collectors.toList() );
LineCursor cursor = new LineCursor();
cursor.lines = lines;
for ( cursor.index = 0; cursor.index<lines.size(); cursor.index++ ) {
String currentLine = lines.get( cursor.index );
if ( condition.apply( cursor ).booleanValue() ) {
cursor.setLines( lines );
cursor.setIndex( 0 );
while ( cursor.getIndex()<lines.size() ) {
String currentLine = lines.get( cursor.getIndex() );
if ( condition.test( cursor ) ) {
writer.println( customContent );
}
writer.println( currentLine );
cursor.setIndex( cursor.getIndex()+1 );
}
return buffer.toString();
}
Expand All @@ -102,9 +107,9 @@ public static String addBeforeClassEnd( CharSequence text, String customContent

class LineCursor {

public List<String> lines;
@Getter @Setter private List<String> lines;

public int index;
@Getter @Setter private int index;

public boolean isLast() {
return this.lines.size()-1 == this.index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.util.PropsIO;
import org.fugerit.java.daogen.base.config.DaogenFacade;
import org.fugerit.java.daogen.base.tool.handler.CompareHandler;
import org.fugerit.java.daogen.base.config.DaogenConfigDump;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,13 +35,17 @@ private DaoGenToolHandler() {}
public static final String ARG_ACTION = "action";
public static final String ARG_ACTION_DAOGEN = "daogen";
public static final String ARG_ACTION_DUMP = "dump";
public static final String ARG_ACTION_COMPARE = "compare";

public static void handle( Properties params ) {
String action = params.getProperty( ARG_ACTION );
if ( ARG_ACTION_DAOGEN.equalsIgnoreCase( action ) ) {
handleDaogen(params);
} else if ( ARG_ACTION_DUMP.equalsIgnoreCase( action ) ) {
handleDump(params);
} else if ( ARG_ACTION_COMPARE.equalsIgnoreCase( action ) ) {
CompareHandler handler = new CompareHandler();
handler.handleCompare(params);
} else {
throw new ConfigRuntimeException( "Reuired parameter : "+ARG_ACTION+" ("+ARG_ACTION_DAOGEN+"|"+ARG_ACTION_DUMP+")" );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.stream.Collectors;

import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.io.FileIO;
import org.fugerit.java.core.javagen.SimpleJavaGenerator;
Expand All @@ -27,6 +28,10 @@ public class CompareHandler {

private static final String SERIAL_UID_STRING = "serialVersionUID";

public static final String ARG_BASE_FOLDER = "base-folder";
public static final String ARG_FOLDER1 = "folder1";
public static final String ARG_FOLDER2 = "folder1";

public static final String ARG_REPORT = "report";

public static final String ARG_TRY_DELETE_EQUAL = "try-delere-equal";
Expand All @@ -37,6 +42,29 @@ public class CompareHandler {
public static final String ARG_CUSTOM_IMPORT_START = "custom-import-start";
public static final String ARG_CUSTOM_IMPORT_END = "custom-import-end";

private File getFolder( Properties params, String param, boolean required ) {
File file = null;
String path = params.getProperty( param );
if ( required && StringUtils.isEmpty( path ) ) {
throw new ConfigRuntimeException( "Required arg missing : "+param );
} else {
if ( StringUtils.isNotEmpty( path ) ) {
file = new File( path );
if ( !file.exists() ) {
throw new ConfigRuntimeException( "Directory not exists : "+param+", path : "+path );
}
}
}
return file;
}

public void handleCompare( Properties params ) {
File baseDir = getFolder( params, ARG_BASE_FOLDER, false );
File file1 = getFolder( params, ARG_FOLDER1, true );
File file2 = getFolder( params, ARG_FOLDER2, true );
handleCompare(baseDir, file1, file2, params);
}

public void handleCompare( File baseDir, File file1, File file2, Properties params ) {
SafeFunction.apply( () -> {
try ( StringWriter writer = new StringWriter();
Expand Down
14 changes: 3 additions & 11 deletions fj-daogen-tool/src/main/java/tool/DaoGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import java.util.Properties;

import org.fugerit.java.core.cli.ArgUtils;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.daogen.base.tool.DaoGenToolHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
Expand All @@ -14,18 +13,11 @@
*/
public class DaoGen {

private static final Logger logger = LoggerFactory.getLogger( DaoGen.class );

public static void main( String[] args ) {
int exit = 0;
try {
SafeFunction.apply( () -> {
Properties params = ArgUtils.getArgs( args, true );
DaoGenToolHandler.handle( params );
} catch (Exception e) {
logger.error( "Error "+e, e );
exit = 1;
}
System.exit( exit );
} );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import java.io.IOException;
import java.util.Properties;

import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.cli.ArgUtils;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.io.StreamIO;
import org.fugerit.java.core.lang.helpers.BooleanUtils;
import org.fugerit.java.daogen.base.tool.DaoGenToolHandler;
import org.fugerit.java.daogen.base.tool.handler.CompareHandler;
import org.junit.Assert;
import org.junit.Test;

import lombok.extern.slf4j.Slf4j;
import tool.DaoGen;

@Slf4j
public class TestCompareHandler {
Expand All @@ -22,6 +26,8 @@ public class TestCompareHandler {

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

private static final File OUTPUT_BASE = new File( "target" );

private static void copyHelper( File baseSource, File baseDest, File currentSource ) throws IOException {
String relPath = currentSource.getCanonicalPath().substring( baseSource.getCanonicalPath().length() );
File newFile = new File( baseDest, relPath );
Expand Down Expand Up @@ -70,7 +76,7 @@ private static void prepare( File file1, File file2 ) {

@Test
public void test001() {
File baseDir = new File( "target" );
File baseDir = OUTPUT_BASE;
File file1 = new File( baseDir, "daogen1" );
File file2 = new File( baseDir, "daogen2" );
prepare(file1, file2);
Expand All @@ -84,4 +90,31 @@ public void test001() {
handler.handleCompare( baseDir, file1, file2, params );
}

@Test
public void test002() {
File baseDir = OUTPUT_BASE;
File file1 = new File( baseDir, "daogen1alt" );
File file2 = new File( baseDir, "daogen2alt" );
prepare(file1, file2);
Assert.assertTrue( file1.exists() );
Assert.assertTrue( file2.exists() );
SafeFunction.apply( () -> {
String[] args = {
ArgUtils.getArgString( DaoGenToolHandler.ARG_ACTION ), DaoGenToolHandler.ARG_ACTION_COMPARE,
ArgUtils.getArgString( CompareHandler.ARG_FOLDER1 ), file1.getCanonicalPath(),
ArgUtils.getArgString( CompareHandler.ARG_FOLDER2 ), file2.getCanonicalPath(),
};
DaoGen.main(args);
} );
}

@Test
public void test003Fail() {
CompareHandler handler = new CompareHandler();
Properties params = new Properties();
Assert.assertThrows( ConfigRuntimeException.class , () -> handler.handleCompare(params) );
params.setProperty( CompareHandler.ARG_FOLDER1 , "not-exists" );
Assert.assertThrows( ConfigRuntimeException.class , () -> handler.handleCompare(params) );
}

}

0 comments on commit 73f5a9c

Please sign in to comment.