Skip to content

Commit

Permalink
Sonar cloud issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Sep 15, 2023
1 parent 6ec8fb6 commit 8ed30f7
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import javax.xml.transform.dom.DOMSource;

import org.fugerit.java.core.cfg.ConfigException;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.cfg.helpers.AbstractConfigurableObject;
import org.fugerit.java.core.cfg.helpers.XMLConfigurableObject;
import org.fugerit.java.core.cfg.provider.ConfigProviderFacade;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.io.helper.StreamHelper;
import org.fugerit.java.core.lang.helpers.BooleanUtils;
import org.fugerit.java.core.lang.helpers.ClassHelper;
Expand Down Expand Up @@ -268,11 +270,25 @@ public GenericListCatalogConfig( String attTagDataList, String attTagData ) {

private Set<String> entryIdCheck;

public static <T> GenericListCatalogConfig<T> load( InputStream is, GenericListCatalogConfig<T> config ) throws Exception {
Document doc = DOMIO.loadDOMDoc( is, true );
Element root = doc.getDocumentElement();
config.configure( root );
return config;
/**
* <p>Configure an instance of GenericListCatalogConfig</p>
*
* <p>NOTE: starting in version 8.4.X java.lang.Exception removed in favor of org.fugerit.java.core.cfg.ConfigRuntimeException.
* ({@link https://fuzzymemory.fugerit.org/src/docs/sonar_cloud/java-S112.html} </p>
*
* @param <T> the type of the elements in catalog
* @param is the input stream to load from
* @param config the instance to be configured
* @return the configured instance
* @throws ConfigRuntimeException in case of issues during loading
*/
public static <T> GenericListCatalogConfig<T> load( InputStream is, GenericListCatalogConfig<T> config ) {
return SafeFunction.get( () -> {
Document doc = DOMIO.loadDOMDoc( is, true );
Element root = doc.getDocumentElement();
config.configure( root );
return config;
} );
}

protected Set<String> getEntryIdCheck() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static <T> void setFromElement( T bean, Element config ) throws Exception
setFromElement( bean, config, MODE_XML_DEFAULT );
}

public static <T> void setFromElement( T bean, Element config, String mode ) throws Exception {
public static <T> void setFromElement( T bean, Element config, String mode ) {
if ( MODE_XML_ATTRIBUTES.equalsIgnoreCase( mode ) || MODE_XML_FULL.equalsIgnoreCase( mode ) ) {
setFromElementAtts(bean, config);
}
Expand All @@ -53,19 +53,22 @@ public static <T> void setFromElement( T bean, Element config, String mode ) thr
}
}

public static <T> void setFromElementSafe( T bean, Element config, String mode ) {
public static <T> boolean setFromElementSafe( T bean, Element config, String mode ) {
boolean set = false;
try {
setFromElement( bean , config, mode );
set = true;
} catch (Exception e) {
log.warn( "Cannot set all parameters from bean, usually safe to ignore : {}", e );
}
return set;
}

public static <T> void setFromElementSafe( T bean, Element config ) {
setFromElementSafe(bean, config, MODE_XML_DEFAULT );
}

public static <T> void setFromElementAtts( T bean, Element config ) throws Exception {
public static <T> void setFromElementAtts( T bean, Element config ) {
NamedNodeMap atts = config.getAttributes();
for ( int ak=0; ak<atts.getLength(); ak++ ) {
Attr att = (Attr)atts.item( ak );
Expand All @@ -75,7 +78,7 @@ public static <T> void setFromElementAtts( T bean, Element config ) throws Excep
}
}

public static <T> void setFromElementKids( T bean, Element config ) throws Exception {
public static <T> void setFromElementKids( T bean, Element config ) {
NodeList kids = config.getChildNodes();
for ( int ak=0; ak<kids.getLength(); ak++ ) {
Node current = kids.item( ak );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package test.org.fugerit.java.core.cfg.xml;

import java.io.Reader;
import java.io.StringReader;

import org.fugerit.java.core.cfg.xml.TextValueType;
import org.fugerit.java.core.cfg.xml.XmlBeanHelper;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.xml.dom.DOMIO;
import org.junit.Assert;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import lombok.Getter;
import lombok.Setter;
import test.org.fugerit.java.BasicTest;

public class TestXmlBeanHelper extends BasicTest {

public static final String TEST_VALUE = "test1";

// test bean
public class XmlBeanCheck {
@Getter @Setter private String field1;
}

// test bean
public class XmlBeanText implements TextValueType {
@Getter @Setter private String textValue;
}

private boolean setWorker( String xml, String mode, boolean expectedSet ) {
return SafeFunction.get( () -> {
try ( Reader reader = new StringReader( xml ) ) {
Document doc = DOMIO.loadDOMDoc( reader );
Element root = doc.getDocumentElement();
XmlBeanCheck check = new XmlBeanCheck();
Assert.assertNull( check.getField1() );
boolean set = XmlBeanHelper.setFromElementSafe( check, root, mode );
if ( expectedSet ) {
Assert.assertEquals( TEST_VALUE , check.getField1() );
} else {
Assert.assertNull( check.getField1() );
}
return set;
}
} );
}

@Test
public void testTextValyeType() {
SafeFunction.apply( () -> {
try ( Reader reader = new StringReader( "<config>test1</config>" ) ) {
Document doc = DOMIO.loadDOMDoc( reader );
Element root = doc.getDocumentElement();
XmlBeanText check = new XmlBeanText();
Assert.assertNull( check.getTextValue() );
boolean set = XmlBeanHelper.setFromElementSafe( check, root, XmlBeanHelper.MODE_XML_FULL );
Assert.assertEquals( TEST_VALUE , check.getTextValue() );
Assert.assertTrue( set );
}
} );
}

@Test
public void testSetFromAttributes() {
boolean set = this.setWorker( "<config field1='test1'/>" , XmlBeanHelper.MODE_XML_ATTRIBUTES, true );
Assert.assertTrue( set );
}

@Test
public void testSetFromAttributesFull() {
boolean set = this.setWorker( "<config field1='test1'/>" , XmlBeanHelper.MODE_XML_FULL, true );
Assert.assertTrue( set );
}

@Test
public void testSetFromElements() {
boolean set = this.setWorker( "<config><field1>test1</field1></config>" , XmlBeanHelper.MODE_XML_ELEMENTS, true );
Assert.assertTrue( set );
}

@Test
public void testSetFromElementsFull() {
boolean set = this.setWorker( "<config><field1>test1</field1></config>" , XmlBeanHelper.MODE_XML_FULL, true );
Assert.assertTrue( set );
}

@Test
public void testSetFromAttributesFail() {
boolean set = this.setWorker( "<config fieldDoesNotExist='test1'/>" , XmlBeanHelper.MODE_XML_ATTRIBUTES, false );
Assert.assertFalse( set );
}

@Test
public void testSetFromElementsFAil() {
boolean set = this.setWorker( "<config><fieldDoesNotExist>test1</fieldDoesNotExist></config>" , XmlBeanHelper.MODE_XML_ELEMENTS, false );
Assert.assertFalse( set );
}

}

0 comments on commit 8ed30f7

Please sign in to comment.