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.XNameContainer;
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  *
38  */
39 public class CheckNamedPropertyValues {
checkNamedPropertyValues()40     @Test public void checkNamedPropertyValues()
41         throws com.sun.star.uno.Exception
42     {
43         XNameContainer xCont = UnoRuntime.queryInterface(
44             XNameContainer.class,
45             (connection.getComponentContext().getServiceManager().
46              createInstanceWithContext(
47                  "com.sun.star.document.NamedPropertyValues",
48                  connection.getComponentContext())));
49 
50         assertNotNull("XNameContainer 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         assertFalse("Initial container is not empty.", xCont.hasElements());
63 
64         xCont.insertByName("prop1", prop1);
65         PropertyValue[]ret = (PropertyValue[])xCont.getByName("prop1");
66         assertEquals(prop1[0].Name, ret[0].Name);
67         assertEquals(prop1[0].Value, ret[0].Value);
68         xCont.replaceByName("prop1", prop2);
69         ret = (PropertyValue[])xCont.getByName("prop1");
70         assertEquals(prop2[0].Name, ret[0].Name);
71         assertEquals(prop2[0].Value, ret[0].Value);
72         xCont.removeByName("prop1");
73         assertFalse("Could not remove PropertyValue.", xCont.hasElements());
74         xCont.insertByName("prop1", prop1);
75         xCont.insertByName("prop2", prop2);
76         assertTrue("Did not insert PropertyValue.", xCont.hasElements());
77         String[] names = xCont.getElementNames();
78         assertEquals("Not all element names were returned.", 2, names.length);
79         for (int i=0; i<names.length; i++) {
80             assertTrue(
81                 "Got a wrong element name",
82                 names[i].equals("prop1") || names[i].equals("prop2"));
83         }
84 
85         try {
86             xCont.insertByName("prop2", prop1);
87             fail("ElementExistException was not thrown.");
88         }
89         catch(com.sun.star.container.ElementExistException e) {
90         }
91 
92         try {
93             xCont.insertByName("prop3", "Example String");
94             fail("IllegalArgumentException was not thrown.");
95         }
96         catch(com.sun.star.lang.IllegalArgumentException e) {
97         }
98 
99         try {
100             xCont.removeByName("prop3");
101             fail("NoSuchElementException was not thrown.");
102         }
103         catch(com.sun.star.container.NoSuchElementException e) {
104         }
105     }
106 
setUpConnection()107     @BeforeClass public static void setUpConnection() throws Exception {
108         connection.setUp();
109     }
110 
tearDownConnection()111     @AfterClass public static void tearDownConnection()
112         throws InterruptedException, com.sun.star.uno.Exception
113     {
114         connection.tearDown();
115     }
116 
117     private static final OfficeConnection connection = new OfficeConnection();
118 }
119