Skip to content

Commit

Permalink
Test converage helper
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 12, 2023
1 parent 6c5ee64 commit 0686cf5
Show file tree
Hide file tree
Showing 52 changed files with 986 additions and 168 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.io.Serializable;
import java.util.Properties;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
public class ChainStepModel implements Serializable {

private static final long serialVersionUID = 622077549080786391L;

private String stepType;
@Getter @Setter private String stepType;

private Properties attributes;
@Getter @Setter private Properties attributes;

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
import org.fugerit.java.core.cfg.xml.IdConfigType;
import org.fugerit.java.core.util.collection.KeyString;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
public class DocChainModel implements IdConfigType, KeyString, Serializable {

public static final String MAP_ATTS_ALL = "all";
Expand All @@ -23,17 +23,17 @@ public class DocChainModel implements IdConfigType, KeyString, Serializable {

private static final long serialVersionUID = 9076457107043072322L;

private String id;
@Getter @Setter private String id;

private String templatePath = DEFAULT_TEMPLATE_PATH;
@Getter @Setter private String templatePath = DEFAULT_TEMPLATE_PATH;

private String mapAtts = MAP_ATTS_DEFAULT;
@Getter @Setter private String mapAtts = MAP_ATTS_DEFAULT;

private String parent;
@Getter @Setter private String parent;

private Properties mapAttsEnum;
@Getter @Setter private Properties mapAttsEnum;

private List<ChainStepModel> chainStepList = new ArrayList<>();
@Getter private List<ChainStepModel> chainStepList = new ArrayList<>();

@Override
public String getKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private static void handleChainStepList( DocChainModel model, Element currentTag
if ( STEP_TYPE_CONFIG.equalsIgnoreCase( chainStepModel.getStepType() ) ) {
NodeList configList = currentChainStepTag.getElementsByTagName( STEP_TYPE_CONFIG );
if ( configList.getLength() != 1 ) {
throw new ConfigException( "Expcted only one config tag : "+configList.getLength() );
throw new ConfigException( "Expected only one config tag : "+configList.getLength() );
} else {
Element configTag = (Element)configList.item( 0 );
atts.putAll( DOMUtils.attributesToProperties( configTag ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
public class ChainModel implements Serializable {

private static final long serialVersionUID = 3421438389573953861L;

private List<StepModel> stepList;
@Getter private List<StepModel> stepList;

private String id;
@Getter @Setter private String id;

private String parent;
@Getter @Setter private String parent;

public ChainModel( String id ) {
this.setId( id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.Getter;

@Data
public class ConfigModel implements Serializable {

private static final long serialVersionUID = 5198896174327509127L;

private List<ChainModel> chainList;
@Getter private List<ChainModel> chainList;

public ConfigModel() {
this.chainList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import java.util.LinkedHashMap;
import java.util.Set;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

@Data
public class StepModel {

private String type;
@Getter @Setter private String type;

private LinkedHashMap<String, String> atts;
@Getter private LinkedHashMap<String, String> atts;

public StepModel(String type) {
super();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package test.org.fugerit.java.doc.freemarker.coverage;

import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.function.SimpleValue;
import org.fugerit.java.core.lang.helpers.ClassHelper;
import org.fugerit.java.doc.base.config.DocInput;
import org.fugerit.java.doc.base.config.DocOutput;
import org.fugerit.java.doc.base.config.DocTypeHandler;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlFragmentTypeHandler;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlFragmentTypeHandlerUTF8;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandler;
import org.fugerit.java.doc.freemarker.html.FreeMarkerHtmlTypeHandlerUTF8;
import org.junit.Assert;
import org.junit.Test;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestFreemarkerCoverage {

private final static TestEntry[] TEST_LIST = {
new TestEntry( "default_doc" , true ),
new TestEntry( "default_doc_alt" , true ),
new TestEntry( "default_doc_fail1" , true )
};

private final static DocTypeHandler[] HANDLERS = { FreeMarkerHtmlTypeHandler.HANDLER,
FreeMarkerHtmlTypeHandlerUTF8.HANDLER,
FreeMarkerHtmlFragmentTypeHandler.HANDLER,
FreeMarkerHtmlFragmentTypeHandlerUTF8.HANDLER };

private boolean worker( String path, boolean result ) {
SimpleValue<Boolean> res = new SimpleValue<>(false);
SafeFunction.apply( () -> {
for ( int k=0; k<HANDLERS.length; k++ ) {
DocTypeHandler handler = HANDLERS[k];
try ( InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader(path) );
ByteArrayOutputStream buffer = new ByteArrayOutputStream() ) {
handler.handle( DocInput.newInput( handler.getType() , reader ) , DocOutput.newOutput( buffer ) );
if ( result ) {
res.setValue( buffer.toByteArray().length > 0 );
} else {
res.setValue( buffer.toByteArray().length == 0 );
}
}
}
} );
return res.getValue();
}

@Test
public void test01() {
Arrays.asList( TEST_LIST ).stream().forEach( c -> {
log.info( "test -> {}", c );
Assert.assertTrue( this.worker( "coverage/xml/"+c.getId()+".xml", c.isResult() ) );
} );
}

}

@AllArgsConstructor
@ToString
class TestEntry {

@Getter private String id;

@Getter private boolean result;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import static org.junit.Assert.fail;

import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.lang.helpers.ClassHelper;
import org.fugerit.java.doc.base.config.DocConfig;
import org.fugerit.java.doc.base.process.DocProcessContext;
import org.fugerit.java.doc.freemarker.config.FreeMarkerConfigStep;
import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfig;
import org.fugerit.java.doc.freemarker.process.FreemarkerDocProcessConfigFacade;
Expand All @@ -24,13 +28,49 @@ public void testConfigRead001() {
try ( Reader xmlReader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( "fj_doc_test/freemarker-doc-process.xml" ) ) ) {
FreemarkerDocProcessConfig config = FreemarkerDocProcessConfigFacade.loadConfig(xmlReader);
log.info( "config {}", config.getChain( "sample_chain" ) );
Assert.assertNotNull( config );
} catch (Exception e) {
String message = "Error : "+e;
log.error( message, e );
fail(message);
}
}

private void templateTesting( FreemarkerDocProcessConfig config ) {
DocProcessContext context = DocProcessContext.newContext( "test", "testString" );
runTestEx( () -> {
try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) {
config.process( "test_01", DocConfig.TYPE_MD, context, baos, false );
}
} );
runTestEx( () -> {
try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) {
config.process( "test_01_alt", DocConfig.TYPE_MD, context, baos, false );
}
} );
runTestEx( () -> {
try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) {
// need to make it work in the future!
Assert.assertThrows( NullPointerException.class, () -> {
config.process( "test_01_inline", DocConfig.TYPE_MD, context, baos, false );
} );
}
} );
Assert.assertThrows( ConfigException.class , () -> {
try ( ByteArrayOutputStream baos = new ByteArrayOutputStream() ) {
config.process( "test_01_fail", DocConfig.TYPE_MD, context, baos, false );
}
} );
}

@Test
public void testConfigRead002() {
FreemarkerDocProcessConfig config =
FreemarkerDocProcessConfigFacade.loadConfigSafe( "cl://fj_doc_test/freemarker-doc-process_alt.xml" );
Assert.assertNotNull( config );
this.templateTesting(config);
}

@Test
public void testConfigValidate001() {
try ( Reader xmlReader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( "fj_doc_test/freemarker-doc-process.xml" ) ) ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package test.org.fugerit.java.doc.freemarker.tool;

import java.io.InputStream;
import java.io.StringWriter;
import java.util.Properties;

import org.fugerit.java.core.lang.helpers.ClassHelper;
import org.fugerit.java.doc.freemarker.tool.GenerateStub;
import org.junit.Assert;
import org.junit.Test;

import test.org.fugerit.java.BasicTest;

public class TestGenerateStub extends BasicTest {

@Test
public void genTest001() {
runTestEx( () -> {
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "generate-stub-test/doc-process-autodoc.xml" );
StringWriter writer = new StringWriter() ) {
Properties props = new Properties();
GenerateStub.generate( writer, props, is );
Assert.assertNotEquals( 0 , writer.toString().length() );
}
} );
}

@Test
public void genTest002() {
runTestEx( () -> {
try (StringWriter writer = new StringWriter() ) {
Properties props = new Properties();
props.setProperty( GenerateStub.ARG_INPUT_FILE , "src/test/resources/generate-stub-test/doc-process-autodoc.xml" );
GenerateStub.generate( writer, props );
Assert.assertNotEquals( 0 , writer.toString().length() );
}
} );
}

}
87 changes: 87 additions & 0 deletions fj-doc-freemarker/src/test/resources/coverage/xml/default_doc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<doc
xmlns="http://javacoredoc.fugerit.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://javacoredoc.fugerit.org https://www.fugerit.org/data/java/doc/xsd/doc-2-1.xsd" >

<metadata>
<!-- Margin for document : left;right;top;bottom -->
<info name="margins">10;10;10;30</info>
<info name="excel-table-id">excel-table=print</info>
<!-- documenta meta information -->
<info name="doc-title">Basic example</info>
<info name="doc-subject">fj doc venus sample source xml</info>
<info name="doc-author">fugerit79</info>
<info name="doc-language">en</info>
<!-- additional properties -->
<info name="set-total-page">true</info>
<info name="html-css-link">/css/test.css</info>
<!-- CSV options -->
<info name="csv-table-id">excel-table</info>
<header-ext>
<para align="center" fore-color="#eeeeee">header test</para>
</header-ext>
<footer-ext>
<para align="left">test</para>
<para align="center">${r"${currentPage}"} / ${r"${pageCount}"}</para>
<para align="right">test</para>
</footer-ext>
<bookmark-tree>
<bookmark ref="title">Test</bookmark>
</bookmark-tree>
</metadata>
<body>
<h id="title" head-level="1">main title h1</h>
<para font-name="times-roman" style="bold">Test times roman</para>
<para font-name="courier" style="bolditalic">Courier</para>
<para font-name="symbol" style="italic">Symbol</para>
<para font-name="helvetica" style="underline">Symbol</para>
<para size="3" fore-color="#dddddd">Test default font</para>
<br/>
<page-break/>
<table columns="3" colwidths="30;30;40" width="100" id="excel-table" padding="2">
<row>
<cell align="center" border-color="#000000" border-width="1"><para style="bold">Name</para></cell>
<cell align="center"><para style="bold">Surname</para></cell>
<cell align="center"><para style="bold">Title</para></cell>
</row>
<row>
<cell><para><![CDATA[Luthien]]></para></cell>
<cell><para><![CDATA[Tinuviel]]></para></cell>
<cell><para><![CDATA[Queen]]></para></cell>
</row>
<row>
<cell><para><![CDATA[Thorin]]></para></cell>
<cell><para><![CDATA[Oakshield]]></para></cell>
<cell><para><![CDATA[King]]></para></cell>
</row>
<row>
<cell><phrase><![CDATA[Phrase]]></phrase></cell>
<cell><phrase anchor="1"><![CDATA[Oakshield]]></phrase></cell>
<cell><phrase link="1"><![CDATA[King]]></phrase></cell>
</row>
</table>
<list>
<li><para>test 1</para></li>
</list>
<list list-type="ul">
<li><para>test 2</para></li>
</list>
<list list-type="ol">
<li><para>test 3</para></li>
</list>
<list list-type="uld">
<li><para>test 4</para></li>
</list>
<list list-type="ulm">
<li><para>test 5</para></li>
</list>
<list list-type="oll">
<li><para>test 6</para></li>
</list>
<list list-type="oln">
<li><para>test 7</para></li>
</list>
</body>

</doc>
Loading

0 comments on commit 0686cf5

Please sign in to comment.