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