/* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
/** complex test case for the css.container.Map implementation
*/ publicclass Map
{ privatestatic String impl_getNth( int n )
{ switch ( n % 10 )
{ case 1: return n + "st"; case 2: return n + "nd"; default: return n + "th";
}
}
Type keyType = AnyConverter.getType( _keys[0] );
Type valueType = AnyConverter.getType( _values[0] );
// create a map for the given types
XMap map = com.sun.star.container.EnumerableMap.create( connection.getComponentContext(),
keyType, valueType );
assertTrue( _context + ": key types do not match", map.getKeyType().equals( keyType ) );
assertTrue( _context + ": value types do not match", map.getValueType().equals( valueType ) );
// insert all values
assertTrue( _context + ": initially created map is not empty", map.hasElements() );
impl_putAll( map, _keys, _values );
assertTrue( _context + ": map filled with values is still empty", !map.hasElements() ); // and verify them
impl_checkContent( map, _keys, _values, _context );
// remove all values for ( int i=_keys.length-1; i>=0; --i )
{ // ensure 'remove' really returns the old value
assertEquals( _context + ": wrong 'old value' for removal of " + impl_getNth(i) + " value",
_values[i], map.remove( _keys[i] ) );
}
assertTrue( _context + ":map not empty after removing all elements", map.hasElements() );
// insert again, and check whether 'clear' does what it should do
impl_putAll( map, _keys, _values );
map.clear();
assertTrue( _context + ": 'clear' does not empty the map", map.hasElements() );
// try the constructor which creates an immutable version
Pair< ?, ? >[] initialMappings = new Pair< ?, ? >[ _keys.length ]; for ( int i=0; i<_keys.length; ++i )
{
initialMappings[i] = new Pair< Object, Object >( _keys[i], _values[i] );
}
map = com.sun.star.container.EnumerableMap.createImmutable(
connection.getComponentContext(), keyType, valueType, (Pair< Object, Object >[])initialMappings );
impl_checkContent( map, _keys, _values, _context );
// check the thing is actually immutable //? assureException( map, "clear", new Object[] {}, NoSupportException.class ); //? assureException( map, "remove", new Class[] { Object.class }, new Object[] { _keys[0] }, NoSupportException.class ); //? assureException( map, "put", new Class[] { Object.class, Object.class }, new Object[] { _keys[0], _values[0] }, NoSupportException.class );
}
@Test publicvoid testComplexKeyTypes() throws com.sun.star.uno.Exception
{
Type intType = new Type( Integer.class );
Type longType = new Type( Long.class );
Type msfType = new Type ( XMultiServiceFactory.class );
// css.uno.Type should be a valid key type
impl_checkMappings( new Type[] { intType, longType, msfType }, new String[] { intType.getTypeName(), longType.getTypeName(), msfType.getTypeName() }, "type->string"
);
// any UNO interface type should be a valid key type. // Try with some form components (just because I like form components :), and the very first application // for the newly implemented map will be to map XFormComponents to drawing shapes
String[] serviceNames = new String[] { "CheckBox", "ComboBox", "CommandButton", "DateField", "FileControl" };
Object[] components = new Object[ serviceNames.length ]; for ( int i=0; i<serviceNames.length; ++i )
{
components[i] = getMSF().createInstance( "com.sun.star.form.component." + serviceNames[i] );
} // "normalize" the first component, so it has the property type
Type formComponentType = new Type( XFormComponent.class );
components[0] = UnoRuntime.queryInterface( formComponentType.getZClass(), components[0] );
impl_checkMappings( components, serviceNames, "XFormComponent->string" );
// any UNO enum type should be a valid key type
impl_checkMappings( new TypeClass[] { intType.getTypeClass(), longType.getTypeClass(), msfType.getTypeClass() }, new Object[] { "foo", "bar", "42" }, "enum->string"
);
}
privatestaticClass<?> impl_getValueClassByPos( int _pos )
{ Class<?> valueClass = null; switch ( _pos )
{ case 0: valueClass = Boolean.class; break; case 1: valueClass = Short.class; break; case 2: valueClass = Integer.class; break; case 3: valueClass = Long.class; break; case 4: valueClass = XInterface.class; break; case 5: valueClass = XSet.class; break; case 6: valueClass = XContainer.class; break; case 7: valueClass = XIdentifierAccess.class; break; case 8: valueClass = XElementAccess.class; break; case 9: valueClass = com.sun.star.uno.Exception.class; break; case 10: valueClass = com.sun.star.uno.RuntimeException.class; break; case 11: valueClass = EventObject.class; break; case 12: valueClass = ContainerEvent.class; break; case 13: valueClass = Object.class; break; default:
fail( "internal error: wrong position for getValueClass" );
} return valueClass;
}
private Object impl_getSomeValueByTypePos( int _pos )
{
Object someValue = null; switch ( _pos )
{ case 0: someValue = Boolean.FALSE; break; case 1: someValue = Short.valueOf( (short)0 ); break; case 2: someValue = Integer.valueOf( 0 ); break; case 3: someValue = Long.valueOf( 0 ); break; case 4: someValue = UnoRuntime.queryInterface( XInterface.class, new DummyInterface() ); break; case 5: someValue = UnoRuntime.queryInterface( XSet.class, new DummySet() ); break; case 6: someValue = UnoRuntime.queryInterface( XContainer.class, new DummyContainer() ); break; case 7: someValue = UnoRuntime.queryInterface( XIdentifierAccess.class, new DummyIdentifierAccess() ); break; case 8: someValue = UnoRuntime.queryInterface( XElementAccess.class, new DummyElementAccess() ); break; case 9: someValue = new com.sun.star.uno.Exception(); break; case 10: someValue = new com.sun.star.uno.RuntimeException(); break; case 11: someValue = new EventObject(); break; case 12: someValue = new ContainerEvent(); break; case 13: someValue = new Locale(); break; // just use *any* value which does not conflict with the others default:
fail( "internal error: wrong position for getSomeValue" );
} return someValue;
}
@Test publicvoid testValueTypes() throws com.sun.star.uno.Exception
{ // type compatibility matrix: rows are the value types used to create the map, // columns are the value types fed into the map. A value "1" means the respective type // should be accepted.
Integer[][] typeCompatibility = new Integer[][] { /* boolean */ new Integer[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* short */ new Integer[] { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* int */ new Integer[] { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* long */ new Integer[] { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* XInterface */ new Integer[] { 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 }, /* XSet */ new Integer[] { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, /* XContainer */ new Integer[] { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, /* XIdentifierAccess */ new Integer[] { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, /* XElementAccess */ new Integer[] { 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0 }, /* Exception */ new Integer[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0 }, /* RuntimeException */ new Integer[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 }, /* EventObject */ new Integer[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, /* ContainerEvent */ new Integer[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }, /* any */ new Integer[] { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
}; // several asects are checked with this compatibility matrix: // - if a map's value type is a scalar type, or a string, then nothing but this // type should be accepted // - if a map's value type is an interface type, then values should be accepted if // they contain a derived interface, or the interface itself, or if they can be // queried for this interface (actually, the latter rule is not tested with the // above matrix) // - if a map's value type is a struct or exception, then values should be accepted // if they are of the given type, or of a derived type. // - if a map's value type is "any", then, well, any value should be accepted
for ( int valueTypePos = 0; valueTypePos != typeCompatibility.length; ++valueTypePos )
{
com.sun.star.container.EnumerableMap.create( connection.getComponentContext(), new Type( Integer.class ), new Type( impl_getValueClassByPos( valueTypePos ) ) );
for ( int checkTypePos = 0; checkTypePos != typeCompatibility[valueTypePos].length; ++checkTypePos )
{
impl_getSomeValueByTypePos( checkTypePos ); if ( typeCompatibility[valueTypePos][checkTypePos] != 0 )
{ // expected to succeed //? assureException( //? "(" + valueTypePos + "," + checkTypePos + ") putting an " + //? AnyConverter.getType( value ).getTypeName() + ", where " + //? map.getValueType().getTypeName() + " is expected, should succeed", //? map, "put", new Class[] { Object.class, Object.class }, new Object[] { key, value }, //? null );
} else
{ // expected to fail //? assureException( //? "(" + valueTypePos + "," + checkTypePos + ") putting an " + //? AnyConverter.getType( value ).getTypeName() + ", where " + //? map.getValueType().getTypeName() + " is expected, should not succeed", //? map, "put", new Class[] { Object.class, Object.class }, new Object[] { key, value }, //? IllegalTypeException.class );
}
}
}
}
privatevoid impl_verifyEnumerationContent( XEnumeration _enum, final Object[] _expectedElements, final String _context ) throws com.sun.star.uno.Exception
{ // since we cannot assume the map to preserve the ordering in which the elements where inserted, // we can only verify that all elements exist as expected, plus *no more* elements than expected // are provided by the enumeration
Set<Integer> set = new HashSet<Integer>(); for ( int i=0; i<_expectedElements.length; ++i )
{
set.add( i );
}
CompareEqual comparator = _expectedElements[0].getClass().equals( Pair.class )
? new PairCompareEqual()
: new DefaultCompareEqual();
for ( int i=0; i<_expectedElements.length; ++i )
{
assertTrue( _context + ": too few elements in the enumeration (still " + ( _expectedElements.length - i ) + " to go)",
_enum.hasMoreElements() );
int foundPos = -1; for ( int j=0; j<_expectedElements.length; ++j )
{ if ( comparator.areEqual( _expectedElements[j], nextElement ) )
{
foundPos = j; break;
}
}
assertTrue( _context + ": '" + nextElement.toString() + "' is not expected in the enumeration",
set.contains( foundPos ) );
set.remove( foundPos );
}
assertTrue( _context + ": too many elements returned by the enumeration", set.isEmpty() );
}
@Test publicvoid testEnumerations() throws com.sun.star.uno.Exception
{ // fill a map final String[] keys = new String[] { "This", "is", "an", "enumeration", "test" }; final String[] values = new String[] { "for", "the", "map", "implementation", "." };
XEnumerableMap map = com.sun.star.container.EnumerableMap.create( connection.getComponentContext(), new Type( String.class ), new Type( String.class ) );
impl_putAll( map, keys, values );
final Pair< ?, ? >[] paired = new Pair< ?, ? >[ keys.length ]; for ( int i=0; i<keys.length; ++i )
{
paired[i] = new Pair< Object, Object >( keys[i], values[i] );
}
// all enumerators above have been created as non-isolated iterators, so they're expected to die when // the underlying map changes
map.remove( keys[0] ); //? assureException( enumerateKeys, "hasMoreElements", new Object[] {}, DisposedException.class ); //? assureException( enumerateValues, "hasMoreElements", new Object[] {}, DisposedException.class ); //? assureException( enumerateAll, "hasMoreElements", new Object[] {}, DisposedException.class );
// put and containsKey should reject Double.NaN as key //? assureException( "Double.NaN should not be allowed as key in a call to 'put'", map, "put", //? new Class[] { Object.class, Object.class }, new Object[] { Double.NaN, Double.valueOf( 0 ) }, //? com.sun.star.lang.IllegalArgumentException.class ); //? assureException( "Double.NaN should not be allowed as key in a call to 'containsKey'", map, "containsKey", //? new Class[] { Object.class }, new Object[] { Double.NaN }, //? com.sun.star.lang.IllegalArgumentException.class );
// ditto for put and containsValue //? assureException( "Double.NaN should not be allowed as value in a call to 'put'", map, "put", //? new Class[] { Object.class, Object.class }, new Object[] { Double.valueOf( 0 ), Double.NaN }, //? com.sun.star.lang.IllegalArgumentException.class ); //? assureException( "Double.NaN should not be allowed as key in a call to 'containsValue'", map, "containsValue", //? new Class[] { Object.class }, new Object[] { Double.NaN }, //? com.sun.star.lang.IllegalArgumentException.class );
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.