OSDN Git Service

replacing DatatypeConverter(JAXB).
[mikutoga/TogaGem.git] / src / test / java / jp / sfjp / mikutoga / xml / BasicXmlExporterTest.java
1 /*
2  */
3
4 package jp.sfjp.mikutoga.xml;
5
6 import java.io.IOException;
7 import org.junit.After;
8 import org.junit.AfterClass;
9 import org.junit.Before;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12 import static org.junit.Assert.*;
13
14 /**
15  *
16  */
17 public class BasicXmlExporterTest {
18
19     public BasicXmlExporterTest() {
20     }
21
22     @BeforeClass
23     public static void setUpClass() {
24     }
25
26     @AfterClass
27     public static void tearDownClass() {
28     }
29
30     @Before
31     public void setUp() {
32     }
33
34     @After
35     public void tearDown() {
36     }
37
38     /**
39      * Test of append method, of class BasicXmlExporter.
40      */
41     @Test
42     public void testAppend() {
43         System.out.println("setAppendable");
44
45         BasicXmlExporter instance;
46         Appendable app;
47         StringBuffer buf;
48         
49         instance = new BasicXmlExporter();
50
51         try{
52             instance.append(null);
53             fail();
54         }catch(NullPointerException e){
55             assert true;
56         }catch(IOException e){
57             fail();
58         }
59
60         buf = new StringBuffer();
61         app = buf;
62
63         instance.setAppendable(app);
64
65         try{
66             instance.append("abc");
67             instance.append('d');
68             instance.append("abcdef", 4, 5);
69         }catch(IOException e){
70             fail();
71         }
72
73         assertEquals("abcde", buf.toString());
74
75         try{
76             instance.flush();
77             instance.close();
78         }catch(IOException e){
79             fail();
80         }
81
82         return;
83     }
84
85     @Test
86     public void testPutXsdInt() throws IOException{
87         System.out.println("putXsdInt");
88
89         BasicXmlExporter instance;
90         StringBuffer buf;
91
92         instance = new BasicXmlExporter();
93
94         buf = new StringBuffer();
95         instance.setAppendable(buf);
96         instance.putXsdInt(-1).putCh(',').putXsdInt(0).putCh(',').putXsdInt(1);
97         assertEquals("-1,0,1", buf.toString());
98
99         buf = new StringBuffer();
100         instance.setAppendable(buf);
101         instance.putXsdInt(-999).putCh(',').putXsdInt(9999);
102         assertEquals("-999,9999", buf.toString());
103
104         buf = new StringBuffer();
105         instance.setAppendable(buf);
106         instance.putXsdInt(Integer.MIN_VALUE).putCh(',').putXsdInt(Integer.MAX_VALUE);
107         assertEquals("-2147483648,2147483647", buf.toString());
108
109         return;
110     }
111
112     @Test
113     public void testPutXsdFloat() throws IOException{
114         System.out.println("putXsdFloat");
115
116         BasicXmlExporter instance;
117         StringBuffer buf;
118
119         instance = new BasicXmlExporter();
120
121         buf = new StringBuffer();
122         instance.setAppendable(buf);
123         instance.putXsdFloat(-1.0f).putCh(',')
124                 .putXsdFloat(-0.0f).putCh(',')
125                 .putXsdFloat(0.0f).putCh(',')
126                 .putXsdFloat(1.0f);
127         assertEquals("-1.0,-0.0,0.0,1.0", buf.toString());
128
129         buf = new StringBuffer();
130         instance.setAppendable(buf);
131         instance.putXsdFloat(Float.NEGATIVE_INFINITY).putCh(',')
132                 .putXsdFloat(Float.POSITIVE_INFINITY).putCh(',')
133                 .putXsdFloat(Float.NaN);
134         assertEquals("-INF,INF,NaN", buf.toString());
135
136         buf = new StringBuffer();
137         instance.setAppendable(buf);
138         instance.putXsdFloat(Float.MIN_VALUE).putCh(',')
139                 .putXsdFloat(Float.MIN_NORMAL).putCh(',')
140                 .putXsdFloat(Float.MAX_VALUE);
141         assertEquals("1.4E-45,1.17549435E-38,3.4028235E38", buf.toString());
142
143         buf = new StringBuffer();
144         instance.setAppendable(buf);
145         instance.putXsdFloat(0.001f);
146         assertEquals("0.001", buf.toString()); // fuzzy "0.0010" on JDK1.6
147
148         buf = new StringBuffer();
149         instance.setAppendable(buf);
150         instance.putXsdFloat(StrictMath.nextDown(0.001f));
151         assertEquals("9.999999E-4", buf.toString());
152
153         buf = new StringBuffer();
154         instance.setAppendable(buf);
155         instance.putXsdFloat(10000000.0f);
156         assertEquals("1.0E7", buf.toString());
157
158         buf = new StringBuffer();
159         instance.setAppendable(buf);
160         instance.putXsdFloat(StrictMath.nextDown(10000000.0f));
161         assertEquals("9999999.0", buf.toString());
162
163         buf = new StringBuffer();
164         instance.setAppendable(buf);
165         instance.putXsdFloat((float)StrictMath.E).putCh(',')
166                 .putXsdFloat((float)StrictMath.PI);
167         assertEquals("2.7182817,3.1415927", buf.toString());
168
169         return;
170     }
171
172 }