11/*
2- * Copyright 2002-2012 the original author or authors.
2+ * Copyright 2002-2013 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
1616
1717package org .springframework .util ;
1818
19- import static org .hamcrest .Matchers .*;
20- import static org .junit .Assert .*;
21-
2219import java .lang .ref .WeakReference ;
2320import java .util .ArrayList ;
2421import java .util .Collections ;
3633import org .junit .Rule ;
3734import org .junit .Test ;
3835import org .junit .rules .ExpectedException ;
36+
3937import org .springframework .util .ConcurrentReferenceHashMap .Entry ;
4038import org .springframework .util .ConcurrentReferenceHashMap .Reference ;
4139import org .springframework .util .ConcurrentReferenceHashMap .Restructure ;
4240import org .springframework .util .comparator .ComparableComparator ;
4341import org .springframework .util .comparator .NullSafeComparator ;
4442
43+ import static org .hamcrest .Matchers .*;
44+ import static org .junit .Assert .*;
45+
4546/**
4647 * Tests for {@link ConcurrentReferenceHashMap}.
48+ *
4749 * @author Phillip Webb
4850 */
4951public class ConcurrentReferenceHashMapTests {
@@ -56,6 +58,7 @@ public class ConcurrentReferenceHashMapTests {
5658
5759 private TestWeakConcurrentCache <Integer , String > map = new TestWeakConcurrentCache <Integer , String >();
5860
61+
5962 @ Test
6063 public void shouldCreateWithDefaults () throws Exception {
6164 ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >();
@@ -66,66 +69,62 @@ public void shouldCreateWithDefaults() throws Exception {
6669
6770 @ Test
6871 public void shouldCreateWithInitialCapacity () throws Exception {
69- ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >(
70- 32 );
72+ ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >(32 );
7173 assertThat (map .getSegmentsSize (), is (16 ));
7274 assertThat (map .getSegment (0 ).getSize (), is (2 ));
7375 assertThat (map .getLoadFactor (), is (0.75f ));
7476 }
7577
7678 @ Test
7779 public void shouldCreateWithInitialCapacityAndLoadFactor () throws Exception {
78- ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >(
79- 32 , 0.5f );
80+ ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >(32 , 0.5f );
8081 assertThat (map .getSegmentsSize (), is (16 ));
8182 assertThat (map .getSegment (0 ).getSize (), is (2 ));
8283 assertThat (map .getLoadFactor (), is (0.5f ));
8384 }
8485
8586 @ Test
8687 public void shouldCreateWithInitialCapacityAndConcurrenyLevel () throws Exception {
87- ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >(
88- 16 , 2 );
88+ ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >(16 , 2 );
8989 assertThat (map .getSegmentsSize (), is (2 ));
9090 assertThat (map .getSegment (0 ).getSize (), is (8 ));
9191 assertThat (map .getLoadFactor (), is (0.75f ));
9292 }
9393
9494 @ Test
9595 public void shouldCreateFullyCustom () throws Exception {
96- ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >(
97- 5 , 0.5f , 3 );
96+ ConcurrentReferenceHashMap <Integer , String > map = new ConcurrentReferenceHashMap <Integer , String >(5 , 0.5f , 3 );
9897 // concurrencyLevel of 3 ends up as 4 (nearest power of 2)
9998 assertThat (map .getSegmentsSize (), is (4 ));
10099 // initialCapacity is 5/4 (rounded up, to nearest power of 2)
101100 assertThat (map .getSegment (0 ).getSize (), is (2 ));
102101 assertThat (map .getLoadFactor (), is (0.5f ));
103102 }
104103
105- @ Test
106- public void shouldNeedPositiveConcurrenyLevel () throws Exception {
107- new ConcurrentReferenceHashMap <Integer , String >(1 , 1 );
108- this .thrown .expect (IllegalArgumentException .class );
109- this .thrown .expectMessage ("ConcurrencyLevel must be positive" );
110- new TestWeakConcurrentCache <Integer , String >(1 , 0 );
111- }
112-
113104 @ Test
114105 public void shouldNeedNonNegativeInitialCapacity () throws Exception {
115106 new ConcurrentReferenceHashMap <Integer , String >(0 , 1 );
116107 this .thrown .expect (IllegalArgumentException .class );
117- this .thrown .expectMessage ("InitialCapacity must not be negative" );
108+ this .thrown .expectMessage ("Initial capacity must not be negative" );
118109 new TestWeakConcurrentCache <Integer , String >(-1 , 1 );
119110 }
120111
121112 @ Test
122113 public void shouldNeedPositiveLoadFactor () throws Exception {
123114 new ConcurrentReferenceHashMap <Integer , String >(0 , 0.1f , 1 );
124115 this .thrown .expect (IllegalArgumentException .class );
125- this .thrown .expectMessage ("LoadFactor must be positive" );
116+ this .thrown .expectMessage ("Load factor must be positive" );
126117 new TestWeakConcurrentCache <Integer , String >(0 , 0.0f , 1 );
127118 }
128119
120+ @ Test
121+ public void shouldNeedPositiveConcurrencyLevel () throws Exception {
122+ new ConcurrentReferenceHashMap <Integer , String >(1 , 1 );
123+ this .thrown .expect (IllegalArgumentException .class );
124+ this .thrown .expectMessage ("Concurrency level must be positive" );
125+ new TestWeakConcurrentCache <Integer , String >(1 , 0 );
126+ }
127+
129128 @ Test
130129 public void shouldPutAndGet () throws Exception {
131130 // NOTE we are using mock references so we don't need to worry about GC
@@ -521,13 +520,11 @@ public void shouldSupportNullReference() throws Exception {
521520
522521 /**
523522 * Time a multi-threaded access to a cache.
524- *
525- * @param cache the cache to test
526523 * @return the timing stopwatch
527- * @throws InterruptedException
528524 */
529525 private <V > StopWatch timeMultiThreaded (String id , final Map <Integer , V > map ,
530526 ValueFactory <V > factory ) throws InterruptedException {
527+
531528 StopWatch stopWatch = new StopWatch (id );
532529 for (int i = 0 ; i < 500 ; i ++) {
533530 map .put (i , factory .newValue (i ));
@@ -536,37 +533,37 @@ private <V> StopWatch timeMultiThreaded(String id, final Map<Integer, V> map,
536533 stopWatch .start ("Running threads" );
537534 for (int threadIndex = 0 ; threadIndex < threads .length ; threadIndex ++) {
538535 threads [threadIndex ] = new Thread ("Cache access thread " + threadIndex ) {
539-
540536 @ Override
541537 public void run () {
542538 for (int j = 0 ; j < 1000 ; j ++) {
543539 for (int i = 0 ; i < 1000 ; i ++) {
544540 map .get (i );
545541 }
546542 }
547- };
543+ }
548544 };
549545 }
550- for (int i = 0 ; i < threads . length ; i ++ ) {
551- threads [ i ] .start ();
546+ for (Thread thread : threads ) {
547+ thread .start ();
552548 }
553549
554- for (int i = 0 ; i < threads . length ; i ++ ) {
555- if (threads [ i ] .isAlive ()) {
556- threads [ i ] .join (2000 );
550+ for (Thread thread : threads ) {
551+ if (thread .isAlive ()) {
552+ thread .join (2000 );
557553 }
558554 }
559555 stopWatch .stop ();
560556 return stopWatch ;
561557 }
562558
559+
563560 private static interface ValueFactory <V > {
564561
565562 V newValue (int k );
566563 }
567564
568- private static class TestWeakConcurrentCache < K , V > extends
569- ConcurrentReferenceHashMap <K , V > {
565+
566+ private static class TestWeakConcurrentCache < K , V > extends ConcurrentReferenceHashMap <K , V > {
570567
571568 private int supplimentalHash ;
572569
@@ -582,8 +579,7 @@ public void setDisableTestHooks(boolean disableTestHooks) {
582579 this .disableTestHooks = disableTestHooks ;
583580 }
584581
585- public TestWeakConcurrentCache (int initialCapacity , float loadFactor ,
586- int concurrencyLevel ) {
582+ public TestWeakConcurrentCache (int initialCapacity , float loadFactor , int concurrencyLevel ) {
587583 super (initialCapacity , loadFactor , concurrencyLevel );
588584 }
589585
@@ -607,9 +603,7 @@ public int getSupplimentalHash() {
607603
608604 @ Override
609605 protected ReferenceManager createReferenceManager () {
610-
611606 return new ReferenceManager () {
612-
613607 @ Override
614608 public Reference <K , V > createReference (Entry <K , V > entry , int hash ,
615609 Reference <K , V > next ) {
@@ -618,7 +612,6 @@ public Reference<K, V> createReference(Entry<K, V> entry, int hash,
618612 }
619613 return new MockReference <K , V >(entry , hash , next , TestWeakConcurrentCache .this .queue );
620614 }
621-
622615 @ Override
623616 public Reference <K , V > pollForPurge () {
624617 if (TestWeakConcurrentCache .this .disableTestHooks ) {
@@ -634,6 +627,7 @@ public MockReference<K, V> getMockReference(K key, Restructure restructure) {
634627 }
635628 }
636629
630+
637631 private static class MockReference <K , V > implements Reference <K , V > {
638632
639633 private final int hash ;
@@ -644,8 +638,7 @@ private static class MockReference<K, V> implements Reference<K, V> {
644638
645639 private final LinkedList <MockReference <K , V >> queue ;
646640
647- public MockReference (Entry <K , V > entry , int hash , Reference <K , V > next ,
648- LinkedList <MockReference <K , V >> queue ) {
641+ public MockReference (Entry <K , V > entry , int hash , Reference <K , V > next , LinkedList <MockReference <K , V >> queue ) {
649642 this .hash = hash ;
650643 this .entry = entry ;
651644 this .next = next ;
@@ -677,4 +670,5 @@ public void queueForPurge() {
677670 this .queue .add (this );
678671 }
679672 }
673+
680674}
0 commit comments