1   /*
2    * Copyright 2004,2007 The Apache Software Foundation.
3    * Copyright 2006 International Business Machines Corp.
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   *
17   */
18  package tests;
19  
20  import junit.framework.TestCase;
21  import org.apache.ws.commons.schema.*;
22  
23  import javax.xml.namespace.QName;
24  import javax.xml.transform.stream.StreamSource;
25  import java.io.FileInputStream;
26  import java.io.InputStream;
27  import java.util.HashSet;
28  import java.util.Iterator;
29  import java.util.Set;
30  
31  public class FacetsTest extends TestCase {
32  
33      /**
34       * This method will test for the length facet.
35       *
36       * @throws Exception Any exception encountered
37       */
38      public void testLengthFacet() throws Exception {
39  
40          /*
41          <simpleType name="zipCode">
42            <restriction base="string">
43              <length value="5"/>
44              <pattern value="\d{5}"/>
45            </restriction>
46          </simpleType>
47          <element name="myZipCode" type="tns:zipCode"/>
48          */
49  
50          QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
51                                          "myZipCode");
52          InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
53          XmlSchemaCollection schemaCol = new XmlSchemaCollection();
54          XmlSchema schema = schemaCol.read(new StreamSource(is), null);
55  
56          XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
57          assertNotNull(elem);
58          assertEquals("myZipCode", elem.getName());
59          assertEquals(new QName("http://soapinterop.org/types", "myZipCode"),
60                       elem.getQName());
61          assertEquals(new QName("http://soapinterop.org/types", "zipCode"),
62                       elem.getSchemaTypeName());
63  
64          XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
65          
66          XmlSchemaSimpleTypeRestriction r =
67              (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
68          assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "string"),
69                       r.getBaseTypeName());
70          
71          XmlSchemaSimpleType xsst = r.getBaseType();
72          assertNull(xsst);
73  
74          XmlSchemaObjectCollection collection = r.getFacets();
75          assertEquals(2, collection.getCount());
76  
77          Set s = new HashSet();
78          s.add(XmlSchemaLengthFacet.class.getName());
79          s.add(XmlSchemaPatternFacet.class.getName());
80          for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
81              Object o = i.next();
82              assertTrue(s.remove(o.getClass().getName()));
83              if (o instanceof XmlSchemaLengthFacet) {
84                  assertEquals("5", ((XmlSchemaLengthFacet)o).getValue());
85                  assertEquals(false, ((XmlSchemaLengthFacet)o).isFixed());
86                  String toStr = ((XmlSchemaLengthFacet)o).toString("xsd", 1);
87                  assertTrue("The toString(String, int) method did not contain "
88                             + "\"length\", but did contain: " + toStr,
89                             toStr.indexOf("length value=\"5\"") != -1);
90              } else if (o instanceof XmlSchemaPatternFacet) {
91                  assertEquals("\\d{5}", ((XmlSchemaPatternFacet)o).getValue());
92                  assertEquals(false, ((XmlSchemaPatternFacet)o).isFixed());
93                  String toStr = ((XmlSchemaPatternFacet)o).toString("xsd", 1);
94                  assertTrue("The toString(String, int) method did not contain "
95                             + "\"pattern\", but did contain: " + toStr,
96                             toStr.indexOf("pattern value=\"\\d{5}\"") != -1);
97              } else {
98                  fail("Unexpected object encountered: " + o.getClass().getName());
99              }
100         }
101 
102         assertTrue("The set should have been empty, but instead contained: "
103                    + s + ".",
104                    s.isEmpty());
105 
106     }
107 
108     /**
109      * This method will test for the pattern facet.
110      *
111      * @throwss Exception Any Exception encountered
112      */
113     public void testPatternFacet() throws Exception {
114 
115         /*
116         <simpleType name="creditCardNumber">
117           <restriction base="integer">
118             <pattern value="\d{15}"/>
119           </restriction>
120         </simpleType>
121         <element name="myCreditCardNumber" type="tns:creditCardNumber"/>
122         */
123 
124         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
125                                         "myCreditCardNumber");
126         InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
127         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
128         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
129 
130         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
131         assertNotNull(elem);
132         assertEquals("myCreditCardNumber", elem.getName());
133         assertEquals(new QName("http://soapinterop.org/types", "myCreditCardNumber"),
134                      elem.getQName());
135         assertEquals(new QName("http://soapinterop.org/types", "creditCardNumber"),
136                      elem.getSchemaTypeName());
137 
138         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
139         
140         XmlSchemaSimpleTypeRestriction r =
141             (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
142         assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "integer"),
143                      r.getBaseTypeName());
144         
145         XmlSchemaSimpleType xsst = r.getBaseType();
146         assertNull(xsst);
147 
148         XmlSchemaObjectCollection collection = r.getFacets();
149         assertEquals(1, collection.getCount());
150 
151         Set s = new HashSet();
152         s.add(XmlSchemaPatternFacet.class.getName());
153         for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
154             Object o = i.next();
155             assertTrue(s.remove(o.getClass().getName()));
156             if (o instanceof XmlSchemaPatternFacet) {
157                 assertEquals("\\d{15}", ((XmlSchemaPatternFacet)o).getValue());
158                 assertEquals(false, ((XmlSchemaPatternFacet)o).isFixed());
159                 String toStr = ((XmlSchemaPatternFacet)o).toString("xsd", 1);
160                 assertTrue("The toString(String, int) method did not contain "
161                            + "\"pattern\", but did contain: " + toStr,
162                            toStr.indexOf("pattern value=\"\\d{15}\"") != -1);
163             } else {
164                 fail("Unexpected object encountered: " + o.getClass().getName());
165             }
166         }
167 
168         assertTrue("The set should have been empty, but instead contained: "
169                    + s + ".",
170                    s.isEmpty());
171 
172     }
173 
174     /**
175      *  This method will test the total digits facet.
176      *
177      * @throws Exception Any exception encountered
178      */
179     public void testTotalDigitsFacet() throws Exception {
180 
181         /*
182         <simpleType name="age">
183           <restriction base="decimal">
184             <totalDigits value="3"/>
185           </restriction>
186         </simpleType>
187         <element name="myAge" type="tns:age"/>
188         */
189 
190         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
191                                         "myAge");
192         InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
193         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
194         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
195 
196         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
197         assertNotNull(elem);
198         assertEquals("myAge", elem.getName());
199         assertEquals(new QName("http://soapinterop.org/types", "myAge"),
200                      elem.getQName());
201         assertEquals(new QName("http://soapinterop.org/types", "age"),
202                      elem.getSchemaTypeName());
203 
204         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
205         
206         XmlSchemaSimpleTypeRestriction r =
207             (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
208         assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "decimal"),
209                      r.getBaseTypeName());
210         
211         XmlSchemaSimpleType xsst = r.getBaseType();
212         assertNull(xsst);
213 
214         XmlSchemaObjectCollection collection = r.getFacets();
215         assertEquals(1, collection.getCount());
216 
217         Set s = new HashSet();
218         s.add(XmlSchemaTotalDigitsFacet.class.getName());
219         for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
220             Object o = i.next();
221             assertTrue(s.remove(o.getClass().getName()));
222             if (o instanceof XmlSchemaTotalDigitsFacet) {
223                 assertEquals("3", ((XmlSchemaTotalDigitsFacet)o).getValue());
224                 assertEquals(false, ((XmlSchemaTotalDigitsFacet)o).isFixed());
225                 String toStr = ((XmlSchemaTotalDigitsFacet)o).toString("xsd", 1);
226                 assertTrue("The toString(String, int) method did not contain "
227                            + "\"totalDigits\", but did contain: " + toStr,
228                            toStr.indexOf("totalDigits value=\"3\"") != -1);
229             } else {
230                 fail("Unexpected object encountered: " + o.getClass().getName());
231             }
232         }
233 
234         assertTrue("The set should have been empty, but instead contained: "
235                    + s + ".",
236                    s.isEmpty());
237 
238     }
239 
240     /**
241      * This method will test the Min and Max Inclusive facets.
242      *
243      * @throws Exception Any Exception encountered
244      */
245     public void testMinMaxInclusiveFacets() throws Exception {
246 
247         /*
248         <simpleType name="distance">
249           <restriction base="integer">
250             <maxInclusive value="100" fixed="true"/>
251             <minInclusive value="0"/>
252           </restriction>
253         </simpleType>
254         <element name="myDistance" type="tns:distance"/>
255         */
256 
257         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
258                                         "myDistance");
259         InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
260         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
261         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
262 
263         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
264         assertNotNull(elem);
265         assertEquals("myDistance", elem.getName());
266         assertEquals(new QName("http://soapinterop.org/types", "myDistance"),
267                      elem.getQName());
268         assertEquals(new QName("http://soapinterop.org/types", "distance"),
269                      elem.getSchemaTypeName());
270 
271         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
272         
273         XmlSchemaSimpleTypeRestriction r =
274             (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
275         assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "integer"),
276                      r.getBaseTypeName());
277         
278         XmlSchemaSimpleType xsst = r.getBaseType();
279         assertNull(xsst);
280 
281         XmlSchemaObjectCollection collection = r.getFacets();
282         assertEquals(2, collection.getCount());
283 
284         Set s = new HashSet();
285         s.add(XmlSchemaMaxInclusiveFacet.class.getName());
286         s.add(XmlSchemaMinInclusiveFacet.class.getName());
287         for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
288             Object o = i.next();
289             assertTrue(s.remove(o.getClass().getName()));
290             if (o instanceof XmlSchemaMaxInclusiveFacet) {
291                 assertEquals("100", ((XmlSchemaMaxInclusiveFacet)o).getValue());
292                 assertEquals(true, ((XmlSchemaMaxInclusiveFacet)o).isFixed());
293                 String toStr = ((XmlSchemaMaxInclusiveFacet)o).toString("xsd", 1);
294                 assertTrue("The toString(String, int) method did not contain "
295                            + "\"maxInclusive\", but did contain: " + toStr,
296                            toStr.indexOf("maxInclusive value=\"100\"") != -1);
297             } else if (o instanceof XmlSchemaMinInclusiveFacet) {
298                 assertEquals("0", ((XmlSchemaMinInclusiveFacet)o).getValue());
299                 assertEquals(false, ((XmlSchemaMinInclusiveFacet)o).isFixed());
300                 String toStr = ((XmlSchemaMinInclusiveFacet)o).toString("xsd", 1);
301                 assertTrue("The toString(String, int) method did not contain "
302                            + "\"minInclusive\", but did contain: " + toStr,
303                            toStr.indexOf("minInclusive value=\"0\"") != -1);
304             } else {
305                 fail("Unexpected object encountered: " + o.getClass().getName());
306             }
307         }
308 
309         assertTrue("The set should have been empty, but instead contained: "
310                    + s + ".",
311                    s.isEmpty());
312 
313     }
314 
315     /**
316      * This method will test the Min and Max Exclusive facets.
317      *
318      * @throws Exception Any Exception encountered
319      */
320     public void testMinMaxExlusiveFacets() throws Exception {
321 
322         /*
323         <simpleType name="weight">
324           <restriction base="integer">
325             <maxExclusive value="200"/>
326             <minExclusive value="1"/>
327           </restriction>
328         </simpleType>
329         <element name="myWeight" type="tns:weight"/>
330         */
331 
332         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
333                                         "myWeight");
334         InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
335         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
336         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
337 
338         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
339         assertNotNull(elem);
340         assertEquals("myWeight", elem.getName());
341         assertEquals(new QName("http://soapinterop.org/types", "myWeight"),
342                      elem.getQName());
343         assertEquals(new QName("http://soapinterop.org/types", "weight"),
344                      elem.getSchemaTypeName());
345 
346         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
347         
348         XmlSchemaSimpleTypeRestriction r =
349             (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
350         assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "integer"),
351                      r.getBaseTypeName());
352         
353         XmlSchemaSimpleType xsst = r.getBaseType();
354         assertNull(xsst);
355 
356         XmlSchemaObjectCollection collection = r.getFacets();
357         assertEquals(2, collection.getCount());
358 
359         Set s = new HashSet();
360         s.add(XmlSchemaMaxExclusiveFacet.class.getName());
361         s.add(XmlSchemaMinExclusiveFacet.class.getName());
362         for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
363             Object o = i.next();
364             assertTrue(s.remove(o.getClass().getName()));
365             if (o instanceof XmlSchemaMaxExclusiveFacet) {
366                 assertEquals("200", ((XmlSchemaMaxExclusiveFacet)o).getValue());
367                 assertEquals(false, ((XmlSchemaMaxExclusiveFacet)o).isFixed());
368                 String toStr = ((XmlSchemaMaxExclusiveFacet)o).toString("xsd", 1);
369                 assertTrue("The toString(String, int) method did not contain "
370                            + "\"maxExclusive\", but did contain: " + toStr,
371                            toStr.indexOf("maxExclusive value=\"200\"") != -1);
372             } else if (o instanceof XmlSchemaMinExclusiveFacet) {
373                 assertEquals("1", ((XmlSchemaMinExclusiveFacet)o).getValue());
374                 assertEquals(false, ((XmlSchemaMinExclusiveFacet)o).isFixed());
375                 String toStr = ((XmlSchemaMinExclusiveFacet)o).toString("xsd", 1);
376                 assertTrue("The toString(String, int) method did not contain "
377                            + "\"minExclusive\", but did contain: " + toStr,
378                            toStr.indexOf("minExclusive value=\"1\"") != -1);
379             } else {
380                 fail("Unexpected object encountered: " + o.getClass().getName());
381             }
382         }
383 
384         assertTrue("The set should have been empty, but instead contained: "
385                    + s + ".",
386                    s.isEmpty());
387 
388     }
389 
390     /**
391      * This will test the whiteSpace facet.
392      *
393      * @throws Exception Any Exception encountered
394      */
395     public void testWhiteSpaceFacet() throws Exception {
396 
397         /*
398         <simpleType name="noWhiteSpace">
399           <restriction base="integer">
400             <whiteSpace value="collapse"/>
401           </restriction>
402         </simpleType>
403         <element name="myWhiteSpace" type="tns:noWhiteSpace"/>
404         */
405 
406         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
407                                         "myWhiteSpace");
408         InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
409         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
410         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
411 
412         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
413         assertNotNull(elem);
414         assertEquals("myWhiteSpace", elem.getName());
415         assertEquals(new QName("http://soapinterop.org/types", "myWhiteSpace"),
416                      elem.getQName());
417         assertEquals(new QName("http://soapinterop.org/types", "noWhiteSpace"),
418                      elem.getSchemaTypeName());
419 
420         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
421         
422         XmlSchemaSimpleTypeRestriction r =
423             (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
424         assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "normalizedString"),
425                      r.getBaseTypeName());
426         
427         XmlSchemaSimpleType xsst = r.getBaseType();
428         assertNull(xsst);
429 
430         XmlSchemaObjectCollection collection = r.getFacets();
431         assertEquals(1, collection.getCount());
432 
433         Set s = new HashSet();
434         s.add(XmlSchemaWhiteSpaceFacet.class.getName());
435         for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
436             Object o = i.next();
437             assertTrue(s.remove(o.getClass().getName()));
438             if (o instanceof XmlSchemaWhiteSpaceFacet) {
439                 assertEquals("collapse", ((XmlSchemaWhiteSpaceFacet)o).getValue());
440                 assertEquals(false, ((XmlSchemaWhiteSpaceFacet)o).isFixed());
441                 String toStr = ((XmlSchemaWhiteSpaceFacet)o).toString("xsd", 1);
442                 assertTrue("The toString(String, int) method did not contain "
443                            + "\"minExclusive\", but did contain: " + toStr,
444                            toStr.indexOf("whiteSpace value=\"collapse\"") != -1);
445             } else {
446                 fail("Unexpected object encountered: " + o.getClass().getName());
447             }
448         }
449 
450         assertTrue("The set should have been empty, but instead contained: "
451                    + s + ".",
452                    s.isEmpty());
453 
454     }
455 
456     /**
457      * This will test the fractionDigits facet.
458      *
459      * @throws Exception Any Exception encountered
460      */
461     public void testFractionDigitsFacet() throws Exception {
462 
463         /*
464         <simpleType name="height">
465           <restriction base="decimal">
466             <totalDigits value="3"/>
467             <fractionDigits value="2"/>
468           </restriction>
469         </simpleType>
470         <element name="myHeight" type="tns:height"/>
471         */
472 
473         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
474                                         "myHeight");
475         InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
476         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
477         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
478 
479         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
480         assertNotNull(elem);
481         assertEquals("myHeight", elem.getName());
482         assertEquals(new QName("http://soapinterop.org/types", "myHeight"),
483                      elem.getQName());
484         assertEquals(new QName("http://soapinterop.org/types", "height"),
485                      elem.getSchemaTypeName());
486 
487         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
488         
489         XmlSchemaSimpleTypeRestriction r =
490             (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
491         assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "decimal"),
492                      r.getBaseTypeName());
493         
494         XmlSchemaSimpleType xsst = r.getBaseType();
495         assertNull(xsst);
496 
497         XmlSchemaObjectCollection collection = r.getFacets();
498         assertEquals(2, collection.getCount());
499 
500         Set s = new HashSet();
501         s.add(XmlSchemaFractionDigitsFacet.class.getName());
502         s.add(XmlSchemaTotalDigitsFacet.class.getName());
503         for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
504             Object o = i.next();
505             assertTrue(s.remove(o.getClass().getName()));
506             if (o instanceof XmlSchemaFractionDigitsFacet) {
507                 assertEquals("2", ((XmlSchemaFractionDigitsFacet)o).getValue());
508                 assertEquals(false, ((XmlSchemaFractionDigitsFacet)o).isFixed());
509                 String toStr = ((XmlSchemaFractionDigitsFacet)o).toString("xsd", 1);
510                 assertTrue("The toString(String, int) method did not contain "
511                            + "\"fractionDigits\", but did contain: " + toStr,
512                            toStr.indexOf("fractionDigits value=\"2\"") != -1);
513             } else if (o instanceof XmlSchemaTotalDigitsFacet) {
514                 assertEquals("3", ((XmlSchemaTotalDigitsFacet)o).getValue());
515                 assertEquals(false, ((XmlSchemaTotalDigitsFacet)o).isFixed());
516                 String toStr = ((XmlSchemaTotalDigitsFacet)o).toString("xsd", 1);
517                 assertTrue("The toString(String, int) method did not contain "
518                            + "\"totalDigits\", but did contain: " + toStr,
519                            toStr.indexOf("totalDigits value=\"3\"") != -1);
520             } else {
521                 fail("Unexpected object encountered: " + o.getClass().getName());
522             }
523         }
524 
525         assertTrue("The set should have been empty, but instead contained: "
526                    + s + ".",
527                    s.isEmpty());
528 
529     }
530 
531     /**
532      * This method will test the Min and Max Length facets.
533      *
534      * @throws Exception Any Exception encountered
535      */
536     public void testMinMaxLengthFacets() throws Exception {
537 
538         /*
539         <simpleType name="yardLength">
540           <restriction base="nonNegativeInteger">
541             <minLength value="45"/>
542             <maxLength value="205"/>
543           </restriction>
544         </simpleType>
545         <element name="myYardLength" type="tns:yardLength"/>
546         */
547 
548         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
549                                         "myYardLength");
550         InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
551         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
552         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
553 
554         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
555         assertNotNull(elem);
556         assertEquals("myYardLength", elem.getName());
557         assertEquals(new QName("http://soapinterop.org/types", "myYardLength"),
558                      elem.getQName());
559         assertEquals(new QName("http://soapinterop.org/types", "yardLength"),
560                      elem.getSchemaTypeName());
561 
562         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
563         
564         XmlSchemaSimpleTypeRestriction r =
565             (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
566         assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "nonNegativeInteger"),
567                      r.getBaseTypeName());
568         
569         XmlSchemaSimpleType xsst = r.getBaseType();
570         assertNull(xsst);
571 
572         XmlSchemaObjectCollection collection = r.getFacets();
573         assertEquals(2, collection.getCount());
574 
575         Set s = new HashSet();
576         s.add(XmlSchemaMinLengthFacet.class.getName());
577         s.add(XmlSchemaMaxLengthFacet.class.getName());
578         for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
579             Object o = i.next();
580             assertTrue(s.remove(o.getClass().getName()));
581             if (o instanceof XmlSchemaMinLengthFacet) {
582                 assertEquals("45", ((XmlSchemaMinLengthFacet)o).getValue());
583                 assertEquals(false, ((XmlSchemaMinLengthFacet)o).isFixed());
584                 String toStr = ((XmlSchemaMinLengthFacet)o).toString("xsd", 1);
585                 assertTrue("The toString(String, int) method did not contain "
586                            + "\"minExclusive\", but did contain: " + toStr,
587                            toStr.indexOf("minLength value=\"45\"") != -1);
588             } else if (o instanceof XmlSchemaMaxLengthFacet) {
589                 assertEquals("205", ((XmlSchemaMaxLengthFacet)o).getValue());
590                 assertEquals(false, ((XmlSchemaMaxLengthFacet)o).isFixed());
591                 String toStr = ((XmlSchemaMaxLengthFacet)o).toString("xsd", 1);
592                 assertTrue("The toString(String, int) method did not contain "
593                            + "\"maxLength\", but did contain: " + toStr,
594                            toStr.indexOf("maxLength value=\"205\"") != -1);
595             } else {
596                 fail("Unexpected object encountered: " + o.getClass().getName());
597             }
598         }
599 
600         assertTrue("The set should have been empty, but instead contained: "
601                    + s + ".",
602                    s.isEmpty());
603 
604     }
605 
606     /**
607      * This method will test the enumeration facet.
608      *
609      * @throws Exception Any Exception encountered
610      */
611     public void testEnumerationFacet() throws Exception {
612         
613         /*
614         <simpleType name="layoutComponentType">
615           <restriction base="string">
616             <enumeration value="Field"/>
617             <enumeration value="Separator"/>
618           </restriction>
619         </simpleType>
620         <element name="layoutComponent" type="tns:layoutComponentType"/>
621         */
622 
623         QName ELEMENT_QNAME = new QName("http://soapinterop.org/types",
624                                         "layoutComponent");
625         InputStream is = new FileInputStream(Resources.asURI("facets.xsd"));
626         XmlSchemaCollection schemaCol = new XmlSchemaCollection();
627         XmlSchema schema = schemaCol.read(new StreamSource(is), null);
628 
629         XmlSchemaElement elem = schemaCol.getElementByQName(ELEMENT_QNAME);
630         assertNotNull(elem);
631         assertEquals("layoutComponent", elem.getName());
632         assertEquals(new QName("http://soapinterop.org/types", "layoutComponent"),
633                      elem.getQName());
634         assertEquals(new QName("http://soapinterop.org/types", "layoutComponentType"),
635                      elem.getSchemaTypeName());
636 
637         XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)elem.getSchemaType();
638         
639         XmlSchemaSimpleTypeRestriction r =
640             (XmlSchemaSimpleTypeRestriction)simpleType.getContent();
641         assertEquals(new QName("http://www.w3.org/2001/XMLSchema", "string"),
642                      r.getBaseTypeName());
643         
644         XmlSchemaSimpleType xsst = r.getBaseType();
645         assertNull(xsst);
646 
647         XmlSchemaObjectCollection collection = r.getFacets();
648         assertEquals(2, collection.getCount());
649 
650         Set s = new HashSet();
651         s.add("Field");
652         s.add("Separator");
653         for (Iterator i  = collection.getIterator(); i.hasNext(); ) {
654             XmlSchemaEnumerationFacet xsef = (XmlSchemaEnumerationFacet)i.next();
655             String value = (String)xsef.getValue();
656             assertTrue("Atempted to remove an enumeration with the value of "
657                        + "\"" + value + "\", but the value was not in the set.",
658                        s.remove(value));
659             String toStr = xsef.toString("xsd", 1);
660             if (value.equals("Field")) {
661                 assertTrue("The toString(String, int) method did not contain "
662                            + "\"enumeration\", but did contain: " + toStr,
663                            toStr.indexOf("enumeration value=\"Field\"") != -1);
664             } else if (value.equals("Separator")) {
665                 assertTrue("The toString(String, int) method did not contain "
666                            + "\"enumeration\", but did contain: " + toStr,
667                            toStr.indexOf("enumeration value=\"Separator\"") != -1);
668             }
669         }
670 
671         assertTrue("The set should have been empty, but instead contained: "
672                    + s + ".",
673                    s.isEmpty());
674 
675     }
676 
677 }