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