xref: /AOO41X/main/dbaccess/qa/complex/dbaccess/PropertyBag.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
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.dbaccess;
29 
30 // import complexlib.ComplexTestCase;
31 
32 import com.sun.star.beans.NamedValue;
33 import com.sun.star.beans.PropertyState;
34 import com.sun.star.beans.PropertyValue;
35 import com.sun.star.beans.PropertyAttribute;
36 import com.sun.star.beans.XPropertyAccess;
37 import com.sun.star.beans.XPropertySet;
38 import com.sun.star.beans.XPropertyContainer;
39 import com.sun.star.uno.UnoRuntime;
40 import com.sun.star.uno.XInterface;
41 import com.sun.star.lang.XMultiServiceFactory;
42 
43 // ---------- junit imports -----------------
44 import org.junit.Before;
45 import org.junit.Test;
46 import static org.junit.Assert.*;
47 // ------------------------------------------
48 
49 public class PropertyBag extends TestCase
50 {
51     private static final String VALUE = "Value";
52     private XPropertyContainer      m_bag;
53     private XPropertySet            m_set;
54     private XPropertyAccess         m_access;
55     private XMultiServiceFactory    m_orb = null;
56 
57     public String getTestObjectName()
58     {
59         return "PropertyBag";
60     }
61 
62     @Before
63     @Override
64     public void before()
65     {
66         m_orb = getMSF();
67     }
68 
69     @Test
70     public void checkBasics()
71     {
72         createEmptyBag();
73         System.out.println("testing the basics");
74 
75         // check whether empty property names are rejected
76         boolean caughtExpected = false;
77         try
78         {
79             m_bag.addProperty( "", PropertyAttribute.BOUND, Integer.valueOf( 3 ) );
80         }
81         catch(com.sun.star.lang.IllegalArgumentException e) { caughtExpected = true; }
82         catch(com.sun.star.uno.Exception e) { }
83         if ( !caughtExpected )
84         {
85             fail("empty property names are not rejected by XPropertyContainer::addProperty");
86         }
87 
88         // check whether duplicate insertions are rejected
89         caughtExpected = false;
90         try
91         {
92             m_bag.addProperty( VALUE, PropertyAttribute.BOUND, "" );
93             m_bag.addProperty( VALUE, PropertyAttribute.BOUND, "" );
94         }
95         catch(com.sun.star.beans.PropertyExistException e) { caughtExpected = true; }
96         catch(com.sun.star.uno.Exception e) { }
97         if ( !caughtExpected )
98         {
99             fail("insertion of duplicate property names is not rejected");
100         }
101 
102         // try removing the property we just added - this should fail, as it does not have
103         // the REMOVEABLE attribute
104         caughtExpected = false;
105         try
106         {
107             m_bag.removeProperty( VALUE);
108         }
109         catch(com.sun.star.beans.NotRemoveableException e) { caughtExpected = true; }
110         catch(com.sun.star.uno.Exception e) { }
111         if ( !caughtExpected )
112         {
113             fail("removing non-removeable properties is expected to fail - but it didn't");
114         }
115 
116         // try removing a non-existent property
117         caughtExpected = false;
118         try
119         {
120             m_bag.removeProperty( "NonExistent" );
121         }
122         catch(com.sun.star.beans.UnknownPropertyException e) { caughtExpected = true; }
123         catch(com.sun.star.uno.Exception e) { }
124         if ( !caughtExpected )
125         {
126             fail("removing non-existent properties is expected to fail - but it didn't");
127         }
128 
129         // try writing and reading a value for the one property we have so far
130         try
131         {
132             final String testValue = "someArbitraryValue";
133             m_set.setPropertyValue(  VALUE , testValue);
134             final String currentValue = (String)m_set.getPropertyValue( VALUE);
135             if ( !currentValue.equals( testValue ) )
136             {
137                 fail("set property is not remembered");
138             }
139         }
140         catch(com.sun.star.uno.Exception e)
141         {
142             fail( "setting or getting a property value failed" );
143         }
144 
145         // try setting an illegal value for the property
146         caughtExpected = false;
147         try
148         {
149             m_set.setPropertyValue( VALUE, Integer.valueOf( 3 ) );
150         }
151         catch(com.sun.star.lang.IllegalArgumentException e) { caughtExpected = true; }
152         catch(com.sun.star.uno.Exception e) { }
153         if ( !caughtExpected )
154         {
155             fail("the bag does not respect the property type we declared for the property");
156         }
157     }
158 
159     @Test
160     public void checkSequenceAccess() throws com.sun.star.uno.Exception
161     {
162         System.out.println( "checking PropertySetAccess via sequences" );
163         createStandardBag( false );
164 
165         // ---------------------------------
166         // XPropertyAccess.setPropertyValues
167         final PropertyValue expectedValues[] =
168         {
169             new PropertyValue( "BoolValue", -1, Boolean.FALSE, PropertyState.DIRECT_VALUE ),
170             new PropertyValue( "StringValue", -1, "some text", PropertyState.DIRECT_VALUE ),
171             new PropertyValue( "IntegerValue", -1, Integer.valueOf( 3 ), PropertyState.DIRECT_VALUE ),
172             new PropertyValue( "InterfaceValue", -1, m_bag, PropertyState.DIRECT_VALUE )
173         };
174         m_access.setPropertyValues( expectedValues );
175 
176         for ( int i=0; i<expectedValues.length; ++i )
177         {
178             final Object value = m_set.getPropertyValue( expectedValues[i].Name );
179             if ( !value.equals( expectedValues[i].Value ) )
180             {
181                 System.out.println( "property name : " + expectedValues[i].Name );
182                 System.out.println( "expected value: " + expectedValues[i].Value.toString() );
183                 System.out.println( "current value : " + value.toString() );
184                 fail( "retrieving a previously set property (" + expectedValues[i].Value.getClass().toString() + ") failed" );
185             }
186         }
187 
188         // ---------------------------------
189         // XPropertyAccess.getPropertyValues
190         final PropertyValue currentValues[] = m_access.getPropertyValues();
191         for ( int i=0; i<currentValues.length; ++i )
192         {
193             final String name = currentValues[i].Name;
194             final Object value = currentValues[i].Value;
195             for ( int j=0; j<expectedValues.length; ++j )
196             {
197                 if ( expectedValues[j].Name.equals( name ) )
198                 {
199                     if ( !expectedValues[j].Value.equals( value ) )
200                     {
201                         System.out.println( "property name : " + expectedValues[j].Name );
202                         System.out.println( "expected value: " + expectedValues[j].Value.toString() );
203                         System.out.println( "current value : " + value.toString() );
204                         fail( "getPropertyValues failed for property '" + name + "' failed" );
205                     }
206                     break;
207                 }
208             }
209 
210             if ( !m_set.getPropertyValue( name ).equals( value ) )
211             {
212                 fail("XPropertyAccess::getPropertyValues() and XPropertyset::getPropertyValue results are inconsistent");
213             }
214         }
215     }
216 
217     @Test
218     public void checkDynamicSet() throws com.sun.star.uno.Exception
219     {
220         System.out.println( "checking proper dynamic of the set" );
221         createStandardBag( false );
222 
223         final PropertyValue props[] =
224         {
225             new PropertyValue( "BoolValue", -1, Boolean.FALSE, PropertyState.DIRECT_VALUE),
226             new PropertyValue( "StringValue", -1, "test", PropertyState.DIRECT_VALUE ),
227             new PropertyValue( "SomeOtherStringValue", -1, "string value", PropertyState.DIRECT_VALUE )
228         };
229 
230         // try setting some property values which are not existent
231         boolean caughtExpected = false;
232         try
233         {
234             m_access.setPropertyValues( props );
235         }
236         catch( com.sun.star.beans.UnknownPropertyException e ) { caughtExpected = true; }
237         catch( com.sun.star.uno.Exception e ) { }
238         if ( !caughtExpected )
239         {
240             fail("the set shouldn't accept unknown property values, if not explicitly told to do so");
241         }
242 
243         // re-create the bag, this time allow it to implicitly add properties
244         createStandardBag( true );
245         boolean success = false;
246         try { m_access.setPropertyValues( props ); success = true; }
247         catch( com.sun.star.uno.Exception e ) { }
248         if ( !success )
249         {
250             fail("property bag failed to implicitly add unknown properties");
251         }
252 
253         // see whether this property was really added, and not just ignored
254         final PropertyValue newlyAdded = props[ props.length - 1 ];
255         try
256         {
257             if ( !m_set.getPropertyValue( newlyAdded.Name ).equals( newlyAdded.Value ) )
258             {
259                 fail("the new property was not really added, or not added with the proper value");
260             }
261         }
262         catch( com.sun.star.uno.Exception e ) { }
263     }
264 
265     private void createEmptyBag()
266     {
267         try
268         {
269             m_bag = null;
270             final String serviceName = "com.sun.star.beans.PropertyBag";
271             m_bag = UnoRuntime.queryInterface(XPropertyContainer.class, m_orb.createInstance(serviceName));
272             if ( m_bag == null )
273             {
274                 fail("could not create a " + serviceName + " instance");
275             }
276             m_set = UnoRuntime.queryInterface(XPropertySet.class, m_bag);
277             m_access = UnoRuntime.queryInterface(XPropertyAccess.class, m_bag);
278         }
279         catch( com.sun.star.uno.Exception e )
280         {
281         }
282     }
283 
284     private void createStandardBag( boolean allowLazyAdding )
285     {
286         try
287         {
288             m_bag = null;
289 
290             final Object initArgs[] = { new NamedValue( "AutomaticAddition", Boolean.valueOf( allowLazyAdding ) ) };
291 
292             final String serviceName = "com.sun.star.beans.PropertyBag";
293             m_bag = UnoRuntime.queryInterface(XPropertyContainer.class, m_orb.createInstanceWithArguments(serviceName, initArgs));
294             if ( m_bag == null )
295             {
296                 fail("could not create a " + serviceName + " instance");
297             }
298             m_set = UnoRuntime.queryInterface(XPropertySet.class, m_bag);
299             m_access = UnoRuntime.queryInterface(XPropertyAccess.class, m_bag);
300 
301             final Object properties[][] =
302             {
303                 { "BoolValue", Boolean.TRUE },
304                 { "StringValue", "" },
305                 { "IntegerValue", Integer.valueOf( 3 ) },
306                 { "InterfaceValue", (XInterface)m_bag }
307             };
308             for ( int i=0; i<properties.length; ++i )
309             {
310                 m_bag.addProperty(
311                     (String)properties[i][0],
312                     PropertyAttribute.MAYBEVOID,
313                     properties[i][1]
314                 );
315             }
316         }
317         catch( com.sun.star.uno.Exception e )
318         {
319         }
320     }
321 }
322