]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e9893fecc56b44c11c58b85e816f17a0f125f376
[tmp/jakarta-migration.git] /
1 package org.collectionspace.hello.test;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5
6 import java.io.BufferedReader;
7 import java.io.InputStreamReader;
8 import java.io.OutputStream;
9 import java.net.HttpURLConnection;
10 import java.net.URL;
11
12 /**
13  * @version $Revision: 1 $
14  */
15 public class IdentifierServiceRawXmlTest {
16
17     @Test
18     public void testIdentifierResource() throws Exception {
19         verbose("create a new Identifier");
20         // Create a new object
21         String newIdentifier = "<ns2:identifier xmlns:ns2=\"http://collectionspace.org/hello\">" +
22                 "<namespace>edu.stanford</namespace>" +
23                 "</ns2:identifier>";
24
25         URL postUrl = new URL("http://localhost:8080/helloworld/cspace/identifiers");
26         HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
27         connection.setDoOutput(true);
28         connection.setInstanceFollowRedirects(false);
29         connection.setRequestMethod("POST");
30         connection.setRequestProperty("Content-Type", "application/xml");
31         OutputStream os = connection.getOutputStream();
32         os.write(newIdentifier.getBytes());
33         os.flush();
34         Assert.assertEquals(HttpURLConnection.HTTP_CREATED, connection.getResponseCode());
35         String createdUrl = connection.getHeaderField("Location");
36         verbose("Location: " + createdUrl);
37         connection.disconnect();
38
39
40         // Get the new object
41         verbose("get created Identifier");
42         URL getUrl = new URL(createdUrl);
43         connection = (HttpURLConnection) getUrl.openConnection();
44         connection.setRequestMethod("GET");
45         verbose("Content-Type: " + connection.getContentType());
46
47         BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
48
49         String line = reader.readLine();
50         while (line != null) {
51             verbose(line);
52             line = reader.readLine();
53         }
54         Assert.assertEquals(HttpURLConnection.HTTP_OK, connection.getResponseCode());
55         connection.disconnect();
56
57     }
58
59     private void verbose(String msg) {
60         System.out.println("IdentifierServiceRawXmlTest : " + msg);
61     }
62 }