View Javadoc
1   /**
2    * Copyright 2005-2016 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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 edu.sampleu.common;
17  
18  import freemarker.cache.ClassTemplateLoader;
19  import freemarker.template.Configuration;
20  import freemarker.template.Template;
21  import freemarker.template.TemplateException;
22  import org.apache.log4j.Logger;
23  import org.junit.Before;
24  import org.kuali.rice.testtools.common.PropertiesUtils;
25  import org.kuali.rice.testtools.selenium.WebDriverLegacyITBase;
26  
27  import java.io.File;
28  import java.io.FileInputStream;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.util.Properties;
32  
33  /**
34   * @see FreemarkerUtil
35   * @author Kuali Rice Team (rice.collab@kuali.org)
36   */
37  public abstract class FreemarkerAftBase extends WebDriverLegacyITBase {
38  
39      protected final Logger LOG = Logger.getLogger(getClass());
40  
41      protected Configuration cfg;
42  
43      protected abstract String getTemplateDir();
44  
45      @Override
46      @Before
47      public void testSetUp() {
48          super.testSetUp();
49          // generated load users and group resources
50          cfg = new Configuration();
51          cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), getTemplateDir()));
52      }
53  
54      /**
55       * Calls ftlWrite that also accepts a key, using the output getName as the key.
56       * {@link FreemarkerUtil#ftlWrite(java.io.File, freemarker.template.Template, java.io.InputStream)}
57       * @param output
58       * @param template
59       * @return
60       * @throws IOException
61       * @throws TemplateException
62       */
63      public File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
64  
65          return FreemarkerUtil.ftlWrite(output.getName(), output, template, inputStream);
66      }
67  
68      /**
69       * TODO can we cut this down to one param?
70       * {@link FreemarkerUtil#loadProperties(java.io.InputStream)}
71       * @param fileLocation null means use resourceLocation
72       * @param resourceLocation
73       * @return
74       * @throws IOException
75       */
76      protected Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
77          Properties props = null;
78          InputStream in = null;
79          if(fileLocation != null) {
80              in = new FileInputStream(fileLocation);
81          } else {
82              in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
83          }
84          if(in != null) {
85              props = new PropertiesUtils().loadProperties(in);
86              in.close();
87          }
88  
89          return props;
90      }
91  
92      /**
93      * {@link FreemarkerUtil#writeTemplateToFile(java.io.File, freemarker.template.Template, java.util.Properties)}
94      * @param file
95      * @param template
96      * @param props
97      * @return
98      * @throws IOException
99      * @throws TemplateException
100     */
101     protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
102         return FreemarkerUtil.writeTemplateToFile(file, template, props);
103     }
104 }