public static void assertIntegerInRange(String candidate, int start, int end, String message) throws CruiseControlException { try { int asInt = Integer.parseInt(candidate); if (asInt < start || asInt > end) { fail(message); } } catch (NumberFormatException e) { fail(message); } }
/** * Assertion for encoding string (must be recognised by {@link InputStreamReader} constructor) * * @param encoding * @param plugin * @throws CruiseControlException if encoding is invalid */ public static void assertEncoding(String encoding, Class plugin) throws CruiseControlException { if (encoding == null || plugin == null) { throw new IllegalArgumentException("All parameters are required."); } try { new InputStreamReader(new ByteArrayInputStream(new byte[10]), encoding).close(); } catch (UnsupportedEncodingException e) { fail("Encoding " + encoding + " not supported on plugin [" + plugin.getName() + "]", e); } catch (IOException e) { fail("Failed", e); } }
@Override public void validate() throws CruiseControlException { // Must be set ValidationHelper.assertEncoding(this.encoding, getClass()); ValidationHelper.assertIsSet(this.file, "file", getClass()); // Invalid combination ValidationHelper.assertFalse(!this.overwrite && this.append, "action not set properly"); // Set the file this.file = joinPath(this.file); ValidationHelper.assertIsNotDirectory(this.file, "file", getClass()); if (!this.overwrite) { // When overwrite is disabled, the file must not exist try { ValidationHelper.assertNotExists(this.file, "file", getClass()); } catch (CruiseControlException e) { ValidationHelper.fail("Trying to overwrite file without permition."); } } for (Content c : this.messages) { c.validate(); } }
/** Handle required plugin attributes. */ public static void assertIsSet( final Object attribute, final String attributeName, final String pluginName) throws CruiseControlException { if (attribute == null) { fail("'" + attributeName + "' is required for " + pluginName); } }
/** * Called after the configuration is read to make sure that all the mandatory parameters were * specified.. * * @throws CruiseControlException if there was a configuration error. */ public void validate() throws CruiseControlException { if (name == null && file == null && environment == null) { ValidationHelper.fail("At least one of name, file or environment must be set."); } if ((name != null && (file != null || environment != null) || (file != null && (environment != null)))) { ValidationHelper.fail("At most one of name, file or environment can be set."); } if (file != null && file.trim().length() > 0) { // TODO FIXME add exists check. } if (name != null && value == null) { ValidationHelper.fail("name and value must be set simultaneoulsy."); } }
/** Handle required plugin child elements. */ public static void assertHasChild( final Object child, final String usualChildNodeName, final Class plugin) throws CruiseControlException { if (child == null) { fail( "child <" + usualChildNodeName + "> is required for plugin " + getShortClassName(plugin)); } }
public static void assertIsReadable(File file, String attributeName, Class plugin) throws CruiseControlException { if (file == null || attributeName == null || plugin == null) { throw new IllegalArgumentException("All parameters are required."); } if (!file.canRead()) { fail( "File specified [" + file.getAbsolutePath() + "] for attribute [" + attributeName + "] on plugin [" + plugin.getName() + "] is not readable."); } }
public static void assertIsNotDirectory(File file, String attributeName, Class plugin) throws CruiseControlException { if (file == null || attributeName == null || plugin == null) { throw new IllegalArgumentException("All parameters are required."); } if (file.isDirectory()) { fail( "File specified [" + file.getAbsolutePath() + "] for attribute [" + attributeName + "] on plugin [" + plugin.getName() + "] is really a directory where a file was expected."); } }
/** * @param masterAttribute * @param childAttribute * @param plugin * @throws CruiseControlException */ public static void assertIsDependentSet( String masterAttribute, String masterAttributeName, String childAttribute, String childAttributeName, Class plugin) throws CruiseControlException { if (masterAttribute != null) { if (childAttribute == null) { fail( "'" + childAttributeName + "' is required for " + getShortClassName(plugin) + " if '" + masterAttributeName + "' is set"); } } }
public static void assertFalse(boolean condition, String message) throws CruiseControlException { if (condition) { fail(message); } }