View Javadoc

1   package org.codehaus.modello.verifier;
2   
3   /*
4    * Copyright (c) 2004, Codehaus.org
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy of
7    * this software and associated documentation files (the "Software"), to deal in
8    * the Software without restriction, including without limitation the rights to
9    * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10   * of the Software, and to permit persons to whom the Software is furnished to do
11   * so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in all
14   * copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22   * SOFTWARE.
23   */
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.Reader;
28  import java.lang.reflect.Method;
29  
30  import junit.framework.Assert;
31  
32  import org.codehaus.plexus.util.ReaderFactory;
33  
34  public abstract class Verifier
35      extends Assert
36  {
37      public abstract void verify()
38          throws Throwable;
39  
40      protected File getTestFile( String name )
41      {
42          String basedir = System.getProperty( "basedir", new File( "" ).getAbsolutePath() );
43  
44          return new File( basedir, name );
45      }
46  
47      protected String getTestPath( String name )
48      {
49          String basedir = System.getProperty( "basedir", new File( "" ).getAbsolutePath() );
50  
51          return new File( basedir, name ).getAbsolutePath();
52      }
53  
54      protected Reader getXmlResourceReader( String name )
55          throws IOException
56      {
57          return ReaderFactory.newXmlReader( getClass().getResourceAsStream( name ) );
58      }
59  
60      protected void assertReader( Class<?> reader, Class<?> model, Class<?> input, Class<?> exception )
61      {
62          Method read;
63  
64          // Model read( InputStream|Reader ) throws IOException, ?
65          try
66          {
67              read = reader.getMethod( "read", input );
68  
69              assertEquals( "Bad return type of " + read, model, read.getReturnType() );
70  
71              for ( Class<?> e : read.getExceptionTypes() )
72              {
73                  assertTrue( "Unexpected exception " + e.getName() + " at " + read, IOException.class.equals( e )
74                      || exception.equals( e ) );
75              }
76          }
77          catch ( NoSuchMethodException e )
78          {
79              fail( e.toString() );
80          }
81  
82          // Model read( InputStream|Reader, boolean ) throws IOException, ?
83          try
84          {
85              read = reader.getMethod( "read", input, Boolean.TYPE );
86  
87              assertEquals( "Bad return type of " + read, model, read.getReturnType() );
88  
89              for ( Class<?> e : read.getExceptionTypes() )
90              {
91                  assertTrue( "Unexpected exception " + e.getName() + " at " + read, IOException.class.equals( e )
92                      || exception.equals( e ) );
93              }
94          }
95          catch ( NoSuchMethodException e )
96          {
97              fail( e.toString() );
98          }
99      }
100 
101     protected void assertWriter( Class<?> writer, Class<?> model, Class<?> output, Class<?> exception )
102     {
103         Method write;
104 
105         // write( OutputStream|Writer, Model ) throws IOException, ?
106         try
107         {
108             write = writer.getMethod( "write", output, model );
109 
110             for ( Class<?> e : write.getExceptionTypes() )
111             {
112                 assertTrue( "Unexpected exception " + e.getName() + " at " + write, IOException.class.equals( e )
113                     || exception.equals( e ) );
114             }
115         }
116         catch ( NoSuchMethodException e )
117         {
118             fail( e.toString() );
119         }
120     }
121 
122 }