-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
fj-core/src/test/java/test/org/fugerit/java/core/cfg/xml/TestXmlBeanHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
|
||
} | ||
|