Source Code Cross Referenced for N3ParserTest.java in  » RSS-RDF » sesame » org » openrdf » rio » n3 » 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.n3 
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.n3;
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.LinkedList;
013:        import java.util.List;
014:        import java.util.Set;
015:
016:        import junit.framework.Test;
017:        import junit.framework.TestCase;
018:        import junit.framework.TestSuite;
019:
020:        import org.openrdf.model.Statement;
021:        import org.openrdf.model.util.ModelUtil;
022:        import org.openrdf.query.BindingSet;
023:        import org.openrdf.query.QueryLanguage;
024:        import org.openrdf.query.TupleQueryResult;
025:        import org.openrdf.repository.Repository;
026:        import org.openrdf.repository.RepositoryConnection;
027:        import org.openrdf.repository.sail.SailRepository;
028:        import org.openrdf.rio.RDFFormat;
029:        import org.openrdf.rio.RDFParseException;
030:        import org.openrdf.rio.RDFParser;
031:        import org.openrdf.rio.helpers.StatementCollector;
032:        import org.openrdf.rio.ntriples.NTriplesParser;
033:        import org.openrdf.rio.turtle.TurtleParser;
034:        import org.openrdf.sail.memory.MemoryStore;
035:
036:        /**
037:         * JUnit test for the N3 parser that uses the tests that are available <a
038:         * href="http://www.w3.org/2000/10/swap/test/n3parser.tests">online</a>.
039:         */
040:        public class N3ParserTest {
041:
042:            /*-----------*
043:             * Constants *
044:             *-----------*/
045:
046:            private static String BASE_URL = "http://www.w3.org/2000/10/swap/test/";
047:
048:            private static String MANIFEST_URL = "http://www.w3.org/2000/10/swap/test/n3parser.tests";
049:
050:            /*--------------------*
051:             * Static initializer *
052:             *--------------------*/
053:
054:            public static Test suite() throws Exception {
055:                // Create test suite
056:                TestSuite suite = new TestSuite();
057:
058:                // Add the manifest to a repository and query it
059:                Repository repository = new SailRepository(new MemoryStore());
060:                repository.initialize();
061:                RepositoryConnection con = repository.getConnection();
062:
063:                URL url = new URL(MANIFEST_URL);
064:                con.add(url, MANIFEST_URL, RDFFormat.TURTLE);
065:
066:                // Add all positive parser tests to the test suite
067:                String query = "SELECT testURI, inputURL, outputURL "
068:                        + "FROM {testURI} rdf:type {n3test:PositiveParserTest}; "
069:                        + "               n3test:inputDocument {inputURL}; "
070:                        + "               n3test:outputDocument {outputURL} "
071:                        + "USING NAMESPACE n3test = <http://www.w3.org/2004/11/n3test#>";
072:
073:                TupleQueryResult queryResult = con.prepareTupleQuery(
074:                        QueryLanguage.SERQL, query).evaluate();
075:                while (queryResult.hasNext()) {
076:                    BindingSet bindingSet = queryResult.next();
077:                    String testURI = bindingSet.getValue("testURI").toString();
078:                    String inputURL = bindingSet.getValue("inputURL")
079:                            .toString();
080:                    String outputURL = bindingSet.getValue("outputURL")
081:                            .toString();
082:
083:                    suite.addTest(new PositiveParserTest(testURI, inputURL,
084:                            outputURL));
085:                }
086:
087:                queryResult.close();
088:
089:                // Add all negative parser tests to the test suite
090:                query = "SELECT testURI, inputURL "
091:                        + "FROM {testURI} rdf:type {n3test:NegativeParserTest}; "
092:                        + "               n3test:inputDocument {inputURL} "
093:                        + "USING NAMESPACE n3test = <http://www.w3.org/2004/11/n3test#>";
094:
095:                queryResult = con.prepareTupleQuery(QueryLanguage.SERQL, query)
096:                        .evaluate();
097:
098:                while (queryResult.hasNext()) {
099:                    BindingSet bindingSet = queryResult.next();
100:                    String testURI = bindingSet.getValue("testURI").toString();
101:                    String inputURL = bindingSet.getValue("inputURL")
102:                            .toString();
103:
104:                    suite.addTest(new NegativeParserTest(testURI, inputURL));
105:                }
106:                queryResult.close();
107:                con.close();
108:                repository.shutDown();
109:
110:                return suite;
111:            }
112:
113:            /*--------------------------------*
114:             * Inner class PositiveParserTest *
115:             *--------------------------------*/
116:
117:            private static class PositiveParserTest extends TestCase {
118:
119:                /*-----------*
120:                 * Variables *
121:                 *-----------*/
122:
123:                private URL inputURL;
124:
125:                private URL outputURL;
126:
127:                /*--------------*
128:                 * Constructors *
129:                 *--------------*/
130:
131:                public PositiveParserTest(String testURI, String inputURL,
132:                        String outputURL) throws MalformedURLException {
133:                    super (testURI);
134:                    this .inputURL = new URL(inputURL);
135:                    this .outputURL = new URL(outputURL);
136:                }
137:
138:                /*---------*
139:                 * Methods *
140:                 *---------*/
141:
142:                @Override
143:                protected void runTest() throws Exception {
144:                    // Parse input data
145:                    TurtleParser turtleParser = new TurtleParser();
146:                    turtleParser
147:                            .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
148:
149:                    Set<Statement> inputCollection = new LinkedHashSet<Statement>();
150:                    StatementCollector inputCollector = new StatementCollector(
151:                            inputCollection);
152:                    turtleParser.setRDFHandler(inputCollector);
153:
154:                    InputStream in = inputURL.openStream();
155:                    turtleParser.parse(in, inputURL.toExternalForm());
156:                    in.close();
157:
158:                    // Parse expected output data
159:                    NTriplesParser ntriplesParser = new NTriplesParser();
160:                    ntriplesParser
161:                            .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
162:
163:                    Set<Statement> outputCollection = new LinkedHashSet<Statement>();
164:                    StatementCollector outputCollector = new StatementCollector(
165:                            outputCollection);
166:                    ntriplesParser.setRDFHandler(outputCollector);
167:
168:                    in = outputURL.openStream();
169:                    ntriplesParser.parse(in, outputURL.toExternalForm());
170:                    in.close();
171:
172:                    // Check equality of the two models
173:                    if (!ModelUtil.equals(inputCollection, outputCollection)) {
174:                        System.err.println("===models not equal===");
175:                        // System.err.println("Expected: " + outputCollection);
176:                        // System.err.println("Actual : " + inputCollection);
177:                        // System.err.println("======================");
178:
179:                        List<Statement> missing = new LinkedList<Statement>(
180:                                outputCollection);
181:                        missing.removeAll(inputCollection);
182:
183:                        List<Statement> unexpected = new LinkedList<Statement>(
184:                                inputCollection);
185:                        unexpected.removeAll(outputCollection);
186:
187:                        if (!missing.isEmpty()) {
188:                            System.err.println("Missing   : " + missing);
189:                        }
190:                        if (!unexpected.isEmpty()) {
191:                            System.err.println("Unexpected: " + unexpected);
192:                        }
193:
194:                        fail("models not equal");
195:                    }
196:                }
197:
198:            } // end inner class PositiveParserTest
199:
200:            /*--------------------------------*
201:             * Inner class NegativeParserTest *
202:             *--------------------------------*/
203:
204:            private static class NegativeParserTest extends TestCase {
205:
206:                /*-----------*
207:                 * Variables *
208:                 *-----------*/
209:
210:                private URL inputURL;
211:
212:                /*--------------*
213:                 * Constructors *
214:                 *--------------*/
215:
216:                public NegativeParserTest(String testURI, String inputURL)
217:                        throws MalformedURLException {
218:                    super (testURI);
219:                    this .inputURL = new URL(inputURL);
220:                }
221:
222:                /*---------*
223:                 * Methods *
224:                 *---------*/
225:
226:                @Override
227:                protected void runTest() {
228:                    try {
229:                        // Try parsing the input; this should result in an error being
230:                        // reported.
231:                        TurtleParser turtleParser = new TurtleParser();
232:                        turtleParser
233:                                .setDatatypeHandling(RDFParser.DatatypeHandling.IGNORE);
234:
235:                        turtleParser.setRDFHandler(new StatementCollector());
236:
237:                        InputStream in = inputURL.openStream();
238:                        turtleParser.parse(in, inputURL.toExternalForm());
239:                        in.close();
240:
241:                        fail("Parser parses erroneous data without reporting errors");
242:                    } catch (RDFParseException e) {
243:                        // This is expected as the input file is incorrect RDF
244:                    } catch (Exception e) {
245:                        fail("Error: " + e.getMessage());
246:                    }
247:                }
248:
249:            } // end inner class NegativeParserTest
250:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.