1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package complex.writer;
25 
26 import com.sun.star.beans.PropertyValue;
27 import com.sun.star.container.XIndexContainer;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.Type;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.openoffice.test.OfficeConnection;
34 import static org.junit.Assert.*;
35 
36 /**
37  * Test the com.sun.star.document.IndexedPropertyValues service
38  */
39 public class CheckIndexedPropertyValues {
checkIndexedPropertyValues()40     @Test public void checkIndexedPropertyValues()
41         throws com.sun.star.uno.Exception
42     {
43         XIndexContainer xCont = UnoRuntime.queryInterface(
44             XIndexContainer.class,
45             (connection.getComponentContext().getServiceManager().
46              createInstanceWithContext(
47                  "com.sun.star.document.IndexedPropertyValues",
48                  connection.getComponentContext())));
49 
50         assertNotNull("XIndexContainer was queried but returned null.", xCont);
51         PropertyValue[] prop1 = new PropertyValue[1];
52         prop1[0] = new PropertyValue();
53         prop1[0].Name  = "Jupp";
54         prop1[0].Value = "GoodGuy";
55 
56         PropertyValue[] prop2 = new PropertyValue[1];
57         prop2[0] = new PropertyValue();
58         prop2[0].Name  = "Horst";
59         prop2[0].Value = "BadGuy";
60 
61         Type t = xCont.getElementType();
62         assertEquals("Initial container is not empty", 0, xCont.getCount());
63         xCont.insertByIndex(0, prop1);
64         PropertyValue[]ret = (PropertyValue[])xCont.getByIndex(0);
65         assertEquals(prop1[0].Name, ret[0].Name);
66         assertEquals(prop1[0].Value, ret[0].Value);
67         xCont.replaceByIndex(0, prop2);
68         ret = (PropertyValue[])xCont.getByIndex(0);
69         assertEquals(prop2[0].Name, ret[0].Name);
70         assertEquals(prop2[0].Value, ret[0].Value);
71         xCont.removeByIndex(0);
72         assertTrue("Could not remove PropertyValue.",
73                    !xCont.hasElements() && xCont.getCount()==0);
74         xCont.insertByIndex(0, prop1);
75         xCont.insertByIndex(1, prop2);
76         assertTrue("Did not insert PropertyValue.",
77                    xCont.hasElements() && xCont.getCount()==2);
78 
79         try {
80             xCont.insertByIndex(25, prop2);
81             fail("IllegalArgumentException was not thrown.");
82         }
83         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
84         }
85 
86         try {
87             xCont.removeByIndex(25);
88             fail("IndexOutOfBoundsException was not thrown.");
89         }
90         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
91         }
92 
93         try {
94             xCont.insertByIndex(2, "Example String");
95             fail("IllegalArgumentException was not thrown.");
96         }
97         catch(com.sun.star.lang.IllegalArgumentException e) {
98         }
99     }
100 
setUpConnection()101     @BeforeClass public static void setUpConnection() throws Exception {
102         connection.setUp();
103     }
104 
tearDownConnection()105     @AfterClass public static void tearDownConnection()
106         throws InterruptedException, com.sun.star.uno.Exception
107     {
108         connection.tearDown();
109     }
110 
111     private static final OfficeConnection connection = new OfficeConnection();
112 }
113