KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > webservice > test > 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.webservice.test;
18
19 import java.io.File JavaDoc;
20 import java.io.FileWriter JavaDoc;
21 import java.io.InputStream JavaDoc;
22
23 import org.alfresco.webservice.content.Content;
24 import org.alfresco.webservice.repository.UpdateResult;
25 import org.alfresco.webservice.types.CML;
26 import org.alfresco.webservice.types.CMLCreate;
27 import org.alfresco.webservice.types.ContentFormat;
28 import org.alfresco.webservice.types.NamedValue;
29 import org.alfresco.webservice.types.ParentReference;
30 import org.alfresco.webservice.types.Predicate;
31 import org.alfresco.webservice.types.Reference;
32 import org.alfresco.webservice.util.Constants;
33 import org.alfresco.webservice.util.ContentUtils;
34
35 import freemarker.log.Logger;
36
37 public class ContentServiceSystemTest extends BaseWebServiceSystemTest
38 {
39    private static final String JavaDoc CONTENT = "This is a small piece of content to test the create service call";
40    private static final String JavaDoc UPDATED_CONTENT = "This is some updated content to test the write service call";
41    
42    private String JavaDoc fileName = "unit-test.txt";
43    
44    public void testContentService()
45        throws Exception JavaDoc
46    {
47        ParentReference parentRef = new ParentReference();
48        parentRef.setStore(BaseWebServiceSystemTest.store);
49        parentRef.setUuid(BaseWebServiceSystemTest.rootReference.getUuid());
50        parentRef.setAssociationType(Constants.ASSOC_CHILDREN);
51        parentRef.setChildName(Constants.ASSOC_CHILDREN);
52        
53        NamedValue[] properties = new NamedValue[]{new NamedValue(Constants.PROP_NAME, this.fileName)};
54        CMLCreate create = new CMLCreate("1", parentRef, Constants.TYPE_CONTENT, properties);
55        CML cml = new CML();
56        cml.setCreate(new CMLCreate[]{create});
57        UpdateResult[] result = this.repositoryService.update(cml);
58        
59        Reference newContentNode = result[0].getDestination();
60        String JavaDoc property = Constants.PROP_CONTENT;
61        Predicate predicate = new Predicate(new Reference[]{newContentNode}, BaseWebServiceSystemTest.store, null);
62               
63        // First check a node that has no content set
64
Content[] contents1 = this.contentService.read(predicate, property);
65        assertNotNull(contents1);
66        assertEquals(1, contents1.length);
67        Content content1 = contents1[0];
68        assertNotNull(content1);
69        assertEquals(0, content1.getLength());
70        assertEquals(newContentNode.getUuid(), content1.getNode().getUuid());
71        assertEquals(property, content1.getProperty());
72        assertNull(content1.getUrl());
73        assertNull(content1.getFormat());
74        
75        // Write content
76
Content content2 = this.contentService.write(newContentNode, property, CONTENT.getBytes(), new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, "UTF-8"));
77        assertNotNull(content2);
78        assertTrue((content2.getLength() > 0));
79        assertEquals(newContentNode.getUuid(), content2.getNode().getUuid());
80        assertEquals(property, content2.getProperty());
81        assertNotNull(content2.getUrl());
82        assertNotNull(content2.getFormat());
83        ContentFormat format2 = content2.getFormat();
84        assertEquals(Constants.MIMETYPE_TEXT_PLAIN, format2.getMimetype());
85        assertEquals("UTF-8", format2.getEncoding());
86        assertEquals(CONTENT, ContentUtils.getContentAsString(content2));
87               
88        // Read content
89
Content[] contents3 = this.contentService.read(predicate, property);
90        assertNotNull(contents3);
91        assertEquals(1, contents3.length);
92        Content content3 = contents3[0];
93        assertNotNull(content3);
94        assertTrue((content3.getLength() > 0));
95        assertEquals(newContentNode.getUuid(), content3.getNode().getUuid());
96        assertEquals(property, content3.getProperty());
97        assertNotNull(content3.getUrl());
98        assertNotNull(content3.getFormat());
99        ContentFormat format3 = content3.getFormat();
100        assertEquals(Constants.MIMETYPE_TEXT_PLAIN, format3.getMimetype());
101        assertEquals("UTF-8", format3.getEncoding());
102        assertEquals(CONTENT, ContentUtils.getContentAsString(content3));
103        
104        // Update content
105
Content content4 = this.contentService.write(newContentNode, property, UPDATED_CONTENT.getBytes(), new ContentFormat(Constants.MIMETYPE_TEXT_CSS, "UTF-8"));
106        assertNotNull(content4);
107        assertTrue((content4.getLength() > 0));
108        assertEquals(newContentNode.getUuid(), content4.getNode().getUuid());
109        assertEquals(property, content4.getProperty());
110        assertNotNull(content4.getUrl());
111        assertNotNull(content4.getFormat());
112        ContentFormat format4 = content4.getFormat();
113        assertEquals(Constants.MIMETYPE_TEXT_CSS, format4.getMimetype());
114        assertEquals("UTF-8", format4.getEncoding());
115        assertEquals(UPDATED_CONTENT, ContentUtils.getContentAsString(content4));
116        
117        // Read updated content
118
Content[] contents5 = this.contentService.read(predicate, property);
119        assertNotNull(contents5);
120        assertEquals(1, contents5.length);
121        Content content5 = contents5[0];
122        assertNotNull(content5);
123        assertTrue((content5.getLength() > 0));
124        assertEquals(newContentNode.getUuid(), content5.getNode().getUuid());
125        assertEquals(property, content5.getProperty());
126        assertNotNull(content5.getUrl());
127        assertNotNull(content5.getFormat());
128        ContentFormat format5 = content5.getFormat();
129        assertEquals(Constants.MIMETYPE_TEXT_CSS, format5.getMimetype());
130        assertEquals("UTF-8", format5.getEncoding());
131        assertEquals(UPDATED_CONTENT, ContentUtils.getContentAsString(content5));
132        
133        // Clear content
134
Content[] contents6 = this.contentService.clear(predicate, property);
135        assertNotNull(contents6);
136        assertEquals(1, contents6.length);
137        Content content6 = contents6[0];
138        assertNotNull(content6);
139        assertEquals(0, content6.getLength());
140        assertEquals(newContentNode.getUuid(), content6.getNode().getUuid());
141        assertEquals(property, content6.getProperty());
142        assertNull(content6.getUrl());
143        assertNull(content6.getFormat());
144        
145        // Read cleared content
146
Content[] contents7 = this.contentService.read(predicate, property);
147        assertNotNull(contents7);
148        assertEquals(1, contents7.length);
149        Content content7 = contents7[0];
150        assertNotNull(content7);
151        assertEquals(0, content7.getLength());
152        assertEquals(newContentNode.getUuid(), content7.getNode().getUuid());
153        assertEquals(property, content7.getProperty());
154        assertNull(content7.getUrl());
155        assertNull(content7.getFormat());
156    }
157    
158    /**
159     * Test uploading content from file
160     *
161     * @throws Exception
162     */

163    public void testUploadContentFromFile() throws Exception JavaDoc
164    {
165        // Create the parent reference
166
ParentReference parentRef = new ParentReference();
167        parentRef.setStore(BaseWebServiceSystemTest.store);
168        parentRef.setUuid(BaseWebServiceSystemTest.rootReference.getUuid());
169        parentRef.setAssociationType(Constants.ASSOC_CHILDREN);
170        parentRef.setChildName(Constants.ASSOC_CHILDREN);
171        
172        // Create the content
173
NamedValue[] properties = new NamedValue[]{new NamedValue(Constants.PROP_NAME, "quick.doc")};
174        CMLCreate create = new CMLCreate("1", parentRef, Constants.TYPE_CONTENT, properties);
175        CML cml = new CML();
176        cml.setCreate(new CMLCreate[]{create});
177        UpdateResult[] result = this.repositoryService.update(cml);
178        
179        // Get the create node and create the format
180
Reference newContentNode = result[0].getDestination();
181        ContentFormat format = new ContentFormat("application/msword", "UTF-8");
182        
183        // Open the file and convert to byte array
184
InputStream JavaDoc viewStream = getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/quick.doc");
185        byte[] bytes = ContentUtils.convertToByteArray(viewStream);
186        
187        // Write the content
188
this.contentService.write(newContentNode, Constants.PROP_CONTENT, bytes, format);
189        
190        // Try and get the content, saving it to a file
191
Content[] contents = this.contentService.read(convertToPredicate(newContentNode), Constants.PROP_CONTENT);
192        assertNotNull(contents);
193        assertEquals(1, contents.length);
194        Content content = contents[0];
195        File JavaDoc tempFile = File.createTempFile("testDoc", ".doc");
196        System.out.println(tempFile.getPath());
197        ContentUtils.copyContentToFile(content, tempFile);
198    }
199    
200    /**
201     * Test uploading image from file
202     *
203     * @throws Exception
204     */

205    public void testUploadImageFromFile() throws Exception JavaDoc
206    {
207        // Create the parent reference
208
ParentReference parentRef = new ParentReference();
209        parentRef.setStore(BaseWebServiceSystemTest.store);
210        parentRef.setUuid(BaseWebServiceSystemTest.rootReference.getUuid());
211        parentRef.setAssociationType(Constants.ASSOC_CHILDREN);
212        parentRef.setChildName(Constants.ASSOC_CHILDREN);
213        
214        // Create the content
215
NamedValue[] properties = new NamedValue[]{new NamedValue(Constants.PROP_NAME, "test.jpg")};
216        CMLCreate create = new CMLCreate("1", parentRef, Constants.TYPE_CONTENT, properties);
217        CML cml = new CML();
218        cml.setCreate(new CMLCreate[]{create});
219        UpdateResult[] result = this.repositoryService.update(cml);
220        
221        // Get the created node and create the format
222
Reference newContentNode = result[0].getDestination();
223        ContentFormat format = new ContentFormat("image/jpeg", "UTF-8");
224        
225        // Open the file and convert to byte array
226
InputStream JavaDoc viewStream = getClass().getClassLoader().getResourceAsStream("org/alfresco/webservice/test/resources/test.jpg");
227        byte[] bytes = ContentUtils.convertToByteArray(viewStream);
228        
229        // Write the content
230
this.contentService.write(newContentNode, Constants.PROP_CONTENT, bytes, format);
231        
232        // Try and get the content, saving it to a file
233
Content[] contents = this.contentService.read(convertToPredicate(newContentNode), Constants.PROP_CONTENT);
234        assertNotNull(contents);
235        assertEquals(1, contents.length);
236        Content content = contents[0];
237        File JavaDoc tempFile = File.createTempFile("testImage", ".jpg");
238        System.out.println(tempFile.getPath());
239        ContentUtils.copyContentToFile(content, tempFile);
240
241    }
242 }
243
Popular Tags