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