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