View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.chemistry.opencmis.client.bindings.atompub;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.math.BigDecimal;
24  import java.math.BigInteger;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import junit.framework.TestCase;
29  
30  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.AtomEntryWriter;
31  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.AtomPubParser;
32  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomBase;
33  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomElement;
34  import org.apache.chemistry.opencmis.client.bindings.spi.atompub.objects.AtomEntry;
35  import org.apache.chemistry.opencmis.commons.PropertyIds;
36  import org.apache.chemistry.opencmis.commons.data.ContentStream;
37  import org.apache.chemistry.opencmis.commons.data.ObjectData;
38  import org.apache.chemistry.opencmis.commons.data.PropertyData;
39  import org.apache.chemistry.opencmis.commons.enums.CmisVersion;
40  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ContentStreamImpl;
41  import org.apache.chemistry.opencmis.commons.impl.dataobjects.ObjectDataImpl;
42  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertiesImpl;
43  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyDecimalImpl;
44  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyIntegerImpl;
45  import org.apache.chemistry.opencmis.commons.impl.dataobjects.PropertyStringImpl;
46  
47  /**
48   * Minimal test for AtomEntryWriter and AtomPubParser.
49   */
50  public class AtomParserTest extends TestCase {
51  
52      private static final byte[] CONTENT = "This is my test content!".getBytes();
53      private static final String CONTENT_TYPE = "text/plain";
54  
55      public void testParser() throws Exception {
56          ByteArrayOutputStream bao = new ByteArrayOutputStream();
57  
58          // set up an object
59          PropertiesImpl properties = new PropertiesImpl();
60  
61          PropertyStringImpl propName = new PropertyStringImpl(PropertyIds.NAME, "TestName");
62          properties.addProperty(propName);
63  
64          PropertyIntegerImpl propInt = new PropertyIntegerImpl("IntProp", Arrays.asList(new BigInteger[] {
65                  BigInteger.valueOf(1), BigInteger.valueOf(2), BigInteger.valueOf(3) }));
66          properties.addProperty(propInt);
67  
68          PropertyDecimalImpl propDec = new PropertyDecimalImpl("DecProp", new BigDecimal(
69                  "3.14159253589793238462643383279502884197"
70                          + "169399375105820974944592307816406286208998628034825342117067982148086513"));
71          properties.addProperty(propDec);
72  
73          ObjectDataImpl object1 = new ObjectDataImpl();
74          object1.setProperties(properties);
75  
76          // write the entry
77          ContentStream contentStream = new ContentStreamImpl(null, BigInteger.valueOf(CONTENT.length), CONTENT_TYPE,
78                  new ByteArrayInputStream(CONTENT));
79          AtomEntryWriter aew = new AtomEntryWriter(object1, CmisVersion.CMIS_1_1, contentStream);
80          aew.write(bao);
81  
82          byte[] entryContent = bao.toByteArray();
83          assertTrue(entryContent.length > 0);
84  
85          // parse it
86          AtomPubParser parser = new AtomPubParser(new ByteArrayInputStream(entryContent));
87          parser.parse();
88          AtomBase parseResult = parser.getResults();
89  
90          assertTrue(parseResult instanceof AtomEntry);
91          AtomEntry entry = (AtomEntry) parseResult;
92  
93          assertNotNull(entry);
94          assertFalse(entry.getElements().isEmpty());
95  
96          // find the object
97          ObjectData object2 = null;
98          for (AtomElement element : entry.getElements()) {
99              if (element.getObject() instanceof ObjectData) {
100                 assertNull(object2);
101                 object2 = (ObjectData) element.getObject();
102             }
103         }
104 
105         assertNotNull(object2);
106         assertNotNull(object2.getProperties());
107 
108         // compare properties
109         for (PropertyData<?> property1 : object1.getProperties().getPropertyList()) {
110             PropertyData<?> property2 = object2.getProperties().getProperties().get(property1.getId());
111 
112             assertNotNull(property2);
113             assertEquals(property1, property2);
114         }
115     }
116 
117     protected void assertEquals(PropertyData<?> expected, PropertyData<?> actual) throws Exception {
118         if (expected == null && actual == null) {
119             return;
120         }
121 
122         if (expected == null || actual == null) {
123             fail("Property is null!");
124         }
125 
126         assertEquals(expected.getId(), actual.getId());
127         assertSame(expected.getClass(), actual.getClass());
128 
129         List<?> values1 = expected.getValues();
130         assertNotNull(values1);
131         assertFalse(values1.isEmpty());
132 
133         List<?> values2 = actual.getValues();
134         assertNotNull(values2);
135         assertFalse(values2.isEmpty());
136 
137         assertEquals(values1.size(), values2.size());
138 
139         for (int i = 0; i < values1.size(); i++) {
140             assertEquals(values1.get(i), values2.get(i));
141         }
142     }
143 }