View Javadoc

1   package com.atlassian.cache.impl;
2   
3   import com.atlassian.cache.CachedReferenceEvent;
4   
5   import com.google.common.base.Objects;
6   
7   import org.hamcrest.BaseMatcher;
8   import org.hamcrest.Description;
9   import org.hamcrest.Matcher;
10  
11  import static com.google.common.base.Objects.equal;
12  import static org.mockito.Matchers.argThat;
13  
14  public class CachedReferenceEventMatcher<V> extends BaseMatcher<CachedReferenceEvent<V>>
15  {
16      private final V value;
17  
18      private CachedReferenceEventMatcher(V value)
19      {
20          this.value = value;
21      }
22  
23      @Override
24      public boolean matches(Object item)
25      {
26          return match(item) == null;
27      }
28  
29      @Override
30      public void describeMismatch(Object item, Description description)
31      {
32          String match = match(item);
33          if (match != null)
34          {
35              description.appendText(match);
36          }
37      }
38  
39      @Override
40      public void describeTo(Description description)
41      {
42          description.appendText(String.format("CachedReferenceEvent(%s)", value));
43      }
44  
45      private String match(Object item)
46      {
47          if (! (item instanceof CachedReferenceEvent))
48          {
49              return String.format("Not instance of CachedReferenceEvent. Was: %s", item.getClass());
50          }
51  
52          CachedReferenceEvent<V> event = (CachedReferenceEvent) item;
53          if (! equal(value, event.getValue()))
54          {
55              return String.format("Different values expected %s, actual %s", value, event.getValue());
56          }
57  
58          return null;
59      }
60  
61      public static <V> Matcher<CachedReferenceEvent<V>> eventMatcher(V value)
62      {
63          return new CachedReferenceEventMatcher<V>(value);
64      }
65  
66      public static CachedReferenceEvent event(Object value)
67      {
68          return argThat(eventMatcher(value));
69      }
70  }