4 package jp.sourceforge.jindolf.archiver;
6 import java.io.StringWriter;
8 import jp.sourceforge.jindolf.parser.DecodeErrorInfo;
9 import jp.sourceforge.jindolf.parser.DecodedContent;
10 import org.junit.After;
11 import org.junit.AfterClass;
12 import org.junit.Before;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
16 import static org.junit.Assert.*;
21 public class XmlOutTest {
27 public static void setUpClass() {
31 public static void tearDownClass() {
39 public void tearDown() {
43 * Test of isWhiteSpace method, of class XmlUtils.
46 public void testIsWhiteSpace() {
47 System.out.println("isWhiteSpace");
49 assertTrue(XmlOut.isWhiteSpace('\u0020'));
50 assertTrue(XmlOut.isWhiteSpace('\t'));
51 assertTrue(XmlOut.isWhiteSpace('\n'));
52 assertTrue(XmlOut.isWhiteSpace('\r'));
54 assertFalse(XmlOut.isWhiteSpace('\u0000'));
55 assertFalse(XmlOut.isWhiteSpace('\u3000'));
56 assertFalse(XmlOut.isWhiteSpace('A'));
57 assertFalse(XmlOut.isWhiteSpace('亜'));
63 * Test of dumpDocType method, of class XmlUtils.
64 * @throws java.lang.Exception
67 public void testDumpDocType() throws Exception {
68 System.out.println("dumpDocType");
72 writer = new StringWriter();
73 xmlOut = new XmlOut(writer);
77 assertEquals("<!DOCTYPE village SYSTEM \"http://jindolf.sourceforge.jp/xml/dtd/bbsArchive-110421.dtd\" >", writer.toString());
83 * Test of dumpNameSpaceDecl method, of class XmlUtils.
84 * @throws java.lang.Exception
87 public void testDumpNameSpaceDecl() throws Exception {
88 System.out.println("dumpNameSpaceDecl");
92 writer = new StringWriter();
93 xmlOut = new XmlOut(writer);
94 xmlOut.dumpNameSpaceDecl();
97 assertEquals("xmlns=\"http://jindolf.sourceforge.jp/xml/ns/501\"", writer.toString());
103 * Test of dumpSiNameSpaceDecl method, of class XmlUtils.
104 * @throws java.lang.Exception
107 public void testDumpSiNameSpaceDecl() throws Exception {
108 System.out.println("dumpSiNameSpaceDecl");
112 writer = new StringWriter();
113 xmlOut = new XmlOut(writer);
114 xmlOut.dumpSiNameSpaceDecl();
117 assertEquals("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", writer.toString());
123 * Test of dumpSchemeLocation method, of class XmlUtils.
124 * @throws java.lang.Exception
127 public void testDumpSchemeLocation() throws Exception {
128 System.out.println("dumpSchemeLocation");
132 writer = new StringWriter();
133 xmlOut = new XmlOut(writer);
134 xmlOut.dumpSchemeLocation();
137 assertEquals("xsi:schemaLocation=\"http://jindolf.sourceforge.jp/xml/ns/501 http://jindolf.sourceforge.jp/xml/xsd/bbsArchive-110421.xsd\"", writer.toString());
143 * Test of indent method, of class XmlUtils.
144 * @throws java.lang.Exception
147 public void testIndent() throws Exception {
148 System.out.println("indent");
152 writer = new StringWriter();
153 xmlOut = new XmlOut(writer);
156 assertEquals(" ", writer.toString());
158 writer = new StringWriter();
159 xmlOut = new XmlOut(writer);
162 assertEquals(" ", writer.toString());
164 writer = new StringWriter();
165 xmlOut = new XmlOut(writer);
168 assertEquals("", writer.toString());
174 * Test of charRefOut method, of class XmlUtils.
175 * @throws java.lang.Exception
178 public void testCharRefOut() throws Exception {
179 System.out.println("charRefOut");
184 writer = new StringWriter();
185 xmlOut = new XmlOut(writer);
186 xmlOut.charRefOut('\u0020');
188 assertEquals(" ", writer.toString());
190 writer = new StringWriter();
191 xmlOut = new XmlOut(writer);
192 xmlOut.charRefOut('\u0009');
194 assertEquals("	", writer.toString());
196 writer = new StringWriter();
197 xmlOut = new XmlOut(writer);
198 xmlOut.charRefOut('\u0000');
200 assertEquals("�", writer.toString());
202 writer = new StringWriter();
203 xmlOut = new XmlOut(writer);
204 xmlOut.charRefOut('\u0001');
206 assertEquals("", writer.toString());
208 writer = new StringWriter();
209 xmlOut = new XmlOut(writer);
210 xmlOut.charRefOut('\u00ff');
212 assertEquals("ÿ", writer.toString());
214 writer = new StringWriter();
215 xmlOut = new XmlOut(writer);
216 xmlOut.charRefOut('\u0100');
218 assertEquals("Ā", writer.toString());
220 writer = new StringWriter();
221 xmlOut = new XmlOut(writer);
222 xmlOut.charRefOut('\u1000');
224 assertEquals("က", writer.toString());
226 writer = new StringWriter();
227 xmlOut = new XmlOut(writer);
228 xmlOut.charRefOut('\ud800');
230 assertEquals("�", writer.toString());
232 writer = new StringWriter();
233 xmlOut = new XmlOut(writer);
234 xmlOut.charRefOut('亜');
236 assertEquals("亜", writer.toString());
242 * Test of dumpRawData method, of class XmlUtils.
243 * @throws java.lang.Exception
246 public void testDumpRawData() throws Exception {
247 System.out.println("dumpInvalidChar");
252 writer = new StringWriter();
253 xmlOut = new XmlOut(writer);
254 xmlOut.dumpRawData('\u0000');
256 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"00\" >\u2400</rawdata>", writer.toString());
258 writer = new StringWriter();
259 xmlOut = new XmlOut(writer);
260 xmlOut.dumpRawData('\u001f');
262 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"1f\" >\u241f</rawdata>", writer.toString());
264 writer = new StringWriter();
265 xmlOut = new XmlOut(writer);
266 xmlOut.dumpRawData('\u0020');
268 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"20\" >\ufffd</rawdata>", writer.toString());
270 writer = new StringWriter();
271 xmlOut = new XmlOut(writer);
272 xmlOut.dumpRawData('\u00ff');
274 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"ff\" >\ufffd</rawdata>", writer.toString());
276 writer = new StringWriter();
277 xmlOut = new XmlOut(writer);
278 xmlOut.dumpRawData('\u0100');
280 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"0100\" >\ufffd</rawdata>", writer.toString());
286 * Test of charDataOut method, of class XmlUtils.
287 * @throws java.lang.Exception
290 public void testCharDataOut() throws Exception {
291 System.out.println("textOut");
296 writer = new StringWriter();
297 xmlOut = new XmlOut(writer);
298 xmlOut.charDataOut("ABC");
300 assertEquals("ABC", writer.toString());
302 writer = new StringWriter();
303 xmlOut = new XmlOut(writer);
304 xmlOut.charDataOut("&<>\"'");
306 assertEquals("&<>"'", writer.toString());
308 writer = new StringWriter();
309 xmlOut = new XmlOut(writer);
310 xmlOut.charDataOut("A\u0008B");
312 assertEquals("A<rawdata encoding=\"Shift_JIS\" hexBin=\"08\" >\u2408</rawdata>B", writer.toString());
314 writer = new StringWriter();
315 xmlOut = new XmlOut(writer);
316 xmlOut.charDataOut("A\u007fB");
318 assertEquals("A\u007fB", writer.toString());
320 writer = new StringWriter();
321 xmlOut = new XmlOut(writer);
322 xmlOut.charDataOut("A\u009fB");
324 assertEquals("A\u009fB", writer.toString());
326 writer = new StringWriter();
327 xmlOut = new XmlOut(writer);
328 xmlOut.charDataOut("A\u00a0B");
330 assertEquals("A\u00a0B", writer.toString());
332 writer = new StringWriter();
333 xmlOut = new XmlOut(writer);
334 xmlOut.charDataOut("A\\B");
336 assertEquals("A¥B", writer.toString());
338 writer = new StringWriter();
339 xmlOut = new XmlOut(writer);
340 xmlOut.charDataOut("A~B");
342 assertEquals("A‾B", writer.toString());
344 writer = new StringWriter();
345 xmlOut = new XmlOut(writer);
346 xmlOut.charDataOut("A\u0020\u0020");
348 assertEquals("A  ", writer.toString());
350 writer = new StringWriter();
351 xmlOut = new XmlOut(writer);
352 xmlOut.charDataOut("A\u0020");
354 assertEquals("A ", writer.toString());
356 writer = new StringWriter();
357 xmlOut = new XmlOut(writer);
358 xmlOut.charDataOut("A\u0020B");
360 assertEquals("A B", writer.toString());
362 writer = new StringWriter();
363 xmlOut = new XmlOut(writer);
364 xmlOut.charDataOut("A\u0020");
366 assertEquals("A ", writer.toString());
368 writer = new StringWriter();
369 xmlOut = new XmlOut(writer);
370 xmlOut.charDataOut("A\u0020\u0020B");
372 assertEquals("A  B", writer.toString());
374 writer = new StringWriter();
375 xmlOut = new XmlOut(writer);
376 xmlOut.charDataOut("\u0020\u0020B");
378 assertEquals("  B", writer.toString());
380 writer = new StringWriter();
381 xmlOut = new XmlOut(writer);
382 xmlOut.charDataOut("A\u0020\u0020\u0020B");
384 assertEquals("A   B", writer.toString());
386 writer = new StringWriter();
387 xmlOut = new XmlOut(writer);
388 xmlOut.charDataOut("\u0020\u0020\u0020B");
390 assertEquals("   B", writer.toString());
396 * Test of attrOut method, of class XmlUtils.
397 * @throws java.lang.Exception
400 public void testAttrOut() throws Exception {
401 System.out.println("attrOut");
406 writer = new StringWriter();
407 xmlOut = new XmlOut(writer);
408 xmlOut.attrOut("A", "B");
410 assertEquals("A=\"B\"", writer.toString());
412 writer = new StringWriter();
413 xmlOut = new XmlOut(writer);
414 xmlOut.attrOut("A", "B\u0008C");
416 assertEquals("A=\"B\u0008C\"", writer.toString());
418 writer = new StringWriter();
419 xmlOut = new XmlOut(writer);
420 xmlOut.attrOut("A", "B\nC\rD\tE");
422 assertEquals("A=\"B\nC\rD\tE\"", writer.toString());
428 * Test of timeAttrOut method, of class XmlUtils.
429 * @throws java.lang.Exception
432 public void testTimeAttrOut() throws Exception {
433 System.out.println("timeAttrOut");
438 writer = new StringWriter();
439 xmlOut = new XmlOut(writer);
440 xmlOut.timeAttrOut("A", 0, 0);
442 assertEquals("A=\"00:00:00+09:00\"", writer.toString());
444 writer = new StringWriter();
445 xmlOut = new XmlOut(writer);
446 xmlOut.timeAttrOut("A", 9, 9);
448 assertEquals("A=\"09:09:00+09:00\"", writer.toString());
450 writer = new StringWriter();
451 xmlOut = new XmlOut(writer);
452 xmlOut.timeAttrOut("A", 23, 59);
454 assertEquals("A=\"23:59:00+09:00\"", writer.toString());
460 * Test of dateAttrOut method, of class XmlUtils.
461 * @throws java.lang.Exception
464 public void testDateAttrOut() throws Exception {
465 System.out.println("dateAttrOut");
470 writer = new StringWriter();
471 xmlOut = new XmlOut(writer);
472 xmlOut.dateAttrOut("A", 1, 1);
474 assertEquals("A=\"--01-01+09:00\"", writer.toString());
476 writer = new StringWriter();
477 xmlOut = new XmlOut(writer);
478 xmlOut.dateAttrOut("A", 2, 29);
480 assertEquals("A=\"--02-29+09:00\"", writer.toString());
482 writer = new StringWriter();
483 xmlOut = new XmlOut(writer);
484 xmlOut.dateAttrOut("A", 12, 31);
486 assertEquals("A=\"--12-31+09:00\"", writer.toString());
492 * Test of dateTimeAttr method, of class XmlUtils.
493 * @throws java.lang.Exception
496 public void testDateTimeAttr() throws Exception {
497 System.out.println("dateTimeAttr");
502 writer = new StringWriter();
503 xmlOut = new XmlOut(writer);
504 xmlOut.dateTimeAttr("A", 0L);
506 assertEquals("A=\"1970-01-01T09:00:00.000+09:00\"", writer.toString());
508 writer = new StringWriter();
509 xmlOut = new XmlOut(writer);
510 xmlOut.dateTimeAttr("A", 1466871486000L);
512 assertEquals("A=\"2016-06-26T01:18:06.000+09:00\"", writer.toString());
518 * Test of dumpSjisMapError method, of class XmlUtils.
519 * @throws java.lang.Exception
522 public void testDumpDecodeError() throws Exception {
523 System.out.println("dumpErrorInfo");
527 DecodeErrorInfo errorInfo;
529 writer = new StringWriter();
530 xmlOut = new XmlOut(writer);
531 errorInfo = new DecodeErrorInfo(0, (byte) 0x00);
532 xmlOut.dumpErrorInfo(errorInfo);
534 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"00\" >\u2400</rawdata>", writer.toString());
536 writer = new StringWriter();
537 xmlOut = new XmlOut(writer);
538 errorInfo = new DecodeErrorInfo(0, (byte) 0x0f);
539 xmlOut.dumpErrorInfo(errorInfo);
541 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"0f\" >\u240f</rawdata>", writer.toString());
543 writer = new StringWriter();
544 xmlOut = new XmlOut(writer);
545 errorInfo = new DecodeErrorInfo(0, (byte) 0x10);
546 xmlOut.dumpErrorInfo(errorInfo);
548 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"10\" >\u2410</rawdata>", writer.toString());
550 writer = new StringWriter();
551 xmlOut = new XmlOut(writer);
552 errorInfo = new DecodeErrorInfo(0, (byte) 0xff);
553 xmlOut.dumpErrorInfo(errorInfo);
555 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"ff\" >\ufffd</rawdata>", writer.toString());
557 writer = new StringWriter();
558 xmlOut = new XmlOut(writer);
559 errorInfo = new DecodeErrorInfo(0, (byte) 0xee, (byte) 0xef); //92区
560 xmlOut.dumpErrorInfo(errorInfo);
562 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"eeef\" >\u2170</rawdata>", writer.toString());
568 * Test of dumpDecodedContent method, of class XmlUtils.
569 * @throws java.lang.Exception
572 public void testDumpDecodedContent() throws Exception {
573 System.out.println("dumpDecodedContent");
577 DecodedContent content;
579 writer = new StringWriter();
580 xmlOut = new XmlOut(writer);
581 content = new DecodedContent("ABC");
582 xmlOut.dumpDecodedContent(content);
584 assertEquals("ABC", writer.toString());
586 writer = new StringWriter();
587 xmlOut = new XmlOut(writer);
588 content = new DecodedContent();
589 content.append("AB");
590 content.addDecodeError((byte)0x08);
591 content.append("CD");
592 xmlOut.dumpDecodedContent(content);
594 assertEquals("AB<rawdata encoding=\"Shift_JIS\" hexBin=\"08\" >\u2408</rawdata>CD", writer.toString());
596 writer = new StringWriter();
597 xmlOut = new XmlOut(writer);
598 content = new DecodedContent();
599 content.append("AB");
600 content.addDecodeError((byte)0x08);
601 xmlOut.dumpDecodedContent(content);
603 assertEquals("AB<rawdata encoding=\"Shift_JIS\" hexBin=\"08\" >\u2408</rawdata>", writer.toString());
605 writer = new StringWriter();
606 xmlOut = new XmlOut(writer);
607 content = new DecodedContent();
608 content.addDecodeError((byte)0x08);
609 content.append("CD");
610 xmlOut.dumpDecodedContent(content);
612 assertEquals("<rawdata encoding=\"Shift_JIS\" hexBin=\"08\" >\u2408</rawdata>CD", writer.toString());
618 * Test of isXmlChar method, of class XmlOut.
621 public void testIsXmlChar() {
622 System.out.println("isXmlChar");
624 assertFalse(XmlOut.isXmlChar('\u0000'));
625 assertFalse(XmlOut.isXmlChar('\u001f'));
627 assertTrue(XmlOut.isXmlChar('\n'));
628 assertTrue(XmlOut.isXmlChar('\r'));
629 assertTrue(XmlOut.isXmlChar('\t'));
631 assertTrue(XmlOut.isXmlChar('\u0020'));
632 assertTrue(XmlOut.isXmlChar('A'));
633 assertTrue(XmlOut.isXmlChar('\ud7ff'));
635 assertTrue(XmlOut.isXmlChar('\ud800'));
636 assertTrue(XmlOut.isXmlChar('\udbff'));
638 assertTrue(XmlOut.isXmlChar('\udc00'));
639 assertTrue(XmlOut.isXmlChar('\udfff'));
641 assertTrue(XmlOut.isXmlChar('\ue000'));
642 assertTrue(XmlOut.isXmlChar('\ufffd'));
644 assertFalse(XmlOut.isXmlChar('\ufffe'));
645 assertFalse(XmlOut.isXmlChar('\uffff'));
651 * Test of replaceChar method, of class XmlOut.
654 public void testReplaceChar() {
655 System.out.println("replaceChar");
659 result = XmlOut.replaceChar('\u0000');
660 assertEquals('\u2400', result);
662 result = XmlOut.replaceChar('\u001f');
663 assertEquals('\u241f', result);
665 result = XmlOut.replaceChar('\u007f');
666 assertEquals('\u2421', result);
668 result = XmlOut.replaceChar('A');
669 assertEquals('\ufffd', result);
675 * Test of toHex method, of class XmlOut.
678 public void testToHex_byte() {
679 System.out.println("toHex");
685 result = XmlOut.toHex(bVal);
686 assertEquals("00", result);
689 result = XmlOut.toHex(bVal);
690 assertEquals("0a", result);
693 result = XmlOut.toHex(bVal);
694 assertEquals("7f", result);
697 result = XmlOut.toHex(bVal);
698 assertEquals("80", result);
701 result = XmlOut.toHex(bVal);
702 assertEquals("ff", result);
708 * Test of toHex method, of class XmlOut.
711 public void testToHex_char() {
712 System.out.println("toHex");
718 result = XmlOut.toHex(cVal);
719 assertEquals("00", result);
722 result = XmlOut.toHex(cVal);
723 assertEquals("0b", result);
726 result = XmlOut.toHex(cVal);
727 assertEquals("ff", result);
730 result = XmlOut.toHex(cVal);
731 assertEquals("0100", result);
734 result = XmlOut.toHex(cVal);
735 assertEquals("7fff", result);
738 result = XmlOut.toHex(cVal);
739 assertEquals("8000", result);
742 result = XmlOut.toHex(cVal);
743 assertEquals("ffff", result);
749 * Test of toHex method, of class XmlOut.
752 public void testToHex_short() {
753 System.out.println("toHex");
759 result = XmlOut.toHex(sVal);
760 assertEquals("0000", result);
763 result = XmlOut.toHex(sVal);
764 assertEquals("00ff", result);
767 result = XmlOut.toHex(sVal);
768 assertEquals("7fff", result);
770 sVal = (short) 0x8000;
771 result = XmlOut.toHex(sVal);
772 assertEquals("8000", result);
774 sVal = (short) 0xffff;
775 result = XmlOut.toHex(sVal);
776 assertEquals("ffff", result);