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: }
|