KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > example > webservice > content > ContentServiceSystemTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.example.webservice.content;
18
19 import java.io.InputStream JavaDoc;
20 import java.net.URL JavaDoc;
21 import java.net.URLConnection JavaDoc;
22
23 import javax.xml.rpc.ServiceException JavaDoc;
24
25 import junit.framework.AssertionFailedError;
26
27 import org.alfresco.example.webservice.BaseWebServiceSystemTest;
28 import org.alfresco.example.webservice.types.Content;
29 import org.alfresco.example.webservice.types.ContentFormat;
30 import org.alfresco.example.webservice.types.ParentReference;
31 import org.alfresco.example.webservice.types.Predicate;
32 import org.alfresco.example.webservice.types.Reference;
33 import org.apache.axis.EngineConfiguration;
34 import org.apache.axis.configuration.FileProvider;
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 public class ContentServiceSystemTest extends BaseWebServiceSystemTest
39 {
40    private static Log logger = LogFactory.getLog(ContentServiceSystemTest.class);
41    private static final String JavaDoc CONTENT = "This is a small piece of content to test the create service call";
42    private static final String JavaDoc UPDATED_CONTENT = "This is some updated content to test the write service call";
43    
44    private static String JavaDoc newContentId;
45    private ContentServiceSoapBindingStub contentService;
46    private String JavaDoc fileName = "unit-test.txt";
47    
48
49    @Override JavaDoc
50    protected void setUp() throws Exception JavaDoc
51    {
52       super.setUp();
53
54       try
55       {
56          EngineConfiguration config = new FileProvider(getResourcesDir(), "client-deploy.wsdd");
57          this.contentService = (ContentServiceSoapBindingStub)new ContentServiceLocator(config).getContentService();
58       }
59       catch (ServiceException JavaDoc jre)
60       {
61          if (jre.getLinkedCause() != null)
62          {
63             jre.getLinkedCause().printStackTrace();
64          }
65          
66          throw new AssertionFailedError("JAX-RPC ServiceException caught: " + jre);
67       }
68       
69       assertNotNull("contentService is null", this.contentService);
70       
71       // Time out after a minute
72
this.contentService.setTimeout(60000);
73    }
74    
75    /**
76     * Tests the create content service method
77     *
78     * @throws Exception
79     */

80    public void testCreate() throws Exception JavaDoc
81    {
82       // get the root node (hard code for now until we have a way to query for the root node)
83
ParentReference root = new ParentReference();
84       root.setStore(STORE);
85       root.setUuid(companyHomeId);
86       
87       String JavaDoc mimetype = "text/plain";
88       Content content = this.contentService.create(root, this.fileName, new ContentFormat(mimetype, "UTF-8"), CONTENT.getBytes());
89       assertNotNull("returned content should not be null", content);
90       assertNotNull("format should not be null", content.getFormat());
91       assertEquals("Mimetype should match what was sent", mimetype, content.getFormat().getMimetype());
92       newContentId = content.getReference().getUuid();
93       logger.debug("created new content with id: " + newContentId);
94    }
95    
96    /**
97     * Tests the read method
98     *
99     * @throws Exception
100     */

101    public void testRead() throws Exception JavaDoc
102    {
103       if (newContentId == null)
104       {
105          fail("Failed to locate id of " + this.fileName);
106       }
107       
108       Reference node = new Reference();
109       node.setStore(STORE);
110       node.setUuid(newContentId);
111       
112       ReadResult result = this.contentService.read(node);
113       assertNotNull("read result should not be null", result);
114       logger.debug("url for download is: " + result.getUrl());
115       assertNotNull("Url to read content from must not be null", result.getUrl());
116       
117       // make sure length is reported as the same as the content used to create the file
118
assertEquals("Content length's don't match", CONTENT.length(), result.getContent().getLength());
119    }
120    
121    /**
122     * Tests the write service method
123     *
124     * @throws Exception
125     */

126    public void testWrite() throws Exception JavaDoc
127    {
128       if (newContentId == null)
129       {
130          fail("Failed to locate id of " + this.fileName);
131       }
132       
133       Reference node = new Reference();
134       node.setStore(STORE);
135       node.setUuid(newContentId);
136       
137       this.contentService.write(node, UPDATED_CONTENT.getBytes());
138
139       ReadResult result = this.contentService.read(node);
140       assertNotNull("read result should not be null", result);
141       assertNotNull("Url to read content from must not be null", result.getUrl());
142       
143       long contentLength = result.getContent().getLength();
144       assertEquals("Content length's don't match", UPDATED_CONTENT.length(), contentLength);
145       assertTrue("Content length of update content should not be the same as the previous content length",
146             contentLength != CONTENT.length());
147       
148       /* TODO: At some point we will have to provide the login credentials
149                for the download servlet (auth filter); it needs to look for a ticket
150                on the URL */

151                
152       // read the contents of the URL and make sure they match
153
StringBuilder JavaDoc readContent = new StringBuilder JavaDoc();
154       URL JavaDoc url = new URL JavaDoc(result.getUrl());
155       URLConnection JavaDoc conn = url.openConnection();
156       InputStream JavaDoc is = conn.getInputStream();
157       int read = is.read();
158       while (read != -1)
159       {
160          readContent.append((char)read);
161          read = is.read();
162       }
163       
164       // make sure the content in the repository is correct
165
logger.debug("Content from repository: " + readContent.toString());
166       assertEquals("Content does not match", UPDATED_CONTENT, readContent.toString());
167    }
168    
169    /**
170     * Tests the exists service method
171     *
172     * @throws Exception
173     */

174    public void testExists() throws Exception JavaDoc
175    {
176       if (newContentId == null)
177       {
178          fail("Failed to locate id of " + this.fileName);
179       }
180       
181       // create the predicate representation of the content
182
Reference ref = new Reference();
183       ref.setStore(STORE);
184       ref.setUuid(newContentId);
185       Predicate predicate = new Predicate(new Reference[] {ref}, null, null);
186       
187       ExistsResult[] existsResult = this.contentService.exists(predicate);
188       assertNotNull("exists result should not be null", existsResult);
189       
190       // we only added one object so there should only be one result!
191
assertTrue("There should be one result", existsResult.length == 1);
192       assertTrue("The node should have existed", existsResult[0].isExists());
193       assertTrue("Content length should match", existsResult[0].getLength() == UPDATED_CONTENT.length());
194    }
195    
196    /**
197     * Tests the describe service method
198     *
199     * @throws Exception
200     */

201    public void testDescribe() throws Exception JavaDoc
202    {
203       if (newContentId == null)
204       {
205          fail("Failed to locate id of " + this.fileName);
206       }
207       
208       // create the predicate representation of the content
209
Reference ref = new Reference();
210       ref.setStore(STORE);
211       ref.setUuid(newContentId);
212       Predicate predicate = new Predicate(new Reference[] {ref}, null, null);
213       
214       Content[] contentDesc = this.contentService.describe(predicate);
215       assertNotNull("describe result should not be null", contentDesc);
216       
217       // we only added one object so there should only be one result!
218
assertTrue("There should be one result", contentDesc.length == 1);
219       
220       // dump all the results
221
Content content = contentDesc[0];
222       String JavaDoc id = content.getReference().getUuid();
223       String JavaDoc type = content.getType();
224       String JavaDoc mimetype = content.getFormat().getMimetype();
225       String JavaDoc encoding = content.getFormat().getEncoding();
226       long length = content.getLength();
227       if (logger.isDebugEnabled())
228       {
229          logger.debug("id = " + id);
230          logger.debug("type = " + type);
231          logger.debug("mimetype = " + mimetype);
232          logger.debug("encoding = " + encoding);
233          logger.debug("length = " + length);
234       }
235       
236       // do some sanity checking
237
assertEquals("The id is incorrect", newContentId, id);
238       assertEquals("The type is incorrect", "{http://www.alfresco.org/model/content/1.0}content", type);
239       assertEquals("The mimetype is incorrect", "text/plain", mimetype);
240       assertEquals("The encoding is incorrect", "UTF-8", encoding);
241       assertEquals("The length is incorrect", UPDATED_CONTENT.length(), length);
242    }
243    
244    /**
245     * Tests the delete service method
246     *
247     * @throws Exception
248     */

249    public void testDelete() throws Exception JavaDoc
250    {
251       if (newContentId == null)
252       {
253          fail("Failed to locate id of " + this.fileName);
254       }
255       
256       // create the predicate representation of the content
257
Reference ref = new Reference();
258       ref.setStore(STORE);
259       ref.setUuid(newContentId);
260       Predicate predicate = new Predicate(new Reference[] {ref}, null, null);
261       
262       Reference[] refs = this.contentService.delete(predicate);
263       assertNotNull("delete result should not be null", refs);
264       
265       // now check that the node no longer exists
266
ExistsResult[] existsResult = this.contentService.exists(predicate);
267       assertNotNull("exists result should not be null", existsResult);
268       assertFalse("The node should no longer exist", existsResult[0].isExists());
269    }
270 }
271
Popular Tags