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 org.codehaus.plexus.util.xml.XmlStreamReader;
31  
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertTrue;
34  import static org.junit.Assert.fail;
35  
36  public abstract class Verifier {
37      public abstract void verify() throws Throwable;
38  
39      protected File getTestFile(String name) {
40          String basedir = System.getProperty("basedir", new File("").getAbsolutePath());
41  
42          return new File(basedir, name);
43      }
44  
45      protected String getTestPath(String name) {
46          String basedir = System.getProperty("basedir", new File("").getAbsolutePath());
47  
48          return new File(basedir, name).getAbsolutePath();
49      }
50  
51      protected Reader getXmlResourceReader(String name) throws IOException {
52          return new XmlStreamReader(getClass().getResourceAsStream(name));
53      }
54  
55      protected void assertReader(Class<?> reader, Class<?> model, Class<?> input, Class<?> exception) {
56          Method read;
57  
58          // Model read( InputStream|Reader ) throws IOException, ?
59          try {
60              read = reader.getMethod("read", input);
61  
62              assertEquals("Bad return type of " + read, model, read.getReturnType());
63  
64              for (Class<?> e : read.getExceptionTypes()) {
65                  assertTrue(
66                          "Unexpected exception " + e.getName() + " at " + read,
67                          IOException.class.equals(e) || exception.equals(e));
68              }
69          } catch (NoSuchMethodException e) {
70              fail(e.toString());
71          }
72  
73          // Model read( InputStream|Reader, boolean ) throws IOException, ?
74          try {
75              read = reader.getMethod("read", input, Boolean.TYPE);
76  
77              assertEquals("Bad return type of " + read, model, read.getReturnType());
78  
79              for (Class<?> e : read.getExceptionTypes()) {
80                  assertTrue(
81                          "Unexpected exception " + e.getName() + " at " + read,
82                          IOException.class.equals(e) || exception.equals(e));
83              }
84          } catch (NoSuchMethodException e) {
85              fail(e.toString());
86          }
87      }
88  
89      protected void assertWriter(Class<?> writer, Class<?> model, Class<?> output, Class<?> exception) {
90          Method write;
91  
92          // write( OutputStream|Writer, Model ) throws IOException, ?
93          try {
94              write = writer.getMethod("write", output, model);
95  
96              for (Class<?> e : write.getExceptionTypes()) {
97                  assertTrue(
98                          "Unexpected exception " + e.getName() + " at " + write,
99                          IOException.class.equals(e) || exception.equals(e));
100             }
101         } catch (NoSuchMethodException e) {
102             fail(e.toString());
103         }
104     }
105 }