KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > ValidationHelper


1 package net.sourceforge.cruisecontrol.util;
2
3 import net.sourceforge.cruisecontrol.CruiseControlException;
4 import java.io.File JavaDoc;
5
6 /**
7  * Reusable assertion like facility for handling configuration mistakes (e.g. unsupported/required attributes).
8  *
9  * @author <a HREF="mailto:jerome@coffeebreaks.org">Jerome Lacoste</a>
10  */

11 public final class ValidationHelper {
12     private ValidationHelper() {
13     }
14
15     /**
16      * Handle required plugin attributes.
17      */

18     public static void assertIsSet(final Object JavaDoc attribute, final String JavaDoc attributeName, final Class JavaDoc plugin)
19             throws CruiseControlException {
20         assertIsSet(attribute, attributeName, getShortClassName(plugin));
21     }
22
23     /**
24      * Handle required plugin attributes.
25      */

26     public static void assertIsSet(final Object JavaDoc attribute, final String JavaDoc attributeName, final String JavaDoc pluginName)
27             throws CruiseControlException {
28         if (attribute == null) {
29             fail("'" + attributeName + "' is required for " + pluginName);
30         }
31     }
32
33     /**
34      * Handle required plugin attributes.
35      * @throws CruiseControlException if empty (null OK)
36      */

37     public static void assertNotEmpty(final String JavaDoc attribute, final String JavaDoc attributeName, final Class JavaDoc plugin)
38             throws CruiseControlException {
39         assertTrue(attribute == null || !"".equals(attribute), attributeName
40                 + " must be meaningful or not provided on " + getShortClassName(plugin));
41     }
42
43     /**
44      * Handle required plugin child elements.
45      */

46     public static void assertHasChild(final Object JavaDoc child, final Class JavaDoc childType,
47         final String JavaDoc usualChildNodeName, final Class JavaDoc 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     /**
55      * Handle required plugin child elements.
56      */

57     public static void assertHasChild(final Object JavaDoc child,
58         final String JavaDoc usualChildNodeName, final Class JavaDoc 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 JavaDoc message) throws CruiseControlException {
65         if (!condition) {
66             fail(message);
67         }
68     }
69
70     public static void fail(String JavaDoc message) throws CruiseControlException {
71         throw new CruiseControlException(message);
72     }
73
74     public static void fail(String JavaDoc message, Exception JavaDoc e) throws CruiseControlException {
75         throw new CruiseControlException(message, e);
76     }
77
78     public static void assertFalse(boolean condition, String JavaDoc message) throws CruiseControlException {
79         if (condition) {
80             fail(message);
81         }
82     }
83
84     /**
85      * The short class name of an object.
86      * @return The short class name
87      * @throws NullPointerException if object is null
88      */

89     private static String JavaDoc getShortClassName(final Class JavaDoc plugin) {
90         final String JavaDoc fullClassName = plugin.getName();
91         return fullClassName.substring(fullClassName.lastIndexOf('.') + 1);
92     }
93
94     public static void assertExists(File JavaDoc file, String JavaDoc attributeName, Class JavaDoc plugin) throws CruiseControlException {
95         if (file == null || attributeName == null || plugin == null) {
96             throw new IllegalArgumentException JavaDoc("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 JavaDoc file, String JavaDoc attributeName, Class JavaDoc plugin)
106             throws CruiseControlException {
107         if (file == null || attributeName == null || plugin == null) {
108             throw new IllegalArgumentException JavaDoc("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 JavaDoc file, String JavaDoc attributeName, Class JavaDoc plugin) throws CruiseControlException {
118         if (file == null || attributeName == null || plugin == null) {
119             throw new IllegalArgumentException JavaDoc("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