Skip to content

Commit 036b2fe

Browse files
authored
Merge pull request #70 from fugerit-org/feature/issue_67_coverage_80
Increase test coverage
2 parents aa09243 + 0139054 commit 036b2fe

19 files changed

+508
-127
lines changed

fj-doc-base/src/main/java/org/fugerit/java/doc/base/config/InitHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.io.InputStreamReader;
55

66
import org.fugerit.java.core.cfg.ConfigException;
7+
import org.fugerit.java.core.function.SafeFunction;
78
import org.fugerit.java.core.lang.helpers.ClassHelper;
89
import org.fugerit.java.core.util.checkpoint.CheckpointUtils;
910

@@ -39,12 +40,10 @@ public static void initDocAsync( DocTypeHandler handler ) {
3940
@Override
4041
public void run() {
4142
log.info( "Init handler start : {}", handler );
42-
try {
43+
SafeFunction.applySilent( () -> {
4344
boolean initOk = initDoc(handler);
4445
log.info( "Init handler end : {} -> {}", handler, initOk );
45-
} catch (ConfigException e) {
46-
log.info( "Init handler error "+e, e );
47-
}
46+
});
4847
}
4948
};
5049
Thread t = new Thread( runInitDoc );

fj-doc-base/src/main/java/org/fugerit/java/doc/base/facade/DocFacade.java

Lines changed: 29 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The Apache Software Foundation (http://www.apache.org/).
3232
import java.util.Iterator;
3333
import java.util.Properties;
3434

35-
import org.fugerit.java.core.cfg.ConfigRuntimeException;
35+
import org.fugerit.java.core.function.SafeFunction;
3636
import org.fugerit.java.core.io.helper.StreamHelper;
3737
import org.fugerit.java.core.lang.helpers.BooleanUtils;
3838
import org.fugerit.java.core.log.LogFacade;
@@ -108,41 +108,38 @@ public static boolean validate( Reader is) throws DocException {
108108
}
109109

110110
public static boolean validate( Reader is, Properties params ) throws DocException {
111-
boolean valRes = false;
112-
try {
113-
params = ObjectUtils.objectWithDefault( params , DEFAULT_PARAMS );
114-
DocXmlParser parser = new DocXmlParser( DocHelper.DEFAULT );
115-
int result = parser.validate( is );
116-
valRes = ( result == DocValidationResult.VALIDATION_OK );
117-
} catch (Exception e) {
118-
throw DocException.convertExMethod( "validate", e );
119-
} finally {
120-
if ( BooleanUtils.isTrue( params.getProperty( PARAM_KEY_CLOSE_STREAM, PARAM_VALUE_CLOSE_STREAM_DEFAULT ) ) ) {
121-
StreamHelper.closeSafe( is );
122-
}
123-
}
124-
return valRes;
111+
return SafeFunction.get( () -> {
112+
Properties realParams = ObjectUtils.objectWithDefault( params , DEFAULT_PARAMS );
113+
try {
114+
115+
DocXmlParser parser = new DocXmlParser( DocHelper.DEFAULT );
116+
int result = parser.validate( is );
117+
return ( result == DocValidationResult.VALIDATION_OK );
118+
} finally {
119+
if ( BooleanUtils.isTrue( realParams.getProperty( PARAM_KEY_CLOSE_STREAM, PARAM_VALUE_CLOSE_STREAM_DEFAULT ) ) ) {
120+
StreamHelper.closeSafe( is );
121+
}
122+
}
123+
} );
125124
}
126125

127126
public static DocBase parse( Reader is, DocHelper docHelper ) throws DocException {
128127
return parse( is, docHelper, DEFAULT_PARAMS );
129128
}
130129

131130
public static DocBase parse( Reader is, DocHelper docHelper, Properties params ) throws DocException {
132-
DocBase docBase = null;
133-
try {
134-
LogFacade.getLog().warn( "parse() method with DocHelper parameter should be avoided , as currently supported, param value : {}", docHelper );
135-
params = ObjectUtils.objectWithDefault( params , DEFAULT_PARAMS );
136-
DocXmlParser parser = new DocXmlParser( DocHelper.DEFAULT );
137-
docBase = parser.parse(is);
138-
} catch (Exception e) {
139-
throw DocException.convertExMethod( "parse", e );
140-
} finally {
141-
if ( BooleanUtils.isTrue( params.getProperty( PARAM_KEY_CLOSE_STREAM, PARAM_VALUE_CLOSE_STREAM_DEFAULT ) ) ) {
142-
StreamHelper.closeSafe( is );
131+
return SafeFunction.get( () -> {
132+
Properties realParams = ObjectUtils.objectWithDefault( params , DEFAULT_PARAMS );
133+
try {
134+
LogFacade.getLog().warn( "parse() method with DocHelper parameter should be avoided , as currently supported, param value : {}", docHelper );
135+
DocXmlParser parser = new DocXmlParser( DocHelper.DEFAULT );
136+
return parser.parse(is);
137+
} finally {
138+
if ( BooleanUtils.isTrue( realParams.getProperty( PARAM_KEY_CLOSE_STREAM, PARAM_VALUE_CLOSE_STREAM_DEFAULT ) ) ) {
139+
StreamHelper.closeSafe( is );
140+
}
143141
}
144-
}
145-
return docBase;
142+
} );
146143
}
147144

148145
public static DocBase parse( InputStream is, DocHelper docHelper, Properties params ) throws DocException {
@@ -154,25 +151,15 @@ public static DocBase parse( Reader is ) throws DocException {
154151
}
155152

156153
public static DocBase parseRE( Reader is, int sourceType ) {
157-
DocBase doc = null;
158-
try {
154+
return SafeFunction.get( () -> {
159155
log.debug( "sourceType : {}", sourceType );
160-
doc = parse( is, DocHelper.DEFAULT, DEFAULT_PARAMS );
161-
} catch (Exception e) {
162-
throw new ConfigRuntimeException( "Exception on parseRE : "+e, e );
163-
}
164-
return doc;
156+
return parse( is, DocHelper.DEFAULT, DEFAULT_PARAMS );
157+
} );
165158
}
166159

167160

168161
public static DocBase parseRE( Reader is ) {
169-
DocBase doc = null;
170-
try {
171-
doc = parse( is, DocHelper.DEFAULT, DEFAULT_PARAMS );
172-
} catch (Exception e) {
173-
throw new ConfigRuntimeException( "Exception on parseRE : "+e, e );
174-
}
175-
return doc;
162+
return SafeFunction.get( () -> parse( is, DocHelper.DEFAULT, DEFAULT_PARAMS ) );
176163
}
177164

178165
public static DocBase parse( InputStream is ) throws DocException {

fj-doc-base/src/main/java/org/fugerit/java/doc/base/facade/DocFacadeSourceConfig.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
package org.fugerit.java.doc.base.facade;
22

33
import lombok.AllArgsConstructor;
4-
import lombok.Data;
54
import lombok.Getter;
65
import lombok.NoArgsConstructor;
76
import lombok.With;
87

9-
@Data
108
@NoArgsConstructor
119
@AllArgsConstructor
1210
public class DocFacadeSourceConfig {

fj-doc-base/src/main/java/org/fugerit/java/doc/base/helper/DefaultMimeHelper.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,15 @@
11
package org.fugerit.java.doc.base.helper;
22

3-
import java.io.InputStream;
43
import java.util.Properties;
54

6-
import org.fugerit.java.core.cfg.ConfigRuntimeException;
7-
import org.fugerit.java.core.lang.helpers.ClassHelper;
5+
import org.fugerit.java.core.util.PropsIO;
86

97
public class DefaultMimeHelper {
108

119
private DefaultMimeHelper() {} // java:S1118
1210

13-
private static Properties props = new Properties();
14-
static {
15-
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "config/default_mime.xml" ) ) {
16-
props.loadFromXML( is );
17-
} catch (Exception e) {
18-
throw new ConfigRuntimeException( "Exception on init : "+e, e );
19-
}
20-
}
21-
11+
private static Properties props = PropsIO.loadFromClassLoaderSafe( "config/default_mime.xml" );
12+
2213
public static String getDefaultMime(String type) {
2314
return props.getProperty(type);
2415
}

fj-doc-base/src/main/java/org/fugerit/java/doc/base/helper/SourceResolverHelper.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.net.URL;
55

66
import org.fugerit.java.core.io.StreamIO;
7+
import org.fugerit.java.core.io.helper.HelperIOException;
78
import org.fugerit.java.core.io.helper.StreamHelper;
89
import org.fugerit.java.core.lang.helpers.StringUtils;
910
import org.fugerit.java.doc.base.model.DocImage;
@@ -15,10 +16,10 @@ private SourceResolverHelper() {} // java:S1118
1516
public static final String MODE_CLASSLOADER = StreamHelper.PATH_CLASSLOADER;
1617

1718
public static String resolveImageToBase64( DocImage img ) throws IOException {
18-
String path = img.getUrl();
19-
String base64 = img.getBase64();
20-
if ( StringUtils.isEmpty( base64 ) && path != null ) {
21-
try {
19+
return HelperIOException.get( () -> {
20+
String path = img.getUrl();
21+
String base64 = img.getBase64();
22+
if ( StringUtils.isEmpty( base64 ) && path != null ) {
2223
byte[] data = null;
2324
if ( path.startsWith( StreamHelper.PATH_CLASSLOADER ) ) {
2425
data = StreamIO.readBytes( StreamHelper.resolveStream( path ) );
@@ -27,36 +28,32 @@ public static String resolveImageToBase64( DocImage img ) throws IOException {
2728
data = StreamIO.readBytes( url.openConnection().getInputStream() );
2829
}
2930
base64 = Base64Helper.encodeBase64String( data );
30-
} catch (Exception e) {
31-
throw new IOException( "Errore on resolveImageToBase64 : "+e , e );
31+
} else {
32+
throw new IOException( "Null path and base64 provided!" );
3233
}
33-
} else {
34-
throw new IOException( "Null path and base64 provided!" );
35-
}
36-
return base64;
34+
return base64;
35+
} );
3736
}
3837

3938
public static byte[] resolveImage( DocImage img ) throws IOException {
40-
byte[] data = null;
41-
String path = img.getUrl();
42-
String base64 = img.getBase64();
43-
if ( StringUtils.isNotEmpty( base64 ) ) {
44-
data = Base64Helper.decodeBase64String( base64 );
45-
} else if ( path != null ) {
46-
try {
39+
return HelperIOException.get( () -> {
40+
byte[] data = null;
41+
String path = img.getUrl();
42+
String base64 = img.getBase64();
43+
if ( StringUtils.isNotEmpty( base64 ) ) {
44+
data = Base64Helper.decodeBase64String( base64 );
45+
} else if ( path != null ) {
4746
if ( path.startsWith( StreamHelper.PATH_CLASSLOADER ) ) {
4847
data = StreamIO.readBytes( StreamHelper.resolveStream( path ) );
4948
} else {
5049
URL url = new URL( path );
5150
data = StreamIO.readBytes( url.openConnection().getInputStream() );
5251
}
53-
} catch (Exception e) {
54-
throw new IOException( "Errore on resolveImage : "+e , e );
52+
} else {
53+
throw new IOException( "Null path provided!" );
5554
}
56-
} else {
57-
throw new IOException( "Null path provided!" );
58-
}
59-
return data;
55+
return data;
56+
} );
6057
}
6158

6259
}

fj-doc-base/src/main/java/org/fugerit/java/doc/base/process/DocProcessConfig.java

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
import java.util.Set;
88

99
import org.fugerit.java.core.cfg.ConfigException;
10-
import org.fugerit.java.core.cfg.ConfigRuntimeException;
1110
import org.fugerit.java.core.cfg.xml.GenericListCatalogConfig;
1211
import org.fugerit.java.core.cfg.xml.ListMapConfig;
12+
import org.fugerit.java.core.function.SafeFunction;
1313
import org.fugerit.java.core.io.helper.StreamHelper;
1414
import org.fugerit.java.core.util.filterchain.MiniFilterChain;
1515
import org.fugerit.java.core.util.filterchain.MiniFilterConfig;
@@ -31,35 +31,28 @@ public DocProcessConfig() {
3131
}
3232

3333
public static DocProcessConfig loadConfig( InputStream is, DocProcessConfig config ) {
34-
config.miniFilterConfig = new MiniFilterConfig();
35-
try {
34+
return SafeFunction.get( () -> {
3635
MiniFilterConfig.loadConfig(is, config.miniFilterConfig);
37-
} catch (Exception e) {
38-
throw ConfigRuntimeException.convertExMethod( "loadConfig" , e );
39-
}
40-
return config;
36+
return config;
37+
} );
4138
}
4239

4340
public static DocProcessConfig loadConfigSafe(String configPath) {
44-
DocProcessConfig config = null;
45-
try (InputStream is = StreamHelper.resolveStream(configPath)) {
46-
config = loadConfig(is);
47-
} catch (Exception e) {
48-
throw new ConfigRuntimeException("Exception on loadConfigSafe : " + e, e);
49-
}
50-
return config;
41+
return SafeFunction.get( () -> {
42+
try (InputStream is = StreamHelper.resolveStream(configPath)) {
43+
return loadConfig(is);
44+
}
45+
} );
5146
}
5247

5348
public static DocProcessConfig loadConfig(InputStream is) throws ConfigException {
5449
DocProcessConfig config = new DocProcessConfig();
5550
config.miniFilterConfig.getGeneralProps().setProperty(GenericListCatalogConfig.ATT_TYPE,
5651
MiniFilterConfigEntry.class.getName());
57-
try {
52+
return SafeFunction.get( () -> {
5853
MiniFilterConfig.loadConfigMap(is, config.miniFilterConfig);
59-
} catch (Exception e) {
60-
throw ConfigException.stadardExceptionWrapping(e);
61-
}
62-
return config;
54+
return config;
55+
} );
6356
}
6457

6558
@Override

fj-doc-base/src/main/java/org/fugerit/java/doc/base/xml/DocXmlParser.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import javax.xml.parsers.SAXParser;
66

7+
import org.fugerit.java.core.function.SafeFunction;
78
import org.fugerit.java.core.xml.sax.SAXParseResult;
89
import org.fugerit.java.core.xml.sax.XMLFactorySAX;
910
import org.fugerit.java.core.xml.sax.dh.DefaultHandlerComp;
@@ -33,39 +34,37 @@ public DocXmlParser() {
3334

3435
@Override
3536
protected DocBase parseWorker(Reader reader) throws DocException {
36-
DocContentHandler dch = new DocContentHandler( this.docHelper );
37-
try {
37+
return SafeFunction.get( () -> {
38+
DocContentHandler dch = new DocContentHandler( this.docHelper );
3839
SAXParser parser = XMLFactorySAX.makeSAXParser( false , true );
3940
DefaultHandlerComp dh = new DefaultHandlerComp( dch );
4041
parser.parse( new InputSource(reader), dh);
41-
} catch (Exception e) {
42-
throw DocException.convertExMethod( "parseWorker" , e );
43-
}
44-
return dch.getDocBase();
42+
return dch.getDocBase();
43+
} );
4544
}
4645

4746
@Override
4847
protected DocValidationResult validateWorker(Reader reader, boolean parseVersion) throws DocException {
49-
DocValidationResult docResult = DocValidationResult.newDefaultNotDefinedResult();
50-
SAXParseResult result = null;
51-
try {
48+
return SafeFunction.get( () -> {
49+
DocValidationResult docResult = DocValidationResult.newDefaultNotDefinedResult();
50+
SAXParseResult result = null;
5251
if ( parseVersion ) {
5352
result = DocValidator.validateVersion( reader );
5453
} else {
5554
result = DocValidator.validate( reader );
5655
}
57-
} catch (Exception e) {
58-
throw DocException.convertExMethod( "validateWorker" , e );
59-
}
60-
for ( Exception e : result.fatalsAndErrors() ) {
61-
docResult.getErrorList().add( e.toString() );
62-
}
63-
for ( Exception e : result.warnings() ) {
64-
docResult.getInfoList().add( e.toString() );
65-
}
66-
docResult.evaluateResult();
67-
log.debug( "Validation result {}", docResult );
68-
return docResult;
56+
for ( Exception e : result.fatalsAndErrors() ) {
57+
docResult.getErrorList().add( e.toString() );
58+
}
59+
for ( Exception e : result.warnings() ) {
60+
docResult.getInfoList().add( e.toString() );
61+
}
62+
docResult.evaluateResult();
63+
log.debug( "Validation result {}", docResult );
64+
return docResult;
65+
} );
66+
67+
6968
}
7069

7170
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test.org.fugerit.java.doc.base.coverage;
2+
3+
import org.fugerit.java.core.io.StreamIO;
4+
import org.fugerit.java.core.lang.helpers.ClassHelper;
5+
import org.fugerit.java.doc.base.process.DocProcessContext;
6+
import org.fugerit.java.doc.base.process.DocProcessData;
7+
import org.fugerit.java.doc.base.process.DocProcessorBasic;
8+
9+
import lombok.extern.slf4j.Slf4j;
10+
11+
@Slf4j
12+
public class ProcessStepCoverage extends DocProcessorBasic {
13+
14+
private static final long serialVersionUID = 6389046120807963369L;
15+
16+
@Override
17+
public int process(DocProcessContext context, DocProcessData data) throws Exception {
18+
String xmlPath = this.getCustomConfig().getProperty( "xmlPath" );
19+
log.info( "xmlPath {}", xmlPath );
20+
data.setCurrentXmlData( StreamIO.readString( ClassHelper.loadFromDefaultClassLoader( xmlPath ) ) );
21+
return super.process(context, data);
22+
}
23+
24+
}

0 commit comments

Comments
 (0)