Source Code Cross Referenced for RDFXMLParserTest.java in  » RSS-RDF » sesame » org » openrdf » rio » rdfxml » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » RSS RDF » sesame » org.openrdf.rio.rdfxml 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright Aduna (http://www.aduna-software.com/) (c) 1997-2006.
003:         *
004:         * Licensed under the Aduna BSD-style license.
005:         */
006:        package org.openrdf.rio.rdfxml;
007:
008:        import java.io.InputStream;
009:        import java.net.MalformedURLException;
010:        import java.net.URL;
011:        import java.util.LinkedHashSet;
012:        import java.util.Set;
013:
014:        import junit.framework.Test;
015:        import junit.framework.TestCase;
016:        import junit.framework.TestSuite;
017:
018:        import org.openrdf.model.Statement;
019:        import org.openrdf.model.util.ModelUtil;
020:        import org.openrdf.query.BindingSet;
021:        import org.openrdf.query.QueryLanguage;
022:        import org.openrdf.query.TupleQueryResult;
023:        import org.openrdf.repository.Repository;
024:        import org.openrdf.repository.RepositoryConnection;
025:        import org.openrdf.repository.sail.SailRepository;
026:        import org.openrdf.rio.RDFFormat;
027:        import org.openrdf.rio.RDFParseException;
028:        import org.openrdf.rio.RDFParser;
029:        import org.openrdf.rio.helpers.StatementCollector;
030:        import org.openrdf.rio.ntriples.NTriplesParser;
031:        import org.openrdf.sail.memory.MemoryStore;
032:
033:        /**
034:         * JUnit test for the RDF/XML parser that uses the test manifest that is
035:         * available <a
036:         * href="http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf">online</a>.
037:         */
038:        public class RDFXMLParserTest {
039:
040:            /*-----------*
041:             * Constants *
042:             *-----------*/
043:
044:            private static String W3C_TESTS_DIR = "http://www.w3.org/2000/10/rdf-tests/rdfcore/";
045:
046:            private static String LOCAL_TESTS_DIR = "/testcases/rdfxml/";
047:
048:            private static String W3C_MANIFEST_FILE = W3C_TESTS_DIR
049:                    + "Manifest.rdf";
050:
051:            private static String OPENRDF_MANIFEST_FILE = LOCAL_TESTS_DIR
052:                    + "openrdf/Manifest.rdf";
053:
054:            /*--------------------*
055:             * Static initializer *
056:             *--------------------*/
057:
058:            public static Test suite() throws Exception {
059:                // Create an RDF repository for the manifest data
060:                Repository repository = new SailRepository(new MemoryStore());
061:                repository.initialize();
062:                RepositoryConnection con = repository.getConnection();
063:
064:                // Add W3C's manifest
065:                URL w3cManifest = resolveURL(W3C_MANIFEST_FILE);
066:                con.add(w3cManifest, W3C_MANIFEST_FILE, RDFFormat.RDFXML);
067:
068:                // Add our own manifest
069:                URL localManifest = resolveURL(OPENRDF_MANIFEST_FILE);
070:                con.add(localManifest, localManifest.toString(),
071:                        RDFFormat.RDFXML);
072:
073:                // Create test suite
074:                TestSuite suite = new TestSuite();
075:
076:                // Add all positive parser tests
077:                String query = "select TESTCASE, INPUT, OUTPUT "
078:                        + "from {TESTCASE} rdf:type {test:PositiveParserTest}; "
079:                        + "                test:inputDocument {INPUT}; "
080:                        + "                test:outputDocument {OUTPUT}; "
081:                        + "                test:status {\"APPROVED\"} "
082:                        + "using namespace test = <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#>";
083:                TupleQueryResult queryResult = con.prepareTupleQuery(
084:                        QueryLanguage.SERQL, query).evaluate();
085:                while (queryResult.hasNext()) {
086:                    BindingSet bindingSet = queryResult.next();
087:                    String caseURI = bindingSet.getValue("TESTCASE").toString();
088:                    String inputURL = bindingSet.getValue("INPUT").toString();
089:                    String outputURL = bindingSet.getValue("OUTPUT").toString();
090:                    suite.addTest(new PositiveParserTest(caseURI, inputURL,
091:                            outputURL));
092:                }
093:
094:                queryResult.close();
095:
096:                // Add all negative parser tests
097:                query = "select TESTCASE, INPUT "
098:                        + "from {TESTCASE} rdf:type {test:NegativeParserTest}; "
099:                        + "                test:inputDocument {INPUT}; "
100:                        + "                test:status {\"APPROVED\"} "
101:                        + "using namespace test = <http://www.w3.org/2000/10/rdf-tests/rdfcore/testSchema#>";
102:                queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query)
103:                        .evaluate();
104:                while (queryResult.hasNext()) {
105:                    BindingSet bindingSet = queryResult.next();
106:                    String caseURI = bindingSet.getValue("TESTCASE").toString();
107:                    String inputURL = bindingSet.getValue("INPUT").toString();
108:                    suite.addTest(new NegativeParserTest(caseURI, inputURL));
109:                }
110:
111:                queryResult.close();
112:                con.close();
113:                repository.shutDown();
114:
115:                return suite;
116:            }
117:
118:            private static URL resolveURL(String urlString)
119:                    throws MalformedURLException {
120:                if (urlString.startsWith(W3C_TESTS_DIR)) {
121:                    // resolve to local copy
122:                    urlString = LOCAL_TESTS_DIR + "w3c-approved/"
123:                            + urlString.substring(W3C_TESTS_DIR.length());
124:                }
125:
126:                if (urlString.startsWith("/")) {
127:                    return RDFXMLParserTest.class.getResource(urlString);
128:                } else {
129:                    return new URL(urlString);
130:                }
131:            }
132:
133:            /*--------------------------------*
134:             * Inner class PositiveParserTest *
135:             *--------------------------------*/
136:
137:            private static class PositiveParserTest extends TestCase {
138:
139:                /*-----------*
140:                 * Variables *
141:                 *-----------*/
142:
143:                private String inputURL;
144:
145:                private String outputURL;
146:
147:                /*--------------*
148:                 * Constructors *
149:                 *--------------*/
150:
151:                public PositiveParserTest(String caseURI, String inputURL,
152:                        String outputURL) {
153:                    super (caseURI);
154:                    this .inputURL = inputURL;
155:                    this .outputURL = outputURL;
156:                }
157:
158:                /*---------*
159:                 * Methods *
160:                 *---------*/
161:
162:                @Override
163:                protected void runTest() throws Exception {
164:                    // Parse input data
165:                    RDFXMLParser rdfxmlParser = new RDFXMLParser();
166:                    rdfxmlParser
167:                            .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
168:                    rdfxmlParser.setParseStandAloneDocuments(true);
169:
170:                    Set<Statement> inputCollection = new LinkedHashSet<Statement>();
171:                    StatementCollector inputCollector = new StatementCollector(
172:                            inputCollection);
173:                    rdfxmlParser.setRDFHandler(inputCollector);
174:
175:                    InputStream in = resolveURL(inputURL).openStream();
176:                    rdfxmlParser.parse(in, inputURL);
177:                    in.close();
178:
179:                    // Parse expected output data
180:                    NTriplesParser ntriplesParser = new NTriplesParser();
181:                    ntriplesParser
182:                            .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
183:
184:                    Set<Statement> outputCollection = new LinkedHashSet<Statement>();
185:                    StatementCollector outputCollector = new StatementCollector(
186:                            outputCollection);
187:                    ntriplesParser.setRDFHandler(outputCollector);
188:
189:                    in = resolveURL(outputURL).openStream();
190:                    ntriplesParser.parse(in, inputURL);
191:                    in.close();
192:
193:                    // Check equality of the two models
194:                    if (!ModelUtil.equals(inputCollection, outputCollection)) {
195:                        StringBuilder sb = new StringBuilder(1024);
196:                        sb.append("models not equal\n");
197:                        sb.append("Expected:\n");
198:                        for (Statement st : outputCollection) {
199:                            sb.append(st).append("\n");
200:                        }
201:                        sb.append("Actual:\n");
202:                        for (Statement st : inputCollection) {
203:                            sb.append(st).append("\n");
204:                        }
205:
206:                        fail(sb.toString());
207:                    }
208:                }
209:
210:            } // end inner class PositiveParserTest
211:
212:            /*--------------------------------*
213:             * Inner class NegativeParserTest *
214:             *--------------------------------*/
215:
216:            private static class NegativeParserTest extends TestCase {
217:
218:                /*-----------*
219:                 * Variables *
220:                 *-----------*/
221:
222:                private String inputURL;
223:
224:                /*--------------*
225:                 * Constructors *
226:                 *--------------*/
227:
228:                public NegativeParserTest(String caseURI, String inputURL) {
229:                    super (caseURI);
230:                    this .inputURL = inputURL;
231:                }
232:
233:                /*---------*
234:                 * Methods *
235:                 *---------*/
236:
237:                @Override
238:                protected void runTest() {
239:                    try {
240:                        // Try parsing the input; this should result in an error being
241:                        // reported.
242:                        RDFXMLParser rdfxmlParser = new RDFXMLParser();
243:                        rdfxmlParser
244:                                .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
245:                        rdfxmlParser.setParseStandAloneDocuments(true);
246:
247:                        rdfxmlParser.setRDFHandler(new StatementCollector());
248:
249:                        InputStream in = resolveURL(inputURL).openStream();
250:                        rdfxmlParser.parse(in, inputURL);
251:                        in.close();
252:
253:                        fail("Parser parses erroneous data without reporting errors");
254:                    } catch (RDFParseException e) {
255:                        // This is expected as the input file is incorrect RDF
256:                    } catch (Exception e) {
257:                        fail("Error: " + e.getMessage());
258:                    }
259:                }
260:
261:            } // end inner class NegativeParserTest
262:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.