View Javadoc
1   /*
2    * #%L
3    * JRst :: Api
4    * %%
5    * Copyright (C) 2004 - 2011 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  package org.nuiton.jrst;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.junit.Assert;
27  import org.junit.BeforeClass;
28  import org.nuiton.util.TimeLog;
29  
30  import java.io.File;
31  import java.io.IOException;
32  import java.io.InputStream;
33  
34  /**
35   * @author sletellier <letellier@codelutin.com>
36   */
37  public class JRSTAbstractTest extends Assert {
38  
39      /** to use log facility, just put in your code: log.info("..."); */
40      protected static Log log = LogFactory.getLog(JRSTAbstractTest.class);
41  
42      protected static File testWorkDir;
43      protected static File testResourcesDir;
44  
45      @BeforeClass
46      public static void initTest() throws IOException {
47          // get maven env basedir
48          String basedir = System.getenv("basedir");
49          if (basedir == null) {
50  
51              // says basedir is where we start tests.
52              basedir = new File("").getAbsolutePath();
53          }
54          File basedirFile = new File(basedir);
55          testWorkDir = new File(basedirFile,
56                                 "target" + File.separator +
57                                 "surefire-workdir");
58  
59          boolean b = testWorkDir.exists() || testWorkDir.mkdirs();
60          if (!b) {
61              throw new IOException(
62                      "Could not create workdir directory " + testWorkDir);
63          }
64  
65          testResourcesDir = new File(basedirFile,
66                                      "src" + File.separator +
67                                      "test" + File.separator +
68                                      "resources" + File.separator);
69      }
70  
71      public static File getOutputTestFile(String fileName) throws IOException {
72          File file = new File(testWorkDir,System.nanoTime()+"_"+ fileName);
73          file.createNewFile();
74          //file.deleteOnExit();
75          return file;
76      }
77  
78      public static String getOutputTestPath(String fileName) throws IOException {
79          File file = getOutputTestFile(fileName);
80          return file.getPath();
81      }
82  
83      public static File getTestFile(String testName) {
84          return new File(testResourcesDir, testName);
85      }
86  
87      public static File getBugTestFile(String testName) {
88          return new File(testResourcesDir, File.separator + "bugs" + File.separator + testName);
89      }
90  
91      public static InputStream getTestStream(String testName) {
92          return JRSTAbstractTest.class.getResourceAsStream("/" + testName);
93      }
94  
95      public static class JRSTTestGenerator {
96          protected String outputType;
97          protected File in;
98          protected File out;
99          protected JRST.Overwrite overwrite;
100 
101         public JRSTTestGenerator(String outputType, File in, File out, JRST.Overwrite overwrite) throws Exception {
102             this.outputType = outputType;
103             this.in = in;
104             this.out = out;
105             this.overwrite = overwrite;
106             generate();
107         }
108 
109         public void generate() throws Exception {
110             TimeLog simpleModeTimeLog = new TimeLog("Simple Mode");
111             simpleModeTimeLog.setTimeToLogInfo(100l);
112             Long start = TimeLog.getTime();
113             JRST.generate(outputType, in, out, overwrite, true);
114             simpleModeTimeLog.log(start, "Simple Mode generation");
115             assertJRST(in, out);
116 
117             TimeLog docutilsTimeLog = new TimeLog("DocUtils");
118             start = TimeLog.getTime();
119             JRST.generate(outputType, in, out, overwrite, false);
120             docutilsTimeLog.log(start, "DocUtils generation");
121             assertJRST(in, out);
122         }
123 
124         /**
125          * Overide this method to add assert
126          * @param in
127          * @param out
128          * @throws Exception
129          */
130         public void assertJRST(File in, File out) throws Exception {
131         }
132     }
133 
134     public void generate(String outputType, File in, File out, JRST.Overwrite overwrite) throws Exception {
135         new JRSTTestGenerator(outputType, in, out, overwrite);
136     }
137 }