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