コード例 #1
0
  public void testHit() {
    CharacterObjectStorage storage = mock(CharacterObjectStorage.class);

    CharacterIntCache cache =
        (CharacterIntCache)
            Wrapping.getFactory(
                    new Signature(char.class, Object.class),
                    new Signature(char.class, int.class),
                    false)
                .wrap("123", CALCULATABLE, storage, new MutableStatisticsImpl());
    cache.setDependencyNode(DependencyTracker.DUMMY_NODE);

    when(storage.load('*')).thenReturn(42);
    when(storage.size()).thenReturn(1);

    assert cache.getSize() == 1;
    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 0;

    assert cache.getOrCreate('*') == 42;

    assert cache.getStatistics().getHits() == 1;
    assert cache.getStatistics().getMisses() == 0;

    verify(storage).size();
    verify(storage, atLeast(2)).load('*');
    verifyNoMoreInteractions(storage);
  }
コード例 #2
0
  public void testSetDuringDependencyNodeOperations() {
    CharacterObjectStorage storage = mock(CharacterObjectStorage.class);

    when(storage.load('*')).thenReturn(Storage.UNDEFINED, 42);

    CharacterIntCalculatable calculatable = mock(CharacterIntCalculatable.class);
    MxResource r = mock(MxResource.class);
    when(calculatable.calculate("123", '*')).thenThrow(new ResourceOccupied(r));

    CharacterIntCache cache =
        (CharacterIntCache)
            Wrapping.getFactory(
                    new Signature(char.class, Object.class),
                    new Signature(char.class, int.class),
                    false)
                .wrap("123", calculatable, storage, new MutableStatisticsImpl());
    cache.setDependencyNode(DependencyTracker.DUMMY_NODE);

    assert cache.getStatistics().getHits() == 0;
    assert cache.getStatistics().getMisses() == 0;

    assert cache.getOrCreate('*') == 42;

    assert cache.getStatistics().getHits() == 1;
    assert cache.getStatistics().getMisses() == 0;

    verify(storage, atLeast(2)).load('*');
    verifyNoMoreInteractions(storage);
    verify(calculatable).calculate("123", '*');
    verifyNoMoreInteractions(calculatable);
  }