Source Code Cross Referenced for ProtocolTest.java in  » RSS-RDF » sesame » org » openrdf » http » server » 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.http.server 
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.http.server;
007:
008:        import java.io.InputStream;
009:        import java.io.OutputStream;
010:        import java.net.HttpURLConnection;
011:        import java.net.URL;
012:        import java.net.URLEncoder;
013:
014:        import junit.framework.TestCase;
015:
016:        import info.aduna.io.IOUtil;
017:        import info.aduna.net.http.HttpClientUtil;
018:
019:        import org.openrdf.http.protocol.Protocol;
020:        import org.openrdf.model.impl.URIImpl;
021:        import org.openrdf.query.QueryLanguage;
022:        import org.openrdf.query.TupleQueryResult;
023:        import org.openrdf.query.resultio.QueryResultIO;
024:        import org.openrdf.query.resultio.TupleQueryResultFormat;
025:        import org.openrdf.rio.RDFFormat;
026:
027:        public class ProtocolTest extends TestCase {
028:
029:            private TestServer server;
030:
031:            @Override
032:            protected void setUp() throws Exception {
033:                server = new TestServer();
034:                server.start();
035:            }
036:
037:            @Override
038:            protected void tearDown() throws Exception {
039:                server.stop();
040:            }
041:
042:            /**
043:             * Tests the server's methods for updating all data in a repository.
044:             */
045:            public void testRepository_PUT() throws Exception {
046:                putFile(Protocol
047:                        .getStatementsLocation(TestServer.REPOSITORY_URL),
048:                        "/testcases/default-graph-1.ttl");
049:            }
050:
051:            /**
052:             * Tests the server's methods for deleting all data in a repository.
053:             */
054:            public void testRepository_DELETE() throws Exception {
055:                delete(Protocol
056:                        .getStatementsLocation(TestServer.REPOSITORY_URL));
057:            }
058:
059:            /**
060:             * Tests the server's methods for updating the data in the default context of
061:             * a repository.
062:             */
063:            public void testNullContext_PUT() throws Exception {
064:                String location = Protocol
065:                        .getStatementsLocation(TestServer.REPOSITORY_URL);
066:                location = HttpClientUtil.appendParameter(location,
067:                        Protocol.CONTEXT_PARAM_NAME, Protocol.NULL_PARAM_VALUE);
068:                putFile(location, "/testcases/default-graph-1.ttl");
069:            }
070:
071:            /**
072:             * Tests the server's methods for deleting the data from the default context
073:             * of a repository.
074:             */
075:            public void testNullContext_DELETE() throws Exception {
076:                String location = Protocol
077:                        .getStatementsLocation(TestServer.REPOSITORY_URL);
078:                location = HttpClientUtil.appendParameter(location,
079:                        Protocol.CONTEXT_PARAM_NAME, Protocol.NULL_PARAM_VALUE);
080:                delete(location);
081:            }
082:
083:            /**
084:             * Tests the server's methods for updating the data in a named context of a
085:             * repository.
086:             */
087:            public void testNamedContext_PUT() throws Exception {
088:                String location = Protocol
089:                        .getStatementsLocation(TestServer.REPOSITORY_URL);
090:                String encContext = Protocol.encodeValue(new URIImpl(
091:                        "urn:x-local:graph1"));
092:                location = HttpClientUtil.appendParameter(location,
093:                        Protocol.CONTEXT_PARAM_NAME, encContext);
094:                putFile(location, "/testcases/named-graph-1.ttl");
095:            }
096:
097:            /**
098:             * Tests the server's methods for deleting the data from a named context of a
099:             * repository.
100:             */
101:            public void testNamedContext_DELETE() throws Exception {
102:                String location = Protocol
103:                        .getStatementsLocation(TestServer.REPOSITORY_URL);
104:                String encContext = Protocol.encodeValue(new URIImpl(
105:                        "urn:x-local:graph1"));
106:                location = HttpClientUtil.appendParameter(location,
107:                        Protocol.CONTEXT_PARAM_NAME, encContext);
108:                delete(location);
109:            }
110:
111:            /**
112:             * Tests the server's methods for quering a repository using GET requests to
113:             * send SeRQL-select queries.
114:             */
115:            public void testSeRQLselect() throws Exception {
116:                TupleQueryResult queryResult = evaluate(
117:                        TestServer.REPOSITORY_URL, "select * from {X} P {Y}",
118:                        QueryLanguage.SERQL);
119:                QueryResultIO.write(queryResult, TupleQueryResultFormat.SPARQL,
120:                        System.out);
121:            }
122:
123:            private void putFile(String location, String file) throws Exception {
124:                System.out.println("Put file to " + location);
125:
126:                URL url = new URL(location);
127:                HttpURLConnection conn = (HttpURLConnection) url
128:                        .openConnection();
129:                conn.setRequestMethod("PUT");
130:                conn.setDoOutput(true);
131:
132:                RDFFormat dataFormat = RDFFormat.forFileName(file,
133:                        RDFFormat.RDFXML);
134:                conn.setRequestProperty("Content-Type", dataFormat
135:                        .getDefaultMIMEType());
136:
137:                InputStream dataStream = ProtocolTest.class
138:                        .getResourceAsStream(file);
139:                try {
140:                    OutputStream connOut = conn.getOutputStream();
141:
142:                    try {
143:                        IOUtil.transfer(dataStream, connOut);
144:                    } finally {
145:                        connOut.close();
146:                    }
147:                } finally {
148:                    dataStream.close();
149:                }
150:
151:                conn.connect();
152:
153:                int responseCode = conn.getResponseCode();
154:
155:                if (responseCode != HttpURLConnection.HTTP_OK && // 200 OK
156:                        responseCode != HttpURLConnection.HTTP_NO_CONTENT) // 204 NO CONTENT
157:                {
158:                    String response = "location " + location + " responded: "
159:                            + conn.getResponseMessage() + " (" + responseCode
160:                            + ")";
161:                    fail(response);
162:                }
163:            }
164:
165:            private void delete(String location) throws Exception {
166:                URL url = new URL(location);
167:                HttpURLConnection conn = (HttpURLConnection) url
168:                        .openConnection();
169:                conn.setRequestMethod("DELETE");
170:
171:                conn.connect();
172:
173:                int responseCode = conn.getResponseCode();
174:
175:                if (responseCode != HttpURLConnection.HTTP_OK && // 200 OK
176:                        responseCode != HttpURLConnection.HTTP_NO_CONTENT) // 204 NO CONTENT
177:                {
178:                    String response = "location " + location + " responded: "
179:                            + conn.getResponseMessage() + " (" + responseCode
180:                            + ")";
181:                    fail(response);
182:                }
183:            }
184:
185:            private TupleQueryResult evaluate(String location, String query,
186:                    QueryLanguage queryLn) throws Exception {
187:                location += "?query=" + URLEncoder.encode(query, "UTF-8")
188:                        + "&queryLn=" + queryLn.getName();
189:
190:                URL url = new URL(location);
191:
192:                HttpURLConnection conn = (HttpURLConnection) url
193:                        .openConnection();
194:
195:                // Request SPARQL-XML formatted results:
196:                conn.setRequestProperty("Accept", TupleQueryResultFormat.SPARQL
197:                        .getDefaultMIMEType());
198:
199:                conn.connect();
200:
201:                try {
202:                    int responseCode = conn.getResponseCode();
203:                    if (responseCode == HttpURLConnection.HTTP_OK) {
204:                        // Process query results
205:                        return QueryResultIO.parse(conn.getInputStream(),
206:                                TupleQueryResultFormat.SPARQL);
207:                    } else {
208:                        String response = "location " + location
209:                                + " responded: " + conn.getResponseMessage()
210:                                + " (" + responseCode + ")";
211:                        fail(response);
212:                        throw new RuntimeException(response);
213:                    }
214:                } finally {
215:                    conn.disconnect();
216:                }
217:            }
218:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.