2013-04-16 11:17:19 -07:00
/ *
* Copyright ( c ) 2013 , Oracle and / or its affiliates . All rights reserved .
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER .
*
* This code is free software ; you can redistribute it and / or modify it
* under the terms of the GNU General Public License version 2 only , as
* published by the Free Software Foundation .
*
* This code is distributed in the hope that it will be useful , but WITHOUT
* ANY WARRANTY ; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE . See the GNU General Public License
* version 2 for more details ( a copy is included in the LICENSE file that
* accompanied this code ) .
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work ; if not , write to the Free Software Foundation ,
* Inc . , 51 Franklin St , Fifth Floor , Boston , MA 02110 - 1301 USA .
*
* Please contact Oracle , 500 Oracle Parkway , Redwood Shores , CA 94065 USA
* or visit www . oracle . com if you need additional information or have any
* questions .
* /
/ *
* @test
2013-10-23 14:32:41 -07:00
* @bug 8010122 8004518 8024331 8024688
2013-04-16 11:17:19 -07:00
* @summary Test Map default methods
* @author Mike Duigou
* @run testng Defaults
* /
import java.util.AbstractMap ;
import java.util.AbstractSet ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Collection ;
import java.util.Collections ;
import java.util.EnumMap ;
import java.util.HashMap ;
import java.util.Hashtable ;
2013-10-23 14:32:41 -07:00
import java.util.HashSet ;
2013-04-16 11:17:19 -07:00
import java.util.IdentityHashMap ;
import java.util.Iterator ;
import java.util.LinkedHashMap ;
import java.util.Map ;
import java.util.TreeMap ;
import java.util.Set ;
import java.util.WeakHashMap ;
import java.util.concurrent.ConcurrentMap ;
import java.util.concurrent.ConcurrentHashMap ;
import java.util.concurrent.ConcurrentSkipListMap ;
2013-06-18 16:03:10 -07:00
import java.util.function.BiFunction ;
2013-04-16 11:17:19 -07:00
import java.util.function.Supplier ;
import org.testng.annotations.Test ;
import org.testng.annotations.DataProvider ;
import static org.testng.Assert.fail ;
import static org.testng.Assert.assertEquals ;
import static org.testng.Assert.assertTrue ;
import static org.testng.Assert.assertFalse ;
import static org.testng.Assert.assertNull ;
import static org.testng.Assert.assertSame ;
public class Defaults {
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=all keys=withNull values=withNull " )
2013-04-16 11:17:19 -07:00
public void testGetOrDefaultNulls ( String description , Map < IntegerEnum , String > map ) {
2013-06-18 16:03:10 -07:00
assertTrue ( map . containsKey ( null ) , description + " : null key absent " ) ;
assertNull ( map . get ( null ) , description + " : value not null " ) ;
assertSame ( map . get ( null ) , map . getOrDefault ( null , EXTRA_VALUE ) , description + " : values should match " ) ;
2013-04-16 11:17:19 -07:00
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=all keys=all values=all " )
2013-04-16 11:17:19 -07:00
public void testGetOrDefault ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( KEYS [ 1 ] ) , " expected key missing " ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , map . getOrDefault ( KEYS [ 1 ] , EXTRA_VALUE ) , " values should match " ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) , " expected absent key " ) ;
assertSame ( map . getOrDefault ( EXTRA_KEY , EXTRA_VALUE ) , EXTRA_VALUE , " value not returned as default " ) ;
assertNull ( map . getOrDefault ( EXTRA_KEY , null ) , " null not returned as default " ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=withNull values=withNull " )
2013-04-16 11:17:19 -07:00
public void testPutIfAbsentNulls ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertNull ( map . get ( null ) , " value not null " ) ;
assertNull ( map . putIfAbsent ( null , EXTRA_VALUE ) , " previous not null " ) ;
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertSame ( map . get ( null ) , EXTRA_VALUE , " unexpected value " ) ;
assertSame ( map . putIfAbsent ( null , null ) , EXTRA_VALUE , " previous not expected value " ) ;
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertSame ( map . get ( null ) , EXTRA_VALUE , " unexpected value " ) ;
assertSame ( map . remove ( null ) , EXTRA_VALUE , " removed unexpected value " ) ;
assertFalse ( map . containsKey ( null ) , description + " : key present after remove " ) ;
assertNull ( map . putIfAbsent ( null , null ) , " previous not null " ) ;
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertNull ( map . get ( null ) , " value not null " ) ;
assertNull ( map . putIfAbsent ( null , EXTRA_VALUE ) , " previous not null " ) ;
assertSame ( map . get ( null ) , EXTRA_VALUE , " value not expected " ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
2013-04-16 11:17:19 -07:00
public void testPutIfAbsent ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( KEYS [ 1 ] ) ) ;
Object expected = map . get ( KEYS [ 1 ] ) ;
assertTrue ( null = = expected | | expected = = VALUES [ 1 ] ) ;
assertSame ( map . putIfAbsent ( KEYS [ 1 ] , EXTRA_VALUE ) , expected ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , expected ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertSame ( map . putIfAbsent ( EXTRA_KEY , EXTRA_VALUE ) , null ) ;
assertSame ( map . get ( EXTRA_KEY ) , EXTRA_VALUE ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=all keys=all values=all " )
2013-04-16 11:17:19 -07:00
public void testForEach ( String description , Map < IntegerEnum , String > map ) {
IntegerEnum [ ] EACH_KEY = new IntegerEnum [ map . size ( ) ] ;
map . forEach ( ( k , v ) - > {
int idx = ( null = = k ) ? 0 : k . ordinal ( ) ; // substitute for index.
assertNull ( EACH_KEY [ idx ] ) ;
EACH_KEY [ idx ] = ( idx = = 0 ) ? KEYS [ 0 ] : k ; // substitute for comparison.
assertSame ( v , map . get ( k ) ) ;
} ) ;
2013-06-18 16:03:10 -07:00
assertEquals ( KEYS , EACH_KEY , description ) ;
2013-04-16 11:17:19 -07:00
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
public static void testReplaceAll ( String description , Map < IntegerEnum , String > map ) {
IntegerEnum [ ] EACH_KEY = new IntegerEnum [ map . size ( ) ] ;
Set < String > EACH_REPLACE = new HashSet < > ( map . size ( ) ) ;
map . replaceAll ( ( k , v ) - > {
int idx = ( null = = k ) ? 0 : k . ordinal ( ) ; // substitute for index.
assertNull ( EACH_KEY [ idx ] ) ;
EACH_KEY [ idx ] = ( idx = = 0 ) ? KEYS [ 0 ] : k ; // substitute for comparison.
assertSame ( v , map . get ( k ) ) ;
String replacement = v + " replaced " ;
EACH_REPLACE . add ( replacement ) ;
return replacement ;
} ) ;
assertEquals ( KEYS , EACH_KEY , description ) ;
assertEquals ( map . values ( ) . size ( ) , EACH_REPLACE . size ( ) , description + EACH_REPLACE ) ;
assertTrue ( EACH_REPLACE . containsAll ( map . values ( ) ) , description + " : " + EACH_REPLACE + " != " + map . values ( ) ) ;
assertTrue ( map . values ( ) . containsAll ( EACH_REPLACE ) , description + " : " + EACH_REPLACE + " != " + map . values ( ) ) ;
}
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull " )
public static void testReplaceAllNoNullReplacement ( String description , Map < IntegerEnum , String > map ) {
assertThrows (
( ) - > { map . replaceAll ( null ) ; } ,
NullPointerException . class ,
description ) ;
assertThrows (
( ) - > { map . replaceAll ( ( k , v ) - > null ) ; } ,
NullPointerException . class ,
2013-09-13 11:18:44 -07:00
description + " should not allow replacement with null value " ) ;
2013-06-18 16:03:10 -07:00
}
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=withNull values=withNull " )
2013-04-16 11:17:19 -07:00
public static void testRemoveNulls ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertNull ( map . get ( null ) , " value not null " ) ;
assertFalse ( map . remove ( null , EXTRA_VALUE ) , description ) ;
assertTrue ( map . containsKey ( null ) ) ;
assertNull ( map . get ( null ) ) ;
assertTrue ( map . remove ( null , null ) ) ;
assertFalse ( map . containsKey ( null ) ) ;
assertNull ( map . get ( null ) ) ;
assertFalse ( map . remove ( null , null ) ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
2013-04-16 11:17:19 -07:00
public static void testRemove ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( KEYS [ 1 ] ) ) ;
Object expected = map . get ( KEYS [ 1 ] ) ;
assertTrue ( null = = expected | | expected = = VALUES [ 1 ] ) ;
assertFalse ( map . remove ( KEYS [ 1 ] , EXTRA_VALUE ) , description ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , expected ) ;
assertTrue ( map . remove ( KEYS [ 1 ] , expected ) ) ;
assertNull ( map . get ( KEYS [ 1 ] ) ) ;
assertFalse ( map . remove ( KEYS [ 1 ] , expected ) ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertFalse ( map . remove ( EXTRA_KEY , EXTRA_VALUE ) ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=withNull values=withNull " )
2013-04-16 11:17:19 -07:00
public void testReplaceKVNulls ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertNull ( map . get ( null ) , " value not null " ) ;
assertSame ( map . replace ( null , EXTRA_VALUE ) , null ) ;
assertSame ( map . get ( null ) , EXTRA_VALUE ) ;
}
2013-09-13 11:18:44 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull " )
public void testReplaceKVNoNulls ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( FIRST_KEY ) , " expected key missing " ) ;
assertSame ( map . get ( FIRST_KEY ) , FIRST_VALUE , " found wrong value " ) ;
assertThrows ( ( ) - > { map . replace ( FIRST_KEY , null ) ; } , NullPointerException . class , description + " : should throw NPE " ) ;
assertSame ( map . replace ( FIRST_KEY , EXTRA_VALUE ) , FIRST_VALUE , description + " : replaced wrong value " ) ;
assertSame ( map . get ( FIRST_KEY ) , EXTRA_VALUE , " found wrong value " ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
2013-04-16 11:17:19 -07:00
public void testReplaceKV ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( KEYS [ 1 ] ) ) ;
Object expected = map . get ( KEYS [ 1 ] ) ;
assertTrue ( null = = expected | | expected = = VALUES [ 1 ] ) ;
assertSame ( map . replace ( KEYS [ 1 ] , EXTRA_VALUE ) , expected ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , EXTRA_VALUE ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertNull ( map . replace ( EXTRA_KEY , EXTRA_VALUE ) ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertNull ( map . get ( EXTRA_KEY ) ) ;
assertNull ( map . put ( EXTRA_KEY , EXTRA_VALUE ) ) ;
assertSame ( map . get ( EXTRA_KEY ) , EXTRA_VALUE ) ;
assertSame ( map . replace ( EXTRA_KEY , ( String ) expected ) , EXTRA_VALUE ) ;
assertSame ( map . get ( EXTRA_KEY ) , expected ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=withNull values=withNull " )
2013-04-16 11:17:19 -07:00
public void testReplaceKVVNulls ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertNull ( map . get ( null ) , " value not null " ) ;
assertFalse ( map . replace ( null , EXTRA_VALUE , EXTRA_VALUE ) ) ;
assertNull ( map . get ( null ) ) ;
assertTrue ( map . replace ( null , null , EXTRA_VALUE ) ) ;
assertSame ( map . get ( null ) , EXTRA_VALUE ) ;
assertTrue ( map . replace ( null , EXTRA_VALUE , EXTRA_VALUE ) ) ;
assertSame ( map . get ( null ) , EXTRA_VALUE ) ;
}
2013-09-13 11:18:44 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull " )
public void testReplaceKVVNoNulls ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( FIRST_KEY ) , " expected key missing " ) ;
assertSame ( map . get ( FIRST_KEY ) , FIRST_VALUE , " found wrong value " ) ;
assertThrows ( ( ) - > { map . replace ( FIRST_KEY , FIRST_VALUE , null ) ; } , NullPointerException . class , description + " : should throw NPE " ) ;
assertThrows ( ( ) - > { if ( ! map . replace ( FIRST_KEY , null , EXTRA_VALUE ) ) throw new NullPointerException ( " default returns false rather than throwing " ) ; } , NullPointerException . class , description + " : should throw NPE " ) ;
assertTrue ( map . replace ( FIRST_KEY , FIRST_VALUE , EXTRA_VALUE ) , description + " : replaced wrong value " ) ;
assertSame ( map . get ( FIRST_KEY ) , EXTRA_VALUE , " found wrong value " ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
2013-04-16 11:17:19 -07:00
public void testReplaceKVV ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( KEYS [ 1 ] ) ) ;
Object expected = map . get ( KEYS [ 1 ] ) ;
assertTrue ( null = = expected | | expected = = VALUES [ 1 ] ) ;
assertFalse ( map . replace ( KEYS [ 1 ] , EXTRA_VALUE , EXTRA_VALUE ) ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , expected ) ;
assertTrue ( map . replace ( KEYS [ 1 ] , ( String ) expected , EXTRA_VALUE ) ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , EXTRA_VALUE ) ;
assertTrue ( map . replace ( KEYS [ 1 ] , EXTRA_VALUE , EXTRA_VALUE ) ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , EXTRA_VALUE ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertFalse ( map . replace ( EXTRA_KEY , EXTRA_VALUE , EXTRA_VALUE ) ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertNull ( map . get ( EXTRA_KEY ) ) ;
assertNull ( map . put ( EXTRA_KEY , EXTRA_VALUE ) ) ;
assertTrue ( map . containsKey ( EXTRA_KEY ) ) ;
assertSame ( map . get ( EXTRA_KEY ) , EXTRA_VALUE ) ;
assertTrue ( map . replace ( EXTRA_KEY , EXTRA_VALUE , EXTRA_VALUE ) ) ;
assertSame ( map . get ( EXTRA_KEY ) , EXTRA_VALUE ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=withNull values=withNull " )
2013-04-16 11:17:19 -07:00
public void testComputeIfAbsentNulls ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertNull ( map . get ( null ) , " value not null " ) ;
assertSame ( map . computeIfAbsent ( null , ( k ) - > EXTRA_VALUE ) , EXTRA_VALUE , description ) ;
assertSame ( map . get ( null ) , EXTRA_VALUE , description ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
2013-04-16 11:17:19 -07:00
public void testComputeIfAbsent ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( KEYS [ 1 ] ) ) ;
Object expected = map . get ( KEYS [ 1 ] ) ;
assertTrue ( null = = expected | | expected = = VALUES [ 1 ] , description + String . valueOf ( expected ) ) ;
expected = ( null = = expected ) ? EXTRA_VALUE : expected ;
assertSame ( map . computeIfAbsent ( KEYS [ 1 ] , ( k ) - > EXTRA_VALUE ) , expected , description ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , expected , description ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertSame ( map . computeIfAbsent ( EXTRA_KEY , ( k ) - > EXTRA_VALUE ) , EXTRA_VALUE ) ;
assertSame ( map . get ( EXTRA_KEY ) , EXTRA_VALUE ) ;
}
2013-10-23 14:32:41 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
public void testComputeIfAbsentNullFunction ( String description , Map < IntegerEnum , String > map ) {
assertThrows ( ( ) - > { map . computeIfAbsent ( KEYS [ 1 ] , null ) ; } ,
NullPointerException . class ,
" Should throw NPE " ) ;
2013-09-20 15:12:05 -07:00
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=withNull values=withNull " )
2013-04-16 11:17:19 -07:00
public void testComputeIfPresentNulls ( String description , Map < IntegerEnum , String > map ) {
2013-06-20 07:23:51 -07:00
assertTrue ( map . containsKey ( null ) , description + " : null key absent " ) ;
assertNull ( map . get ( null ) , description + " : value not null " ) ;
2013-04-16 11:17:19 -07:00
assertSame ( map . computeIfPresent ( null , ( k , v ) - > {
2013-06-20 07:23:51 -07:00
fail ( description + " : null value is not deemed present " ) ;
2013-04-16 11:17:19 -07:00
return EXTRA_VALUE ;
} ) , null , description ) ;
assertTrue ( map . containsKey ( null ) ) ;
2013-06-20 07:23:51 -07:00
assertNull ( map . get ( null ) , description ) ;
assertNull ( map . remove ( EXTRA_KEY ) , description + " : unexpected mapping " ) ;
assertNull ( map . put ( EXTRA_KEY , null ) , description + " : unexpected value " ) ;
assertSame ( map . computeIfPresent ( EXTRA_KEY , ( k , v ) - > {
fail ( description + " : null value is not deemed present " ) ;
return EXTRA_VALUE ;
} ) , null , description ) ;
assertNull ( map . get ( EXTRA_KEY ) , description + " : null mapping gone " ) ;
2013-04-16 11:17:19 -07:00
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
2013-04-16 11:17:19 -07:00
public void testComputeIfPresent ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( KEYS [ 1 ] ) ) ;
Object value = map . get ( KEYS [ 1 ] ) ;
assertTrue ( null = = value | | value = = VALUES [ 1 ] , description + String . valueOf ( value ) ) ;
Object expected = ( null = = value ) ? null : EXTRA_VALUE ;
assertSame ( map . computeIfPresent ( KEYS [ 1 ] , ( k , v ) - > {
assertSame ( v , value ) ;
return EXTRA_VALUE ;
} ) , expected , description ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , expected , description ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertSame ( map . computeIfPresent ( EXTRA_KEY , ( k , v ) - > {
fail ( ) ;
return EXTRA_VALUE ;
} ) , null ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertSame ( map . get ( EXTRA_KEY ) , null ) ;
}
2013-10-23 14:32:41 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
public void testComputeIfPresentNullFunction ( String description , Map < IntegerEnum , String > map ) {
assertThrows ( ( ) - > { map . computeIfPresent ( KEYS [ 1 ] , null ) ; } ,
NullPointerException . class ,
" Should throw NPE " ) ;
2013-09-20 15:12:05 -07:00
}
2013-10-23 14:32:41 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=withNull values=withNull " )
2013-04-16 11:17:19 -07:00
public void testComputeNulls ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( null ) , " null key absent " ) ;
assertNull ( map . get ( null ) , " value not null " ) ;
2013-06-20 07:23:51 -07:00
assertSame ( map . compute ( null , ( k , v ) - > {
assertNull ( k ) ;
assertNull ( v ) ;
return null ;
} ) , null , description ) ;
assertFalse ( map . containsKey ( null ) , description + " : null key present. " ) ;
2013-04-16 11:17:19 -07:00
assertSame ( map . compute ( null , ( k , v ) - > {
assertSame ( k , null ) ;
assertNull ( v ) ;
return EXTRA_VALUE ;
} ) , EXTRA_VALUE , description ) ;
assertTrue ( map . containsKey ( null ) ) ;
assertSame ( map . get ( null ) , EXTRA_VALUE , description ) ;
2013-06-20 07:23:51 -07:00
assertSame ( map . remove ( null ) , EXTRA_VALUE , description + " : removed value not expected " ) ;
// no mapping before and after
assertFalse ( map . containsKey ( null ) , description + " : null key present " ) ;
2013-04-16 11:17:19 -07:00
assertSame ( map . compute ( null , ( k , v ) - > {
2013-06-20 07:23:51 -07:00
assertNull ( k ) ;
assertNull ( v ) ;
return null ;
} ) , null , description + " : expected null result " ) ;
assertFalse ( map . containsKey ( null ) , description + " : null key present " ) ;
// compute with map not containing value
assertNull ( map . remove ( EXTRA_KEY ) , description + " : unexpected mapping " ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) , description + " : key present " ) ;
assertSame ( map . compute ( EXTRA_KEY , ( k , v ) - > {
assertSame ( k , EXTRA_KEY ) ;
assertNull ( v ) ;
return null ;
} ) , null , description ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) , description + " : null key present " ) ;
// ensure removal.
assertNull ( map . put ( EXTRA_KEY , EXTRA_VALUE ) ) ;
assertSame ( map . compute ( EXTRA_KEY , ( k , v ) - > {
assertSame ( k , EXTRA_KEY ) ;
assertSame ( v , EXTRA_VALUE ) ;
return null ;
} ) , null , description + " : null resulted expected " ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) , description + " : null key present " ) ;
// compute with map containing null value
assertNull ( map . put ( EXTRA_KEY , null ) , description + " : unexpected value " ) ;
assertSame ( map . compute ( EXTRA_KEY , ( k , v ) - > {
assertSame ( k , EXTRA_KEY ) ;
2013-04-16 11:17:19 -07:00
assertNull ( v ) ;
return null ;
} ) , null , description ) ;
2013-06-20 07:23:51 -07:00
assertFalse ( map . containsKey ( EXTRA_KEY ) , description + " : null key present " ) ;
assertNull ( map . put ( EXTRA_KEY , null ) , description + " : unexpected value " ) ;
assertSame ( map . compute ( EXTRA_KEY , ( k , v ) - > {
assertSame ( k , EXTRA_KEY ) ;
assertNull ( v ) ;
return EXTRA_VALUE ;
} ) , EXTRA_VALUE , description ) ;
assertTrue ( map . containsKey ( EXTRA_KEY ) , " null key present " ) ;
2013-04-16 11:17:19 -07:00
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
2013-04-16 11:17:19 -07:00
public void testCompute ( String description , Map < IntegerEnum , String > map ) {
assertTrue ( map . containsKey ( KEYS [ 1 ] ) ) ;
Object value = map . get ( KEYS [ 1 ] ) ;
assertTrue ( null = = value | | value = = VALUES [ 1 ] , description + String . valueOf ( value ) ) ;
assertSame ( map . compute ( KEYS [ 1 ] , ( k , v ) - > {
assertSame ( k , KEYS [ 1 ] ) ;
assertSame ( v , value ) ;
return EXTRA_VALUE ;
} ) , EXTRA_VALUE , description ) ;
assertSame ( map . get ( KEYS [ 1 ] ) , EXTRA_VALUE , description ) ;
assertNull ( map . compute ( KEYS [ 1 ] , ( k , v ) - > {
assertSame ( v , EXTRA_VALUE ) ;
return null ;
} ) , description ) ;
assertFalse ( map . containsKey ( KEYS [ 1 ] ) ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) ) ;
assertSame ( map . compute ( EXTRA_KEY , ( k , v ) - > {
assertNull ( v ) ;
return EXTRA_VALUE ;
} ) , EXTRA_VALUE ) ;
assertTrue ( map . containsKey ( EXTRA_KEY ) ) ;
assertSame ( map . get ( EXTRA_KEY ) , EXTRA_VALUE ) ;
}
2013-06-18 16:03:10 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
2013-10-23 14:32:41 -07:00
public void testComputeNullFunction ( String description , Map < IntegerEnum , String > map ) {
assertThrows ( ( ) - > { map . compute ( KEYS [ 1 ] , null ) ; } ,
NullPointerException . class ,
" Should throw NPE " ) ;
}
@Test ( dataProvider = " MergeCases " )
private void testMerge ( String description , Map < IntegerEnum , String > map , Merging . Value oldValue , Merging . Value newValue , Merging . Merger merger , Merging . Value put , Merging . Value result ) {
// add and check initial conditions.
switch ( oldValue ) {
case ABSENT :
map . remove ( EXTRA_KEY ) ;
assertFalse ( map . containsKey ( EXTRA_KEY ) , " key not absent " ) ;
break ;
case NULL :
map . put ( EXTRA_KEY , null ) ;
assertTrue ( map . containsKey ( EXTRA_KEY ) , " key absent " ) ;
assertNull ( map . get ( EXTRA_KEY ) , " wrong value " ) ;
break ;
case OLDVALUE :
map . put ( EXTRA_KEY , VALUES [ 1 ] ) ;
assertTrue ( map . containsKey ( EXTRA_KEY ) , " key absent " ) ;
assertSame ( map . get ( EXTRA_KEY ) , VALUES [ 1 ] , " wrong value " ) ;
break ;
default :
fail ( " unexpected old value " ) ;
}
String returned = map . merge ( EXTRA_KEY ,
newValue = = Merging . Value . NULL ? ( String ) null : VALUES [ 2 ] ,
merger
) ;
2013-09-20 15:12:05 -07:00
2013-10-23 14:32:41 -07:00
// check result
switch ( result ) {
case NULL :
assertNull ( returned , " wrong value " ) ;
break ;
case NEWVALUE :
assertSame ( returned , VALUES [ 2 ] , " wrong value " ) ;
break ;
case RESULT :
assertSame ( returned , VALUES [ 3 ] , " wrong value " ) ;
break ;
default :
fail ( " unexpected new value " ) ;
}
// check map
switch ( put ) {
case ABSENT :
assertFalse ( map . containsKey ( EXTRA_KEY ) , " key not absent " ) ;
break ;
case NULL :
assertTrue ( map . containsKey ( EXTRA_KEY ) , " key absent " ) ;
assertNull ( map . get ( EXTRA_KEY ) , " wrong value " ) ;
break ;
case NEWVALUE :
assertTrue ( map . containsKey ( EXTRA_KEY ) , " key absent " ) ;
assertSame ( map . get ( EXTRA_KEY ) , VALUES [ 2 ] , " wrong value " ) ;
break ;
case RESULT :
assertTrue ( map . containsKey ( EXTRA_KEY ) , " key absent " ) ;
assertSame ( map . get ( EXTRA_KEY ) , VALUES [ 3 ] , " wrong value " ) ;
break ;
default :
fail ( " unexpected new value " ) ;
}
2013-09-20 15:12:05 -07:00
}
2013-10-23 14:32:41 -07:00
@Test ( dataProvider = " Map<IntegerEnum,String> rw=true keys=all values=all " )
public void testMergeNullMerger ( String description , Map < IntegerEnum , String > map ) {
assertThrows ( ( ) - > { map . merge ( KEYS [ 1 ] , VALUES [ 1 ] , null ) ; } ,
NullPointerException . class ,
" Should throw NPE " ) ;
2013-09-20 15:12:05 -07:00
}
2013-10-23 14:32:41 -07:00
public enum IntegerEnum {
2013-04-16 11:17:19 -07:00
e0 , e1 , e2 , e3 , e4 , e5 , e6 , e7 , e8 , e9 ,
e10 , e11 , e12 , e13 , e14 , e15 , e16 , e17 , e18 , e19 ,
e20 , e21 , e22 , e23 , e24 , e25 , e26 , e27 , e28 , e29 ,
e30 , e31 , e32 , e33 , e34 , e35 , e36 , e37 , e38 , e39 ,
e40 , e41 , e42 , e43 , e44 , e45 , e46 , e47 , e48 , e49 ,
e50 , e51 , e52 , e53 , e54 , e55 , e56 , e57 , e58 , e59 ,
e60 , e61 , e62 , e63 , e64 , e65 , e66 , e67 , e68 , e69 ,
e70 , e71 , e72 , e73 , e74 , e75 , e76 , e77 , e78 , e79 ,
e80 , e81 , e82 , e83 , e84 , e85 , e86 , e87 , e88 , e89 ,
e90 , e91 , e92 , e93 , e94 , e95 , e96 , e97 , e98 , e99 ,
EXTRA_KEY ;
public static final int SIZE = values ( ) . length ;
} ;
private static final int TEST_SIZE = IntegerEnum . SIZE - 1 ;
/ * *
* Realized keys ensure that there is always a hard ref to all test objects .
* /
private static final IntegerEnum [ ] KEYS = new IntegerEnum [ TEST_SIZE ] ;
/ * *
* Realized values ensure that there is always a hard ref to all test
* objects .
* /
private static final String [ ] VALUES = new String [ TEST_SIZE ] ;
static {
IntegerEnum [ ] keys = IntegerEnum . values ( ) ;
for ( int each = 0 ; each < TEST_SIZE ; each + + ) {
KEYS [ each ] = keys [ each ] ;
VALUES [ each ] = String . valueOf ( each ) ;
}
}
2013-09-13 11:18:44 -07:00
private static final IntegerEnum FIRST_KEY = KEYS [ 0 ] ;
private static final String FIRST_VALUE = VALUES [ 0 ] ;
2013-04-16 11:17:19 -07:00
private static final IntegerEnum EXTRA_KEY = IntegerEnum . EXTRA_KEY ;
private static final String EXTRA_VALUE = String . valueOf ( TEST_SIZE ) ;
2013-06-18 16:03:10 -07:00
@DataProvider ( name = " Map<IntegerEnum,String> rw=all keys=all values=all " , parallel = true )
public static Iterator < Object [ ] > allMapProvider ( ) {
2013-04-16 11:17:19 -07:00
return makeAllMaps ( ) . iterator ( ) ;
}
2013-06-18 16:03:10 -07:00
@DataProvider ( name = " Map<IntegerEnum,String> rw=all keys=withNull values=withNull " , parallel = true )
public static Iterator < Object [ ] > allMapWithNullsProvider ( ) {
return makeAllMapsWithNulls ( ) . iterator ( ) ;
2013-04-16 11:17:19 -07:00
}
2013-06-18 16:03:10 -07:00
@DataProvider ( name = " Map<IntegerEnum,String> rw=true keys=nonNull values=nonNull " , parallel = true )
public static Iterator < Object [ ] > rwNonNullMapProvider ( ) {
return makeRWNoNullsMaps ( ) . iterator ( ) ;
}
@DataProvider ( name = " Map<IntegerEnum,String> rw=true keys=nonNull values=all " , parallel = true )
public static Iterator < Object [ ] > rwNonNullKeysMapProvider ( ) {
2013-04-16 11:17:19 -07:00
return makeRWMapsNoNulls ( ) . iterator ( ) ;
}
2013-06-18 16:03:10 -07:00
@DataProvider ( name = " Map<IntegerEnum,String> rw=true keys=all values=all " , parallel = true )
public static Iterator < Object [ ] > rwMapProvider ( ) {
return makeAllRWMaps ( ) . iterator ( ) ;
}
@DataProvider ( name = " Map<IntegerEnum,String> rw=true keys=withNull values=withNull " , parallel = true )
2013-04-16 11:17:19 -07:00
public static Iterator < Object [ ] > rwNullsMapProvider ( ) {
2013-06-18 16:03:10 -07:00
return makeAllRWMapsWithNulls ( ) . iterator ( ) ;
2013-04-16 11:17:19 -07:00
}
2013-06-18 16:03:10 -07:00
private static Collection < Object [ ] > makeAllRWMapsWithNulls ( ) {
2013-04-16 11:17:19 -07:00
Collection < Object [ ] > all = new ArrayList < > ( ) ;
2013-06-18 16:03:10 -07:00
all . addAll ( makeRWMaps ( true , true ) ) ;
2013-04-16 11:17:19 -07:00
return all ;
}
2013-06-18 16:03:10 -07:00
2013-04-16 11:17:19 -07:00
private static Collection < Object [ ] > makeRWMapsNoNulls ( ) {
Collection < Object [ ] > all = new ArrayList < > ( ) ;
2013-06-18 16:03:10 -07:00
all . addAll ( makeRWNoNullKeysMaps ( false ) ) ;
2013-04-16 11:17:19 -07:00
all . addAll ( makeRWNoNullsMaps ( ) ) ;
return all ;
}
2013-06-18 16:03:10 -07:00
private static Collection < Object [ ] > makeAllROMaps ( ) {
2013-04-16 11:17:19 -07:00
Collection < Object [ ] > all = new ArrayList < > ( ) ;
all . addAll ( makeROMaps ( false ) ) ;
all . addAll ( makeROMaps ( true ) ) ;
return all ;
}
private static Collection < Object [ ] > makeAllRWMaps ( ) {
Collection < Object [ ] > all = new ArrayList < > ( ) ;
all . addAll ( makeRWNoNullsMaps ( ) ) ;
2013-06-18 16:03:10 -07:00
all . addAll ( makeRWMaps ( false , true ) ) ;
all . addAll ( makeRWMaps ( true , true ) ) ;
all . addAll ( makeRWNoNullKeysMaps ( true ) ) ;
return all ;
}
private static Collection < Object [ ] > makeAllMaps ( ) {
Collection < Object [ ] > all = new ArrayList < > ( ) ;
all . addAll ( makeAllROMaps ( ) ) ;
all . addAll ( makeAllRWMaps ( ) ) ;
2013-04-16 11:17:19 -07:00
return all ;
}
2013-06-18 16:03:10 -07:00
private static Collection < Object [ ] > makeAllMapsWithNulls ( ) {
Collection < Object [ ] > all = new ArrayList < > ( ) ;
all . addAll ( makeROMaps ( true ) ) ;
all . addAll ( makeRWMaps ( true , true ) ) ;
return all ;
}
/ * *
*
* @param nullKeys include null keys
* @param nullValues include null values
* @return
* /
private static Collection < Object [ ] > makeRWMaps ( boolean nullKeys , boolean nullValues ) {
2013-04-16 11:17:19 -07:00
return Arrays . asList (
2013-06-18 16:03:10 -07:00
new Object [ ] { " HashMap " , makeMap ( HashMap : : new , nullKeys , nullValues ) } ,
new Object [ ] { " IdentityHashMap " , makeMap ( IdentityHashMap : : new , nullKeys , nullValues ) } ,
new Object [ ] { " LinkedHashMap " , makeMap ( LinkedHashMap : : new , nullKeys , nullValues ) } ,
new Object [ ] { " WeakHashMap " , makeMap ( WeakHashMap : : new , nullKeys , nullValues ) } ,
new Object [ ] { " Collections.checkedMap(HashMap) " , Collections . checkedMap ( makeMap ( HashMap : : new , nullKeys , nullValues ) , IntegerEnum . class , String . class ) } ,
new Object [ ] { " Collections.synchronizedMap(HashMap) " , Collections . synchronizedMap ( makeMap ( HashMap : : new , nullKeys , nullValues ) ) } ,
new Object [ ] { " ExtendsAbstractMap " , makeMap ( ExtendsAbstractMap : : new , nullKeys , nullValues ) } ) ;
}
/ * *
*
* @param nulls include null values
* @return
* /
private static Collection < Object [ ] > makeRWNoNullKeysMaps ( boolean nulls ) {
return Arrays . asList (
// null key hostile
new Object [ ] { " EnumMap " , makeMap ( ( ) - > new EnumMap ( IntegerEnum . class ) , false , nulls ) } ,
2013-09-13 11:18:44 -07:00
new Object [ ] { " TreeMap " , makeMap ( TreeMap : : new , false , nulls ) } ,
new Object [ ] { " ExtendsAbstractMap(TreeMap) " , makeMap ( ( ) - > { return new ExtendsAbstractMap ( new TreeMap ( ) ) ; } , false , nulls ) } ,
2013-06-18 16:03:10 -07:00
new Object [ ] { " Collections.synchronizedMap(EnumMap) " , Collections . synchronizedMap ( makeMap ( ( ) - > new EnumMap ( IntegerEnum . class ) , false , nulls ) ) }
) ;
2013-04-16 11:17:19 -07:00
}
private static Collection < Object [ ] > makeRWNoNullsMaps ( ) {
return Arrays . asList (
2013-06-18 16:03:10 -07:00
// null key and value hostile
new Object [ ] { " Hashtable " , makeMap ( Hashtable : : new , false , false ) } ,
new Object [ ] { " ConcurrentHashMap " , makeMap ( ConcurrentHashMap : : new , false , false ) } ,
new Object [ ] { " ConcurrentSkipListMap " , makeMap ( ConcurrentSkipListMap : : new , false , false ) } ,
2013-09-13 11:18:44 -07:00
new Object [ ] { " Collections.synchronizedMap(ConcurrentHashMap) " , Collections . synchronizedMap ( makeMap ( ConcurrentHashMap : : new , false , false ) ) } ,
2013-06-18 16:03:10 -07:00
new Object [ ] { " Collections.checkedMap(ConcurrentHashMap) " , Collections . checkedMap ( makeMap ( ConcurrentHashMap : : new , false , false ) , IntegerEnum . class , String . class ) } ,
2013-09-13 11:18:44 -07:00
new Object [ ] { " ExtendsAbstractMap(ConcurrentHashMap) " , makeMap ( ( ) - > { return new ExtendsAbstractMap ( new ConcurrentHashMap ( ) ) ; } , false , false ) } ,
2013-06-18 16:03:10 -07:00
new Object [ ] { " ImplementsConcurrentMap " , makeMap ( ImplementsConcurrentMap : : new , false , false ) }
) ;
2013-04-16 11:17:19 -07:00
}
2013-06-18 16:03:10 -07:00
/ * *
*
* @param nulls include nulls
* @return
* /
2013-04-16 11:17:19 -07:00
private static Collection < Object [ ] > makeROMaps ( boolean nulls ) {
return Arrays . asList ( new Object [ ] [ ] {
2013-06-18 16:03:10 -07:00
new Object [ ] { " Collections.unmodifiableMap(HashMap) " , Collections . unmodifiableMap ( makeMap ( HashMap : : new , nulls , nulls ) ) }
2013-04-16 11:17:19 -07:00
} ) ;
}
2013-06-18 16:03:10 -07:00
/ * *
*
* @param supplier a supplier of mutable map instances .
*
* @param nullKeys include null keys
* @param nullValues include null values
* @return
* /
private static Map < IntegerEnum , String > makeMap ( Supplier < Map < IntegerEnum , String > > supplier , boolean nullKeys , boolean nullValues ) {
2013-04-16 11:17:19 -07:00
Map < IntegerEnum , String > result = supplier . get ( ) ;
for ( int each = 0 ; each < TEST_SIZE ; each + + ) {
2013-06-18 16:03:10 -07:00
IntegerEnum key = nullKeys ? ( each = = 0 ) ? null : KEYS [ each ] : KEYS [ each ] ;
String value = nullValues ? ( each = = 0 ) ? null : VALUES [ each ] : VALUES [ each ] ;
result . put ( key , value ) ;
2013-04-16 11:17:19 -07:00
}
return result ;
}
2013-10-23 14:32:41 -07:00
static class Merging {
public enum Value {
ABSENT ,
NULL ,
OLDVALUE ,
NEWVALUE ,
RESULT
}
public enum Merger implements BiFunction < String , String , String > {
UNUSED {
public String apply ( String oldValue , String newValue ) {
fail ( " should not be called " ) ;
return null ;
}
} ,
NULL {
public String apply ( String oldValue , String newValue ) {
return null ;
}
} ,
RESULT {
public String apply ( String oldValue , String newValue ) {
return VALUES [ 3 ] ;
}
} ,
}
}
@DataProvider ( name = " MergeCases " , parallel = true )
public Iterator < Object [ ] > mergeCasesProvider ( ) {
Collection < Object [ ] > cases = new ArrayList < > ( ) ;
cases . addAll ( makeMergeTestCases ( ) ) ;
cases . addAll ( makeMergeNullValueTestCases ( ) ) ;
return cases . iterator ( ) ;
}
static Collection < Object [ ] > makeMergeTestCases ( ) {
Collection < Object [ ] > cases = new ArrayList < > ( ) ;
for ( Object [ ] mapParams : makeAllRWMaps ( ) ) {
cases . add ( new Object [ ] { mapParams [ 0 ] , mapParams [ 1 ] , Merging . Value . ABSENT , Merging . Value . NEWVALUE , Merging . Merger . UNUSED , Merging . Value . NEWVALUE , Merging . Value . NEWVALUE } ) ;
}
for ( Object [ ] mapParams : makeAllRWMaps ( ) ) {
cases . add ( new Object [ ] { mapParams [ 0 ] , mapParams [ 1 ] , Merging . Value . OLDVALUE , Merging . Value . NEWVALUE , Merging . Merger . NULL , Merging . Value . ABSENT , Merging . Value . NULL } ) ;
}
for ( Object [ ] mapParams : makeAllRWMaps ( ) ) {
cases . add ( new Object [ ] { mapParams [ 0 ] , mapParams [ 1 ] , Merging . Value . OLDVALUE , Merging . Value . NEWVALUE , Merging . Merger . RESULT , Merging . Value . RESULT , Merging . Value . RESULT } ) ;
}
return cases ;
}
static Collection < Object [ ] > makeMergeNullValueTestCases ( ) {
Collection < Object [ ] > cases = new ArrayList < > ( ) ;
for ( Object [ ] mapParams : makeAllRWMapsWithNulls ( ) ) {
cases . add ( new Object [ ] { mapParams [ 0 ] , mapParams [ 1 ] , Merging . Value . OLDVALUE , Merging . Value . NULL , Merging . Merger . NULL , Merging . Value . ABSENT , Merging . Value . NULL } ) ;
}
for ( Object [ ] mapParams : makeAllRWMapsWithNulls ( ) ) {
cases . add ( new Object [ ] { mapParams [ 0 ] , mapParams [ 1 ] , Merging . Value . OLDVALUE , Merging . Value . NULL , Merging . Merger . RESULT , Merging . Value . RESULT , Merging . Value . RESULT } ) ;
}
for ( Object [ ] mapParams : makeAllRWMapsWithNulls ( ) ) {
cases . add ( new Object [ ] { mapParams [ 0 ] , mapParams [ 1 ] , Merging . Value . ABSENT , Merging . Value . NULL , Merging . Merger . UNUSED , Merging . Value . ABSENT , Merging . Value . NULL } ) ;
}
for ( Object [ ] mapParams : makeAllRWMapsWithNulls ( ) ) {
cases . add ( new Object [ ] { mapParams [ 0 ] , mapParams [ 1 ] , Merging . Value . NULL , Merging . Value . NULL , Merging . Merger . UNUSED , Merging . Value . ABSENT , Merging . Value . NULL } ) ;
}
for ( Object [ ] mapParams : makeAllRWMapsWithNulls ( ) ) {
cases . add ( new Object [ ] { mapParams [ 0 ] , mapParams [ 1 ] , Merging . Value . NULL , Merging . Value . NEWVALUE , Merging . Merger . UNUSED , Merging . Value . NEWVALUE , Merging . Value . NEWVALUE } ) ;
}
return cases ;
}
2013-04-16 11:17:19 -07:00
public interface Thrower < T extends Throwable > {
public void run ( ) throws T ;
}
public static < T extends Throwable > void assertThrows ( Thrower < T > thrower , Class < T > throwable ) {
assertThrows ( thrower , throwable , null ) ;
}
public static < T extends Throwable > void assertThrows ( Thrower < T > thrower , Class < T > throwable , String message ) {
2013-09-13 11:18:44 -07:00
Throwable thrown ;
2013-04-16 11:17:19 -07:00
try {
thrower . run ( ) ;
2013-09-13 11:18:44 -07:00
thrown = null ;
2013-04-16 11:17:19 -07:00
} catch ( Throwable caught ) {
2013-09-13 11:18:44 -07:00
thrown = caught ;
2013-04-16 11:17:19 -07:00
}
2013-09-13 11:18:44 -07:00
assertInstance ( thrown , throwable ,
( ( null ! = message ) ? message : " " ) +
" Failed to throw " + throwable . getCanonicalName ( ) ) ;
2013-04-16 11:17:19 -07:00
}
2013-06-18 16:03:10 -07:00
public static < T extends Throwable > void assertThrows ( Class < T > throwable , String message , Thrower < T > . . . throwers ) {
for ( Thrower < T > thrower : throwers ) {
assertThrows ( thrower , throwable , message ) ;
}
}
2013-09-13 11:18:44 -07:00
public static void assertInstance ( Object actual , Class < ? > expected ) {
2013-04-16 11:17:19 -07:00
assertInstance ( expected . isInstance ( actual ) , null ) ;
}
2013-09-13 11:18:44 -07:00
public static void assertInstance ( Object actual , Class < ? > expected , String message ) {
2013-04-16 11:17:19 -07:00
assertTrue ( expected . isInstance ( actual ) , message ) ;
}
/ * *
* A simple mutable map implementation that provides only default
* implementations of all methods . ie . none of the Map interface default
* methods have overridden implementations .
*
* @param < K > Type of keys
* @param < V > Type of values
* /
public static class ExtendsAbstractMap < M extends Map < K , V > , K , V > extends AbstractMap < K , V > {
protected final M map ;
public ExtendsAbstractMap ( ) { this ( ( M ) new HashMap < K , V > ( ) ) ; }
protected ExtendsAbstractMap ( M map ) { this . map = map ; }
public Set < Map . Entry < K , V > > entrySet ( ) {
return new AbstractSet < Map . Entry < K , V > > ( ) {
public int size ( ) {
return map . size ( ) ;
}
public Iterator < Map . Entry < K , V > > iterator ( ) {
final Iterator < Map . Entry < K , V > > source = map . entrySet ( ) . iterator ( ) ;
return new Iterator < Map . Entry < K , V > > ( ) {
public boolean hasNext ( ) { return source . hasNext ( ) ; }
public Map . Entry < K , V > next ( ) { return source . next ( ) ; }
public void remove ( ) { source . remove ( ) ; }
} ;
}
public boolean add ( Map . Entry < K , V > e ) {
return map . entrySet ( ) . add ( e ) ;
}
} ;
}
public V put ( K key , V value ) {
return map . put ( key , value ) ;
}
}
/ * *
* A simple mutable concurrent map implementation that provides only default
* implementations of all methods . ie . none of the ConcurrentMap interface
* default methods have overridden implementations .
*
* @param < K > Type of keys
* @param < V > Type of values
* /
public static class ImplementsConcurrentMap < K , V > extends ExtendsAbstractMap < ConcurrentMap < K , V > , K , V > implements ConcurrentMap < K , V > {
public ImplementsConcurrentMap ( ) { super ( new ConcurrentHashMap < K , V > ( ) ) ; }
// ConcurrentMap reabstracts these methods
public V replace ( K k , V v ) { return map . replace ( k , v ) ; } ;
public boolean replace ( K k , V v , V vv ) { return map . replace ( k , v , vv ) ; } ;
public boolean remove ( Object k , Object v ) { return map . remove ( k , v ) ; }
public V putIfAbsent ( K k , V v ) { return map . putIfAbsent ( k , v ) ; }
}
}