View Javadoc

1   /*
2    * Copyright 2008-2013 smartics, Kronseder & Reiner GmbH
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  package de.smartics.util.test.matcher.io;
17  
18  import org.hamcrest.Description;
19  import org.hamcrest.TypeSafeDiagnosingMatcher;
20  
21  /**
22   * A matcher to check that the contents of two stream contain the identical
23   * information.
24   *
25   * @param <T> the type of the matcher's expected values.
26   */
27  public abstract class AbstractEqualContentAs<T> extends
28      TypeSafeDiagnosingMatcher<T>
29  {
30    // ********************************* Fields *********************************
31  
32    // --- constants ------------------------------------------------------------
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The content read from the other stream.
38     */
39    private final String expectedContent;
40  
41    /**
42     * The actual content found. If <code>null</code> no value has yet been read.
43     * <p>
44     * Since the reader cannot be read twice, the content is stored after the
45     * first read.
46     * </p>
47     */
48    private String actualContent;
49  
50    // ****************************** Initializer *******************************
51  
52    // ****************************** Constructors ******************************
53  
54    /**
55     * Convenience constructor if the content is served as a String.
56     *
57     * @param expectedContent the expected content.
58     */
59    public AbstractEqualContentAs(final String expectedContent)
60    {
61      this.expectedContent = expectedContent;
62    }
63  
64    // ****************************** Inner Classes *****************************
65  
66    // ********************************* Methods ********************************
67  
68    // --- init -----------------------------------------------------------------
69  
70    // --- get&set --------------------------------------------------------------
71  
72    /**
73     * Returns the actual content found. If <code>null</code> no value has yet
74     * been read.
75     * <p>
76     * Since the reader cannot be read twice, the content is stored after the
77     * first read.
78     * </p>
79     *
80     * @return the actual content found. May be <code>null</code>.
81     */
82    public final String getActualContent()
83    {
84      return actualContent;
85    }
86  
87    // --- business -------------------------------------------------------------
88  
89    /**
90     * {@inheritDoc}
91     *
92     * @see org.hamcrest.SelfDescribing#describeTo(org.hamcrest.Description)
93     */
94    public final void describeTo(final Description description)
95    {
96      description.appendValue(expectedContent);
97    }
98  
99    /**
100    * {@inheritDoc}
101    *
102    * @see org.hamcrest.TypeSafeDiagnosingMatcher#matchesSafely(java.lang.Object,
103    *      org.hamcrest.Description)
104    */
105   @Override
106   protected final boolean matchesSafely(final T actual,
107       final Description mismatchDescription)
108   {
109     try
110     {
111       if (actualContent == null)
112       {
113         actualContent = toString(actual);
114       }
115 
116       final boolean result = expectedContent.equals(actualContent);
117       if (!result)
118       {
119         mismatchDescription.appendValue(actualContent);
120       }
121       return result;
122     }
123     catch (final IllegalArgumentException e)
124     {
125       return false;
126     }
127   }
128 
129   /**
130    * Turns the instance to its string representation.
131    *
132    * @param actual the instance whose string representation is requested.
133    * @return the string representation of <code>actual</code>.
134    * @throws IllegalArgumentException if no string representation can be
135    *           created.
136    */
137   protected abstract String toString(T actual) throws IllegalArgumentException;
138 
139   // --- object basics --------------------------------------------------------
140 
141 }