1   /*
2    * Copyright 2004-2008 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.uruma.rcp.configuration.writer;
17  
18  import java.io.StringWriter;
19  
20  import junit.framework.TestCase;
21  
22  import org.seasar.uruma.annotation.ConfigurationAttribute;
23  import org.seasar.uruma.rcp.configuration.ConfigurationWriterFactory;
24  import org.seasar.uruma.rcp.configuration.elements.AbstractConfigurationElement;
25  import org.seasar.uruma.rcp.configuration.elements.AbstractConfigurationElementContainer;
26  
27  /**
28   * {@link GenericConfigurationWriter} に対するテストクラスです。<br />
29   * 
30   * @author y-komori
31   */
32  public class GenericConfigurationWriterTest extends TestCase {
33  
34      @Override
35      protected void setUp() throws Exception {
36          ConfigurationWriterFactory.addWriter(new GenericConfigurationWriter(
37                  TestElement1.class));
38          ConfigurationWriterFactory.addWriter(new GenericConfigurationWriter(
39                  TestElement2.class));
40          ConfigurationWriterFactory.addWriter(new GenericConfigurationWriter(
41                  TestElement3.class));
42          ConfigurationWriterFactory.addWriter(new GenericConfigurationWriter(
43                  TestElement4.class));
44          ConfigurationWriterFactory.addWriter(new GenericConfigurationWriter(
45                  TestElement5.class));
46      }
47  
48      /**
49       * 開始/終了タグがある場合のテストです。<br />
50       */
51      public void testWrite1() {
52          TestElement1 element = new TestElement1();
53  
54          StringWriter sw = new StringWriter();
55          element.writeConfiguration(sw);
56  
57          assertEquals(
58                  "<element attr1=\"abc\" attribute2=\"def\" attr3=\"123\" >\n</element>\n",
59                  sw.getBuffer().toString());
60      }
61  
62      /**
63       * 開始タグのみの場合のテストです。<br />
64       */
65      public void testWrite2() {
66          TestElement2 element = new TestElement2();
67  
68          StringWriter sw = new StringWriter();
69          element.writeConfiguration(sw);
70  
71          assertEquals(
72                  "<element attr1=\"abc\" attribute2=\"def\" attr3=\"123\" />\n",
73                  sw.getBuffer().toString());
74      }
75  
76      /**
77       * 継承されたクラスに対するテストです。<br />
78       */
79      public void testWrite3() {
80          TestElement3 element = new TestElement3();
81  
82          StringWriter sw = new StringWriter();
83          element.writeConfiguration(sw);
84  
85          assertEquals(
86                  "<element attr4=\"ghi\" attribute5=\"jkl\" attr6=\"456\""
87                          + " attr1=\"abc\" attribute2=\"def\" attr3=\"123\" >\n</element>\n",
88                  sw.getBuffer().toString());
89      }
90  
91      /**
92       * 親子関係を持つ場合のテストです。<br />
93       */
94      public void testWrite4() {
95          TestElement4 element = new TestElement4();
96  
97          TestElement5 child1 = new TestElement5();
98          child1.attr = "child1";
99          element.addElement(child1);
100 
101         TestElement5 child2 = new TestElement5();
102         child2.attr = "child2";
103         element.addElement(child2);
104 
105         StringWriter sw = new StringWriter();
106         element.writeConfiguration(sw);
107 
108         assertEquals("<element >\n <child attr=\"child1\" />\n"
109                 + " <child attr=\"child2\" />\n</element>\n", sw.getBuffer()
110                 .toString());
111     }
112 
113     private static class TestElement1 extends
114             AbstractConfigurationElementContainer {
115         public static final String ELEMENT_NAME = "element";
116 
117         @ConfigurationAttribute
118         public String attr1 = "abc";
119 
120         @ConfigurationAttribute(name = "attribute2")
121         public String attr2 = "def";
122 
123         @ConfigurationAttribute
124         public int attr3 = 123;
125     }
126 
127     private static class TestElement2 extends AbstractConfigurationElement {
128         public static final String ELEMENT_NAME = "element";
129 
130         @ConfigurationAttribute
131         public String attr1 = "abc";
132 
133         @ConfigurationAttribute(name = "attribute2")
134         public String attr2 = "def";
135 
136         @ConfigurationAttribute
137         public int attr3 = 123;
138     }
139 
140     private static class TestElement3 extends TestElement1 {
141         @ConfigurationAttribute
142         public String attr4 = "ghi";
143 
144         @ConfigurationAttribute(name = "attribute5")
145         public String attr5 = "jkl";
146 
147         @ConfigurationAttribute
148         public int attr6 = 456;
149     }
150 
151     private static class TestElement4 extends
152             AbstractConfigurationElementContainer {
153         public static final String ELEMENT_NAME = "element";
154     }
155 
156     private static class TestElement5 extends AbstractConfigurationElement {
157         public static final String ELEMENT_NAME = "child";
158 
159         @ConfigurationAttribute
160         public String attr;
161     }
162 }