~ubuntu-branches/ubuntu/trusty/jing-trang/trusty-proposed

« back to all changes in this revision

Viewing changes to mod/infer/src/test/com/thaiopensource/xml/infer/TestInferHandler.java

  • Committer: Package Import Robot
  • Author(s): Samuel Thibault
  • Date: 2013-12-15 21:28:29 UTC
  • mto: (6.1.3 sid)
  • mto: This revision was merged to the branch mainline in revision 11.
  • Revision ID: package-import@ubuntu.com-20131215212829-l4q3ewh0u2lw17d1
Tags: upstream-20131210+dfsg
ImportĀ upstreamĀ versionĀ 20131210+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package com.thaiopensource.xml.infer;
 
2
 
 
3
import java.io.IOException;
 
4
import java.io.StringReader;
 
5
import java.util.Map;
 
6
 
 
7
import org.testng.Assert;
 
8
import org.testng.annotations.DataProvider;
 
9
import org.testng.annotations.Test;
 
10
import org.xml.sax.InputSource;
 
11
import org.xml.sax.SAXException;
 
12
import org.xml.sax.XMLReader;
 
13
 
 
14
import com.thaiopensource.datatype.xsd.DatatypeLibraryFactoryImpl;
 
15
import com.thaiopensource.datatype.xsd.regex.java.RegexEngineImpl;
 
16
import com.thaiopensource.resolver.xml.sax.SAXResolver;
 
17
import com.thaiopensource.xml.util.Name;
 
18
 
 
19
/**
 
20
 * Test the inference handler
 
21
 */
 
22
public class TestInferHandler {
 
23
  @DataProvider(name = "samples")
 
24
  public Object[][] createDateSamples() {
 
25
    return new Object[][] {
 
26
        {
 
27
          "<sample>\n" 
 
28
          + "  <element att=\"true\"/>\n"
 
29
          + "  <element att=\"false\"/>\n"
 
30
          + "  <element att=\"true\"/>\n" 
 
31
          + "</sample>", 
 
32
          "boolean" 
 
33
        },
 
34
        {
 
35
          "<sample>\n" 
 
36
          + "  <element att=\"1\"/>\n"
 
37
          + "  <element att=\"2\"/>\n"
 
38
          + "  <element att=\"3\"/>\n" 
 
39
          + "</sample>", 
 
40
          "integer" 
 
41
        },
 
42
        {
 
43
          "<sample>\n" 
 
44
          + "  <element att=\"1.1\"/>\n"
 
45
          + "  <element att=\"0.3\"/>\n"
 
46
          + "  <element att=\"90\"/>\n" 
 
47
          + "</sample>", 
 
48
          "decimal" 
 
49
        },
 
50
        {
 
51
          "<sample>\n" 
 
52
          + "  <element att=\"1.23E100\"/>\n"
 
53
          + "  <element att=\"2\"/>\n"
 
54
          + "  <element att=\"2.34\"/>\n" 
 
55
          + "</sample>", 
 
56
          "double" 
 
57
        },
 
58
        {
 
59
          "<sample>\n" 
 
60
          + "  <element att=\"test\"/>\n"
 
61
          + "  <element att=\"x\"/>\n"
 
62
          + "  <element att=\"y\"/>\n" 
 
63
          + "</sample>", 
 
64
          "NCName" 
 
65
        },
 
66
        {
 
67
          "<sample>\n" 
 
68
          + "  <element att=\"12:23:00\"/>\n"
 
69
          + "  <element att=\"10:01:11-05:00\"/>\n"
 
70
          + "  <element att=\"09:01:07Z\"/>\n" 
 
71
          + "</sample>", 
 
72
          "time" 
 
73
        },
 
74
        {
 
75
          "<sample>\n" 
 
76
          + "  <element att=\"2012-10-09\"/>\n"
 
77
          + "  <element att=\"2010-02-01\"/>\n"
 
78
          + "  <element att=\"2009-10-10\"/>\n" 
 
79
          + "</sample>", 
 
80
          "date" 
 
81
        },
 
82
        {
 
83
          "<sample>\n" 
 
84
          + "  <element att=\"2010-01-20T08:00:10\"/>\n"
 
85
          + "  <element att=\"2012-02-01T10:00:03-05:00\"/>\n"
 
86
          + "  <element att=\"2010-01-20T12:00:00Z\"/>\n" 
 
87
          + "</sample>", 
 
88
          "dateTime" 
 
89
        },
 
90
        {
 
91
          "<sample>\n" 
 
92
          + "  <element att=\"P364D\"/>\n"
 
93
          + "  <element att=\"P1347Y\"/>\n"
 
94
          + "  <element att=\"-P1347M\"/>\n" 
 
95
          + "</sample>", 
 
96
          "duration" 
 
97
        },
 
98
        {
 
99
          "<sample>\n" 
 
100
          + "  <element att=\"0FB70FB70FB70FB70FB70FB70FB70FB70" 
 
101
          + "FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70" 
 
102
          + "FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70" 
 
103
          + "FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70FB70" 
 
104
          + "FB7\"/>\n"
 
105
          + "  <element att=\"AFB7\"/>\n"
 
106
          + "  <element att=\"DF\"/>\n" 
 
107
          + "</sample>", 
 
108
          "hexBinary" 
 
109
        },
 
110
        {
 
111
          "<sample>\n" 
 
112
          + "  <element att=\"x:a\"/>\n"
 
113
          + "  <element att=\"_b-c.32\"/>\n"
 
114
          + "  <element att=\"test\"/>\n" 
 
115
          + "</sample>", 
 
116
          "NMTOKEN" 
 
117
        },
 
118
        {
 
119
          "<sample>\n" 
 
120
          + "  <element att=\"\n" 
 
121
          + "    AABBCCDDEEFFAABBCCDDEEFFAABBCCDDEEFFAABBCCDDEEFF\n" 
 
122
          + "    AABBCCDDEEFFAABBCCDDEEFFAABBCCDDEEFFAABBCCDDEEFF\n" 
 
123
          + "    AABBCCDDEEFFAABBCCDDEEFFAABBCCDDEEFFAABBCCDDEEFF\"/>\n"
 
124
          + "  <element att=\"AABBCCDDEEFFAA==\"/>\n"
 
125
          + "  <element att=\"AABBCCDDEEAAAAAA\"/>\n" 
 
126
          + "</sample>", 
 
127
          "base64Binary" 
 
128
        },
 
129
        {
 
130
          "<sample>\n" 
 
131
          + "  <element att=\"http://www.example.com\"/>\n"
 
132
          + "  <element att=\"test#a10\"/>\n"
 
133
          + "  <element att=\"ftp://server/path/to/file.xml\"/>\n" 
 
134
          + "</sample>", 
 
135
          "anyURI" 
 
136
        }
 
137
    };
 
138
  }
 
139
 
 
140
  @Test(dataProvider = "samples")
 
141
  public void testTypeInferenceForAttribute(String xmlSource, String type)
 
142
  throws SAXException, IOException {
 
143
    InferHandler handler = new InferHandler(new DatatypeLibraryFactoryImpl(new RegexEngineImpl()));
 
144
    SAXResolver resolver = new SAXResolver();
 
145
    XMLReader xr = resolver.createXMLReader();
 
146
    xr.setContentHandler(handler);
 
147
    xr.parse(new InputSource(new StringReader(xmlSource)));
 
148
    Schema schema = handler.getSchema();
 
149
    for (Map.Entry<Name, ElementDecl> entry : schema.getElementDecls()
 
150
        .entrySet()) {
 
151
      Name name = entry.getKey();
 
152
      ElementDecl elementDecl = entry.getValue();
 
153
      if ("element".equals(name.getLocalName())) {
 
154
        for (Map.Entry<Name, AttributeDecl> attEntry : elementDecl.getAttributeDecls().entrySet()) {
 
155
          AttributeDecl att = attEntry.getValue();
 
156
          Name attName = attEntry.getKey();
 
157
          if ("att".equals(attName.getLocalName())) {
 
158
            Name typeName = att.getDatatype();
 
159
            Assert.assertEquals(typeName.getLocalName(), type);
 
160
          }
 
161
        }
 
162
      }
 
163
    }
 
164
  }
 
165
}
 
 
b'\\ No newline at end of file'