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