001package com.hfg.util; 002 003import com.hfg.exception.ProgrammingException; 004import com.hfg.security.LoginCredentials; 005import com.hfg.util.io.RegexpFilenameFilter; 006 007import java.io.File; 008import java.io.FilenameFilter; 009import java.io.IOException; 010import java.io.InputStream; 011import java.io.UnsupportedEncodingException; 012import java.net.URL; 013import java.net.URLDecoder; 014import java.util.regex.Pattern; 015 016//------------------------------------------------------------------------------ 017/** 018 Utility class for tests. 019 020 @author J. Alex Taylor, hairyfatguy.com 021 */ 022//------------------------------------------------------------------------------ 023// com.hfg XML/HTML Coding Library 024// 025// This library is free software; you can redistribute it and/or 026// modify it under the terms of the GNU Lesser General Public 027// License as published by the Free Software Foundation; either 028// version 2.1 of the License, or (at your option) any later version. 029// 030// This library is distributed in the hope that it will be useful, 031// but WITHOUT ANY WARRANTY; without even the implied warranty of 032// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 033// Lesser General Public License for more details. 034// 035// You should have received a copy of the GNU Lesser General Public 036// License along with this library; if not, write to the Free Software 037// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 038// 039// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 040// jataylor@hairyfatguy.com 041//------------------------------------------------------------------------------ 042 043public class TestUtil 044{ 045 private static File sProjectRootDir; 046 private static File sTestOutputDir; 047 048 //--------------------------------------------------------------------------- 049 public static InputStream getRsrcStream(String inResourcePath, Class inReferenceClass) 050 throws IOException 051 { 052 InputStream stream = inReferenceClass.getResourceAsStream(inResourcePath); 053 if (null == stream) 054 { 055 throw new IOException("The rsrc " + StringUtil.singleQuote(inResourcePath) + " couldn't be located! Check the specified resource patterns."); 056 } 057 058 return stream; 059 } 060 061 //--------------------------------------------------------------------------- 062 public static File getRsrcFile(String inResourcePath, Class inReferenceClass) 063 throws IOException 064 { 065 URL url = inReferenceClass.getResource(inResourcePath); 066 if (null == url) 067 { 068 throw new IOException("The rsrc " + StringUtil.singleQuote(inResourcePath) + " couldn't be located! Check the specified resource patterns."); 069 } 070 071 return new File(URLDecoder.decode(url.getFile(), "UTF-8")); 072 } 073 074 //--------------------------------------------------------------------------- 075 public static LoginCredentials getAnonymousFTP_Credentials() 076 { 077 return new LoginCredentials("anonymous", (System.getProperty("user.name") + "@" + NetworkUtil.getFullyQualifiedDomainName()).toCharArray()); 078 } 079 080 //--------------------------------------------------------------------------- 081 082 /** 083 * Locates the project root directory by searching for the build.gradle file. 084 */ 085 public static File getGradleProjectRootDir() 086 { 087 if (null == sProjectRootDir) 088 { 089 // The current working directory could be anywhere. Use this class file as a reference point. 090 URL selfUrl = TestUtil.class.getResource(TestUtil.class.getSimpleName() + ".class"); 091 File file; 092 try 093 { 094 file = new File(URLDecoder.decode(selfUrl.getFile(), "UTF-8")); 095 } 096 catch (UnsupportedEncodingException e) 097 { 098 throw new ProgrammingException(e); 099 } 100 101 File dir; 102 // If starting in a jar, back out to where the jar is located. 103 if (file.getPath().startsWith("jar:") 104 || file.getPath().startsWith("file:")) 105 { 106 String path = file.getPath(); 107 path = path.substring(path.lastIndexOf(":") + 1); 108 109 int limit = path.indexOf("jar!"); 110 if (limit > 0) 111 { 112 path = path.substring(0, limit); 113 } 114 115 path = path.substring(0, path.lastIndexOf("/")); 116 dir = new File(path); 117 } 118 else 119 { 120 dir = file.getParentFile(); 121 } 122 123 FilenameFilter filter = new RegexpFilenameFilter(Pattern.compile("build.gradle")); 124 125 if (dir != null) 126 { 127 while (dir != null 128 && (null == dir.listFiles(filter) 129 || dir.listFiles(filter).length < 1)) 130 { 131 dir = dir.getParentFile(); 132 } 133 134 if (null == dir 135 || dir.listFiles(filter).length < 1) 136 { 137 throw new RuntimeException("Couldn't locate the project root dir! Where is the build.gradle file?"); 138 } 139 140 sProjectRootDir = dir; 141 } 142 } 143 144 return sProjectRootDir; 145 } 146 147 148 //--------------------------------------------------------------------------- 149 150 /** 151 * Returns the directory that should be used for test output. 152 */ 153 public static File getTestOutputDir() 154 { 155 if (null == sTestOutputDir) 156 { 157 File outputDir = new File(getGradleProjectRootDir(), "testout"); 158 if (!outputDir.exists()) 159 { 160 outputDir.mkdirs(); 161 } 162 163 sTestOutputDir = outputDir; 164 } 165 166 return sTestOutputDir; 167 } 168}