1   /*
2    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
3    * 
4    * This software is open source. 
5    * See the bottom of this file for the licence.
6    * 
7    * $Id: TestAttributeDecl.java,v 1.3 2004/06/25 08:03:49 maartenc Exp $
8    */
9   
10  package org.dom4j.dtd;
11  
12  import junit.framework.*;
13  import junit.textui.TestRunner;
14  
15  /*** Tests the {@link AttributeDecl} functionality.  Tests each of the
16    * property access methods and the serialization mechanisms.  Correct
17    * parsing is tested by {@link TestDocType}.<p>
18    *
19    * There are several key variations that need to be tested both here
20    * and in {@link TestDocType}, which is responsible for testing
21    * correct parsing of the {@link DocumentType}.  Those variations
22    * include the different <code>valueDefault</code> and
23    * <code>value</code> variations so that we can test for correct
24    * acceptance and correct rejection of attribute declarations.<p>
25    *
26    * @todo The dom4j documentation needs to describe what
27    * representation SHOULD be generated by {@link
28    * AttributeDecl#toString()}.
29    *
30    * @todo The dom4j AttributeDecl should expose some methods that make
31    * it easier for people to use the DTD grammar, e.g., isFixed(),
32    * isRequired(), isImplied().
33    *
34    * @author Bryan Thompson
35    * @author Maarten Coene
36    * @version $Revision: 1.3 $
37    */
38  
39  public class TestAttributeDecl extends TestCase {
40  
41      
42      public static void main( String[] args ) {
43          TestRunner.run( suite() );
44      }
45      
46      public static Test suite() {
47          return new TestSuite( TestAttributeDecl.class );
48      }
49      
50      public TestAttributeDecl(String name) {
51          super(name);
52      }
53  
54      // Test case(s)
55      //-------------------------------------------------------------------------                    
56  
57      /***
58       * Test <pre><!ATTLIST foo bar ID #IMPLIED></pre>.
59       */
60  
61      public void test_ID_IMPLIED_none()
62      {
63  
64  	assertSameAttributeDecl
65  	    ( new MyTestAttributeDecl
66  	      ( "foo",		// elementName
67  		"bar",		// attributeName
68  		"ID",		// type
69  		"#IMPLIED",	// valueDefault
70  		null,		// value
71  		"<!ATTLIST foo bar ID #IMPLIED>"
72  		),
73  	      new AttributeDecl
74  	      ( "foo",
75  		"bar",
76  		"ID",
77  		"#IMPLIED",
78  		null
79  	       )
80  	      );
81      }
82  
83      /***
84       * Test <pre><!ATTLIST foo bar CDATA #FIXED \"goo\"></pre>.
85       */
86  
87      public void test_CDATA_Fixed_value()
88      {
89  
90  	assertSameAttributeDecl
91  	    ( new MyTestAttributeDecl
92  	      ( "foo",		// elementName
93  		"bar",		// attributeName
94  		"CDATA",	// type
95  		"#FIXED",	// valueDefault
96  		"goo",		// value
97  		"<!ATTLIST foo bar CDATA #FIXED \"goo\">"
98  		),
99  	      new AttributeDecl
100 	      ( "foo",
101 		"bar",
102 		"CDATA",
103 		"#FIXED",
104 		"goo"
105 	       )
106 	      );
107 
108     }
109 
110     /***
111      * Test <pre><!ATTLIST foo bar CDATA "goo"></pre>.
112      */
113 
114     public void test_CDATA_none_value()
115     {
116 
117 	assertSameAttributeDecl
118 	    ( new MyTestAttributeDecl
119 	      ( "foo",		// elementName
120 		"bar",		// attributeName
121 		"CDATA",	// type
122 		null,		// valueDefault
123 		"goo",		// value
124 		"<!ATTLIST foo bar CDATA \"goo\">"
125 		),
126 	      new AttributeDecl
127 	      ( "foo",
128 		"bar",
129 		"CDATA",
130 		null,
131 		"goo"
132 	       )
133 	      );
134 
135     }
136 
137     // Implementation methods
138     //-------------------------------------------------------------------------                    
139 
140     protected void assertSameAttributeDecl
141 	( MyTestAttributeDecl expected,
142 	  AttributeDecl actual
143 	  )
144     {
145 
146 	assertEquals
147 	    ( "elementName is correct",
148 	      expected.getElementName(),
149 	      actual.getElementName()
150 	      );
151 
152 	assertEquals
153 	    ( "attributeName is correct",
154 	      expected.getAttributeName(),
155 	      actual.getAttributeName()
156 	      );
157 
158 	assertEquals
159 	    ( "type is correct",
160 	      expected.getType(),
161 	      actual.getType()
162 	      );
163 
164 	assertEquals
165 	    ( "valueDefault is correct",
166 	      expected.getValueDefault(),
167 	      actual.getValueDefault()
168 	      );
169 
170 	assertEquals
171 	    ( "toString() is correct",
172 	      expected.getText(),
173 	      actual.toString()
174 	      );
175     
176     }
177 
178 
179     /***
180      * Helper is useful since we are trying to exhaustively test the
181      * ATTLIST variations and their correct serialization.
182      */
183 
184     protected static class MyTestAttributeDecl
185     {
186 
187 	String m_elementName;
188 	String m_attributeName;
189 	String m_type;
190 	String m_valueDefault;
191 	String m_value;
192 	String m_text;
193 
194 	public String getElementName() {return m_elementName;}
195 	public String getAttributeName() {return m_attributeName;}
196 	public String getType() {return m_type;}
197 	public String getValueDefault() {return m_valueDefault;}
198 	public String getValue() {return m_value;}
199 	public String getText() {return m_text;}
200 
201 	/***
202 	 * @param elementName The name of the element whose attribute
203 	 * is being described.
204 	 *
205 	 * @param attributeName The name of the attribute.
206 	 *
207 	 * @param type The type of the declared attribute, e.g.,
208 	 * CDATA, ID, IDREF, IDREFS, ENTITY, ENTITIES, NMTOKEN,
209 	 * NKTOKENS
210 	 *
211 	 * @param valueDefault The type of default that applies for
212 	 * this attribute declaration, e.g., #REQUIRED, #IMPLIED,
213 	 * #FIXED (in which case the <i>value</i> MUST be
214 	 * non-<code>null</code> and specifies the fixed value for the
215 	 * attribute</code>, or <code>null</code> if no valueDefault
216 	 * was specified in the attribute declaration (in which case
217 	 * the <i>value</i> MUST be non-<code>null</code> and
218 	 * specifies the default value for the attribute).
219 	 *
220 	 * @param value The value of the attribute assigned in the
221 	 * attribute declaration -or- <code>null</code> if no value
222 	 * was provided in the attribute declaration.  The value MUST
223 	 * be <code>null</code> unless the <i>valueDefault</i> is
224 	 * either "#FIXED" or <code>null</code>.
225 	 * 
226 	 * @param text The text representation of the attribute
227 	 * declaration, e.g., <code><!ATTLIST foo id ID #IMPLIED></code>.
228 	 *
229 	 * @todo The constructor and properties in {@link
230 	 * AttributeDecl} should have some similar javadoc so that
231 	 * people more easily understand the interaction and
232 	 * difference between the <i>valueDefault</i> and <i>value</i>
233 	 * properties.  The constructor SHOULD be clear about whether
234 	 * and when the <code>valueDefault</code> and
235 	 * <code>value</code> properties MUST be <code>null</code>.
236 	 */
237 	
238 	public MyTestAttributeDecl
239 	    ( String elementName,
240 	      String attributeName,
241 	      String type,
242 	      String valueDefault,
243 	      String value,
244 	      String text
245 	      )
246 	{
247 
248 	    m_elementName   = elementName;
249 
250 	    m_attributeName = attributeName;
251 
252 	    m_type          = type;
253 
254 	    m_valueDefault  = valueDefault;
255 
256 	    m_value         = value;
257 
258 	    m_text          = text;
259 
260 	    /* @todo This logic really belongs in the {@link
261 	     * AttributeDecl} implementation under test as part of a
262 	     * check for legal vs illegal arguments in its
263 	     * constructor.
264 	     */
265 
266 // 	    if( m_valueDefault == null ) {
267 		
268 // 		if( m_value == null ) {
269 
270 // 		    throw new AssertionError
271 // 			( "XML requires attribute value when valueDefault is #FIXED or is not specified."
272 // 			  );
273 
274 // 		}
275 
276 // 	    } else if( m_valueDefault.equals("#FIXED") ) {
277 		
278 // 		if( m_value == null ) {
279 
280 // 		    throw new AssertionError
281 // 			( "XML requires attribute value when valueDefault is #FIXED or is not specified."
282 // 			  );
283 
284 // 		}
285 
286 // 	    }
287 
288 	}
289 
290     } // Class TestAttributeDecl
291 
292 }
293 
294 
295 
296 /*
297  * Redistribution and use of this software and associated documentation
298  * ("Software"), with or without modification, are permitted provided
299  * that the following conditions are met:
300  *
301  * 1. Redistributions of source code must retain copyright
302  *    statements and notices.  Redistributions must also contain a
303  *    copy of this document.
304  *
305  * 2. Redistributions in binary form must reproduce the
306  *    above copyright notice, this list of conditions and the
307  *    following disclaimer in the documentation and/or other
308  *    materials provided with the distribution.
309  *
310  * 3. The name "DOM4J" must not be used to endorse or promote
311  *    products derived from this Software without prior written
312  *    permission of MetaStuff, Ltd.  For written permission,
313  *    please contact dom4j-info@metastuff.com.
314  *
315  * 4. Products derived from this Software may not be called "DOM4J"
316  *    nor may "DOM4J" appear in their names without prior written
317  *    permission of MetaStuff, Ltd. DOM4J is a registered
318  *    trademark of MetaStuff, Ltd.
319  *
320  * 5. Due credit should be given to the DOM4J Project - 
321  *    http://www.dom4j.org
322  *
323  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
324  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
325  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
326  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
327  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
328  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
329  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
330  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
331  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
332  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
333  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
334  * OF THE POSSIBILITY OF SUCH DAMAGE.
335  *
336  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
337  *
338  * $Id: TestAttributeDecl.java,v 1.3 2004/06/25 08:03:49 maartenc Exp $
339  */