| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| Utils |
|
| 3.0;3 |
| 1 | //////////////////////////////////////////////////////////////////////////////// |
|
| 2 | // |
|
| 3 | // ObjectLab is sponsoring QALab |
|
| 4 | // |
|
| 5 | // Based in London, we are world leaders in the design and development |
|
| 6 | // of bespoke applications for the Securities Financing markets. |
|
| 7 | // |
|
| 8 | // <a href="http://www.objectlab.co.uk/open">Click here to learn more</a> |
|
| 9 | // ___ _ _ _ _ _ |
|
| 10 | // / _ \| |__ (_) ___ ___| |_| | __ _| |__ |
|
| 11 | // | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ |
|
| 12 | // | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | |
|
| 13 | // \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ |
|
| 14 | // |__/ |
|
| 15 | // |
|
| 16 | // http://www.ObjectLab.co.uk |
|
| 17 | // --------------------------------------------------------------------------- |
|
| 18 | // |
|
| 19 | //QALab is released under the GNU General Public License. |
|
| 20 | // |
|
| 21 | //QALab: Collects QA Statistics from your build over time. |
|
| 22 | //2005+, ObjectLab Ltd |
|
| 23 | // |
|
| 24 | //This library is free software; you can redistribute it and/or |
|
| 25 | //modify it under the terms of the GNU General Public |
|
| 26 | //License as published by the Free Software Foundation; either |
|
| 27 | //version 2.1 of the License, or (at your option) any later version. |
|
| 28 | // |
|
| 29 | //This library is distributed in the hope that it will be useful, |
|
| 30 | //but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
| 31 | //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
| 32 | //General Public License for more details. |
|
| 33 | // |
|
| 34 | //You should have received a copy of the GNU General Public |
|
| 35 | //License along with this library; if not, write to the Free Software |
|
| 36 | //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
| 37 | // |
|
| 38 | //////////////////////////////////////////////////////////////////////////////// |
|
| 39 | package net.objectlab.qalab.m2.util; |
|
| 40 | ||
| 41 | import java.io.File; |
|
| 42 | import java.io.IOException; |
|
| 43 | import java.io.FileNotFoundException; |
|
| 44 | import java.io.InputStream; |
|
| 45 | ||
| 46 | import java.text.SimpleDateFormat; |
|
| 47 | import java.util.Calendar; |
|
| 48 | import java.util.Locale; |
|
| 49 | ||
| 50 | /** |
|
| 51 | * Some handy utils for the QALab maven 2 plugin. |
|
| 52 | * @author <a href="http://www.davesag.com">Dave Sag</a>. |
|
| 53 | */ |
|
| 54 | 98 | public final class Utils { |
| 55 | ||
| 56 | /** |
|
| 57 | * private contructor as this is a utils class. |
|
| 58 | */ |
|
| 59 | 0 | private Utils() { |
| 60 | // does nothing. |
|
| 61 | 0 | } |
| 62 | ||
| 63 | /** |
|
| 64 | * performs a simple check that the file is real and readable. |
|
| 65 | * @param aFile the file to check |
|
| 66 | * @param aParamName the name of the input param - used for error reporting. |
|
| 67 | * @throws IOException if the file was null or not readable. |
|
| 68 | */ |
|
| 69 | public static void checkFile(final File aFile, final String aParamName) |
|
| 70 | throws IOException { |
|
| 71 | 147 | if (aFile == null) { |
| 72 | 49 | throw new FileNotFoundException(aParamName |
| 73 | + " is mandatory"); |
|
| 74 | } |
|
| 75 | ||
| 76 | 98 | if (!aFile.exists()) { |
| 77 | 49 | throw new FileNotFoundException(aFile |
| 78 | + " does not exist."); |
|
| 79 | } |
|
| 80 | ||
| 81 | 49 | if (!aFile.canRead()) { |
| 82 | 0 | throw new IOException("Unable to read from '" |
| 83 | + aFile + "' while processing value for param " |
|
| 84 | + aParamName); |
|
| 85 | } |
|
| 86 | ||
| 87 | 49 | } |
| 88 | ||
| 89 | /** |
|
| 90 | * extract an input stream from the supplied resource file. The resource is |
|
| 91 | * presumed to be somewhere on the classpath for this app. |
|
| 92 | * @param aResourcePath the path to the resource we need the |
|
| 93 | * actual system path for. Must not be empty or null. |
|
| 94 | * @return an input stream pointing at the named resource. |
|
| 95 | * @throws IOException if the resource path was not readable. |
|
| 96 | */ |
|
| 97 | public static InputStream extractAsInputStream(final String aResourcePath) |
|
| 98 | throws IOException { |
|
| 99 | ||
| 100 | 392 | assert aResourcePath != null : "The supplied path was null"; |
| 101 | 343 | assert !"".equals(aResourcePath) : "The supplied path was empty"; |
| 102 | ||
| 103 | 294 | final ClassLoader l = Utils.class.getClassLoader(); |
| 104 | 294 | final InputStream res = l.getResourceAsStream(aResourcePath); |
| 105 | 294 | if (res == null) { |
| 106 | 49 | throw new FileNotFoundException( |
| 107 | "could not find resource at " + aResourcePath); |
|
| 108 | } |
|
| 109 | 245 | return res; |
| 110 | } |
|
| 111 | ||
| 112 | /** |
|
| 113 | * Generate a yyyy-MM-dd date based on now less the number of hours |
|
| 114 | * to offset. |
|
| 115 | * @param anOffset the number of hours to subtract from now. |
|
| 116 | * @return a yyyy-MM-dd formatted string representing now less the offset. |
|
| 117 | */ |
|
| 118 | public static String formatDateBasedOnOffset(final String anOffset) { |
|
| 119 | 147 | assert anOffset != null : "offset supplied must not be null"; |
| 120 | 98 | final int hours = Integer.parseInt(anOffset); |
| 121 | 49 | assert hours > 0 : "offset supplied must be positive"; |
| 122 | ||
| 123 | 49 | final Calendar c = Calendar.getInstance(); |
| 124 | 49 | c.add(Calendar.HOUR, -hours); |
| 125 | ||
| 126 | 49 | final SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", |
| 127 | Locale.getDefault()); |
|
| 128 | 49 | return df.format(c.getTime()); |
| 129 | } |
|
| 130 | } |
|
| 131 | /* |
|
| 132 | * ObjectLab is sponsoring QALab |
|
| 133 | * |
|
| 134 | * Based in London, we are world leaders in the design and development |
|
| 135 | * of bespoke applications for the securities financing markets. |
|
| 136 | * |
|
| 137 | * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a> |
|
| 138 | * ___ _ _ _ _ _ |
|
| 139 | * / _ \| |__ (_) ___ ___| |_| | __ _| |__ |
|
| 140 | * | | | | '_ \| |/ _ \/ __| __| | / _` | '_ \ |
|
| 141 | * | |_| | |_) | | __/ (__| |_| |__| (_| | |_) | |
|
| 142 | * \___/|_.__// |\___|\___|\__|_____\__,_|_.__/ |
|
| 143 | * |__/ |
|
| 144 | * |
|
| 145 | * www.ObjectLab.co.uk |
|
| 146 | */ |