View Javadoc

1   package com.atlassian.plugin.test;
2   
3   import java.io.File;
4   import java.net.URI;
5   import java.util.Arrays;
6   
7   import javax.annotation.Nullable;
8   
9   import com.google.common.base.Function;
10  import com.google.common.collect.Iterables;
11  
12  import org.hamcrest.Description;
13  import org.hamcrest.Matcher;
14  import org.hamcrest.TypeSafeMatcher;
15  
16  import static org.hamcrest.CoreMatchers.allOf;
17  import static org.hamcrest.Matchers.containsString;
18  import static org.hamcrest.Matchers.equalTo;
19  
20  public class Matchers
21  {
22      /**
23       * Obtain a matcher for {@link File} instances with given name.
24       * @param name the expected name of the file.
25       * @return a matcher which matches files whose {@link File#getName} is {@link org.hamcrest.Matchers#equalTo} name.
26       */
27      public static Matcher<File> fileNamed(final String name)
28      {
29          return new TypeSafeMatcher<File>()
30          {
31              private final Matcher<String> nameMatcher = equalTo(name);
32  
33              @Override
34              protected boolean matchesSafely(final File item)
35              {
36                  return nameMatcher.matches(item.getName());
37              }
38  
39              @Override
40              public void describeTo(final Description description)
41              {
42                  description.appendText("File named ");
43                  nameMatcher.describeTo(description);
44              }
45          };
46      }
47  
48      /**
49       * Obtain a matcher for {@link URI} instances with given path.
50       *
51       * @param path the expected path of the URI.
52       * @return a matcher which matches URIs whoe {@link URI#getPath} is {@link org.hamcrest.Matchers#equalTo} path.
53       */
54      public static Matcher<URI> uriWithPath(final String path)
55      {
56          return new TypeSafeMatcher<URI>()
57          {
58              private final Matcher<String> pathMatcher = equalTo(path);
59  
60              @Override
61              protected boolean matchesSafely(final URI item)
62              {
63                  return pathMatcher.matches(item.getPath());
64              }
65  
66              @Override
67              public void describeTo(final Description description)
68              {
69                  description.appendText("URI with path ");
70                  pathMatcher.describeTo(description);
71              }
72          };
73      }
74  
75      public static Matcher<String> containsAllStrings(final String... substrings)
76      {
77          return allOf(Iterables.transform(Arrays.asList(substrings), new Function<String, Matcher<? super String>>()
78                          {
79                              @Override
80                              public Matcher<? super String> apply(@Nullable final String string)
81                              {
82                                  return containsString(string);
83                              }
84                          }
85                  )
86          );
87      }
88  }