(empty log message)
@@ -0,0 +1,113 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: XmlTest.java,v 1.2 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import java.nio.charset.Charset; | |
16 | + | |
17 | +import org.junit.Test; | |
18 | + | |
19 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
20 | +// XmlTest: Xml クラステストケース | |
21 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
22 | +/** | |
23 | + * Xml クラスのテストケースです。 | |
24 | + * <p> | |
25 | + * @version $Revision: 1.2 $ $Date: 2010/02/24 13:59:38 $ | |
26 | + * @author torao | |
27 | + * @since 2009/04/09 Java2 SE 5.0 | |
28 | + */ | |
29 | +public class XmlTest extends TestCase{ | |
30 | + | |
31 | + // ====================================================================== | |
32 | + // コンストラクタのテスト | |
33 | + // ====================================================================== | |
34 | + /** | |
35 | + * ユーティリティクラスであることをテストします。 | |
36 | + * <p> | |
37 | + */ | |
38 | + @Test | |
39 | + public void testClass(){ | |
40 | + verifyUtilityClass(Xml.class); | |
41 | + return; | |
42 | + } | |
43 | + | |
44 | + // ====================================================================== | |
45 | + // 空白文字判定のテスト | |
46 | + // ====================================================================== | |
47 | + /** | |
48 | + * XML 空白文字判定のテストを行います。 | |
49 | + * <p> | |
50 | + */ | |
51 | + @Test | |
52 | + public void testIsWhitespace() { | |
53 | + assertTrue(Xml.isWhitespace(' ')); | |
54 | + assertTrue(Xml.isWhitespace('\t')); | |
55 | + assertTrue(Xml.isWhitespace('\r')); | |
56 | + assertTrue(Xml.isWhitespace('\n')); | |
57 | + assertFalse(Xml.isWhitespace('\f')); | |
58 | + assertFalse(Xml.isWhitespace(' ')); | |
59 | + assertFalse(Xml.isWhitespace(-1)); | |
60 | + return; | |
61 | + } | |
62 | + | |
63 | + // ====================================================================== | |
64 | + // XMLエスケープのテスト | |
65 | + // ====================================================================== | |
66 | + /** | |
67 | + * 文字列の XML エスケープをテストします。 | |
68 | + * <p> | |
69 | + */ | |
70 | + @Test | |
71 | + public void testEscape() { | |
72 | + assertEquals("", Xml.escape("")); | |
73 | + assertEquals("ABC", Xml.escape("ABC")); | |
74 | + assertEquals(" \t\n\t\n\t\n", Xml.escape(" \t\r\n\t\n\t\r")); | |
75 | + assertEquals("&<>"'", Xml.escape("&<>\"\'")); | |
76 | + assertEquals("&#" + 0xFFFE + ";", Xml.escape("\uFFFE")); | |
77 | + return; | |
78 | + } | |
79 | + | |
80 | + // ====================================================================== | |
81 | + // XMLエスケープ解除のテスト | |
82 | + // ====================================================================== | |
83 | + /** | |
84 | + * 文字列の XML エスケープ解除をテストします。 | |
85 | + * <p> | |
86 | + */ | |
87 | + @Test | |
88 | + public void testUnescape() { | |
89 | + assertEquals("", Xml.unescape("")); | |
90 | + assertEquals("ABC", Xml.unescape("ABC")); | |
91 | + assertEquals("&,<,>,\",\',A,A,A", Xml.unescape("&,<,>,",',&#" + ((int)'A') + ";,A,A")); | |
92 | + assertEquals("&&foo;&#;𐀀&#x;", Xml.unescape("&&foo;&#;𐀀&#x;")); | |
93 | + return; | |
94 | + } | |
95 | + | |
96 | + // ====================================================================== | |
97 | + // 文字セット参照のテスト | |
98 | + // ====================================================================== | |
99 | + /** | |
100 | + * Content-Type から文字セットの参照をテストします。 | |
101 | + * <p> | |
102 | + */ | |
103 | + @Test | |
104 | + public void testGetCharset() { | |
105 | + assertEquals(Charset.forName("utf-8"), Xml.getCharset("text/xml;charset=UTF-8")); | |
106 | + assertEquals(Charset.forName("utf-8"), Xml.getCharset("application/javascript;\ncharset=\"UTF-8\"\r\n;foo=\"bar\";")); | |
107 | + assertNull(Xml.getCharset("application/javascript;foo=\"bar\";")); | |
108 | + assertNull(Xml.getCharset("text/xml;charset=UNKNOWN")); | |
109 | + assertNull(Xml.getCharset("text/xml;charset=")); | |
110 | + return; | |
111 | + } | |
112 | + | |
113 | +} |
@@ -0,0 +1,124 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2009 koiroha.org All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * takami torao <torao@mars.dti.ne.jp> | |
8 | + * http://www.koiroha.org | |
9 | + * $Id: HtmlTest.java,v 1.1 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml; | |
12 | + | |
13 | +import static junit.framework.Assert.*; | |
14 | + | |
15 | +import org.junit.Test; | |
16 | + | |
17 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
18 | +// HtmlTest: HTML テストケース | |
19 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
20 | +/** | |
21 | + * Html クラスのテストを行います。 | |
22 | + * <p> | |
23 | + * @version $Revision: 1.1 $ $Date: 2010/02/24 13:59:38 $ | |
24 | + * @author torao | |
25 | + * @since 2010/02/24 Java SE 6 | |
26 | + */ | |
27 | +public class HtmlTest extends TestCase{ | |
28 | + | |
29 | + // ====================================================================== | |
30 | + // コンストラクタのテスト | |
31 | + // ====================================================================== | |
32 | + /** | |
33 | + * ユーティリティクラスであることをテストします。 | |
34 | + * <p> | |
35 | + */ | |
36 | + @Test | |
37 | + public void testClass(){ | |
38 | + verifyUtilityClass(Html.class); | |
39 | + return; | |
40 | + } | |
41 | + | |
42 | + // ====================================================================== | |
43 | + // エンティティから文字の参照 | |
44 | + // ====================================================================== | |
45 | + /** | |
46 | + * エンティティから文字参照をテストします。 | |
47 | + * <p> | |
48 | + */ | |
49 | + @Test | |
50 | + public void testGetCharacter(){ | |
51 | + | |
52 | + // 一般的な HTML エンティティ | |
53 | + assertEquals('<', Html.getCharacter("<")); | |
54 | + assertEquals('>', Html.getCharacter(">")); | |
55 | + assertEquals('&', Html.getCharacter("&")); | |
56 | + assertEquals('\"', Html.getCharacter(""")); | |
57 | + | |
58 | + // HTML エンティティ | |
59 | + assertEquals('©', Html.getCharacter("©")); | |
60 | + assertEquals('\u00A0', Html.getCharacter(" ")); | |
61 | + assertTrue(Html.getCharacter("'") < 0); // HTML では未定義 | |
62 | + | |
63 | + // 数値参照 | |
64 | + assertEquals(0, Html.getCharacter("�")); | |
65 | + assertEquals(1024, Html.getCharacter("Ѐ")); | |
66 | + assertEquals(0x1, Html.getCharacter("")); | |
67 | + assertEquals(0xFFFF, Html.getCharacter("")); | |
68 | + assertTrue(Html.getCharacter("&#-1;") < 0); | |
69 | + assertTrue(Html.getCharacter("𐀀") < 0); | |
70 | + assertTrue(Html.getCharacter("𐀀") < 0); | |
71 | + assertTrue(Html.getCharacter("઼G;") < 0); | |
72 | + | |
73 | + assertTrue(Html.getCharacter("") < 0); // 数値参照は未対応 | |
74 | + | |
75 | + try{ | |
76 | + Html.getCharacter(null); | |
77 | + fail(); | |
78 | + } catch(NullPointerException ex){/* */} | |
79 | + | |
80 | + return; | |
81 | + } | |
82 | + | |
83 | + // ====================================================================== | |
84 | + // 実体参照の参照 | |
85 | + // ====================================================================== | |
86 | + /** | |
87 | + * 文字に対する HTML 実体参照を参照します。 | |
88 | + * <p> | |
89 | + */ | |
90 | + @Test | |
91 | + public void testGetEntityReference(){ | |
92 | + | |
93 | + // 一般的な HTML エンティティ | |
94 | + assertEquals("<", Html.getEntityReference('<')); | |
95 | + assertEquals(">", Html.getEntityReference('>')); | |
96 | + assertEquals("&", Html.getEntityReference('&')); | |
97 | + assertEquals(""", Html.getEntityReference('\"')); | |
98 | + | |
99 | + // HTML エンティティ | |
100 | + assertEquals("©", Html.getEntityReference('©')); | |
101 | + assertEquals(" ", Html.getEntityReference('\u00A0')); | |
102 | + | |
103 | + assertEquals("'", Html.getEntityReference('\'')); | |
104 | + assertEquals("�", Html.getEntityReference('\0')); | |
105 | + return; | |
106 | + } | |
107 | + | |
108 | + // ====================================================================== | |
109 | + // 実体参照の解除 | |
110 | + // ====================================================================== | |
111 | + /** | |
112 | + * 実体参照の解除をテストします。 | |
113 | + * <p> | |
114 | + */ | |
115 | + @Test | |
116 | + public void unescape(){ | |
117 | + assertEquals("", Html.unescape("")); | |
118 | + assertEquals("あいうえお", Html.unescape("あいうえお")); | |
119 | + assertEquals("a=& b=\"", Html.unescape("a=& b="")); | |
120 | + assertEquals("abc&fooooo;def", Html.unescape("abc&fooooo;def")); | |
121 | + return; | |
122 | + } | |
123 | + | |
124 | +} |
@@ -0,0 +1,171 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: DefaultNamespaceContextTest.java,v 1.2 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import java.util.*; | |
16 | + | |
17 | +import javax.xml.XMLConstants; | |
18 | + | |
19 | +import org.junit.Test; | |
20 | + | |
21 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
22 | +// DefaultNamespaceContextTest: | |
23 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
24 | +/** | |
25 | + * | |
26 | + * <p> | |
27 | + * @version $Revision: 1.2 $ $Date: 2010/02/24 13:59:38 $ | |
28 | + * @author torao | |
29 | + * @since 2009/04/09 Java2 SE 5.0 | |
30 | + */ | |
31 | +public class DefaultNamespaceContextTest extends TestCase{ | |
32 | + | |
33 | + // ====================================================================== | |
34 | + // コンストラクタのテスト | |
35 | + // ====================================================================== | |
36 | + /** | |
37 | + * 構築時に例外が発生しない事を確認します。 | |
38 | + * <p> | |
39 | + */ | |
40 | + @Test | |
41 | + public void test全部() { | |
42 | + DefaultNamespaceContext parent = new DefaultNamespaceContext(); | |
43 | + DefaultNamespaceContext child = new DefaultNamespaceContext(parent); | |
44 | + DefaultNamespaceContext grandchild = new DefaultNamespaceContext(child); | |
45 | + | |
46 | + // 規定値を確認 | |
47 | + assertEquals(XMLConstants.NULL_NS_URI, grandchild.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX)); | |
48 | + assertEquals(XMLConstants.XML_NS_URI, grandchild.getNamespaceURI(XMLConstants.XML_NS_PREFIX)); | |
49 | + assertEquals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, grandchild.getNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE)); | |
50 | + assertEquals(XMLConstants.DEFAULT_NS_PREFIX, parent.getPrefix(XMLConstants.NULL_NS_URI)); | |
51 | + assertEquals(XMLConstants.XML_NS_PREFIX, parent.getPrefix(XMLConstants.XML_NS_URI)); | |
52 | + assertEquals(XMLConstants.XMLNS_ATTRIBUTE, parent.getPrefix(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)); | |
53 | + | |
54 | + // 未定義の接頭辞に対するデフォルトの名前空間を確認 | |
55 | + assertEquals(XMLConstants.NULL_NS_URI, parent.getNamespaceURI("undefined")); | |
56 | + assertEquals(XMLConstants.NULL_NS_URI, child.getNamespaceURI("undefined")); | |
57 | + assertEquals(XMLConstants.NULL_NS_URI, grandchild.getNamespaceURI("undefined")); | |
58 | + | |
59 | + String prefix = "px"; | |
60 | + String uri = "http://foo.bar.com/px"; | |
61 | + parent.setNamespaceURI(prefix, uri); | |
62 | + assertEquals(uri, parent.getNamespaceURI(prefix)); | |
63 | + assertEquals(uri, child.getNamespaceURI(prefix)); | |
64 | + assertEquals(uri, grandchild.getNamespaceURI(prefix)); | |
65 | + assertEquals(prefix, parent.getPrefix(uri)); | |
66 | + assertEquals(prefix, child.getPrefix(uri)); | |
67 | + assertEquals(prefix, grandchild.getPrefix(uri)); | |
68 | + | |
69 | + prefix = "cx"; | |
70 | + uri = "http://foo.bar.com/cx"; | |
71 | + child.setNamespaceURI(prefix, uri); | |
72 | + assertEquals(XMLConstants.NULL_NS_URI, parent.getNamespaceURI(prefix)); | |
73 | + assertEquals(uri, child.getNamespaceURI(prefix)); | |
74 | + assertEquals(uri, grandchild.getNamespaceURI(prefix)); | |
75 | + assertNull(parent.getPrefix(uri)); | |
76 | + assertEquals(prefix, child.getPrefix(uri)); | |
77 | + assertEquals(prefix, grandchild.getPrefix(uri)); | |
78 | + | |
79 | + prefix = "gx"; | |
80 | + uri = "http://foo.bar.com/gx"; | |
81 | + grandchild.setNamespaceURI(prefix, uri); | |
82 | + assertEquals(XMLConstants.NULL_NS_URI, parent.getNamespaceURI(prefix)); | |
83 | + assertEquals(XMLConstants.NULL_NS_URI, child.getNamespaceURI(prefix)); | |
84 | + assertEquals(uri, grandchild.getNamespaceURI(prefix)); | |
85 | + assertNull(parent.getPrefix(uri)); | |
86 | + assertNull(child.getPrefix(uri)); | |
87 | + assertEquals(prefix, grandchild.getPrefix(uri)); | |
88 | + | |
89 | + // デフォルトの名前空間は置き換えが可能であることを確認 | |
90 | + uri = "http://foo.bar.com/default"; | |
91 | + parent.setNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX, uri); | |
92 | + assertEquals(uri, grandchild.getNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX)); | |
93 | + | |
94 | + // 名前空間に定義されている全ての接頭辞を列挙できることを確認 | |
95 | + uri = "http://foo.bar.com/duplicate"; | |
96 | + parent.setNamespaceURI("px", uri); | |
97 | + parent.setNamespaceURI("cx", uri); | |
98 | + parent.setNamespaceURI(XMLConstants.DEFAULT_NS_PREFIX, uri); | |
99 | + child.setNamespaceURI("cx", uri); | |
100 | + grandchild.setNamespaceURI("gx", uri); | |
101 | + List<String> px = new ArrayList<String>(); | |
102 | + Iterator<String> it = grandchild.getPrefixes(uri); | |
103 | + while(it.hasNext()){ | |
104 | + px.add(it.next()); | |
105 | + } | |
106 | + assertEquals(4, px.size()); | |
107 | + assertTrue(px.contains("px")); | |
108 | + assertTrue(px.contains("cx")); | |
109 | + assertTrue(px.contains("gx")); | |
110 | + assertTrue(px.contains(XMLConstants.DEFAULT_NS_PREFIX)); | |
111 | + | |
112 | + it = grandchild.getPrefixes(XMLConstants.XML_NS_URI); | |
113 | + assertTrue(it.hasNext()); | |
114 | + assertEquals(XMLConstants.XML_NS_PREFIX, it.next()); | |
115 | + assertFalse(it.hasNext()); | |
116 | + | |
117 | + it = grandchild.getPrefixes(XMLConstants.XMLNS_ATTRIBUTE_NS_URI); | |
118 | + assertTrue(it.hasNext()); | |
119 | + assertEquals(XMLConstants.XMLNS_ATTRIBUTE, it.next()); | |
120 | + assertFalse(it.hasNext()); | |
121 | + | |
122 | + // 未定義の名前空間に対する接頭辞の列挙 | |
123 | + it = grandchild.getPrefixes("undefined"); | |
124 | + assertFalse(it.hasNext()); | |
125 | + | |
126 | + // 接頭辞に null を指定した場合 | |
127 | + try{ | |
128 | + grandchild.setNamespaceURI(null, ""); | |
129 | + fail(); | |
130 | + } catch(IllegalArgumentException ex){/* */} | |
131 | + | |
132 | + // 名前空間に null を指定した場合 | |
133 | + try{ | |
134 | + grandchild.setNamespaceURI("", null); | |
135 | + fail(); | |
136 | + } catch(IllegalArgumentException ex){/* */} | |
137 | + | |
138 | + // 接頭辞に null を指定した場合 | |
139 | + try{ | |
140 | + grandchild.getNamespaceURI(null); | |
141 | + fail(); | |
142 | + } catch(IllegalArgumentException ex){/* */} | |
143 | + | |
144 | + // 名前空間に null を指定した場合 | |
145 | + try{ | |
146 | + grandchild.getPrefix(null); | |
147 | + fail(); | |
148 | + } catch(IllegalArgumentException ex){/* */} | |
149 | + | |
150 | + // 名前空間に null を指定した場合 | |
151 | + try{ | |
152 | + grandchild.getPrefixes(null); | |
153 | + fail(); | |
154 | + } catch(IllegalArgumentException ex){/* */} | |
155 | + | |
156 | + // 規定の接頭辞を再定義しようとした場合 | |
157 | + try{ | |
158 | + grandchild.setNamespaceURI(XMLConstants.XML_NS_PREFIX, "http://foo.bar.com/xxx"); | |
159 | + fail(); | |
160 | + } catch(IllegalArgumentException ex){/* */} | |
161 | + | |
162 | + // 規定の接頭辞を再定義しようとした場合 | |
163 | + try{ | |
164 | + grandchild.setNamespaceURI(XMLConstants.XMLNS_ATTRIBUTE, "http://foo.bar.com/xxx"); | |
165 | + fail(); | |
166 | + } catch(IllegalArgumentException ex){/* */} | |
167 | + | |
168 | + return; | |
169 | + } | |
170 | + | |
171 | +} |
@@ -0,0 +1,35 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: TestSuite.java,v 1.2 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml; | |
12 | + | |
13 | +import org.junit.runner.RunWith; | |
14 | +import org.junit.runners.Suite; | |
15 | +import org.junit.runners.Suite.SuiteClasses; | |
16 | + | |
17 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
18 | +// TestSuite: テストスイートクラス | |
19 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
20 | +/** | |
21 | + * このパッケージのテストスイートです。 | |
22 | + * <p> | |
23 | + * @version $Revision: 1.2 $ $Date: 2010/02/24 13:59:38 $ | |
24 | + * @author torao | |
25 | + * @since 2009/04/02 Java2 SE 5.0 | |
26 | + */ | |
27 | +@RunWith(Suite.class) | |
28 | +@SuiteClasses({ | |
29 | + DefaultNamespaceContextTest.class, | |
30 | + XmlTest.class, | |
31 | + HtmlTest.class, | |
32 | +}) | |
33 | +public class TestSuite { | |
34 | + /* */ | |
35 | +} |
@@ -0,0 +1,146 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: TestCase.java,v 1.3 2010/07/18 15:30:18 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import java.io.*; | |
16 | +import java.lang.reflect.*; | |
17 | +import java.util.Date; | |
18 | +import java.util.logging.*; | |
19 | + | |
20 | +import org.junit.BeforeClass; | |
21 | + | |
22 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
23 | +// TestCase: テストケースクラス | |
24 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
25 | +/** | |
26 | + * テストケース用ののスーパークラスです。 | |
27 | + * <p> | |
28 | + * @version $Revision: 1.3 $ $Date: 2010/07/18 15:30:18 $ | |
29 | + * @author torao | |
30 | + * @since 2009/04/09 Java2 SE 5.0 | |
31 | + */ | |
32 | +public class TestCase { | |
33 | + | |
34 | + // ====================================================================== | |
35 | + // ログ出力の初期化 | |
36 | + // ====================================================================== | |
37 | + /** | |
38 | + * ログ出力を初期化します。 | |
39 | + * <p> | |
40 | + */ | |
41 | + @BeforeClass | |
42 | + public static void initLogging(){ | |
43 | + | |
44 | + // フォーマッターの構築 | |
45 | + Formatter formatter = new Formatter(){ | |
46 | + @Override | |
47 | + public String format(LogRecord r) { | |
48 | + return String.format("[%1$tY-%1$tm-%1$td %1$tT] %2$s - %3$s%n", | |
49 | + new Date(r.getMillis()), r.getLevel(), r.getMessage()); | |
50 | + } | |
51 | + }; | |
52 | + | |
53 | + // ログ出力先から全てのハンドラを取り外し | |
54 | + Logger logger = Logger.getLogger("org.koiroha.xml"); | |
55 | + for(Handler h: logger.getHandlers()){ | |
56 | + logger.removeHandler(h); | |
57 | + } | |
58 | + | |
59 | + // ハンドラとレベルを再設定 | |
60 | + Handler handler = new ConsoleHandler(); | |
61 | + logger.addHandler(handler); | |
62 | + handler.setLevel(Level.ALL); | |
63 | + logger.setLevel(Level.ALL); | |
64 | + handler.setFormatter(formatter); | |
65 | + return; | |
66 | + } | |
67 | + | |
68 | + // ====================================================================== | |
69 | + // ユーティリティクラスのテスト | |
70 | + // ====================================================================== | |
71 | + /** | |
72 | + * 指定されたクラスがユーティリティクラスとして定義されていることをテストします。具体的には | |
73 | + * リフレクションを使用して以下を確認します。 | |
74 | + * <p> | |
75 | + * <ul> | |
76 | + * <li> クラスが final 宣言されている。</li> | |
77 | + * <li> private 宣言されているデフォルトコンストラクタのみが定義されている。</li> | |
78 | + * <li> 全ての public なメソッドが static 宣言されている。</li> | |
79 | + * </ul> | |
80 | + * <p> | |
81 | + * また全パス確認のためにデフォルトコンストラクタを起動します。 | |
82 | + * <p> | |
83 | + * @param clazz 検証するクラス | |
84 | + */ | |
85 | + public static void verifyUtilityClass(Class<?> clazz){ | |
86 | + | |
87 | + // クラスが final 宣言されていることを確認 | |
88 | + assertTrue("ユーティリティクラスが final 宣言されていません", Modifier.isFinal(clazz.getModifiers())); | |
89 | + | |
90 | + // private 宣言されたデフォルトコンストラクタのみが定義されていることを確認 | |
91 | + assertEquals("コンストラクタが複数定義されています", 1, clazz.getDeclaredConstructors().length); | |
92 | + try{ | |
93 | + | |
94 | + // デフォルトコンストラクタが private 宣言されていることを確認 | |
95 | + Constructor<?> c = clazz.getDeclaredConstructor(); | |
96 | + assertTrue("デフォルトコンストラクタが private 宣言されていません", Modifier.isPrivate(c.getModifiers())); | |
97 | + | |
98 | + // 全パス確認用にインスタンスを生成する | |
99 | + c.setAccessible(true); | |
100 | + c.newInstance(); | |
101 | + | |
102 | + } catch(NoSuchMethodException ex){ | |
103 | + fail("デフォルトコンストラクタが定義されていません"); | |
104 | + } catch(Exception ex){ | |
105 | + fail("コンストラクタの呼び出しで例外が発生しました"); | |
106 | + } | |
107 | + | |
108 | + // アクセス可能な全てのメソッドが public static 宣言されていることを確認 | |
109 | + Method[] m = clazz.getDeclaredMethods(); | |
110 | + for(int i=0; i<m.length; i++){ | |
111 | + assertTrue("メソッドが static 宣言されていません: " + m[i].toGenericString(), | |
112 | + Modifier.isStatic(m[i].getModifiers())); | |
113 | + } | |
114 | + return; | |
115 | + } | |
116 | + | |
117 | + // ====================================================================== | |
118 | + // シリアライズのテスト | |
119 | + // ====================================================================== | |
120 | + /** | |
121 | + * 指定されたインスタンスがシリアライズ可能かどうかを確認します。 | |
122 | + * <p> | |
123 | + * @param obj 検証するオブジェクト | |
124 | + */ | |
125 | + public static void verifySerializable(Object obj){ | |
126 | + try{ | |
127 | + | |
128 | + // シリアライズの実行 | |
129 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
130 | + ObjectOutputStream out = new ObjectOutputStream(baos); | |
131 | + out.writeObject(obj); | |
132 | + out.flush(); | |
133 | + byte[] binary = baos.toByteArray(); | |
134 | + | |
135 | + // 復元の実行 | |
136 | + ByteArrayInputStream bais = new ByteArrayInputStream(binary); | |
137 | + ObjectInputStream in = new ObjectInputStream(bais); | |
138 | + in.readObject(); | |
139 | + | |
140 | + } catch(Exception ex){ | |
141 | + fail(ex.toString()); | |
142 | + } | |
143 | + return; | |
144 | + } | |
145 | + | |
146 | +} |
@@ -0,0 +1,176 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: HTMLParserTest.java,v 1.3 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import javax.xml.parsers.SAXParser; | |
16 | + | |
17 | +import org.junit.Test; | |
18 | + | |
19 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
20 | +// HTMLParserTest: HTML パーサテスト | |
21 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
22 | +/** | |
23 | + * HTML パーサのテストケースです。 | |
24 | + * <p> | |
25 | + * @version $Revision: 1.3 $ $Date: 2010/02/24 13:59:38 $ | |
26 | + * @author torao | |
27 | + * @since 2009/04/01 Java2 SE 5.0 | |
28 | + */ | |
29 | +public class HTMLParserTest { | |
30 | + | |
31 | + // ====================================================================== | |
32 | + // コンストラクタ | |
33 | + // ====================================================================== | |
34 | + /** | |
35 | + * コンストラクタは何も行いません。 | |
36 | + * <p> | |
37 | + */ | |
38 | + public HTMLParserTest() { | |
39 | + return; | |
40 | + } | |
41 | + | |
42 | + // ====================================================================== | |
43 | + // コンストラクタテスト | |
44 | + // ====================================================================== | |
45 | + /** | |
46 | + * コンストラクタのテストを行います。 | |
47 | + * <p> | |
48 | + */ | |
49 | + @Test | |
50 | + public void testコンストラクタ() { | |
51 | + HTMLParserFactory factory = new HTMLParserFactory(); | |
52 | + factory.newSAXParser(); | |
53 | + return; | |
54 | + } | |
55 | + | |
56 | + // ====================================================================== | |
57 | + // 名前空間設定のテスト | |
58 | + // ====================================================================== | |
59 | + /** | |
60 | + * 名前空間の設定が有効かどうかをテストします。このテストは有効性の on/off が適切に引き継が | |
61 | + * れているかのみをテストし実際の動きまでは関知しません。 | |
62 | + * <p> | |
63 | + */ | |
64 | + @Test | |
65 | + public void test名前空間の有効性設定() { | |
66 | + HTMLParserFactory factory = new HTMLParserFactory(); | |
67 | + | |
68 | + // デフォルト状態で無効になっていることを確認 | |
69 | + SAXParser parser = factory.newSAXParser(); | |
70 | + assertFalse(parser.isNamespaceAware()); | |
71 | + | |
72 | + // 明示的に有効にした場合に有効になっていることを確認 | |
73 | + factory.setNamespaceAware(true); | |
74 | + parser = factory.newSAXParser(); | |
75 | + assertTrue(parser.isNamespaceAware()); | |
76 | + factory.setNamespaceAware(false); | |
77 | + assertTrue(parser.isNamespaceAware()); | |
78 | + | |
79 | + // 明示的に無効にした場合に無効になっていることを確認 | |
80 | + factory.setNamespaceAware(false); | |
81 | + parser = factory.newSAXParser(); | |
82 | + assertFalse(parser.isNamespaceAware()); | |
83 | + factory.setNamespaceAware(true); | |
84 | + assertFalse(parser.isNamespaceAware()); | |
85 | + | |
86 | + return; | |
87 | + } | |
88 | + | |
89 | + // ====================================================================== | |
90 | + // ドキュメント検証設定のテスト | |
91 | + // ====================================================================== | |
92 | + /** | |
93 | + * ドキュメント検証の設定が有効かどうかをテストします。このテストは有効性の on/off が適切に | |
94 | + * 引き継がれているかのみをテストし実際の動きまでは関知しません。 | |
95 | + * <p> | |
96 | + */ | |
97 | + @Test | |
98 | + public void testドキュメント検証の有効性設定() { | |
99 | + HTMLParserFactory factory = new HTMLParserFactory(); | |
100 | + | |
101 | + // デフォルト状態で無効になっていることを確認 | |
102 | + SAXParser parser = factory.newSAXParser(); | |
103 | + assertFalse(parser.isValidating()); | |
104 | + | |
105 | + // 明示的に有効にした場合に有効になっていることを確認 | |
106 | + factory.setValidating(true); | |
107 | + parser = factory.newSAXParser(); | |
108 | + assertTrue(parser.isValidating()); | |
109 | + factory.setValidating(false); | |
110 | + assertTrue(parser.isValidating()); | |
111 | + | |
112 | + // 明示的に無効にした場合に無効になっていることを確認 | |
113 | + factory.setValidating(false); | |
114 | + parser = factory.newSAXParser(); | |
115 | + assertFalse(parser.isValidating()); | |
116 | + factory.setValidating(true); | |
117 | + assertFalse(parser.isValidating()); | |
118 | + | |
119 | + return; | |
120 | + } | |
121 | + | |
122 | + // ====================================================================== | |
123 | + // プロパティ設定のテスト | |
124 | + // ====================================================================== | |
125 | + /** | |
126 | + * プロパティの設定が有効かどうかをテストします。 | |
127 | + * <p> | |
128 | + * @throws Exception テストに失敗した場合 | |
129 | + */ | |
130 | + @Test | |
131 | + public void testプロパティの設定() throws Exception{ | |
132 | +// HTMLParserFactory factory = new HTMLParserFactory(); | |
133 | +// SAXParser parser = factory.newSAXParser(); | |
134 | +// | |
135 | +// // デフォルト値を参照できることを確認 | |
136 | +// assertNull(parser.getProperty(Feature.PROPERTY_PREFIX + "lexical-handler")); | |
137 | +// | |
138 | +// // 設定値が参照できることを確認 | |
139 | +// LexicalHandler lh = new DefaultHandler2(); | |
140 | +// parser.setProperty(Feature.PROPERTY_PREFIX + "lexical-handler", lh); | |
141 | +// assertTrue(parser.getProperty(Feature.PROPERTY_PREFIX + "lexical-handler") == lh); | |
142 | +// | |
143 | +// 未定義プロパティの参照に対して例外が発生することを確認 | |
144 | +// try{ | |
145 | +// parser.getProperty("foo"); | |
146 | +// fail(); | |
147 | +// } catch(SAXNotRecognizedException ex){/* */} | |
148 | + | |
149 | + // 未定義プロパティの設定に対して例外が発生することを確認 | |
150 | +// try{ | |
151 | +// parser.setProperty("foo", "bar"); | |
152 | +// fail(); | |
153 | +// } catch(SAXNotRecognizedException ex){/* */} | |
154 | + | |
155 | + return; | |
156 | + } | |
157 | + | |
158 | + // ====================================================================== | |
159 | + // プロパティ設定のテスト | |
160 | + // ====================================================================== | |
161 | + /** | |
162 | + * プロパティの設定が有効かどうかをテストします。 | |
163 | + * <p> | |
164 | + * @throws Exception テストに失敗した場合 | |
165 | + */ | |
166 | + @Test | |
167 | + public void testXMLパーサ参照() throws Exception{ | |
168 | + HTMLParserFactory factory = new HTMLParserFactory(); | |
169 | + | |
170 | + SAXParser parser = factory.newSAXParser(); | |
171 | + parser.getXMLReader(); | |
172 | + parser.getParser(); | |
173 | + return; | |
174 | + } | |
175 | + | |
176 | +} |
@@ -0,0 +1,43 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2009 koiroha.org All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * takami torao <torao@mars.dti.ne.jp> | |
8 | + * http://www.koiroha.org | |
9 | + * $Id: HTMLDocumentBuilderFactoryTest.java,v 1.1 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import org.junit.Test; | |
14 | + | |
15 | + | |
16 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
17 | +// HTMLDocumentBuilderFactoryTest: | |
18 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
19 | +/** | |
20 | + * | |
21 | + * <p> | |
22 | + * @version $Revision: 1.1 $ $Date: 2010/02/24 13:59:38 $ | |
23 | + * @author torao | |
24 | + * @since 2010/02/24 Java SE 6 | |
25 | + */ | |
26 | +public class HTMLDocumentBuilderFactoryTest { | |
27 | + | |
28 | + // ====================================================================== | |
29 | + // コンストラクタ | |
30 | + // ====================================================================== | |
31 | + /** | |
32 | + * コンストラクタは何も行いません。 | |
33 | + * <p> | |
34 | + */ | |
35 | + @Test | |
36 | + public void testAttribute(){ | |
37 | +// HTMLDocumentBuilderFactory f = new HTMLDocumentBuilderFactory(); | |
38 | +// f.setAttribute("foo", "bar"); | |
39 | +// assertEquals("bar", f.getAttribute("foo")); | |
40 | + return; | |
41 | + } | |
42 | + | |
43 | +} |
@@ -0,0 +1,96 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: LFReaderTest.java,v 1.2 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import java.io.*; | |
16 | + | |
17 | +import org.junit.Test; | |
18 | + | |
19 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
20 | +// LFReaderTest: LFReader テストケース | |
21 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
22 | +/** | |
23 | + * LFReader のテストケースです。 | |
24 | + * <p> | |
25 | + * @version $Revision: 1.2 $ $Date: 2010/02/24 13:59:38 $ | |
26 | + * @author torao | |
27 | + * @since 2009/04/09 Java2 SE 5.0 | |
28 | + */ | |
29 | +public class LFReaderTest { | |
30 | + | |
31 | + // ====================================================================== | |
32 | + // テストの実行 | |
33 | + // ====================================================================== | |
34 | + /** | |
35 | + * ストリームから読み出される全ての CR, CRLF が LF に変換されていることを確認します。 | |
36 | + * <p> | |
37 | + * @throws IOException テストに失敗した場合 | |
38 | + */ | |
39 | + @Test | |
40 | + public void test() throws IOException { | |
41 | + String sample = "\r\n \n \r \r"; | |
42 | + String expected = "\n \n \n \n"; | |
43 | + | |
44 | + // 1文字読み込みのテスト | |
45 | + Reader r = new LFReader(new StringReader(sample)); | |
46 | + for(int i=0; i<expected.length(); i++){ | |
47 | + assertEquals((int)expected.charAt(i), r.read()); | |
48 | + } | |
49 | + assertTrue(r.read() < 0); | |
50 | + | |
51 | + // バッファ読み込みのテスト | |
52 | + r = new LFReader(new StringReader(sample)); | |
53 | + assertEquals(expected, read(r)); | |
54 | + | |
55 | + // バッファ読み込みテスト | |
56 | + r = new StringReader(sample){ | |
57 | + @Override | |
58 | + public int read(char[] buf, int off, int len) throws IOException{ | |
59 | + int ch = super.read(); | |
60 | + if(ch < 0){ | |
61 | + return ch; | |
62 | + } | |
63 | + buf[off] = (char)ch; | |
64 | + return 1; | |
65 | + } | |
66 | + }; | |
67 | + r = new LFReader(r); | |
68 | + assertEquals(expected, read(r)); | |
69 | + | |
70 | + return; | |
71 | + } | |
72 | + | |
73 | + // ====================================================================== | |
74 | + // 文字列の読み込み | |
75 | + // ====================================================================== | |
76 | + /** | |
77 | + * 指定されたストリームから文字列を読み込みます。 | |
78 | + * <p> | |
79 | + * @param in 入力ストリーム | |
80 | + * @return ストリームから読み込んだ文字列 | |
81 | + * @throws IOException 読み込みに失敗した場合 | |
82 | + */ | |
83 | + private String read(Reader in) throws IOException{ | |
84 | + StringBuilder buffer = new StringBuilder(); | |
85 | + char[] buf = new char[1024]; | |
86 | + while(true){ | |
87 | + int len = in.read(buf); | |
88 | + if(len < 0){ | |
89 | + break; | |
90 | + } | |
91 | + buffer.append(buf, 0, len); | |
92 | + } | |
93 | + return buffer.toString(); | |
94 | + } | |
95 | + | |
96 | +} |
@@ -0,0 +1,64 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: PositionTest.java,v 1.2 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import org.junit.Test; | |
16 | +import org.koiroha.xml.TestCase; | |
17 | + | |
18 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
19 | +// PositionTest: Position クラステスト | |
20 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
21 | +/** | |
22 | + * Position クラスをテストします。 | |
23 | + * <p> | |
24 | + * @version $Revision: 1.2 $ $Date: 2010/02/24 13:59:38 $ | |
25 | + * @author torao | |
26 | + * @since 2009/04/09 Java2 SE 5.0 | |
27 | + */ | |
28 | +public class PositionTest extends TestCase{ | |
29 | + | |
30 | + // ====================================================================== | |
31 | + // 位置クラスのテスト | |
32 | + // ====================================================================== | |
33 | + /** | |
34 | + * 位置クラスをテストします。 | |
35 | + * <p> | |
36 | + */ | |
37 | + @Test | |
38 | + public void test(){ | |
39 | + Position p = new Position(1, 1); | |
40 | + Position p0 = new Position(1, 1); | |
41 | + Position p1 = new Position(1, 2); | |
42 | + Position p2 = new Position(2, 1); | |
43 | + Position p3 = new Position(2, 2); | |
44 | + | |
45 | + assertTrue(p.hashCode() == p0.hashCode()); | |
46 | + assertTrue(p.equals(p0)); | |
47 | + assertTrue(p0.equals(p)); | |
48 | + assertFalse(p.equals(null)); | |
49 | + assertFalse(p.equals("boo")); | |
50 | + | |
51 | + assertTrue(p0.compareTo(p) == 0); | |
52 | + assertTrue(p.compareTo(p0) == 0); | |
53 | + assertTrue(p0.compareTo(p1) < 0); | |
54 | + assertTrue(p1.compareTo(p0) > 0); | |
55 | + assertTrue(p1.compareTo(p2) < 0); | |
56 | + assertTrue(p2.compareTo(p1) > 0); | |
57 | + assertTrue(p2.compareTo(p3) < 0); | |
58 | + assertTrue(p3.compareTo(p2) > 0); | |
59 | + | |
60 | + assertEquals("(1,1)", p.toString()); | |
61 | + return; | |
62 | + } | |
63 | + | |
64 | +} |
@@ -0,0 +1,139 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2009 koiroha.org All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * takami torao <torao@mars.dti.ne.jp> | |
8 | + * http://www.koiroha.org | |
9 | + * $Id: LooseXMLReaderTest.java,v 1.1 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import static junit.framework.Assert.*; | |
14 | + | |
15 | +import java.lang.reflect.Method; | |
16 | +import java.util.*; | |
17 | + | |
18 | +import org.junit.Test; | |
19 | + | |
20 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
21 | +// LooseXMLReaderTest: | |
22 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
23 | +/** | |
24 | + * | |
25 | + * <p> | |
26 | + * @version $Revision: 1.1 $ $Date: 2010/02/24 13:59:38 $ | |
27 | + * @author torao | |
28 | + * @since 2010/02/24 Java2 SE 5.0 | |
29 | + */ | |
30 | +public class LooseXMLReaderTest { | |
31 | + | |
32 | + /** テスト用ルーズ XML リーダー */ | |
33 | + private LooseXMLReader reader = new LooseXMLReader(); | |
34 | + | |
35 | + // ====================================================================== | |
36 | + // 属性値解析のテスト | |
37 | + // ====================================================================== | |
38 | + /** | |
39 | + * 属性値の解析をテストします。 | |
40 | + * <p> | |
41 | + * @throws Exception テストに失敗した場合 | |
42 | + */ | |
43 | + @Test | |
44 | + public void testParseElementBody() throws Exception{ | |
45 | + Method method = LooseXMLReader.class.getDeclaredMethod("parseElementBody", String.class, boolean.class, Map.class); | |
46 | + method.setAccessible(true); | |
47 | + | |
48 | + // 要素内が空の場合 | |
49 | + Map<String,String> attr = new HashMap<String, String>(); | |
50 | + boolean empty = (Boolean)method.invoke(reader, "", true, attr); | |
51 | + assertFalse(empty); | |
52 | + assertEquals(0, attr.size()); | |
53 | + | |
54 | + // 要素内が空の場合 | |
55 | + attr.clear(); | |
56 | + empty = (Boolean)method.invoke(reader, " \n \t \n ", true, attr); | |
57 | + assertFalse(empty); | |
58 | + assertEquals(0, attr.size()); | |
59 | + | |
60 | + // 空要素の検出 | |
61 | + attr.clear(); | |
62 | + empty = (Boolean)method.invoke(reader, "/", true, attr); | |
63 | + assertTrue(empty); | |
64 | + assertEquals(0, attr.size()); | |
65 | + | |
66 | + // 空要素の検出 (前方に空白あり) | |
67 | + attr.clear(); | |
68 | + empty = (Boolean)method.invoke(reader, " /", true, attr); | |
69 | + assertTrue(empty); | |
70 | + assertEquals(0, attr.size()); | |
71 | + | |
72 | + // 空要素の検出 (属性値の後ろ) | |
73 | + attr.clear(); | |
74 | + empty = (Boolean)method.invoke(reader, "a=\"b\" c=\"d\"/", true, attr); | |
75 | + assertTrue(empty); | |
76 | + assertEquals(attr.toString(), 2, attr.size()); | |
77 | + assertEquals("b", attr.get("a")); | |
78 | + assertEquals("d", attr.get("c")); | |
79 | + | |
80 | + // 非空要素指示のスラッシュを属性名として認識する事を確認 | |
81 | + attr.clear(); | |
82 | + empty = (Boolean)method.invoke(reader, "/ ", true, attr); | |
83 | + assertFalse(empty); | |
84 | + assertEquals(1, attr.size()); | |
85 | + assertEquals("/", attr.get("/")); | |
86 | + | |
87 | + // クオート省略の属性値がスラッシュで終わっているのを空要素と認識しないことを確認 | |
88 | + attr.clear(); | |
89 | + empty = (Boolean)method.invoke(reader, "path=/foo/", true, attr); | |
90 | + assertFalse(empty); | |
91 | + assertEquals(1, attr.size()); | |
92 | + assertEquals("/foo/", attr.get("path")); | |
93 | + | |
94 | + // ダブルクオートの属性値指定1 | |
95 | + attr.clear(); | |
96 | + empty = (Boolean)method.invoke(reader, "a=\"b\"", true, attr); | |
97 | + assertFalse(empty); | |
98 | + assertEquals(attr.toString(), 1, attr.size()); | |
99 | + assertEquals("b", attr.get("a")); | |
100 | + | |
101 | + // ダブルクオートの属性値指定2 | |
102 | + attr.clear(); | |
103 | + empty = (Boolean)method.invoke(reader, " a = \"b\" ", true, attr); | |
104 | + assertFalse(empty); | |
105 | + assertEquals(attr.toString(), 1, attr.size()); | |
106 | + assertEquals("b", attr.get("a")); | |
107 | + | |
108 | + // シングルクオートの属性値指定1 | |
109 | + attr.clear(); | |
110 | + empty = (Boolean)method.invoke(reader, "a=\'b\'", true, attr); | |
111 | + assertFalse(empty); | |
112 | + assertEquals(attr.toString(), 1, attr.size()); | |
113 | + assertEquals("b", attr.get("a")); | |
114 | + | |
115 | + // シングルクオートの属性値指定2 | |
116 | + attr.clear(); | |
117 | + empty = (Boolean)method.invoke(reader, " a = \'b\' ", true, attr); | |
118 | + assertFalse(empty); | |
119 | + assertEquals(attr.toString(), 1, attr.size()); | |
120 | + assertEquals("b", attr.get("a")); | |
121 | + | |
122 | + // クオート省略の属性値指定1 | |
123 | + attr.clear(); | |
124 | + empty = (Boolean)method.invoke(reader, "a=b", true, attr); | |
125 | + assertFalse(empty); | |
126 | + assertEquals(attr.toString(), 1, attr.size()); | |
127 | + assertEquals("b", attr.get("a")); | |
128 | + | |
129 | + // クオート省略の属性値指定2 | |
130 | + attr.clear(); | |
131 | + empty = (Boolean)method.invoke(reader, " a = b ", true, attr); | |
132 | + assertFalse(empty); | |
133 | + assertEquals(attr.toString(), 1, attr.size()); | |
134 | + assertEquals("b", attr.get("a")); | |
135 | + | |
136 | + return; | |
137 | + } | |
138 | + | |
139 | +} |
@@ -0,0 +1,241 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: ToolkitTest.java,v 1.4 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import java.io.*; | |
16 | +import java.util.*; | |
17 | + | |
18 | +import javax.xml.parsers.*; | |
19 | + | |
20 | +import org.junit.Test; | |
21 | +import org.koiroha.xml.TestCase; | |
22 | +import org.w3c.dom.*; | |
23 | + | |
24 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
25 | +// ToolkitTest: | |
26 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
27 | +/** | |
28 | + * | |
29 | + * <p> | |
30 | + * @version $Revision: 1.4 $ $Date: 2010/02/24 13:59:38 $ | |
31 | + * @author torao | |
32 | + * @since 2009/04/09 Java2 SE 5.0 | |
33 | + */ | |
34 | +public class ToolkitTest extends TestCase{ | |
35 | + | |
36 | + // ====================================================================== | |
37 | + // クラス定義のテスト | |
38 | + // ====================================================================== | |
39 | + /** | |
40 | + * クラス定義をテストします。 | |
41 | + * <p> | |
42 | + */ | |
43 | + @Test | |
44 | + public void testClass(){ | |
45 | + verifyUtilityClass(Toolkit.class); | |
46 | + return; | |
47 | + } | |
48 | + | |
49 | + // ====================================================================== | |
50 | + // 空要素判定のテスト | |
51 | + // ====================================================================== | |
52 | + /** | |
53 | + * 空要素判定をテストします。 | |
54 | + * <p> | |
55 | + */ | |
56 | + @Test | |
57 | + public void testIsEmptyElement() { | |
58 | + assertTrue(Toolkit.isEmptyElement(null, null, "br")); | |
59 | + assertTrue(Toolkit.isEmptyElement(null, null, "BR")); | |
60 | + assertFalse(Toolkit.isEmptyElement(null, null, "table")); | |
61 | + assertFalse(Toolkit.isEmptyElement(null, null, "TaBlE")); | |
62 | + return; | |
63 | + } | |
64 | + | |
65 | + // ====================================================================== | |
66 | + // ストリームクローズのテスト | |
67 | + // ====================================================================== | |
68 | + /** | |
69 | + * ストリームのクローズをテストします。 | |
70 | + * <p> | |
71 | + */ | |
72 | + @Test | |
73 | + public void testClose() { | |
74 | + Toolkit.close(new ByteArrayInputStream(new byte[]{})); | |
75 | + Toolkit.close(new Closeable(){ | |
76 | + public void close() throws IOException { | |
77 | + throw new IOException(); | |
78 | + } | |
79 | + }); | |
80 | + return; | |
81 | + } | |
82 | + | |
83 | + // ====================================================================== | |
84 | + // メッセージのフォーマットのテスト | |
85 | + // ====================================================================== | |
86 | + /** | |
87 | + * メッセージのフォーマットをテストします。 | |
88 | + * <p> | |
89 | + */ | |
90 | + @Test | |
91 | + public void testFormat() { | |
92 | + | |
93 | + // 定義されているメッセージを参照できることを確認 | |
94 | + ResourceBundle res = ResourceBundle.getBundle("org.koiroha.xml.parser.messages"); | |
95 | + String msgid = res.getKeys().nextElement(); | |
96 | + Toolkit.format(msgid, "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"); | |
97 | + | |
98 | + // 未定義のメッセージを参照して例外が発生することを確認 | |
99 | + try{ | |
100 | + Toolkit.format("unknown"); | |
101 | + fail(); | |
102 | + } catch(MissingResourceException ex){/* */} | |
103 | + | |
104 | + return; | |
105 | + } | |
106 | + | |
107 | + // ====================================================================== | |
108 | + // ストリーム開始判定のテスト | |
109 | + // ====================================================================== | |
110 | + /** | |
111 | + * ストリーム開始判定をテストします。 | |
112 | + * <p> | |
113 | + * @throws IOException テストに失敗した場合 | |
114 | + */ | |
115 | + @Test | |
116 | + public void testStreamStartsWith() throws IOException{ | |
117 | + String sample = "ABC"; | |
118 | + PushbackReader in = new PushbackReader(new StringReader(sample), 10); | |
119 | + | |
120 | + assertTrue(Toolkit.streamStartsWith(in, "ABC", false)); | |
121 | + assertFalse(Toolkit.streamStartsWith(in, "abc", false)); | |
122 | + assertFalse(Toolkit.streamStartsWith(in, "ABCD", false)); | |
123 | + assertFalse(Toolkit.streamStartsWith(in, "ABC\uFFFE", false)); | |
124 | + assertFalse(Toolkit.streamStartsWith(in, "AB\uFFFF", false)); | |
125 | + assertTrue(Toolkit.streamStartsWith(in, "ABC\uFFFF", false)); | |
126 | + assertFalse(Toolkit.streamStartsWith(in, "abc\uFFFF", false)); | |
127 | + return; | |
128 | + } | |
129 | + | |
130 | + // ====================================================================== | |
131 | + // 文字パターン一致判定のテスト | |
132 | + // ====================================================================== | |
133 | + /** | |
134 | + * 文字パターン一致判定をテストします。 | |
135 | + * <p> | |
136 | + */ | |
137 | + @Test | |
138 | + public void testMatches() { | |
139 | + assertTrue(Toolkit.matches('A', 'A', false)); | |
140 | + assertFalse(Toolkit.matches('A', 'a', false)); | |
141 | + assertFalse(Toolkit.matches('A', 'b', false)); | |
142 | + assertTrue(Toolkit.matches('A', 'A', true)); | |
143 | + assertTrue(Toolkit.matches('A', 'a', true)); | |
144 | + assertFalse(Toolkit.matches('A', 'b', true)); | |
145 | + assertTrue(Toolkit.matches('\0', ' ', false)); | |
146 | + assertTrue(Toolkit.matches('\0', '\t', false)); | |
147 | + assertTrue(Toolkit.matches('\0', '\r', false)); | |
148 | + assertTrue(Toolkit.matches('\0', '\n', false)); | |
149 | + assertFalse(Toolkit.matches('\0', 'A', false)); | |
150 | + assertFalse(Toolkit.matches('\0', -1, false)); | |
151 | + assertFalse(Toolkit.matches('\uFFFF', 'A', false)); | |
152 | + assertFalse(Toolkit.matches('\uFFFF', ' ', false)); | |
153 | + assertTrue(Toolkit.matches('\uFFFF', -1, false)); | |
154 | + assertTrue(Toolkit.matches('\uFFFE', 'A', false)); | |
155 | + assertTrue(Toolkit.matches('\uFFFE', ' ', false)); | |
156 | + assertFalse(Toolkit.matches('\uFFFE', -1, false)); | |
157 | + return; | |
158 | + } | |
159 | + | |
160 | + // ====================================================================== | |
161 | + // 親要素判定のテスト | |
162 | + // ====================================================================== | |
163 | + /** | |
164 | + * 親要素判定をテストします。 | |
165 | + * <p> | |
166 | + * @throws ParserConfigurationException テストに失敗した場合 | |
167 | + */ | |
168 | + @Test | |
169 | + public void testGetPreferredParent() throws ParserConfigurationException{ | |
170 | + | |
171 | + // 名前空間無効でテスト | |
172 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
173 | + factory.setNamespaceAware(false); | |
174 | + DocumentBuilder builder = factory.newDocumentBuilder(); | |
175 | + Document doc = builder.newDocument(); | |
176 | + Element html = doc.createElement("html"); | |
177 | + Element body = doc.createElement("BODY"); | |
178 | + Element ul = doc.createElement("UL"); | |
179 | + Element big = doc.createElement("x:big"); | |
180 | + Element li = doc.createElement("LI"); | |
181 | + Element p = doc.createElement("P"); | |
182 | + Element p2 = doc.createElement("P"); | |
183 | + Element small = doc.createElement("SMALL"); | |
184 | + doc.appendChild(html); | |
185 | + html.appendChild(body); | |
186 | + html.appendChild(ul); | |
187 | + html.appendChild(p); | |
188 | + ul.appendChild(big); | |
189 | + assertTrue(Toolkit.getPreferredParent(big, li) == ul); | |
190 | + assertTrue(Toolkit.getPreferredParent(p, p2) == html); | |
191 | + assertTrue(Toolkit.getPreferredParent(p, small) == p); | |
192 | + | |
193 | + doc.removeChild(html); | |
194 | + doc.appendChild(small); | |
195 | + assertTrue(Toolkit.getPreferredParent(small, p) == small); | |
196 | + | |
197 | + // 名前空間と接頭辞付きでテスト | |
198 | + String ns = "http://www.w3.org/1999/xhtml"; | |
199 | + factory.setNamespaceAware(true); | |
200 | + builder = factory.newDocumentBuilder(); | |
201 | + doc = builder.newDocument(); | |
202 | + html = doc.createElementNS(ns, "x:html"); | |
203 | + body = doc.createElementNS(ns, "x:BODY"); | |
204 | + ul = doc.createElementNS(ns, "x:UL"); | |
205 | + big = doc.createElementNS(ns, "x:big"); | |
206 | + li = doc.createElementNS(ns, "x:LI"); | |
207 | + p = doc.createElementNS(ns, "x:P"); | |
208 | + p2 = doc.createElementNS(ns, "x:P"); | |
209 | + small = doc.createElementNS(ns, "x:SMALL"); | |
210 | + doc.appendChild(html); | |
211 | + html.appendChild(body); | |
212 | + html.appendChild(ul); | |
213 | + html.appendChild(p); | |
214 | + ul.appendChild(big); | |
215 | + assertTrue(Toolkit.getPreferredParent(big, li) == ul); | |
216 | + assertTrue(Toolkit.getPreferredParent(p, p2) == html); | |
217 | + assertTrue(Toolkit.getPreferredParent(p, small) == p); | |
218 | + | |
219 | + return; | |
220 | + } | |
221 | + | |
222 | + // ====================================================================== | |
223 | + // 属性解析のテスト | |
224 | + // ====================================================================== | |
225 | + /** | |
226 | + * 属性解析をテストします。 | |
227 | + * <p> | |
228 | + */ | |
229 | + @Test | |
230 | + public void testParseAttributesSimply() { | |
231 | + Map<String,String> attr = Toolkit.parseAttributesSimply("a=b c=\"d\" e = \'f\' g=\""\" foo"); | |
232 | + assertEquals(5, attr.size()); | |
233 | + assertEquals("b", attr.get("a")); | |
234 | + assertEquals("d", attr.get("c")); | |
235 | + assertEquals("f", attr.get("e")); | |
236 | + assertEquals("\"", attr.get("g")); | |
237 | + assertEquals("foo", attr.get("foo")); | |
238 | + return; | |
239 | + } | |
240 | + | |
241 | +} |
@@ -0,0 +1,552 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: SpecPatternTest.java,v 1.5 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import java.io.*; | |
16 | +import java.nio.charset.Charset; | |
17 | +import java.util.*; | |
18 | + | |
19 | +import javax.xml.parsers.*; | |
20 | +import javax.xml.transform.*; | |
21 | +import javax.xml.transform.dom.DOMSource; | |
22 | +import javax.xml.transform.stream.StreamResult; | |
23 | +import javax.xml.xpath.*; | |
24 | + | |
25 | +import org.junit.*; | |
26 | +import org.koiroha.xml.TestCase; | |
27 | +import org.w3c.dom.*; | |
28 | +import org.xml.sax.*; | |
29 | +import org.xml.sax.ext.DefaultHandler2; | |
30 | + | |
31 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
32 | +// SpecPatternTest: データパターンテスト | |
33 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
34 | +/** | |
35 | + * | |
36 | + * <p> | |
37 | + * @version $Revision: 1.5 $ $Date: 2010/02/24 13:59:38 $ | |
38 | + * @author torao | |
39 | + * @since 2009/04/01 Java2 SE 5.0 | |
40 | + */ | |
41 | +public class SpecPatternTest extends TestCase{ | |
42 | + | |
43 | + // ====================================================================== | |
44 | + // ログ出力先 | |
45 | + // ====================================================================== | |
46 | + /** | |
47 | + * このクラスのログ出力先です。 | |
48 | + * <p> | |
49 | + */ | |
50 | + private static final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(SpecPatternTest.class.getName()); | |
51 | + | |
52 | + // ====================================================================== | |
53 | + // テスト結果の出力先 | |
54 | + // ====================================================================== | |
55 | + /** | |
56 | + * テスト結果の出力先ディレクトリです。 | |
57 | + * <p> | |
58 | + */ | |
59 | + private static final File dir = new File("./build/doc/"); | |
60 | + | |
61 | + // ====================================================================== | |
62 | + // XML 出力先 | |
63 | + // ====================================================================== | |
64 | + /** | |
65 | + * テスト結果の出力先です。 | |
66 | + * <p> | |
67 | + */ | |
68 | + private static XMLStreamWriter out = null; | |
69 | + | |
70 | + // ====================================================================== | |
71 | + // 仕様ドキュメント | |
72 | + // ====================================================================== | |
73 | + /** | |
74 | + * 仕様のドキュメントです。 | |
75 | + * <p> | |
76 | + */ | |
77 | + private static Document specification = null; | |
78 | + | |
79 | + // ====================================================================== | |
80 | + // XML 出力先 | |
81 | + // ====================================================================== | |
82 | + /** | |
83 | + * テスト結果の出力先です。 | |
84 | + * <p> | |
85 | + */ | |
86 | + private static int line = 0; | |
87 | + | |
88 | + /** | |
89 | + * 初期化処理 | |
90 | + * @throws IOException */ | |
91 | + @BeforeClass | |
92 | + public static void prepare() throws IOException{ | |
93 | + dir.mkdirs(); | |
94 | + | |
95 | + // ストリームのオープン | |
96 | + String xmlns = "http://www.w3.org/1999/xhtml"; | |
97 | + File file = new File(dir, "specification.xhtml"); | |
98 | + out = new XMLStreamWriter(new FileWriter(file)); | |
99 | + out.setDefaultNamespace(xmlns); | |
100 | + | |
101 | + // | |
102 | + out.writeStartDocument(Charset.defaultCharset().name(), "1.0"); | |
103 | + out.writeCharacters("\n"); | |
104 | + out.writeDTD("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"); | |
105 | + out.writeCharacters("\n"); | |
106 | + out.writeStartElement("html"); | |
107 | + out.writeAttribute("xmlns", xmlns); | |
108 | + out.writeCharacters("\n"); | |
109 | + out.writeStartElement("head"); | |
110 | + out.writeCharacters("\n"); | |
111 | + out.writeStartElement("title"); | |
112 | + out.writeCharacters("HTML Parser Specification"); | |
113 | + out.writeEndElement(); | |
114 | + out.writeCharacters("\n"); | |
115 | + out.writeStartElement("style"); | |
116 | + out.writeAttribute("type", "text/css"); | |
117 | + out.writeCharacters("\n"); | |
118 | + out.writeCharacters("\t.ctrl { width:8px; height:8px; }\n"); | |
119 | + out.writeCharacters("\t.tab { width:32px; height:8px; }\n"); | |
120 | + out.writeCharacters("\t.smpl { font-size:11px; vertical-align:top; background-color:#F4F4F4; }\n"); | |
121 | + out.writeCharacters("\t.titl { font-size:12px; font-family:sans-serif; padding-top:8px; border-bottom:solid 1px black; }\n"); | |
122 | + out.writeCharacters("\t.desc { font-size:10px; }\n"); | |
123 | + out.writeCharacters("\t.err { font-family:sans-serif; color:red; }\n"); | |
124 | + out.writeEndElement(); | |
125 | + out.writeCharacters("\n"); | |
126 | + out.writeEndElement(); | |
127 | + out.writeCharacters("\n"); | |
128 | + out.writeStartElement("body"); | |
129 | + out.writeCharacters("\n"); | |
130 | + return; | |
131 | + } | |
132 | + | |
133 | + /** | |
134 | + * 終了処理 | |
135 | + * @throws IOException */ | |
136 | + @AfterClass | |
137 | + public static void cleanup() throws IOException{ | |
138 | + if(out != null){ | |
139 | + out.writeEndElement(); | |
140 | + out.writeCharacters("\n"); | |
141 | + out.writeEndElement(); | |
142 | + out.writeEndDocument(); | |
143 | + out.close(); | |
144 | + } | |
145 | + return; | |
146 | + } | |
147 | + | |
148 | + // ====================================================================== | |
149 | + // コンストラクタ | |
150 | + // ====================================================================== | |
151 | + /** | |
152 | + * コンストラクタは何も行いません。 | |
153 | + * <p> | |
154 | + * @throws Exception 初期化に失敗した場合 | |
155 | + */ | |
156 | + @BeforeClass | |
157 | + public static void init() throws Exception{ | |
158 | + | |
159 | + // 仕様のドキュメントを読み込み | |
160 | + File file = new File("build/spec/spec.xml"); | |
161 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
162 | + factory.setNamespaceAware(true); | |
163 | + factory.setXIncludeAware(true); | |
164 | + specification = factory.newDocumentBuilder().parse(file.toURI().toString()); | |
165 | + | |
166 | + return; | |
167 | + } | |
168 | + | |
169 | + // ====================================================================== | |
170 | + // 仕様検証の実行 | |
171 | + // ====================================================================== | |
172 | + /** | |
173 | + * 仕様のテストパターンを検証します。 | |
174 | + * <p> | |
175 | + * @throws Exception テストに失敗した場合 | |
176 | + */ | |
177 | + @Test | |
178 | + public void test() throws Exception{ | |
179 | + testSection("prologue"); | |
180 | + testSection("element"); | |
181 | + testSection("text"); | |
182 | + testSection("entity-reference"); | |
183 | + testSection("cdata"); | |
184 | + testSection("comment"); | |
185 | + testSection("pi"); | |
186 | + testSection("dtd"); | |
187 | + testSection("structure"); | |
188 | + return; | |
189 | + } | |
190 | + | |
191 | + // ====================================================================== | |
192 | + // セクションのテスト | |
193 | + // ====================================================================== | |
194 | + /** | |
195 | + * 指定された ID を持つセクションの仕様をテストします。 | |
196 | + * <p> | |
197 | + * @param id テストするセクションの ID | |
198 | + * @throws Exception テストに失敗した場合 | |
199 | + */ | |
200 | + private void testSection(String id) throws Exception{ | |
201 | + XPathFactory factory = XPathFactory.newInstance(); | |
202 | + XPath xpath = factory.newXPath(); | |
203 | + Element section = (Element)xpath.evaluate("/html-parse-spec/section[@id='" + id + "']", specification, XPathConstants.NODE); | |
204 | + assert(section != null): id; | |
205 | + String title = xpath.evaluate("title", section); | |
206 | + writeTag("h2", title); | |
207 | + writeStartConvertPattern(); | |
208 | + NodeList specs = (NodeList)xpath.evaluate("spec", section, XPathConstants.NODESET); | |
209 | + for(int j=0; j<specs.getLength(); j++){ | |
210 | + Element spec = (Element)specs.item(j); | |
211 | + evaluate(spec, xpath); | |
212 | + } | |
213 | + writeEndConvertPattern(); | |
214 | + return; | |
215 | + } | |
216 | + | |
217 | + // ====================================================================== | |
218 | + // 仕様検証の実行 | |
219 | + // ====================================================================== | |
220 | + /** | |
221 | + * 仕様のテストパターンを検証します。 | |
222 | + * <p> | |
223 | + * @param spec 検証する仕様 | |
224 | + * @param xpath XPath | |
225 | + * @throws Exception テストに失敗した場合 | |
226 | + */ | |
227 | + private void evaluate(Element spec, XPath xpath) throws Exception{ | |
228 | + String title = xpath.evaluate("title", spec); | |
229 | + String desc = xpath.evaluate("desc", spec); | |
230 | + String errcd = xpath.evaluate("error", spec); | |
231 | + String sample = xpath.evaluate("sample", spec); | |
232 | + String expected = xpath.evaluate("expected", spec); | |
233 | + | |
234 | + Set<String> err = new HashSet<String>(); | |
235 | + StringTokenizer tk = new StringTokenizer(errcd, ", \t\r\n"); | |
236 | + while(tk.hasMoreTokens()){ | |
237 | + err.add(tk.nextToken()); | |
238 | + } | |
239 | + | |
240 | + logger.fine("-------------------------------------------------"); | |
241 | + List<Warning> errmsg = new ArrayList<Warning>(); | |
242 | + expected = getText(parse(expected, false, null)); | |
243 | + String actual = getText(parse(sample, true, errmsg)); | |
244 | + | |
245 | + // 変換パターンのレポート | |
246 | + writeConvert(errmsg, sample, actual, title, desc); | |
247 | + | |
248 | + // 解析後に再構築したXML文字列と想定するXML文字列を比較 | |
249 | + logger.fine("ORIGINAL HTML : " + sample.trim()); | |
250 | + logger.fine("RECOGNIZED XML: " + actual.trim()); | |
251 | + if(! actual.equals(expected)){ | |
252 | + logger.fine("EXPECTED XML : " + expected.trim()); | |
253 | + } | |
254 | + assertEquals(expected, actual); | |
255 | + | |
256 | + // 警告が発生し得ない場合 | |
257 | + if(err.size() == 0){ | |
258 | + assertEquals("予期しない警告の発生: " + errmsg.toString(), 0, errmsg.size()); | |
259 | + | |
260 | + // 警告想定がある場合は想定したメッセージかを判定 | |
261 | + } else { | |
262 | + Set<String> notYet = new HashSet<String>(err); | |
263 | + for(Warning w: errmsg){ | |
264 | + assertTrue("想定外の警告が発生: " + w, err.contains(w.code)); | |
265 | + notYet.remove(w.code); | |
266 | + } | |
267 | + assertEquals("予期する警告の未通知: " + notYet.toString(), 0, notYet.size()); | |
268 | + } | |
269 | + return; | |
270 | + } | |
271 | + | |
272 | + // ====================================================================== | |
273 | + // 文字列の解析 | |
274 | + // ====================================================================== | |
275 | + /** | |
276 | + * 指定された文字列を解析して DOM を構築します。 | |
277 | + * <p> | |
278 | + * @param xml 解析する文字列 | |
279 | + * @param html HTML として解析する場合 true | |
280 | + * @param msg 警告メッセージ格納用リスト | |
281 | + * @return 解析したドキュメント | |
282 | + */ | |
283 | + private static Document parse(String xml, boolean html, List<Warning> msg){ | |
284 | + final List<Warning> warning = (msg==null)? new ArrayList<Warning>(): msg; | |
285 | + try{ | |
286 | + InputSource is = new InputSource(new StringReader(xml)); | |
287 | + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); | |
288 | + if(html){ | |
289 | + HTMLDocumentBuilderFactory f = new HTMLDocumentBuilderFactory(); | |
290 | + f.setHtmlOptimize(true); | |
291 | + f.setLowerCaseName(true); | |
292 | + factory = f; | |
293 | + } | |
294 | + DocumentBuilder builder = factory.newDocumentBuilder(); | |
295 | + builder.setErrorHandler(new DefaultHandler2(){ | |
296 | + /** @param e */ | |
297 | + @Override | |
298 | + public void warning(SAXParseException e) throws SAXException { | |
299 | + logger.warning("[" + e.getLineNumber() + ":" + e.getColumnNumber() + "] " + e.getMessage()); | |
300 | + Warning w = new Warning(); | |
301 | + String msg = e.getMessage(); | |
302 | + w.code = msg.substring(0, msg.indexOf(':')); | |
303 | + w.msg = msg.substring(msg.indexOf(':') + 1); | |
304 | + w.line = e.getLineNumber(); | |
305 | + w.col = e.getColumnNumber(); | |
306 | + warning.add(w); | |
307 | + return; | |
308 | + } | |
309 | + }); | |
310 | + return builder.parse(is); | |
311 | + } catch(Exception ex){ | |
312 | + throw new RuntimeException(ex); | |
313 | + } | |
314 | + } | |
315 | + | |
316 | + // ====================================================================== | |
317 | + // DOM の文字列化 | |
318 | + // ====================================================================== | |
319 | + /** | |
320 | + * 指定された DOM を XML の文字列に変換します。 | |
321 | + * <p> | |
322 | + * @param doc ドキュメント | |
323 | + * @return XML 文字列 | |
324 | + */ | |
325 | + private static String getText(Document doc){ | |
326 | + StringWriter sw = new StringWriter(); | |
327 | + try{ | |
328 | + TransformerFactory factory = TransformerFactory.newInstance(); | |
329 | + Transformer transformer = factory.newTransformer(); | |
330 | + transformer.setOutputProperty("method", "xml"); | |
331 | + transformer.setOutputProperty("omit-xml-declaration", "yes"); | |
332 | + transformer.transform(new DOMSource(doc), new StreamResult(sw)); | |
333 | + } catch(Exception ex){ | |
334 | + throw new RuntimeException(ex); | |
335 | + } | |
336 | + return sw.toString(); | |
337 | + } | |
338 | + | |
339 | + /** エラーメッセージ */ | |
340 | + public static class Warning{ | |
341 | + /** エラーコード */ | |
342 | + public String code = null; | |
343 | + /** エラーメッセージ */ | |
344 | + public String msg = null; | |
345 | + /** 行番号 */ | |
346 | + public int line = 0; | |
347 | + /** 桁番号 */ | |
348 | + public int col = 0; | |
349 | + /** 文字列化 */ | |
350 | + @Override | |
351 | + public String toString(){ | |
352 | + return String.format("%2d,%2d: %s:%s", line, col, code, msg); | |
353 | + } | |
354 | + } | |
355 | + | |
356 | + // ====================================================================== | |
357 | + // ブロックタグの出力 | |
358 | + // ====================================================================== | |
359 | + /** | |
360 | + * ブロック要素を出力します。 | |
361 | + * <p> | |
362 | + * @param tag 要素のタグ名 | |
363 | + * @param content 要素の内容 | |
364 | + */ | |
365 | + private static void writeTag(String tag, String content){ | |
366 | + out.writeStartElement(tag); | |
367 | + out.writeCharacters(content); | |
368 | + out.writeEndElement(); | |
369 | + out.writeCharacters("\n"); | |
370 | + return; | |
371 | + } | |
372 | + | |
373 | + // ====================================================================== | |
374 | + // セクションの開始 | |
375 | + // ====================================================================== | |
376 | + /** | |
377 | + * セクションを開始します。 | |
378 | + * <p> | |
379 | + */ | |
380 | + private static void writeStartConvertPattern(){ | |
381 | + line = 0; | |
382 | + out.writeStartElement("table"); | |
383 | + out.writeCharacters("\n"); | |
384 | + return; | |
385 | + } | |
386 | + | |
387 | + // ====================================================================== | |
388 | + // セクションの開始 | |
389 | + // ====================================================================== | |
390 | + /** | |
391 | + * セクションを開始します。 | |
392 | + * <p> | |
393 | + */ | |
394 | + private static void writeEndConvertPattern(){ | |
395 | + out.writeEndElement(); | |
396 | + out.writeCharacters("\n"); | |
397 | + return; | |
398 | + } | |
399 | + | |
400 | + // ====================================================================== | |
401 | + // 変換結果の出力 | |
402 | + // ====================================================================== | |
403 | + /** | |
404 | + * 指定された変換パターンを出力します。 | |
405 | + * <p> | |
406 | + * @param errmsg エラーメッセージ | |
407 | + * @param before 変換前の文字列 | |
408 | + * @param after 変換後の文字列 | |
409 | + * @param title この変換の説明 | |
410 | + * @param desc この結果に関する説明 | |
411 | + */ | |
412 | + private static void writeConvert(List<Warning> errmsg, String before, String after, String title, String desc){ | |
413 | + boolean strict = (errmsg.size() == 0); | |
414 | + | |
415 | + // タイトルの出力 | |
416 | + out.writeStartElement("tr"); | |
417 | + if(line % 2 == 1){ | |
418 | + out.writeAttribute("class", "odd"); | |
419 | + } | |
420 | + out.writeStartElement("td"); | |
421 | + out.writeAttribute("colspan", "3"); | |
422 | + out.writeAttribute("class", "titl"); | |
423 | + if(strict){ | |
424 | + out.writeCharacters("●"); | |
425 | + } else { | |
426 | + out.writeCharacters("○"); | |
427 | + out.writeStartElement("i"); | |
428 | + } | |
429 | + out.writeCharacters(title); | |
430 | + if(! strict){ | |
431 | + out.writeEndElement(); | |
432 | + } | |
433 | + out.writeEndElement(); | |
434 | + out.writeEndElement(); | |
435 | + out.writeCharacters("\n"); | |
436 | + | |
437 | + // 説明の出力 | |
438 | + out.writeStartElement("tr"); | |
439 | + if(line % 2 == 1){ | |
440 | + out.writeAttribute("class", "odd"); | |
441 | + } | |
442 | + out.writeStartElement("td"); | |
443 | + out.writeAttribute("colspan", "3"); | |
444 | + out.writeAttribute("class", "desc"); | |
445 | + out.writeCharacters(desc); | |
446 | + out.writeEndElement(); | |
447 | + out.writeEndElement(); | |
448 | + out.writeCharacters("\n"); | |
449 | + | |
450 | + // 変換内容の出力 | |
451 | + out.writeStartElement("tr"); | |
452 | + if(line % 2 == 1){ | |
453 | + out.writeAttribute("class", "odd"); | |
454 | + } | |
455 | + out.writeCharacters("\n\t"); | |
456 | + out.writeStartElement("td"); | |
457 | + out.writeAttribute("class", "smpl"); | |
458 | + write(before); | |
459 | + out.writeEndElement(); | |
460 | + out.writeCharacters("\n\t"); | |
461 | + out.writeStartElement("td"); | |
462 | + out.writeAttribute("style", "font-size:10px;vertical-align:top;"); | |
463 | + out.writeCharacters("→"); | |
464 | + out.writeEndElement(); | |
465 | + out.writeCharacters("\n\t"); | |
466 | + out.writeStartElement("td"); | |
467 | + out.writeAttribute("class", "smpl"); | |
468 | + write(after); | |
469 | + out.writeEndElement(); | |
470 | + out.writeCharacters("\n"); | |
471 | + out.writeEndElement(); | |
472 | + out.writeCharacters("\n"); | |
473 | + | |
474 | + // 説明の出力 | |
475 | + if(errmsg.size() > 0){ | |
476 | + out.writeStartElement("tr"); | |
477 | + if(line % 2 == 1){ | |
478 | + out.writeAttribute("class", "odd"); | |
479 | + } | |
480 | + out.writeStartElement("td"); | |
481 | + out.writeAttribute("colspan", "3"); | |
482 | + out.writeAttribute("class", "desc err"); | |
483 | + for(int i=0; i<errmsg.size(); i++){ | |
484 | + if(i != 0){ | |
485 | + out.writeEmptyElement("br"); | |
486 | + } | |
487 | + out.writeCharacters(errmsg.get(i).toString()); | |
488 | + } | |
489 | + out.writeEndElement(); | |
490 | + out.writeEndElement(); | |
491 | + out.writeCharacters("\n"); | |
492 | + } | |
493 | + | |
494 | + line ++; | |
495 | + return; | |
496 | + } | |
497 | + | |
498 | + // ====================================================================== | |
499 | + // サンプルXMLの出力 | |
500 | + // ====================================================================== | |
501 | + /** | |
502 | + * サンプル用のXML文字列を出力します。 | |
503 | + * <p> | |
504 | + * @param text 出力する文字列 | |
505 | + */ | |
506 | + private static void write(String text){ | |
507 | + for(int i=0; i<text.length(); i++){ | |
508 | + char ch = text.charAt(i); | |
509 | + switch(ch){ | |
510 | + case '\r': | |
511 | + out.writeEmptyElement("img"); | |
512 | + out.writeAttribute("src", "ctrl/0d.png"); | |
513 | + out.writeAttribute("alt", "\r"); | |
514 | + out.writeAttribute("title", "CR"); | |
515 | + out.writeAttribute("class", "ctrl"); | |
516 | + break; | |
517 | + case '\n': | |
518 | + out.writeEmptyElement("img"); | |
519 | + out.writeAttribute("src", "ctrl/0a.png"); | |
520 | + out.writeAttribute("alt", ""); | |
521 | + out.writeAttribute("title", "LF"); | |
522 | + out.writeAttribute("class", "ctrl"); | |
523 | + out.writeEmptyElement("br"); | |
524 | + break; | |
525 | + case '\t': | |
526 | + out.writeEmptyElement("img"); | |
527 | + out.writeAttribute("src", "ctrl/0c.png"); | |
528 | + out.writeAttribute("alt", "\t"); | |
529 | + out.writeAttribute("title", "TAB"); | |
530 | + out.writeAttribute("class", "ctrl tab"); | |
531 | + break; | |
532 | + case ' ': | |
533 | + out.writeEmptyElement("img"); | |
534 | + out.writeAttribute("src", "ctrl/20.png"); | |
535 | + out.writeAttribute("alt", " "); | |
536 | + out.writeAttribute("title", "SPACE"); | |
537 | + out.writeAttribute("class", "ctrl"); | |
538 | + break; | |
539 | + default: | |
540 | + out.writeCharacters(String.valueOf(ch)); | |
541 | + break; | |
542 | + } | |
543 | + } | |
544 | + out.writeEmptyElement("img"); | |
545 | + out.writeAttribute("src", "ctrl/eof.png"); | |
546 | + out.writeAttribute("alt", ""); | |
547 | + out.writeAttribute("title", "EOF"); | |
548 | + out.writeAttribute("class", "ctrl"); | |
549 | + return; | |
550 | + } | |
551 | + | |
552 | +} |
@@ -0,0 +1,215 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: HTMLParserFactoryTest.java,v 1.5 2010/07/18 15:30:18 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import static org.junit.Assert.*; | |
14 | + | |
15 | +import java.io.*; | |
16 | +import java.nio.charset.Charset; | |
17 | + | |
18 | +import javax.xml.XMLConstants; | |
19 | +import javax.xml.parsers.*; | |
20 | + | |
21 | +import org.junit.Test; | |
22 | +import org.koiroha.xml.TestCase; | |
23 | +import org.xml.sax.*; | |
24 | + | |
25 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
26 | +// HTMLParserFactoryTest: HTML パーサファクトリテストケース | |
27 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
28 | +/** | |
29 | + * HTML パーサファクトリのテストケースです。 | |
30 | + * <p> | |
31 | + * @version $Revision: 1.5 $ $Date: 2010/07/18 15:30:18 $ | |
32 | + * @author torao | |
33 | + * @since 2009/04/01 Java2 SE 5.0 | |
34 | + */ | |
35 | +public class HTMLParserFactoryTest extends TestCase{ | |
36 | + | |
37 | + // ====================================================================== | |
38 | + // コンストラクタテスト | |
39 | + // ====================================================================== | |
40 | + /** | |
41 | + * コンストラクタのテストを行います。 | |
42 | + * <p> | |
43 | + */ | |
44 | + @Test | |
45 | + public void testコンストラクタ() { | |
46 | + | |
47 | + // new で生成できることを確認 | |
48 | + SAXParserFactory factory = new HTMLParserFactory(); | |
49 | + | |
50 | + // 通常の方法で作成 | |
51 | + /* JSE 6 以降 | |
52 | + SAXParserFactory factory = SAXParserFactory.newInstance( | |
53 | + HTMLParserFactory.class.getName(), HTMLParser.class.getClassLoader()); | |
54 | + assertEquals(HTMLParserFactory.class, factory.getClass()); | |
55 | + */ | |
56 | + | |
57 | + // JAXP のシステムプロパティによる作成 | |
58 | + String oldValue = System.getProperty("javax.xml.parsers.SAXParserFactory"); | |
59 | + System.setProperty("javax.xml.parsers.SAXParserFactory", HTMLParserFactory.class.getName()); | |
60 | + factory = SAXParserFactory.newInstance(); | |
61 | + if(oldValue == null){ | |
62 | + System.getProperties().remove("javax.xml.parsers.SAXParserFactory"); | |
63 | + } else { | |
64 | + System.setProperty("javax.xml.parsers.SAXParserFactory", oldValue); | |
65 | + } | |
66 | + assertEquals(HTMLParserFactory.class, factory.getClass()); | |
67 | + | |
68 | + return; | |
69 | + } | |
70 | + | |
71 | + // ====================================================================== | |
72 | + // 機能設定/参照のテスト | |
73 | + // ====================================================================== | |
74 | + /** | |
75 | + * ファクトリの機能設定と参照をテストします。このテストでは実際にその機能を有効にした場合の | |
76 | + * 挙動のテストまでは行いません。 | |
77 | + * <p> | |
78 | + * @throws Exception テストに失敗した場合 | |
79 | + */ | |
80 | + @Test | |
81 | + public void test機能の設定と参照() throws Exception{ | |
82 | + SAXParserFactory factory = new HTMLParserFactory(); | |
83 | + /* JSE 6 以降 | |
84 | + SAXParserFactory factory = SAXParserFactory.newInstance( | |
85 | + HTMLParserFactory.class.getName(), HTMLParser.class.getClassLoader()); | |
86 | + */ | |
87 | + | |
88 | + // API リファレンスに記述されている規定の機能を使用できることを確認 | |
89 | + assertFalse(factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); | |
90 | + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); | |
91 | + assertTrue(factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); | |
92 | + factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, false); | |
93 | + assertFalse(factory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)); | |
94 | + | |
95 | + return; | |
96 | + } | |
97 | + | |
98 | + // ====================================================================== | |
99 | + // HTML パーサ作成のテスト | |
100 | + // ====================================================================== | |
101 | + /** | |
102 | + * HTML パーサ作成をテストします。 | |
103 | + * <p> | |
104 | + * @throws SAXException | |
105 | + * @throws ParserConfigurationException | |
106 | + */ | |
107 | + @Test | |
108 | + public void testSAXパーサ作成() throws ParserConfigurationException, SAXException { | |
109 | + SAXParserFactory factory = new HTMLParserFactory(); | |
110 | + /* JSE 6 以降 | |
111 | + SAXParserFactory factory = SAXParserFactory.newInstance( | |
112 | + HTMLParserFactory.class.getName(), HTMLParser.class.getClassLoader()); | |
113 | + */ | |
114 | + | |
115 | + factory.newSAXParser(); | |
116 | + return; | |
117 | + } | |
118 | + | |
119 | + // ====================================================================== | |
120 | + // 入力ソース推測のテスト | |
121 | + // ====================================================================== | |
122 | + /** | |
123 | + * 入力ストリームの推測をテストします。 | |
124 | + * <p> | |
125 | + * @throws Exception | |
126 | + */ | |
127 | + @Test | |
128 | + public void 入力ソースの推測() throws Exception{ | |
129 | + | |
130 | + // XML 宣言でエンコードが指定されている場合 | |
131 | + String sample = "<?xml encoding='UTF-8'?>あいうえお"; | |
132 | + guessInputSource(sample, "UTF-8", "JISAutoDetect", "UTF-8"); | |
133 | + guessInputSource(sample, "Windows-31J", "JISAutoDetect", "UTF-8"); | |
134 | + | |
135 | + // XML 宣言が存在しエンコードが省略されている場合 | |
136 | + sample = "<?xml version='1.0'?>あいうえお"; | |
137 | + guessInputSource(sample, "UTF-8", "JISAutoDetect", "UTF-8"); | |
138 | + guessInputSource(sample, "Windows-31J", "JISAutoDetect", "UTF-8"); | |
139 | + | |
140 | + // BOM 付きの Unicode Little (内容による判定は省略される) | |
141 | + sample = "<?xml encoding='UTF-8'?>あいうえお"; | |
142 | + guessInputSource(sample, "UnicodeLittle", "JISAutoDetect", "UTF-16"); | |
143 | + | |
144 | + // BOM 付きの Unicode Big (内容による判定は省略される) | |
145 | + sample = "<?xml encoding='UTF-8'?>あいうえお"; | |
146 | + guessInputSource(sample, "UnicodeBig", "JISAutoDetect", "UTF-16"); | |
147 | + | |
148 | + // META 要素にエンコーディングが指定されている HTML | |
149 | + sample = "<html><meta http-equiv = 'content-type' content = 'text/html; charset = Windows-31J'>あいうえお</html>"; | |
150 | + guessInputSource(sample, "Windows-31J", "UTF-8", "Windows-31J"); | |
151 | + guessInputSource(sample, "UTF-8", "JISAutoDetect", "Windows-31J"); | |
152 | + | |
153 | + // 内容からエンコーディングが推測できない場合 (テキスト) | |
154 | + sample = "あいうえお"; | |
155 | + guessInputSource(sample, "Windows-31J", "JISAutoDetect", "JISAutoDetect"); | |
156 | + guessInputSource(sample, "UTF-8", "JISAutoDetect", "JISAutoDetect"); | |
157 | + | |
158 | + // 内容からエンコーディングが推測できない場合 (HTML) | |
159 | + sample = "<html>あいうえお</html>"; | |
160 | + guessInputSource(sample, "Windows-31J", "JISAutoDetect", "JISAutoDetect"); | |
161 | + guessInputSource(sample, "UTF-8", "JISAutoDetect", "JISAutoDetect"); | |
162 | + | |
163 | + // 内容からエンコーディングが推測できない場合 (HTML) | |
164 | + sample = "<html><meta name='foo' content='bar'>あいうえお</html>"; | |
165 | + guessInputSource(sample, "Windows-31J", "JISAutoDetect", "JISAutoDetect"); | |
166 | + guessInputSource(sample, "UTF-8", "JISAutoDetect", "JISAutoDetect"); | |
167 | + | |
168 | + // 内容からエンコーディングが推測できない場合 (HTML) | |
169 | + sample = "<html><meta http-equiv='foo' content='bar'>あいうえお</html>"; | |
170 | + guessInputSource(sample, "Windows-31J", "JISAutoDetect", "JISAutoDetect"); | |
171 | + guessInputSource(sample, "UTF-8", "JISAutoDetect", "JISAutoDetect"); | |
172 | + | |
173 | + // 内容からエンコーディングが推測できない場合 (指定された文字セット未定義) | |
174 | + sample = "<html>あいうえお</html>"; | |
175 | + try{ | |
176 | + guessInputSource(sample, "Windows-31J", "foo", "JISAutoDetect"); | |
177 | + fail(); | |
178 | + } catch(UnsupportedEncodingException ex){/* */} | |
179 | + | |
180 | + return; | |
181 | + } | |
182 | + | |
183 | + // ====================================================================== | |
184 | + // 入力ソース推測のテスト | |
185 | + // ====================================================================== | |
186 | + /** | |
187 | + * 入力ストリームの推測をテストします。 | |
188 | + * <p> | |
189 | + * @param sample テストする文字列 | |
190 | + * @param actual 実際に適用するエンコーディング | |
191 | + * @param def デフォルトとして指定するエンコーディング | |
192 | + * @param expected 予期するエンコーディング | |
193 | + * @throws Exception | |
194 | + */ | |
195 | + private void guessInputSource(String sample, String actual, String def, String expected) throws Exception{ | |
196 | + | |
197 | + // 予想したエンコーディングが推測されていることを確認 | |
198 | + AbstractSAXParserFactory factory = new HTMLParserFactory(); | |
199 | + InputStream in = new ByteArrayInputStream(sample.getBytes(actual)); | |
200 | + InputSource is = factory.guessInputSource(in, def, 8 * 1024); | |
201 | + assertEquals(Charset.forName(expected), Charset.forName(is.getEncoding())); | |
202 | + | |
203 | + // ストリームからサンプルと同じ内容が取得できることを確認 | |
204 | + StringBuilder buffer = new StringBuilder(); | |
205 | + Reader r = is.getCharacterStream(); | |
206 | + while(true){ | |
207 | + int ch = r.read(); | |
208 | + if(ch < 0) break; | |
209 | + buffer.append((char)ch); | |
210 | + } | |
211 | + assertEquals(new String(sample.getBytes(actual), expected), buffer.toString()); | |
212 | + return; | |
213 | + } | |
214 | + | |
215 | +} |
@@ -0,0 +1,52 @@ | ||
1 | +/* ************************************************************************** | |
2 | + * Copyright (C) 2008 BJoRFUAN. All Right Reserved | |
3 | + * ************************************************************************** | |
4 | + * This module, contains source code, binary and documentation, is in the | |
5 | + * BSD License, and comes with NO WARRANTY. | |
6 | + * | |
7 | + * torao <torao@bjorfuan.com> | |
8 | + * http://www.moyo.biz/ | |
9 | + * $Id: TestSuite.java,v 1.3 2010/02/24 13:59:38 torao Exp $ | |
10 | +*/ | |
11 | +package org.koiroha.xml.parser; | |
12 | + | |
13 | +import java.util.logging.*; | |
14 | + | |
15 | +import org.junit.runner.RunWith; | |
16 | +import org.junit.runners.Suite; | |
17 | +import org.junit.runners.Suite.SuiteClasses; | |
18 | + | |
19 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
20 | +// TestSuite: テストスイートクラス | |
21 | +// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
22 | +/** | |
23 | + * テストスイートです。 | |
24 | + * <p> | |
25 | + * @version $Revision: 1.3 $ $Date: 2010/02/24 13:59:38 $ | |
26 | + * @author torao | |
27 | + * @since 2009/04/02 Java2 SE 5.0 | |
28 | + */ | |
29 | +@RunWith(Suite.class) | |
30 | +@SuiteClasses({ | |
31 | + HTMLParserFactoryTest.class, | |
32 | + HTMLParserTest.class, | |
33 | + LFReaderTest.class, | |
34 | + PositionTest.class, | |
35 | + ToolkitTest.class, | |
36 | + SpecPatternTest.class, | |
37 | + LooseXMLReaderTest.class, | |
38 | + HTMLDocumentBuilderFactoryTest.class, | |
39 | +}) | |
40 | +public class TestSuite { | |
41 | + /* */ | |
42 | + static { | |
43 | + Logger logger = Logger.getLogger(""); | |
44 | + for(Handler h: logger.getHandlers()){ | |
45 | + logger.removeHandler(h); | |
46 | + } | |
47 | + Handler handler = new ConsoleHandler(); | |
48 | + logger.addHandler(handler); | |
49 | + handler.setLevel(Level.ALL); | |
50 | + logger.setLevel(Level.ALL); | |
51 | + } | |
52 | +} |