View Javadoc
1   package org.apache.maven.shared.dependency.analyzer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  
25  import org.codehaus.plexus.util.IOUtil;
26  import org.jmock.core.Constraint;
27  
28  /**
29   * 
30   * 
31   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
32   * @version $Id$
33   */
34  public class InputStreamConstraint
35      implements Constraint
36  {
37      // constants --------------------------------------------------------------
38  
39      private static final String DEFAULT_CHARSET_NAME = "UTF-8";
40  
41      // fields -----------------------------------------------------------------
42  
43      private final String expected;
44  
45      private final String charsetName;
46  
47      // constructors -----------------------------------------------------------
48  
49      public InputStreamConstraint( String expected )
50      {
51          this( expected, DEFAULT_CHARSET_NAME );
52      }
53  
54      public InputStreamConstraint( String expected, String charsetName )
55      {
56          this.expected = expected;
57          this.charsetName = charsetName;
58      }
59  
60      // Constraint methods -----------------------------------------------------
61  
62      /*
63       * @see org.jmock.core.Constraint#eval(java.lang.Object)
64       */
65      public boolean eval( Object object )
66      {
67          if ( !( object instanceof InputStream ) )
68          {
69              return false;
70          }
71  
72          InputStream in = (InputStream) object;
73  
74          try
75          {
76              String actual = IOUtil.toString( in, charsetName );
77  
78              return expected.equals( actual );
79          }
80          catch ( IOException exception )
81          {
82              return false;
83          }
84      }
85  
86      // SelfDescribing methods -------------------------------------------------
87  
88      /*
89       * @see org.jmock.core.SelfDescribing#describeTo(java.lang.StringBuffer)
90       */
91      public StringBuffer describeTo( StringBuffer buffer )
92      {
93          buffer.append( "in(" );
94          buffer.append( "\"" ).append( expected ).append( "\"" );
95          buffer.append( "," ).append( charsetName );
96          buffer.append( ")" );
97  
98          return buffer;
99      }
100 }