001    /**
002     * Copyright 2005-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package edu.samplu.common;
017    
018    import freemarker.cache.ClassTemplateLoader;
019    import freemarker.template.Configuration;
020    import freemarker.template.Template;
021    import freemarker.template.TemplateException;
022    import org.apache.log4j.Logger;
023    import org.junit.Before;
024    import org.testng.annotations.BeforeMethod;
025    
026    import java.io.File;
027    import java.io.FileInputStream;
028    import java.io.IOException;
029    import java.io.InputStream;
030    import java.util.Properties;
031    
032    /**
033     * @see FreemarkerUtil
034     * @author Kuali Rice Team (rice.collab@kuali.org)
035     */
036    public abstract class FreemarkerSTBase extends WebDriverLegacyITBase {
037    
038        protected final Logger LOG = Logger.getLogger(getClass());
039    
040        protected Configuration cfg;
041    
042        protected abstract String getTemplateDir();
043    
044        @Override
045        @Before
046        @BeforeMethod
047        public void testSetUp() {
048            super.testSetUp();
049            // generated load users and group resources
050            cfg = new Configuration();
051            cfg.setTemplateLoader(new ClassTemplateLoader(getClass().getClassLoader().getClass(), getTemplateDir()));
052        }
053    
054        /**
055         * Calls ftlWrite that also accepts a key, using the output getName as the key.
056         * {@link FreemarkerUtil#ftlWrite(java.io.File, freemarker.template.Template, java.io.InputStream)}
057         * @param output
058         * @param template
059         * @return
060         * @throws IOException
061         * @throws TemplateException
062         */
063        public File ftlWrite(File output, Template template, InputStream inputStream) throws IOException, TemplateException {
064    
065            return FreemarkerUtil.ftlWrite(output.getName(), output, template, inputStream);
066        }
067    
068        /**
069         * TODO can we cut this down to one param?
070         * {@link FreemarkerUtil#loadProperties(java.io.InputStream)}
071         * @param fileLocation null means use resourceLocation
072         * @param resourceLocation
073         * @return
074         * @throws IOException
075         */
076        protected Properties loadProperties(String fileLocation, String resourceLocation) throws IOException {
077            Properties props = null;
078            InputStream in = null;
079            if(fileLocation != null) {
080                in = new FileInputStream(fileLocation);
081            } else {
082                in = getClass().getClassLoader().getResourceAsStream(resourceLocation);
083            }
084            if(in != null) {
085                props = FreemarkerUtil.loadProperties(in);
086                in.close();
087            }
088    
089            return props;
090        }
091    
092        /**
093         * {@link FreemarkerUtil#systemPropertiesOverride(java.util.Properties, String)}
094         * @param props
095         * @param key
096         */
097        protected void systemPropertiesOverride(Properties props, String key) {
098            FreemarkerUtil.systemPropertiesOverride(props, key);
099        }
100    
101        /**
102        * {@link FreemarkerUtil#writeTemplateToFile(java.io.File, freemarker.template.Template, java.util.Properties)}
103        * @param file
104        * @param template
105        * @param props
106        * @return
107        * @throws IOException
108        * @throws TemplateException
109        */
110        protected static File writeTemplateToFile(File file, Template template, Properties props) throws IOException, TemplateException {
111            return FreemarkerUtil.writeTemplateToFile(file, template, props);
112        }
113    }