コード例 #1
0
  @Test
  public void shouldCacheResultsForSubsequentQueries() throws Exception {
    when(delegate.forName("hej")).thenReturn(lookup, lookup2);

    Lookup first = factory.forName("hej");
    Lookup second = factory.forName("hej");

    assertThat(first == second, is(true));
  }
コード例 #2
0
  @Test
  public void shouldReturnDifferentForDifferentQueries() throws Exception {
    when(delegate.forName("hej")).thenReturn(lookup);
    when(delegate.forName("hopp")).thenReturn(lookup2);

    Lookup first = factory.forName("hej");
    Lookup second = factory.forName("hopp");

    assertThat(first == second, is(false));
  }
コード例 #3
0
  @Test
  public void shouldReturnDifferentForDifferentThreads() throws Exception {
    ExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    factory = new CachingLookupFactory(new SimpleLookupFactory());

    Lookup first = factory.forName("hej");
    Lookup second =
        executorService
            .submit(
                new Callable<Lookup>() {
                  @Override
                  public Lookup call() throws Exception {
                    return factory.forName("hej");
                  }
                })
            .get();

    assertThat(second, not(equalTo(first)));
  }
コード例 #4
0
  @Test
  public void shouldReturnResultsFromDelegate() throws Exception {
    when(delegate.forName("a name")).thenReturn(lookup);

    assertThat(factory.forName("a name"), equalTo(lookup));
  }