All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.TestUtil Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util;

import com.hfg.exception.ProgrammingException;
import com.hfg.security.LoginCredentials;
import com.hfg.util.io.RegexpFilenameFilter;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.regex.Pattern;

//------------------------------------------------------------------------------
/**
 Utility class for tests.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class TestUtil
{
   private static File sProjectRootDir;
   private static File sTestOutputDir;

   //---------------------------------------------------------------------------
   public static InputStream getRsrcStream(String inResourcePath, Class inReferenceClass)
      throws IOException
   {
      InputStream stream = inReferenceClass.getResourceAsStream(inResourcePath);
      if (null == stream)
      {
         throw new IOException("The rsrc " + StringUtil.singleQuote(inResourcePath) + " couldn't be located! Check the specified resource patterns.");
      }

      return stream;
   }

   //---------------------------------------------------------------------------
   public static File getRsrcFile(String inResourcePath, Class inReferenceClass)
      throws IOException
   {
      URL url = inReferenceClass.getResource(inResourcePath);
      if (null == url)
      {
         throw new IOException("The rsrc " + StringUtil.singleQuote(inResourcePath) + " couldn't be located! Check the specified resource patterns.");
      }

      return new File(URLDecoder.decode(url.getFile(), "UTF-8"));
   }

   //---------------------------------------------------------------------------
   public static LoginCredentials getAnonymousFTP_Credentials()
   {
      return new LoginCredentials("anonymous", (System.getProperty("user.name") + "@" + NetworkUtil.getFullyQualifiedDomainName()).toCharArray());
   }

   //---------------------------------------------------------------------------

   /**
    * Locates the project root directory by searching for the build.gradle file.
    */
   public static File getGradleProjectRootDir()
   {
      if (null == sProjectRootDir)
      {
         // The current working directory could be anywhere. Use this class file as a reference point.
         URL selfUrl = TestUtil.class.getResource(TestUtil.class.getSimpleName() + ".class");
         File file;
         try
         {
            file = new File(URLDecoder.decode(selfUrl.getFile(), "UTF-8"));
         }
         catch (UnsupportedEncodingException e)
         {
            throw new ProgrammingException(e);
         }

         File dir;
         // If starting in a jar, back out to where the jar is located.
         if (file.getPath().startsWith("jar:")
               || file.getPath().startsWith("file:"))
         {
            String path = file.getPath();
            path = path.substring(path.lastIndexOf(":") + 1);

            int limit = path.indexOf("jar!");
            if (limit > 0)
            {
               path = path.substring(0, limit);
            }

            path = path.substring(0, path.lastIndexOf("/"));
            dir = new File(path);
         }
         else
         {
            dir = file.getParentFile();
         }

         FilenameFilter filter = new RegexpFilenameFilter(Pattern.compile("build.gradle"));

         if (dir != null)
         {
            while (dir != null
                   && (null == dir.listFiles(filter)
                       || dir.listFiles(filter).length < 1))
            {
               dir = dir.getParentFile();
            }

            if (null == dir
                || dir.listFiles(filter).length < 1)
            {
               throw new RuntimeException("Couldn't locate the project root dir! Where is the build.gradle file?");
            }

            sProjectRootDir = dir;
         }
      }

      return sProjectRootDir;
   }


   //---------------------------------------------------------------------------

   /**
    * Returns the directory that should be used for test output.
    */
   public static File getTestOutputDir()
   {
      if (null == sTestOutputDir)
      {
         File outputDir = new File(getGradleProjectRootDir(), "testout");
         if (!outputDir.exists())
         {
            outputDir.mkdirs();
         }

         sTestOutputDir = outputDir;
      }

      return sTestOutputDir;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy