File: UsageVisitorTest.java

package info (click to toggle)
libjibx1.2-java 1.2.6-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 26,260 kB
  • sloc: java: 75,013; xml: 14,068; makefile: 17
file content (78 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

package org.jibx.schema.codegen;

import org.jibx.runtime.QName;
import org.jibx.schema.SchemaTestBase;
import org.jibx.schema.UsageFinder;
import org.jibx.schema.elements.SchemaElement;
import org.jibx.util.ReferenceCountMap;

/**
 * Test usage visitor operation for counting references.
 */
public class UsageVisitorTest extends SchemaTestBase
{
    public static final String REFERENCE_SCHEMA =
        "<schema targetNamespace='urn:anything'" +
        "    xmlns='http://www.w3.org/2001/XMLSchema'" +
        "    xmlns:tns='urn:anything'" +
        "    elementFormDefault='qualified'>\n" +
        "  <complexType name='simple1'>\n" +
        "    <sequence>\n" +
        "      <element name='name' type='string'/>\n" +
        "      <element name='rated' type='tns:rating'/>\n" +
        "    </sequence>\n" +
        "    <attribute name='male' use='required' type='boolean'/>\n" +
        "  </complexType>\n" +
        "  <element name='simple1' type='tns:simple1'/>\n" +
        "  <simpleType name='simple2'>\n" +
        "    <list itemType='string'/>\n" +
        "  </simpleType>\n" +
        "  <complexType name='simple3'>\n" +
        "    <sequence>\n" +
        "      <element ref='tns:simple1'/>\n" +
        "      <element name='mixed' type='tns:mixedUnion'/>\n" +
        "    </sequence>\n" +
        "    <attribute name='rated' type='tns:rating'/>\n" +
        "  </complexType>\n" +
        "  <simpleType name='rating'>\n" +
        "    <restriction base='int'>\n" +
        "      <minExclusive value='1'/>\n" +
        "      <maxInclusive value='10' fixed='true'/>\n" +
        "    </restriction>\n" +
        "  </simpleType>\n" +
        "  <simpleType name='mixedUnion'>\n" +
        "    <union memberTypes='date dateTime tns:rating'/>\n" +
        "  </simpleType>\n" +
        "</schema>";
    
    private void checkAttributeUsage(String name, ReferenceCountMap map, int count) {
        Object key = m_nameRegister.findAttribute(new QName("urn:anything", name));
        assertEquals("Usage count error on attribute '" + name + '\'', count, map.getCount(key));
    }
    
    private void checkElementUsage(String name, ReferenceCountMap map, int count) {
        Object key = m_nameRegister.findElement(new QName("urn:anything", name));
        assertEquals("Usage count error on element '" + name + '\'', count, map.getCount(key));
    }
    
    private void checkTypeUsage(String name, ReferenceCountMap map, int count) {
        Object key = m_nameRegister.findType(new QName("urn:anything", name));
        assertEquals("Usage count error on type '" + name + '\'', count, map.getCount(key));
    }
    
    public void testComplexReferenced() throws Exception {
        SchemaElement root = runNoErrors(REFERENCE_SCHEMA);
        if (!hasProblem(m_validationContext)) {
            UsageFinder usage = new UsageFinder();
            usage.countSchemaTree(root);
            ReferenceCountMap map = usage.getUsageMap();
            checkTypeUsage("simple1", map, 1);
            checkElementUsage("simple1", map, 1);
            checkTypeUsage("simple2", map, 0);
            checkTypeUsage("simple3", map, 0);
            checkTypeUsage("rating", map, 3);
            checkTypeUsage("mixedUnion", map, 1);
        }
    }
}