1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir package complex.writer;
29*cdf0e10cSrcweir 
30*cdf0e10cSrcweir import com.sun.star.beans.PropertyValue;
31*cdf0e10cSrcweir import com.sun.star.container.XIndexContainer;
32*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33*cdf0e10cSrcweir import com.sun.star.uno.Type;
34*cdf0e10cSrcweir import org.junit.AfterClass;
35*cdf0e10cSrcweir import org.junit.BeforeClass;
36*cdf0e10cSrcweir import org.junit.Test;
37*cdf0e10cSrcweir import org.openoffice.test.OfficeConnection;
38*cdf0e10cSrcweir import static org.junit.Assert.*;
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir /**
41*cdf0e10cSrcweir  * Test the com.sun.star.document.IndexedPropertyValues service
42*cdf0e10cSrcweir  */
43*cdf0e10cSrcweir public class CheckIndexedPropertyValues {
44*cdf0e10cSrcweir     @Test public void checkIndexedPropertyValues()
45*cdf0e10cSrcweir         throws com.sun.star.uno.Exception
46*cdf0e10cSrcweir     {
47*cdf0e10cSrcweir         XIndexContainer xCont = UnoRuntime.queryInterface(
48*cdf0e10cSrcweir             XIndexContainer.class,
49*cdf0e10cSrcweir             (connection.getComponentContext().getServiceManager().
50*cdf0e10cSrcweir              createInstanceWithContext(
51*cdf0e10cSrcweir                  "com.sun.star.document.IndexedPropertyValues",
52*cdf0e10cSrcweir                  connection.getComponentContext())));
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir         assertNotNull("XIndexContainer was queried but returned null.", xCont);
55*cdf0e10cSrcweir         PropertyValue[] prop1 = new PropertyValue[1];
56*cdf0e10cSrcweir         prop1[0] = new PropertyValue();
57*cdf0e10cSrcweir         prop1[0].Name  = "Jupp";
58*cdf0e10cSrcweir         prop1[0].Value = "GoodGuy";
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir         PropertyValue[] prop2 = new PropertyValue[1];
61*cdf0e10cSrcweir         prop2[0] = new PropertyValue();
62*cdf0e10cSrcweir         prop2[0].Name  = "Horst";
63*cdf0e10cSrcweir         prop2[0].Value = "BadGuy";
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir         Type t = xCont.getElementType();
66*cdf0e10cSrcweir         assertEquals("Initial container is not empty", 0, xCont.getCount());
67*cdf0e10cSrcweir         xCont.insertByIndex(0, prop1);
68*cdf0e10cSrcweir         PropertyValue[]ret = (PropertyValue[])xCont.getByIndex(0);
69*cdf0e10cSrcweir         assertEquals(prop1[0].Name, ret[0].Name);
70*cdf0e10cSrcweir         assertEquals(prop1[0].Value, ret[0].Value);
71*cdf0e10cSrcweir         xCont.replaceByIndex(0, prop2);
72*cdf0e10cSrcweir         ret = (PropertyValue[])xCont.getByIndex(0);
73*cdf0e10cSrcweir         assertEquals(prop2[0].Name, ret[0].Name);
74*cdf0e10cSrcweir         assertEquals(prop2[0].Value, ret[0].Value);
75*cdf0e10cSrcweir         xCont.removeByIndex(0);
76*cdf0e10cSrcweir         assertTrue("Could not remove PropertyValue.",
77*cdf0e10cSrcweir                    !xCont.hasElements() && xCont.getCount()==0);
78*cdf0e10cSrcweir         xCont.insertByIndex(0, prop1);
79*cdf0e10cSrcweir         xCont.insertByIndex(1, prop2);
80*cdf0e10cSrcweir         assertTrue("Did not insert PropertyValue.",
81*cdf0e10cSrcweir                    xCont.hasElements() && xCont.getCount()==2);
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir         try {
84*cdf0e10cSrcweir             xCont.insertByIndex(25, prop2);
85*cdf0e10cSrcweir             fail("IllegalArgumentException was not thrown.");
86*cdf0e10cSrcweir         }
87*cdf0e10cSrcweir         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
88*cdf0e10cSrcweir         }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir         try {
91*cdf0e10cSrcweir             xCont.removeByIndex(25);
92*cdf0e10cSrcweir             fail("IndexOutOfBoundsException was not thrown.");
93*cdf0e10cSrcweir         }
94*cdf0e10cSrcweir         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
95*cdf0e10cSrcweir         }
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir         try {
98*cdf0e10cSrcweir             xCont.insertByIndex(2, "Example String");
99*cdf0e10cSrcweir             fail("IllegalArgumentException was not thrown.");
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir         catch(com.sun.star.lang.IllegalArgumentException e) {
102*cdf0e10cSrcweir         }
103*cdf0e10cSrcweir     }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir     @BeforeClass public static void setUpConnection() throws Exception {
106*cdf0e10cSrcweir         connection.setUp();
107*cdf0e10cSrcweir     }
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     @AfterClass public static void tearDownConnection()
110*cdf0e10cSrcweir         throws InterruptedException, com.sun.star.uno.Exception
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         connection.tearDown();
113*cdf0e10cSrcweir     }
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     private static final OfficeConnection connection = new OfficeConnection();
116*cdf0e10cSrcweir }
117