1 // 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html#License 3 /* 4 ******************************************************************************* 5 * Copyright (C) 2016, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ******************************************************************************* 8 */ 9 package com.ibm.icu.dev.test.impl; 10 11 import org.junit.Test; 12 import org.junit.runner.RunWith; 13 import org.junit.runners.JUnit4; 14 15 import com.ibm.icu.dev.test.TestFmwk; 16 import com.ibm.icu.impl.CacheValue; 17 import com.ibm.icu.impl.CacheValue.Strength; 18 19 @RunWith(JUnit4.class) 20 public class CacheTest extends TestFmwk { 21 public CacheTest() {} 22 23 /** Code coverage for CacheValue. */ 24 @Test 25 public void testNullCacheValue() { 26 CacheValue<Object> nv = CacheValue.getInstance(null); 27 assertTrue("null CacheValue isNull()", nv.isNull()); 28 assertTrue("null CacheValue get()==null", nv.get() == null); 29 assertTrue("null CacheValue reset==null", nv.resetIfCleared(null) == null); 30 try { 31 Object v = nv.resetIfCleared(this); 32 fail("null CacheValue reset(not null) should throw an Exception, returned " + 33 v + " instead"); 34 } catch(Exception expected) { 35 } 36 } 37 38 /** Code coverage for CacheValue. */ 39 @Test 40 public void testStrongCacheValue() { 41 boolean wasStrong = CacheValue.futureInstancesWillBeStrong(); 42 CacheValue.setStrength(Strength.STRONG); 43 assertTrue("setStrength(STRONG).futureInstancesWillBeStrong()", 44 CacheValue.futureInstancesWillBeStrong()); 45 CacheValue<Object> sv = CacheValue.<Object>getInstance(this); 46 assertFalse("strong CacheValue not isNull()", sv.isNull()); 47 assertTrue("strong CacheValue get()==same", sv.get() == this); 48 // A strong CacheValue never changes value. 49 // The implementation does not check that the new value is equal to the old one, 50 // or even of equal type, so it does not matter which new value we pass in. 51 assertTrue("strong CacheValue reset==same", sv.resetIfCleared("") == this); 52 if (!wasStrong) { 53 CacheValue.setStrength(Strength.SOFT); 54 } 55 } 56 57 /** Code coverage for CacheValue. */ 58 @Test 59 public void testSoftCacheValue() { 60 boolean wasStrong = CacheValue.futureInstancesWillBeStrong(); 61 CacheValue.setStrength(Strength.SOFT); 62 assertFalse("setStrength(SOFT).futureInstancesWillBeStrong()", 63 CacheValue.futureInstancesWillBeStrong()); 64 CacheValue<Object> sv = CacheValue.<Object>getInstance(this); 65 assertFalse("soft CacheValue not isNull()", sv.isNull()); 66 Object v = sv.get(); 67 assertTrue("soft CacheValue get()==same or null", v == this || v == null); 68 assertTrue("soft CacheValue reset==same", sv.resetIfCleared(this) == this); 69 if (wasStrong) { 70 CacheValue.setStrength(Strength.STRONG); 71 } 72 } 73 } 74