Skip to content

Commit f627618

Browse files
authored
Merge pull request #68 from fugerit-org/feature/issue_67_coverage_80
Feature/issue 67 coverage 80
2 parents a319716 + e549a27 commit f627618

File tree

136 files changed

+3487
-453
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

136 files changed

+3487
-453
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Changed
11+
12+
- fj-core set to 8.2.7
13+
- fj-test-helper8 set to 0.5.0
14+
1015
## [2.0.2] - 2023-09-05
1116

1217
### Changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Framework to produce documents in different formats starting from an XML documen
66
[![Maven Central](https://img.shields.io/maven-central/v/org.fugerit.java/fj-doc.svg)](https://mvnrepository.com/artifact/org.fugerit.java/fj-doc)
77
[![license](https://img.shields.io/badge/License-Apache%20License%202.0-teal.svg)](https://opensource.org/licenses/Apache-2.0)
88
[![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=fugerit-org_fj-doc&metric=alert_status)](https://sonarcloud.io/summary/new_code?id=fugerit-org_fj-doc)
9+
[![Coverage](https://sonarcloud.io/api/project_badges/measure?project=fugerit-org_fj-doc&metric=coverage)](https://sonarcloud.io/summary/new_code?id=fugerit-org_fj-doc)
910

1011
The Core library (fj-doc-base) is all you need to start, even though typically you will use at least :
1112
* [fj-doc-base](fj-doc-base/README.md)

fj-doc-base-json/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<parent>
88
<groupId>org.fugerit.java</groupId>
99
<artifactId>fj-doc</artifactId>
10-
<version>2.0.2</version>
10+
<version>2.0.3-SNAPSHOT</version>
1111
</parent>
1212

1313
<name>fj-doc-base-json</name>

fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocJsonToXml.java

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public DocJsonToXml(ObjectMapper mapper) {
3030

3131
private ObjectMapper mapper;
3232

33-
private void iterateElement( JsonNode current, Document doc, Element tag ) throws Exception {
33+
private void iterateElement( JsonNode current, Document doc, Element tag ) throws ConfigException {
3434
JsonNode elementsNode = current.get( DocObjectMapperHelper.PROPERTY_ELEMENTS );
3535
if ( elementsNode != null ) {
3636
if ( elementsNode.isArray() ) {
@@ -45,7 +45,7 @@ private void iterateElement( JsonNode current, Document doc, Element tag ) throw
4545
}
4646
}
4747

48-
private void iterateAttribute( JsonNode current, Element tag ) throws Exception {
48+
private void iterateAttribute( JsonNode current, Element tag ) {
4949
Iterator<String> itNames = current.fieldNames();
5050
while ( itNames.hasNext() ) {
5151
String currentName = itNames.next();
@@ -55,7 +55,7 @@ private void iterateAttribute( JsonNode current, Element tag ) throws Exception
5555
}
5656
}
5757

58-
private Element create( Document doc, Element parent, JsonNode current ) throws Exception {
58+
private Element create( Document doc, Element parent, JsonNode current ) throws ConfigException {
5959
Element tag = null;
6060
JsonNode tagNode = current.get( DocObjectMapperHelper.PROPERTY_TAG );
6161
if ( tagNode == null ) {
@@ -77,42 +77,33 @@ private Element create( Document doc, Element parent, JsonNode current ) throws
7777
}
7878

7979
public void writerAsXml( Reader jsonReader, Writer writer ) throws ConfigException {
80-
try {
80+
ConfigException.apply( () -> {
8181
Element root = this.convertToElement(jsonReader);
8282
DOMIO.writeDOMIndent( root , writer );
83-
} catch (Exception e) {
84-
throw new ConfigException( "Errore converting json to xml : "+e, e );
85-
}
83+
} );
8684
}
8785

8886
public Element convertToElement( Reader jsonReader ) throws ConfigException {
89-
Element root = null;
90-
try {
87+
return ConfigException.get( () -> {
9188
JsonNode node = this.mapper.readTree( jsonReader );
92-
root = this.convert(node);
93-
} catch (Exception e) {
94-
throw new ConfigException( "Errore converting json to xml : "+e, e );
95-
}
96-
return root;
89+
return this.convert(node);
90+
} );
9791
}
9892

9993
public Element convert( JsonNode json ) throws ConfigException {
100-
Element root = null;
101-
try {
94+
return ConfigException.get( () -> {
10295
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
10396
dbf.setNamespaceAware( true );
10497
dbf.setValidating( false );
10598
DocumentBuilder builder = dbf.newDocumentBuilder();
10699
Document doc = builder.newDocument();
107-
root = this.create(doc, null, json);
100+
Element root = this.create(doc, null, json);
108101
root.setAttribute( "xmlns" , DocFacade.SYSTEM_ID );
109102
root.setAttribute( "xmlns:xsi" , "http://www.w3.org/2001/XMLSchema-instance" );
110103
String xsdVersion = DocObjectMapperHelper.findVersion(json, DocFacade.CURRENT_VERSION) ;
111104
root.setAttribute( "xsi:schemaLocation" , DocParserContext.createXsdVersionXmlns(xsdVersion) );
112-
} catch (Exception e) {
113-
throw new ConfigException( "Conversion error : "+e, e );
114-
}
115-
return root;
105+
return root;
106+
} );
116107

117108
}
118109

fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocObjectMapperHelper.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ private void handleElement( JsonNode node, DocParserContext context ) {
9595
}
9696

9797
public DocValidationResult validateWorkerResult(Reader reader, boolean parseVersion) throws DocException {
98-
DocValidationResult result = DocValidationResult.newDefaultNotDefinedResult();
99-
try {
98+
return DocException.get( () -> {
99+
DocValidationResult result = DocValidationResult.newDefaultNotDefinedResult();
100100
DocJsonToXml convert = new DocJsonToXml( this.mapper );
101101
Element root = convert.convertToElement( reader );
102102
try ( ByteArrayOutputStream buffer = new ByteArrayOutputStream() ) {
@@ -114,27 +114,22 @@ public DocValidationResult validateWorkerResult(Reader reader, boolean parseVers
114114
}
115115
}
116116
}
117-
} catch (Exception e) {
118-
throw DocException.convertExMethod( "validateWorkerResult" , e );
119-
}
120-
return result;
117+
return result;
118+
} );
121119
}
122120

123121
public DocBase parse(Reader reader) throws DocException {
124-
DocBase docBase = null;
125-
try {
122+
return DocException.get( () -> {
126123
DocParserContext context = new DocParserContext();
127124
context.startDocument();
128125
JsonNode root = this.mapper.readTree( reader );
129126
this.handleElement(root, context);
130127
context.endDocument();
131128
log.debug( "Parse done!" );
132-
docBase = context.getDocBase();
129+
DocBase docBase = context.getDocBase();
133130
docBase.setXsdVersion( findVersion(root, DocFacade.CURRENT_VERSION) );
134-
} catch (Exception e) {
135-
throw DocException.convertExMethod( "parse" , e );
136-
}
137-
return docBase;
131+
return docBase;
132+
});
138133
}
139134

140135
}

fj-doc-base-json/src/main/java/org/fugerit/java/doc/json/parse/DocXmlToJson.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,15 @@ private ObjectNode create( Element currentTag, ObjectNode currentNode ) throws E
7070
}
7171

7272
public JsonNode convertToJsonNode( Reader xml ) throws ConfigException {
73-
JsonNode tree;
74-
try {
73+
return ConfigException.get( () -> {
7574
Document doc = DOMIO.loadDOMDoc( xml );
7675
Element root = doc.getDocumentElement();
77-
tree = this.convert( root );
78-
} catch (Exception e) {
79-
throw new ConfigException( "Errore converting xml to json node : "+e, e );
80-
}
81-
return tree;
76+
return this.convert( root );
77+
} );
8278
}
8379

8480
public JsonNode convert( Element root ) throws ConfigException {
85-
JsonNode tree = null;
86-
try {
87-
tree = this.create( root, this.mapper.createObjectNode() );
88-
} catch (Exception e) {
89-
throw new ConfigException( "Conversion error : "+e, e );
90-
}
91-
return tree;
92-
81+
return ConfigException.get( () -> this.create( root, this.mapper.createObjectNode() ) );
9382
}
9483

9584
}
Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package test.org.fugerit.java.doc.json.parse;
22

3-
import static org.junit.Assert.fail;
4-
53
import java.io.FileOutputStream;
64
import java.io.InputStream;
75
import java.io.InputStreamReader;
6+
import java.io.StringWriter;
87

8+
import org.fugerit.java.core.function.SafeFunction;
9+
import org.fugerit.java.core.function.SimpleValue;
910
import org.fugerit.java.core.lang.helpers.ClassHelper;
1011
import org.fugerit.java.core.xml.dom.DOMIO;
1112
import org.fugerit.java.doc.json.parse.DocJsonToXml;
13+
import org.junit.Assert;
1214
import org.junit.Test;
1315
import org.slf4j.Logger;
1416
import org.slf4j.LoggerFactory;
@@ -18,23 +20,57 @@ public class TestDocJsonToXml {
1820

1921
private static final Logger logger = LoggerFactory.getLogger( TestDocJsonToXml.class );
2022

21-
private void worker( String path ) {
22-
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/"+path+".json" );
23-
FileOutputStream fos = new FileOutputStream( "target/"+path+".xml" )) {
24-
DocJsonToXml converter = new DocJsonToXml();
25-
Element root = converter.convertToElement( new InputStreamReader( is ) );
26-
logger.info( "xml -> {}", root);
27-
DOMIO.writeDOMIndent(root, fos);
28-
} catch (Exception e) {
29-
String message = "Error : "+e;
30-
logger.error( message, e );
31-
fail( message ) ;
32-
}
23+
private boolean worker( String path ) {
24+
SimpleValue<Boolean> res = new SimpleValue<>( true );
25+
SafeFunction.apply( () -> {
26+
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/"+path+".json" );
27+
FileOutputStream fos = new FileOutputStream( "target/"+path+".xml" )) {
28+
DocJsonToXml converter = new DocJsonToXml();
29+
Element root = converter.convertToElement( new InputStreamReader( is ) );
30+
logger.info( "xml -> {}", root);
31+
DOMIO.writeDOMIndent(root, fos);
32+
}
33+
} );
34+
return res.getValue();
3335
}
3436

3537
@Test
3638
public void test01() {
37-
this.worker( "doc_test_01" );
39+
Assert.assertTrue( this.worker( "doc_test_01" ) );
40+
}
41+
42+
@Test
43+
public void testConvert() {
44+
SafeFunction.apply( () -> {
45+
try ( InputStreamReader reader = new InputStreamReader( ClassHelper.loadFromDefaultClassLoader( "sample/doc_test_01.json" ) );
46+
StringWriter writer = new StringWriter() ) {
47+
DocJsonToXml converter = new DocJsonToXml();
48+
converter.writerAsXml(reader, writer);
49+
Assert.assertNotEquals( "" , writer.toString() );
50+
}
51+
} );
52+
}
53+
54+
@Test
55+
public void testFail01() {
56+
Assert.assertThrows( Exception.class , () -> {
57+
DocJsonToXml converter = new DocJsonToXml();
58+
try ( InputStreamReader reader = new InputStreamReader(
59+
ClassHelper.loadFromDefaultClassLoader( "sample/doc_test_fail_01.json" ) ) ) {
60+
converter.convertToElement( reader );
61+
}
62+
} );
63+
}
64+
65+
@Test
66+
public void testFail02() {
67+
Assert.assertThrows( Exception.class , () -> {
68+
DocJsonToXml converter = new DocJsonToXml();
69+
try ( InputStreamReader reader = new InputStreamReader(
70+
ClassHelper.loadFromDefaultClassLoader( "sample/doc_test_fail_02.json" ) ) ) {
71+
converter.convertToElement( reader );
72+
}
73+
} );
3874
}
3975

4076
}
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package test.org.fugerit.java.doc.json.parse;
22

3-
import static org.junit.Assert.fail;
4-
53
import java.io.FileOutputStream;
64
import java.io.InputStream;
75
import java.io.InputStreamReader;
86

7+
import org.fugerit.java.core.function.SafeFunction;
98
import org.fugerit.java.core.lang.helpers.ClassHelper;
109
import org.fugerit.java.doc.json.parse.DocXmlToJson;
10+
import org.junit.Assert;
1111
import org.junit.Test;
1212
import org.slf4j.Logger;
1313
import org.slf4j.LoggerFactory;
@@ -18,23 +18,22 @@ public class TestDocXmlToJson {
1818

1919
private static final Logger logger = LoggerFactory.getLogger( TestDocXmlToJson.class );
2020

21-
private void worker( String path ) {
22-
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/"+path+".xml" );
23-
FileOutputStream fos = new FileOutputStream( "target/"+path+".json" )) {
24-
DocXmlToJson converter = new DocXmlToJson();
25-
JsonNode tree = converter.convertToJsonNode( new InputStreamReader( is ) );
26-
logger.info( "xml -> {}", tree);
27-
fos.write( tree.toPrettyString().getBytes() );
28-
} catch (Exception e) {
29-
String message = "Error : "+e;
30-
logger.error( message, e );
31-
fail( message ) ;
32-
}
21+
private boolean worker( String path ) {
22+
return SafeFunction.get( () -> {
23+
try ( InputStream is = ClassHelper.loadFromDefaultClassLoader( "sample/"+path+".xml" );
24+
FileOutputStream fos = new FileOutputStream( "target/"+path+".json" )) {
25+
DocXmlToJson converter = new DocXmlToJson();
26+
JsonNode tree = converter.convertToJsonNode( new InputStreamReader( is ) );
27+
logger.info( "xml -> {}", tree);
28+
fos.write( tree.toPrettyString().getBytes() );
29+
return true;
30+
}
31+
} );
3332
}
3433

3534
@Test
3635
public void test01() {
37-
this.worker( "doc_test_01" );
36+
Assert.assertTrue( this.worker( "doc_test_01" ) );
3837
}
3938

4039
}

0 commit comments

Comments
 (0)