From: Olyutorskii Date: Sun, 2 Jun 2019 16:57:53 +0000 (+0900) Subject: unit tests with JAXB X-Git-Tag: release-3.120.2^2~3^2~1 X-Git-Url: http://git.osdn.net/view?p=mikutoga%2FTogaGem.git;a=commitdiff_plain;h=b5257a6f7325857c1bb9d788cd10e5aae1cf557c;ds=sidebyside unit tests with JAXB --- diff --git a/src/test/java/jp/sfjp/mikutoga/xml/BasicXmlExporterTest.java b/src/test/java/jp/sfjp/mikutoga/xml/BasicXmlExporterTest.java new file mode 100644 index 0000000..abff4ec --- /dev/null +++ b/src/test/java/jp/sfjp/mikutoga/xml/BasicXmlExporterTest.java @@ -0,0 +1,173 @@ +/* + */ + +package jp.sfjp.mikutoga.xml; + +import java.io.IOException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author silva + */ +public class BasicXmlExporterTest { + + public BasicXmlExporterTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of append method, of class BasicXmlExporter. + */ + @Test + public void testAppend() { + System.out.println("setAppendable"); + + BasicXmlExporter instance; + Appendable app; + StringBuffer buf; + + instance = new BasicXmlExporter(); + + try{ + instance.append(null); + fail(); + }catch(NullPointerException e){ + assert true; + }catch(IOException e){ + fail(); + } + + buf = new StringBuffer(); + app = buf; + + instance.setAppendable(app); + + try{ + instance.append("abc"); + instance.append('d'); + instance.append("abcdef", 4, 5); + }catch(IOException e){ + fail(); + } + + assertEquals("abcde", buf.toString()); + + try{ + instance.flush(); + instance.close(); + }catch(IOException e){ + fail(); + } + + return; + } + + @Test + public void testPutXsdInt() throws IOException{ + System.out.println("putXsdInt"); + + BasicXmlExporter instance; + StringBuffer buf; + + instance = new BasicXmlExporter(); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdInt(-1).putCh(',').putXsdInt(0).putCh(',').putXsdInt(1); + assertEquals("-1,0,1", buf.toString()); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdInt(-999).putCh(',').putXsdInt(9999); + assertEquals("-999,9999", buf.toString()); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdInt(Integer.MIN_VALUE).putCh(',').putXsdInt(Integer.MAX_VALUE); + assertEquals("-2147483648,2147483647", buf.toString()); + + return; + } + + @Test + public void testPutXsdFloat() throws IOException{ + System.out.println("putXsdFloat"); + + BasicXmlExporter instance; + StringBuffer buf; + + instance = new BasicXmlExporter(); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdFloat(-1.0f).putCh(',') + .putXsdFloat(-0.0f).putCh(',') + .putXsdFloat(0.0f).putCh(',') + .putXsdFloat(1.0f); + assertEquals("-1.0,-0.0,0.0,1.0", buf.toString()); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdFloat(Float.NEGATIVE_INFINITY).putCh(',') + .putXsdFloat(Float.POSITIVE_INFINITY).putCh(',') + .putXsdFloat(Float.NaN); + assertEquals("-INF,INF,NaN", buf.toString()); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdFloat(Float.MIN_VALUE).putCh(',') + .putXsdFloat(Float.MIN_NORMAL).putCh(',') + .putXsdFloat(Float.MAX_VALUE); + assertEquals("1.4E-45,1.17549435E-38,3.4028235E38", buf.toString()); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdFloat(0.001f); + assertEquals("0.001", buf.toString()); // fuzzy "0.0010" on JDK1.6 + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdFloat(StrictMath.nextDown(0.001f)); + assertEquals("9.999999E-4", buf.toString()); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdFloat(10000000.0f); + assertEquals("1.0E7", buf.toString()); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdFloat(StrictMath.nextDown(10000000.0f)); + assertEquals("9999999.0", buf.toString()); + + buf = new StringBuffer(); + instance.setAppendable(buf); + instance.putXsdFloat((float)StrictMath.E).putCh(',') + .putXsdFloat((float)StrictMath.PI); + assertEquals("2.7182817,3.1415927", buf.toString()); + + return; + } + +} diff --git a/src/test/java/jp/sfjp/mikutoga/xml/DomUtilsTest.java b/src/test/java/jp/sfjp/mikutoga/xml/DomUtilsTest.java new file mode 100644 index 0000000..496a87e --- /dev/null +++ b/src/test/java/jp/sfjp/mikutoga/xml/DomUtilsTest.java @@ -0,0 +1,322 @@ +/* +@see https://www.w3.org/TR/xmlschema-2/ + */ + +package jp.sfjp.mikutoga.xml; + +import javax.xml.bind.DatatypeConverter; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.*; +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +/** + * + * @author silva + */ +public class DomUtilsTest { + + private static final DocumentBuilderFactory FACTORY = + DocumentBuilderFactory.newInstance(); + private static final DocumentBuilder BUILDER; + + private static final String TESTELEM = "testelem"; + private static final String TESTATTR = "testattr"; + + private static Element getTestAttredElem(String attrVal){ + Document doc = BUILDER.newDocument(); + Element elem = doc.createElement(TESTELEM); + elem.setAttribute(TESTATTR, attrVal); + return elem; + } + + static{ + try{ + BUILDER = FACTORY.newDocumentBuilder(); + }catch(ParserConfigurationException e){ + throw new ExceptionInInitializerError(e); + } + } + + public DomUtilsTest() { + } + + @BeforeClass + public static void setUpClass() throws ParserConfigurationException{ + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of getBooleanAttr method, of class DomUtils. + */ + @Test + public void testGetBooleanAttr() throws Exception { + System.out.println("getBooleanAttr"); + + boolean result; + Element elem; + + elem = getTestAttredElem("true"); + result = DomUtils.getBooleanAttr(elem, TESTATTR); + assertTrue(result); + + elem = getTestAttredElem("false"); + result = DomUtils.getBooleanAttr(elem, TESTATTR); + assertFalse(result); + + elem = getTestAttredElem("0"); + result = DomUtils.getBooleanAttr(elem, TESTATTR); + assertFalse(result); + + elem = getTestAttredElem("1"); + result = DomUtils.getBooleanAttr(elem, TESTATTR); + assertTrue(result); + + elem = getTestAttredElem(null); + result = DomUtils.getBooleanAttr(elem, TESTATTR); + assertFalse(result); // Why? + + elem = getTestAttredElem("X"); + result = DomUtils.getBooleanAttr(elem, TESTATTR); + assertFalse(result); // Why? + + elem = getTestAttredElem(""); + result = DomUtils.getBooleanAttr(elem, TESTATTR); + assertFalse(result); // Why? + + elem = getTestAttredElem("\n\rtrue\u0020\t"); + result = DomUtils.getBooleanAttr(elem, TESTATTR); + assertTrue(result); + + return; + } + + /** + * Test of getIntegerAttr method, of class DomUtils. + */ + @Test + public void testGetIntegerAttr() throws TogaXmlException { + System.out.println("getIntegerAttr"); + + int result; + Element elem; + + elem = getTestAttredElem("0"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(0, result); + + elem = getTestAttredElem("1"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(1, result); + + elem = getTestAttredElem("-1"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(-1, result); + + elem = getTestAttredElem("-0"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(0, result); + + elem = getTestAttredElem("+1"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(1, result); + + elem = getTestAttredElem("999"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(999, result); + + elem = getTestAttredElem("-9999"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(-9999, result); + + elem = getTestAttredElem("-2147483648"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(Integer.MIN_VALUE, result); + + elem = getTestAttredElem("2147483647"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(Integer.MAX_VALUE, result); + + elem = getTestAttredElem("\n\r999\u0020\t"); + result = DomUtils.getIntegerAttr(elem, TESTATTR); + assertEquals(999, result); + + elem = getTestAttredElem("X"); + try{ + result = DomUtils.getIntegerAttr(elem, TESTATTR); + fail(); + }catch(TogaXmlException e){ + assert true; + } + + return; + } + + /** + * Test of getFloatAttr method, of class DomUtils. + */ + @Test + public void testGetFloatAttr() throws TogaXmlException { + System.out.println("getFloatAttr"); + + float result; + Element elem; + + elem = getTestAttredElem("0.0"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(0.0f, result, 0.0f); + + elem = getTestAttredElem("-0.0"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(0.0f, result, 0.0f); + assertEquals("-0.0", Float.toString(result)); + + elem = getTestAttredElem("+0.0"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(0.0f, result, 0.0f); + + elem = getTestAttredElem("-123.456"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(-123.456f, result, 0.0f); + + elem = getTestAttredElem("654.321"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(654.321f, result, 0.0f); + + elem = getTestAttredElem("2.718281828459045"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals((float)StrictMath.E, result, 0.0f); + + elem = getTestAttredElem("3.141592653589793"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals((float)StrictMath.PI, result, 0.0f); + + elem = getTestAttredElem("1.401298464324817E-45"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(Float.MIN_VALUE, result, 0.0f); + + elem = getTestAttredElem("1.1754943508222875E-38"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(Float.MIN_NORMAL, result, 0.0f); + + elem = getTestAttredElem("3.4028234663852886E38"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(Float.MAX_VALUE, result, 0.0f); + + elem = getTestAttredElem("2E3"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(2000.0f, result, 0.0f); + + elem = getTestAttredElem("2.3E4"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(23000.0f, result, 0.0f); + + elem = getTestAttredElem("2.3e4"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(23000.0f, result, 0.0f); + + elem = getTestAttredElem("2.3E+4"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(23000.0f, result, 0.0f); + + elem = getTestAttredElem("2.3E-4"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(0.00023f, result, 0.0f); + + elem = getTestAttredElem("INF"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(Float.POSITIVE_INFINITY, result, 0.0f); + + elem = getTestAttredElem("-INF"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(Float.NEGATIVE_INFINITY, result, 0.0f); + + elem = getTestAttredElem("+INF"); + try{ + result = DomUtils.getFloatAttr(elem, TESTATTR); + fail(); + }catch(TogaXmlException e){ + assert true; + } + //assertEquals(Float.POSITIVE_INFINITY, result, 0.0f); + + elem = getTestAttredElem("NaN"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertTrue(Float.isNaN(result)); + + elem = getTestAttredElem("\n\r123.456\u0020\t"); + result = DomUtils.getFloatAttr(elem, TESTATTR); + assertEquals(123.456f, result, 0.0f); + + elem = getTestAttredElem("X"); + try{ + result = DomUtils.getFloatAttr(elem, TESTATTR); + fail(); + }catch(TogaXmlException e){ + assert true; + } + + return; + } + + private static byte parseByte(String str){ + byte result; + result = DatatypeConverter.parseByte(str); + return result; + } + + /** + * Test of parseByte method, of class DomUtils. + */ + @Test + public void testparseByte() throws TogaXmlException { + System.out.println("prseByte"); + + byte result; + + result = parseByte("0"); + assertEquals((byte)0, result); + + result = parseByte("-1"); + assertEquals((byte)-1, result); + + result = parseByte("1"); + assertEquals((byte)1, result); + + result = parseByte("-0"); + assertEquals((byte)0, result); + + result = parseByte("+1"); + assertEquals((byte)1, result); + + result = parseByte("-128"); + assertEquals((byte)-128, result); + + result = parseByte("127"); + assertEquals((byte)127, result); + + result = parseByte("\n\r99\u0020\t"); + assertEquals((byte)99, result); + + return; + } + +}