Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tools.build.apkzlib.utils;
     18 
     19 import static org.junit.Assert.assertEquals;
     20 import static org.junit.Assert.assertFalse;
     21 import static org.junit.Assert.assertTrue;
     22 import static org.junit.Assert.fail;
     23 
     24 import java.util.function.Supplier;
     25 import org.junit.Test;
     26 
     27 public class CachedSupplierTest {
     28 
     29     @Test
     30     public void testGetsOnlyOnce() {
     31         TestSupplier ts = new TestSupplier();
     32         CachedSupplier<String> cs = new CachedSupplier<>(ts);
     33         assertFalse(cs.isValid());
     34 
     35         ts.value = "foo";
     36         assertEquals(0, ts.invocationCount);
     37         assertEquals("foo", cs.get());
     38         assertEquals(1, ts.invocationCount);
     39         assertTrue(cs.isValid());
     40 
     41         ts.value = "bar";
     42         assertEquals("foo", cs.get());
     43         assertEquals(1, ts.invocationCount);
     44         assertTrue(cs.isValid());
     45     }
     46 
     47     @Test
     48     public void cacheCanBePreset() {
     49         TestSupplier ts = new TestSupplier();
     50         ts.value = "foo";
     51         CachedSupplier<String> cs = new CachedSupplier<>(ts);
     52         cs.precomputed("bar");
     53         assertTrue(cs.isValid());
     54 
     55         assertEquals("bar", cs.get());
     56         assertEquals(0, ts.invocationCount);
     57     }
     58 
     59     @Test
     60     public void exceptionThrownBySupplier() {
     61         CachedSupplier<String> cs = new CachedSupplier<>(() -> {
     62             throw new RuntimeException("foo");
     63         });
     64         assertFalse(cs.isValid());
     65 
     66         try {
     67             cs.get();
     68             fail();
     69         } catch (RuntimeException e) {
     70             assertEquals("foo", e.getMessage());
     71         }
     72 
     73         assertFalse(cs.isValid());
     74 
     75         try {
     76             cs.get();
     77             fail();
     78         } catch (RuntimeException e) {
     79             assertEquals("foo", e.getMessage());
     80         }
     81     }
     82 
     83     @Test
     84     public void reset() {
     85         TestSupplier ts = new TestSupplier();
     86         ts.value = "foo";
     87         CachedSupplier<String> cs = new CachedSupplier<>(ts);
     88         assertFalse(cs.isValid());
     89 
     90         assertEquals("foo", cs.get());
     91         assertEquals(1, ts.invocationCount);
     92         assertTrue(cs.isValid());
     93         ts.value = "bar";
     94 
     95         cs.reset();
     96         assertFalse(cs.isValid());
     97         assertEquals("bar", cs.get());
     98         assertEquals(2, ts.invocationCount);
     99     }
    100 
    101     static class TestSupplier implements Supplier<String> {
    102         int invocationCount = 0;
    103         String value;
    104 
    105         @Override
    106         public String get() {
    107             invocationCount++;
    108             return value;
    109         }
    110     }
    111 }
    112