This project has retired. For details please refer to its Attic page.
AtomParserTest xref

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.lang.reflect.Method;
24  import java.math.BigDecimal;
25  import java.math.BigInteger;
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.impl.jaxb.CmisObjectType;
37  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisPropertiesType;
38  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisProperty;
39  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisPropertyDecimal;
40  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisPropertyInteger;
41  import org.apache.chemistry.opencmis.commons.impl.jaxb.CmisPropertyString;
42  
43  /**
44   * Minimal test for AtomEntryWriter and AtomPubParser.
45   *
46   * @author <a href="mailto:fmueller@opentext.com">Florian M&uuml;ller</a>
47   *
48   */
49  public class AtomParserTest extends TestCase {
50  
51      private static final byte[] CONTENT = "This is my test content!".getBytes();
52      private static final String CONTENT_TYPE = "text/plain";
53  
54      public void testParser() throws Exception {
55          ByteArrayOutputStream bao = new ByteArrayOutputStream();
56  
57          // set up an object
58          CmisPropertiesType properties = new CmisPropertiesType();
59  
60          CmisPropertyString propName = new CmisPropertyString();
61          propName.setPropertyDefinitionId(PropertyIds.NAME);
62          propName.getValue().add("TestName");
63          properties.getProperty().add(propName);
64  
65          CmisPropertyInteger propInt = new CmisPropertyInteger();
66          propInt.setPropertyDefinitionId("IntProp");
67          propInt.getValue().add(BigInteger.valueOf(1));
68          propInt.getValue().add(BigInteger.valueOf(2));
69          propInt.getValue().add(BigInteger.valueOf(3));
70          properties.getProperty().add(propInt);
71  
72          CmisPropertyDecimal propDec = new CmisPropertyDecimal();
73          propDec.setPropertyDefinitionId("DecProp");
74          propDec.getValue().add(
75                  new BigDecimal("3.14159253589793238462643383279502884197"
76                          + "169399375105820974944592307816406286208998628034825342117067982148086513"));
77          properties.getProperty().add(propDec);
78  
79          CmisObjectType object1 = new CmisObjectType();
80          object1.setProperties(properties);
81  
82          // write the entry
83          AtomEntryWriter aew = new AtomEntryWriter(object1, CONTENT_TYPE, new ByteArrayInputStream(CONTENT));
84          aew.write(bao);
85  
86          byte[] entryContent = bao.toByteArray();
87          assertTrue(entryContent.length > 0);
88  
89          // parse it
90          AtomPubParser parser = new AtomPubParser(new ByteArrayInputStream(entryContent));
91          parser.parse();
92          AtomBase parseResult = parser.getResults();
93  
94          assertTrue(parseResult instanceof AtomEntry);
95          AtomEntry entry = (AtomEntry) parseResult;
96  
97          assertNotNull(entry);
98          assertTrue(entry.getElements().size() > 0);
99  
100         // find the object
101         CmisObjectType object2 = null;
102         for (AtomElement element : entry.getElements()) {
103             if (element.getObject() instanceof CmisObjectType) {
104                 assertNull(object2);
105                 object2 = (CmisObjectType) element.getObject();
106             }
107         }
108 
109         assertNotNull(object2);
110         assertNotNull(object2.getProperties());
111 
112         // compare properteis
113         for (CmisProperty property1 : object1.getProperties().getProperty()) {
114             boolean found = false;
115 
116             for (CmisProperty property2 : object2.getProperties().getProperty()) {
117                 if (property1.getPropertyDefinitionId().equals(property2.getPropertyDefinitionId())) {
118                     found = true;
119 
120                     assertEquals(property1, property2);
121                     break;
122                 }
123             }
124 
125             assertTrue(found);
126         }
127     }
128 
129     protected void assertEquals(CmisProperty expected, CmisProperty actual) throws Exception {
130         if (expected == null && actual == null) {
131             return;
132         }
133 
134         if (expected == null || actual == null) {
135             fail("Property is null!");
136         }
137 
138         assertEquals(expected.getPropertyDefinitionId(), actual.getPropertyDefinitionId());
139         assertSame(expected.getClass(), actual.getClass());
140 
141         Method m1 = expected.getClass().getMethod("getValue");
142         List<?> values1 = (List<?>) m1.invoke(expected);
143         assertNotNull(values1);
144         assertFalse(values1.isEmpty());
145 
146         Method m2 = actual.getClass().getMethod("getValue");
147         List<?> values2 = (List<?>) m2.invoke(actual);
148         assertNotNull(values2);
149         assertFalse(values2.isEmpty());
150 
151         assertEquals(values1.size(), values2.size());
152 
153         for (int i = 0; i < values1.size(); i++) {
154             assertEquals(values1.get(i), values2.get(i));
155         }
156     }
157 }