001 /** 002 * Copyright 2005-2014 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.sampleu.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.kuali.rice.testtools.common.PropertiesUtils; 025 import org.kuali.rice.testtools.selenium.WebDriverLegacyITBase; 026 027 import java.io.File; 028 import java.io.FileInputStream; 029 import java.io.IOException; 030 import java.io.InputStream; 031 import java.util.Properties; 032 033 /** 034 * @see FreemarkerUtil 035 * @author Kuali Rice Team (rice.collab@kuali.org) 036 */ 037 public abstract class FreemarkerAftBase extends WebDriverLegacyITBase { 038 039 protected final Logger LOG = Logger.getLogger(getClass()); 040 041 protected Configuration cfg; 042 043 protected abstract String getTemplateDir(); 044 045 @Override 046 @Before 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 = new PropertiesUtils().loadProperties(in); 086 in.close(); 087 } 088 089 return props; 090 } 091 092 /** 093 * {@link FreemarkerUtil#writeTemplateToFile(java.io.File, freemarker.template.Template, java.util.Properties)} 094 * @param file 095 * @param template 096 * @param props 097 * @return 098 * @throws IOException 099 * @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 }