1 package net.sourceforge.cruisecontrol.util; 2 3 import net.sourceforge.cruisecontrol.CruiseControlException; 4 import java.io.File ; 5 6 11 public final class ValidationHelper { 12 private ValidationHelper() { 13 } 14 15 18 public static void assertIsSet(final Object attribute, final String attributeName, final Class plugin) 19 throws CruiseControlException { 20 assertIsSet(attribute, attributeName, getShortClassName(plugin)); 21 } 22 23 26 public static void assertIsSet(final Object attribute, final String attributeName, final String pluginName) 27 throws CruiseControlException { 28 if (attribute == null) { 29 fail("'" + attributeName + "' is required for " + pluginName); 30 } 31 } 32 33 37 public static void assertNotEmpty(final String attribute, final String attributeName, final Class plugin) 38 throws CruiseControlException { 39 assertTrue(attribute == null || !"".equals(attribute), attributeName 40 + " must be meaningful or not provided on " + getShortClassName(plugin)); 41 } 42 43 46 public static void assertHasChild(final Object child, final Class childType, 47 final String usualChildNodeName, final Class plugin) throws CruiseControlException { 48 if (child == null) { 49 fail("child <" + usualChildNodeName + "> (or type " + getShortClassName(childType) 50 + ") is required for plugin " + getShortClassName(plugin)); 51 } 52 } 53 54 57 public static void assertHasChild(final Object child, 58 final String usualChildNodeName, final Class plugin) throws CruiseControlException { 59 if (child == null) { 60 fail("child <" + usualChildNodeName + "> is required for plugin " + getShortClassName(plugin)); 61 } 62 } 63 64 public static void assertTrue(boolean condition, String message) throws CruiseControlException { 65 if (!condition) { 66 fail(message); 67 } 68 } 69 70 public static void fail(String message) throws CruiseControlException { 71 throw new CruiseControlException(message); 72 } 73 74 public static void fail(String message, Exception e) throws CruiseControlException { 75 throw new CruiseControlException(message, e); 76 } 77 78 public static void assertFalse(boolean condition, String message) throws CruiseControlException { 79 if (condition) { 80 fail(message); 81 } 82 } 83 84 89 private static String getShortClassName(final Class plugin) { 90 final String fullClassName = plugin.getName(); 91 return fullClassName.substring(fullClassName.lastIndexOf('.') + 1); 92 } 93 94 public static void assertExists(File file, String attributeName, Class plugin) throws CruiseControlException { 95 if (file == null || attributeName == null || plugin == null) { 96 throw new IllegalArgumentException ("All parameters are required."); 97 } 98 99 if (!file.exists()) { 100 fail("File specified [" + file.getAbsolutePath() + "] for attribute [" + attributeName + "] on plugin [" 101 + plugin.getName() + "] doesn't exist."); 102 } 103 } 104 105 public static void assertIsNotDirectory(File file, String attributeName, Class plugin) 106 throws CruiseControlException { 107 if (file == null || attributeName == null || plugin == null) { 108 throw new IllegalArgumentException ("All parameters are required."); 109 } 110 111 if (file.isDirectory()) { 112 fail("File specified [" + file.getAbsolutePath() + "] for attribute [" + attributeName + "] on plugin [" 113 + plugin.getName() + "] is really a directory where a file was expected."); 114 } 115 } 116 117 public static void assertIsReadable(File file, String attributeName, Class plugin) throws CruiseControlException { 118 if (file == null || attributeName == null || plugin == null) { 119 throw new IllegalArgumentException ("All parameters are required."); 120 } 121 122 if (!file.canRead()) { 123 fail("File specified [" + file.getAbsolutePath() + "] for attribute [" + attributeName + "] on plugin [" 124 + plugin.getName() + "] is not readable."); 125 } 126 } 127 } 128 | Popular Tags |