From: <res...@us...> - 2010-10-11 18:33:56
|
Revision: 3768 http://bigdata.svn.sourceforge.net/bigdata/?rev=3768&view=rev Author: resendes Date: 2010-10-11 18:33:47 +0000 (Mon, 11 Oct 2010) Log Message: ----------- Fixes and unit tests for com.bigdata.util.config package. Modified Paths: -------------- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/ConfigDeployUtil.java branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/ConfigurationUtil.java branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/Log4jLoggingHandler.java branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/LogUtil.java branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/NicUtil.java branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/ConfigDeployUtilTest.java Added Paths: ----------- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/ConfigurationUtilTest.java branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/Log4jLoggingHandlerTest.java branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/LogUtilTest.java branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/NicUtilTest.java Modified: branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/ConfigDeployUtil.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/ConfigDeployUtil.java 2010-10-11 17:26:00 UTC (rev 3767) +++ branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/ConfigDeployUtil.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -48,6 +48,12 @@ * invoked from within a Jini configuration. */ public class ConfigDeployUtil { + + private ConfigDeployUtil() { + //prevent instantiation + throw new AssertionError + ("ConfigDeployUtil cannot be instantiated"); + } private static Properties deploymentProps = null; @@ -63,6 +69,22 @@ //use current locale private static final NumberFormat numberFormat = NumberFormat.getInstance(); + /** + * Gets the <code>String</code> value associated with the given parameter + * in the configured properties file. The value, if any, returned will be: + * 1) the explicitly defined value for the property, or + * 2) the default value for that property, if a default exists. + * If the provided parameter has an associated set of valid entries in the + * properties files, then the value obtained, above, will be validated + * against those entries. If either the value (explicit or default) can't be + * found or fails the validation check, + * a <code>ConfigurationException</code> will be thrown. + * @param parameter The property name to lookup + * @return the <code>String</code> value associated the requested parameter, + * if available + * @throws ConfigurationException if the parameter value + * (explicit or default) was not defined. + */ public static String getString(String parameter) throws ConfigurationException { @@ -70,7 +92,23 @@ validateString(parameter, value); return value; } - + + /** + * Gets the <code>String[]</code> value associated with the given parameter + * in the configured properties file. The value, if any, returned will be: + * 1) the explicitly defined value for the property, or + * 2) the default value for that property, if a default exists. + * If the provided parameter has an associated set of valid entries in the + * properties files, then the component values obtained, above, will be validated + * against those entries. If either the value (explicit or default) can't be + * found or fails the validation check, + * a <code>ConfigurationException</code> will be thrown. + * @param parameter The property name to lookup + * @return the <code>String[]</code> value associated the requested parameter, + * if available + * @throws ConfigurationException if the parameter value + * (explicit or default) was not defined. + */ public static String[] getStringArray(String parameter) throws ConfigurationException { @@ -79,12 +117,46 @@ return value; } + /** + * Gets the <code>int</code> value associated with the given parameter + * in the configured properties file. The value, if any, returned will be: + * 1) the explicitly defined value for the property, or + * 2) the default value for that property, if a default exists. + * If the provided parameter has an associated set of valid entries in the + * properties files, then the value obtained, above, will be validated + * against those entries. If either the value (explicit or default) can't be + * found or fails the validation check, + * a <code>ConfigurationException</code> will be thrown. + * @param parameter The property name to lookup + * @return the <code>int</code> value associated the requested parameter, + * if available + * @throws ConfigurationException if the parameter value + * (explicit or default) was not defined. + */ + public static int getInt(String parameter) throws ConfigurationException { int value; value = validateInt(parameter, get(parameter)); return value; } + /** + * Gets the <code>long</code> value associated with the given parameter + * in the configured properties file. The value, if any, returned will be: + * 1) the explicitly defined value for the property, or + * 2) the default value for that property, if a default exists. + * If the provided parameter has an associated set of valid entries in the + * properties files, then the value obtained, above, will be validated + * against those entries. If either the value (explicit or default) can't be + * found or fails the validation check, + * a <code>ConfigurationException</code> will be thrown. + * @param parameter The property name to lookup + * @return the <code>long</code> value associated the requested parameter, + * if available + * @throws ConfigurationException if the parameter value + * (explicit or default) was not defined. + */ + public static long getLong(String parameter) throws ConfigurationException { long value; @@ -92,6 +164,22 @@ return value; } + /** + * Gets the <code>boolean</code> value associated with the given parameter + * in the configured properties file. The value, if any, returned will be: + * 1) the explicitly defined value for the property, or + * 2) the default value for that property, if a default exists. + * If the provided parameter has an associated set of valid entries in the + * properties files, then the value obtained, above, will be validated + * against those entries. If either the value (explicit or default) can't be + * found or fails the validation check, + * a <code>ConfigurationException</code> will be thrown. + * @param parameter The property name to lookup + * @return the <code>boolean</code> value associated the requested parameter, + * if available + * @throws ConfigurationException if the parameter value + * (explicit or default) was not defined. + */ public static boolean getBoolean(String parameter) throws ConfigurationException { boolean value; @@ -114,6 +202,17 @@ return boolValue; } + /** + * Gets the description value associated with the given parameter + * in the configured properties file. The method returns + * <code>null</code> if the property is not found. + * @param parameter The property name to lookup + * @return the <code>String</code> value associated the requested parameter, + * if available. Otherwise return <code>null</code> + * @throws ConfigurationException if there was a problem accessing + * the parameter value. + */ + public static String getDescription(String parameter) throws ConfigurationException { @@ -122,6 +221,18 @@ return value; } + /** + * Gets the type value associated with the given parameter + * in the configured properties file. The method returns + * <code>null</code> if the property is not found. + * @param parameter The property name to lookup + * @return the <code>String</code> value of the type + * associated the requested parameter, + * if available. Otherwise return <code>null</code> + * @throws ConfigurationException if there was a problem accessing + * the parameter value. + */ + public static String getType(String parameter) throws ConfigurationException { @@ -130,6 +241,15 @@ return value; } + /** + * Gets the default value associated with the given parameter + * in the configured properties file. + * @param parameter The property name to lookup + * @return the <code>String</code> value of the type + * associated the requested parameter, + * if available. Otherwise return <code>null</code> + * @throws ConfigurationException if no default value was specified + */ public static String getDefault(String parameter) throws ConfigurationException { @@ -445,10 +565,9 @@ * (defined in <code>loadDefaultProps</code> and <code>loadOverrideProps</code>). * This method is synchronized in order to ensure that the returned reference is * a singleton instance. - * Note: This method should be private, but is needed by the unit test in order - * to access and modify the <code>Properties</code> method. - * @return Properties instance containing the default and user-defined overrides - * for configuration properties. + * Note: This method should be private, but is needed by the unit tests in order + * to access and modify the underlying <code>Properties</code> object. + * @return Properties instance containing the configuration properties. */ synchronized static Properties getDeploymentProperties() { if(deploymentProps == null) { Modified: branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/ConfigurationUtil.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/ConfigurationUtil.java 2010-10-11 17:26:00 UTC (rev 3767) +++ branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/ConfigurationUtil.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -28,10 +28,10 @@ import org.apache.log4j.Level; import org.apache.log4j.Logger; -import com.sun.jini.config.ConfigUtil; import net.jini.url.httpmd.HttpmdUtil; import java.io.IOException; +import java.net.UnknownHostException; /** * Utility class that provides a set of static convenience methods @@ -43,6 +43,11 @@ * This class cannot be instantiated. */ public class ConfigurationUtil { + + private ConfigurationUtil() { + //prevent instantiation + throw new AssertionError + ("ConfigurationUtil cannot be instantiated"); } private static final Logger logger = LogUtil.getLog4jLogger( ConfigurationUtil.class ); @@ -95,7 +100,7 @@ * <p> * Note that the <code>name</code> parameter is handled specially by * this method. The value input to that <code>String</code> parameter - * is interpretted in one of two ways by this method: + * is interpreted in one of two ways by this method: * <p><ul> * <li> The name of the local <i>network interface</i> over which the * class server will communicate with clients requesting the @@ -109,7 +114,7 @@ * This method first treats the value of the <code>name</code> parameter * as a network interface, attempting to determine that interface's * corresponding IP address. Should that attempt fail, then this method - * then assumes that the caller intended this parameter to be interpretted + * then assumes that the caller intended this parameter to be interpreted * as an IP address or host name; in which case, this method uses * the value of the parameter as the <i>address/host</i> component of * codebase being constructed. @@ -181,7 +186,7 @@ * any value, including <code>null</code>). * * @throws IllegalArgumentException if the value input for - * <code>port</code> is negtive. + * <code>port</code> is negative. */ public static String computeCodebase(String name, String jarFile, @@ -220,6 +225,10 @@ logger.log(Level.TRACE, name+" - not a valid " +"network interface, assuming host name"); } + if (ipAddr==null) { + throw new UnknownHostException( + "Could not determine IP address for given name:" + name); + } // Construct the codebase, either httpmd or http if(doHttpmd) { Modified: branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/Log4jLoggingHandler.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/Log4jLoggingHandler.java 2010-10-11 17:26:00 UTC (rev 3767) +++ branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/Log4jLoggingHandler.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -60,11 +60,12 @@ this.log4jLogger = LogUtil.getLog4jRootLogger(); } - // Get the handler's configuration + // Set the handler's configuration this.setLevel(java.util.logging.Level.FINEST); this.setFormatter(new SimpleFormatter()); } + @Override public void publish(LogRecord record) { if(! isLoggable(record)) return; @@ -79,8 +80,10 @@ ( new XLoggingEvent(log4jLogger, record, formattedMessage) ); } + @Override public void close() { /* no-op */ } + @Override public void flush() { /* no-op */ } Modified: branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/LogUtil.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/LogUtil.java 2010-10-11 17:26:00 UTC (rev 3767) +++ branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/LogUtil.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -25,11 +25,6 @@ package com.bigdata.util.config; -import java.io.File; -import java.util.Set; -import java.util.HashSet; - -import com.bigdata.DataFinder; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.apache.log4j.xml.DOMConfigurator; @@ -78,14 +73,31 @@ } } + /** + * Return log4j logger for the provided <code>String<code> argument. + * @param componentName The name of the logger to return + * @return log4j logger associated with the provided name + */ public static Logger getLog4jLogger(String componentName) { return Logger.getLogger(componentName); } + /** + * Return log4j logger for the provided <code>Class<code> argument. + * @param componentClass The class whose name will be used to return the + * associated logger. + * @return log4j logger associated with the provided class' name + */ + public static Logger getLog4jLogger(Class componentClass) { return Logger.getLogger(componentClass); } + /** + * Return the root log4j logger. + * @return the root log4j logger + */ + public static Logger getLog4jRootLogger() { return Logger.getRootLogger(); } Modified: branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/NicUtil.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/NicUtil.java 2010-10-11 17:26:00 UTC (rev 3767) +++ branches/bbb_cleanup/bigdata-core/src/main/java/com/bigdata/util/config/NicUtil.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -78,7 +78,8 @@ * network interface to return (for example, typical * values for this parameter might be, "eth0", "eth1", * "hme01", "lo", etc., depending on how the underlying - * platform is configured). + * platform is configured). The empty string will + * return the loopback interface. * * @return an instance of <code>NetworkInterface</code> that represents * the network interface corresponding to the given @@ -154,13 +155,12 @@ * the desired network interface(s) correspond. * * @return an array whose elements are each instances of - * <code>NetworkInterface[]</code>, in which each such - * instance corresponds to the given <code>name</code>, - * or <code>null</code> if there is no network interface - * corresponding to that name value. + * <code>NetworkInterface</code>, in which each such + * instance corresponds to the given <code>name</code>. * - * Note that if the value given for the <code>name</code> - * parameter is the <code>String</code> "all", then this + * Note that if the special value "all" is given for the + * <code>name</code> + * parameter, then this * method will return an array containing all of the * network interfaces installed on the current node, * regardless of each interface's name. @@ -176,31 +176,24 @@ { NetworkInterface [] nics = null; if (name.equals("all")) { - Enumeration en = NetworkInterface.getNetworkInterfaces(); - List nicList = (en != null) ? - Collections.list(en) : Collections.EMPTY_LIST; - nics = (NetworkInterface[])(nicList.toArray - (new NetworkInterface[nicList.size()]) ); + Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); + List<NetworkInterface> nicList = + ((en != null) + ? Collections.list(en) + : Collections.<NetworkInterface>emptyList()); + nics = (nicList.toArray(new NetworkInterface[nicList.size()]) ); } else { - nics = new NetworkInterface[1]; - nics[0] = NetworkInterface.getByName(name); - if (nics[0] == null) { - // try to lookup by IP address - InetAddress targetIp = null; - try { - targetIp = InetAddress.getByName(name); - nics[0] = NetworkInterface.getByInetAddress(targetIp); - } catch (UnknownHostException uhe) { - // ignore, return null - } - } + NetworkInterface nif = getNetworkInterface(name); + nics = ((nif != null) + ? new NetworkInterface[] { nif } + : new NetworkInterface[0]); } return nics; } /** * Returns the instance of <code>InetAddress</code> that represents - * the i-th IP address assigned to the network interface having the + * the i-th IPv4 address assigned to the network interface having the * given <code>name</code> (where i is specified by the value of the * <code>index</code> parameter). * <p> @@ -268,7 +261,7 @@ * <code>localHost</code> is <code>false</code>. * * @throws IllegalArgumentException if the value input for - * <code>index</code> is negtive. + * <code>index</code> is negative. * * @throws IndexOutOfBoundsException if the value input for * <code>index</code> is out of range; that is if the value @@ -282,10 +275,14 @@ { // Validate input parameters if( (name == null) && (host == null) && (localHost == false) ) { - throw new NullPointerException("name cannot be null"); + throw new NullPointerException( + "Name and host cannot be null with localHost set to false."); } - if(index < 0) throw new IllegalArgumentException - ("index cannot be negative"); + + if(index < 0) { + throw new IllegalArgumentException("index cannot be negative"); + } + // Primary retrieval attempt NetworkInterface nic = null; try { @@ -376,6 +373,7 @@ public static String getMacAddress(String name) throws SocketException { String macAddr = null; NetworkInterface nic = NicUtil.getNetworkInterface(name); + if (nic==null) return null; byte[] hwAddr = nic.getHardwareAddress(); if( (hwAddr != null) && (hwAddr.length > 0) ) { StringBuffer strBuf = new StringBuffer(); @@ -390,6 +388,9 @@ macAddr = strBuf.toString(); } return macAddr; + //TODO - verify MAC address string representation + // (e.g. MSB->LSB ordering) + //TODO - verify that HW address is a MAC address } // /** @@ -530,7 +531,7 @@ * above. * <p> * Note that in all cases, if <code>true</code> is input - * for the <code>loopOk</code> parameter, then upon failing + * for the <code>loopbackOk</code> parameter, then upon failing * to find a valid ip address using the specified search * mechanism, this method will return the <i>loop back</i> * address; otherwise, <code>null</code> is returned. @@ -663,6 +664,8 @@ //get all nics on the current node Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces(); + if (nics == null) return null; + while( nics.hasMoreElements() ) { NetworkInterface curNic = nics.nextElement(); List<InterfaceAddress> interfaceAddrs = Modified: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/ConfigDeployUtilTest.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/ConfigDeployUtilTest.java 2010-10-11 17:26:00 UTC (rev 3767) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/ConfigDeployUtilTest.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -115,6 +115,22 @@ String expected = value; assertEquals(actual, expected); } + + /** + * Test method for {@link com.bigdata.util.config.ConfigDeployUtil#getString(java.lang.String)}. + * @throws ConfigurationException + */ + @Test + public void testGetString_valid_name_default() throws ConfigurationException { + String key = "testGetString_valid_name"; + String value = key + "_value"; + Properties p = ConfigDeployUtil.getDeploymentProperties(); + p.setProperty(key+".default", value); + p.setProperty(key+".stringvals", value + ",other"); + String actual = ConfigDeployUtil.getString(key); + String expected = value; + assertEquals(actual, expected); + } /** * Test method for {@link com.bigdata.util.config.ConfigDeployUtil#getString(java.lang.String)}. @@ -135,6 +151,25 @@ //ignore -- exception } } + /** + * Test method for {@link com.bigdata.util.config.ConfigDeployUtil#getString(java.lang.String)}. + * @throws ConfigurationException + */ + @Test + public void testGetString_invalid_name_default() throws ConfigurationException { + String key = "testGetString_invalid_name"; + String value = key + "_value"; + Properties p = ConfigDeployUtil.getDeploymentProperties(); + p.setProperty(key+".default", value); + p.setProperty(key+".stringvals", "other1,other2"); + try { + ConfigDeployUtil.getString(key); + fail("Successfully called getString with bogus parameter string: [" + + key + ":" + value + "]"); + } catch (ConfigurationException e) { + //ignore -- exception + } + } /** * Test method for {@link com.bigdata.util.config.ConfigDeployUtil#getStringArray(java.lang.String)}. Added: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/ConfigurationUtilTest.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/ConfigurationUtilTest.java (rev 0) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/ConfigurationUtilTest.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -0,0 +1,549 @@ +package com.bigdata.util.config; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Method; +import java.net.InetAddress; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.UUID; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ConfigurationUtilTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testConcat_null() throws SecurityException, NoSuchMethodException { + String[][][] args = { + {null, null}, + {new String[0], null}, + {null, new String[0]} + }; + for (String[][] arg: args) { + try { + ConfigurationUtil.concat(arg[0], arg[1]); + fail("Successfully called concat() with null arg."); + } catch (NullPointerException e) { + // ignore -- expected. + } + } + } + + @Test + public void testConcat_empty() throws SecurityException, NoSuchMethodException { + String [] actual = ConfigurationUtil.concat(new String[0], new String[0]); + assertTrue(actual.length==0); + } + + @Test + public void testConcat_non_empty_singleton() throws SecurityException, NoSuchMethodException { + String[] a = { "abc" }; + String[] b = { "xyz" }; + String [] actual = ConfigurationUtil.concat(a, b); + assertTrue(actual.length==(a.length + b.length)); + assertArrayEquals("Arrays don't match", actual, combine(a, b)); + } + + @Test + public void testConcat_non_empty_multiple() throws SecurityException, NoSuchMethodException { + String[] a = { "abc", "def", "ghi", "jklmnop" }; + String[] b = { "qrs", "tuv", "wxyz" }; + String [] actual = ConfigurationUtil.concat(a, b); + assertTrue(actual.length==(a.length + b.length)); + List<String> l = new ArrayList<String>(); + l.addAll(Arrays.asList(a)); + l.addAll(Arrays.asList(b)); + assertArrayEquals("Arrays don't match", actual, l.toArray(new String[0])); + } + + @Test + public void testConcat_non_empty_multiple_embedded_null() + throws SecurityException, NoSuchMethodException + { + String[] a = { "abc", "def", null, "jklmnop" }; + String[] b = { "qrs", null, "wxyz" }; + String [] actual = ConfigurationUtil.concat(a, b); + assertTrue(actual.length==(a.length + b.length)); + List<String> l = new ArrayList<String>(); + l.addAll(Arrays.asList(a)); + l.addAll(Arrays.asList(b)); + assertArrayEquals("Arrays don't match", actual, l.toArray(new String[0])); + } + + @Test + public void testCreateArgList_null() { + Object[][] args = { + {null, null}, + {new String[0], null}, + }; + for (Object[] arg: args) { + try { + ConfigurationUtil.createArgList((String[])arg[0], (String)arg[1]); + fail("Successfully called createArgList() with null arg: " + + Arrays.asList(arg)); + } catch (NullPointerException e) { + // ignore -- expected. + } + } + } + + @Test + public void testCreateArgList_null2() { + //if second arg is empty or whitespace, return first arg + assertNull(ConfigurationUtil.createArgList(null, "")); + } + + @Test + public void testCreateArgList_null3() { + //if second arg is empty or whitespace, return first arg + assertNull(ConfigurationUtil.createArgList(null, " \t \t\t ")); + } + + @Test + public void testCreateArgList_empty() { + String[] orig = {"abc"}; + String extra = ""; + String[] sum = ConfigurationUtil.createArgList(orig, extra); + assertTrue(orig==sum);//identity obviates equals test + } + + @Test + public void testCreateArgList_single() { + String[] orig = {"abc"}; + String[] extraArray = new String[] {"xyz"}; + List<String> l = Arrays.asList(extraArray); + String extra = join(l); + String[] sum = ConfigurationUtil.createArgList(orig, extra); + assertArrayEquals(sum, combine(orig, extraArray)); + } + + @Test + public void testCreateArgList_multiple() { + String[] orig = {"abc"}; + String[] extraArray = new String[] {"rst", "uvw", "xyz"}; + List<String> l = Arrays.asList(extraArray); + String extra = join(l); + String[] sum = ConfigurationUtil.createArgList(orig, extra); + assertArrayEquals(sum, combine(orig, extraArray)); + } + + @Test + public void testComputeCodebase_5_arg_null() throws IOException { + Integer port = new Integer(5); + String jarf = "bogus_jar_file.jar"; + String name = "bogus_name"; + Object[][] args = { + {null, null, port, null, "sha"}, + {null, jarf, port, null, "sha"}, + {name, null, port, null, "sha"}, + {name, jarf, port, null, "sha"}, + }; + //Test int port method + for (Object[] arg: args) { + try { + ConfigurationUtil.computeCodebase( + (String)arg[0], (String)arg[1], + ((Integer)arg[2]).intValue(), + (String)arg[3], (String)arg[4]); + fail("Successfully called computeCodebase() with null arg: " + + Arrays.asList(arg)); + } catch (NullPointerException e) { + // ignore -- expected. + } + } + //Test String port method + for (Object[] arg: args) { + try { + ConfigurationUtil.computeCodebase( + (String)arg[0], (String)arg[1], + ((Integer)arg[2]).toString(), + (String)arg[3], (String)arg[4]); + fail("Successfully called computeCodebase() with null arg: " + + Arrays.asList(arg)); + } catch (NullPointerException e) { + // ignore -- expected. + } + } + + } + + @Test + public void testComputeCodebase_5_arg_invalid_port() throws IOException { + Integer port = new Integer(-1); + //Try int port + try { + ConfigurationUtil.computeCodebase( + "bogus_name", "bogus_jar_file.jar", + port.intValue(), "bogus_src", "bogus_md"); + fail("Successfully called computeCodebase() with negative port: " + + port); + } catch (IllegalArgumentException e) { + // ignore -- expected. + } + //Try String port + try { + ConfigurationUtil.computeCodebase( + "bogus_name", "bogus_jar_file.jar", + port.toString(), "bogus_src", "bogus_md"); + fail("Successfully called computeCodebase() with negative port: " + + port); + } catch (IllegalArgumentException e) { + // ignore -- expected. + } + } + + @Test + public void testComputeCodebase_5_arg_valid_hostname_http() throws IOException { + Integer port = new Integer(1234); + String hostname = InetAddress.getLocalHost().getHostAddress(); + String jarFile = "file.jar"; + Object[][] args = { + {hostname, jarFile, port, "src", null}, + {hostname, jarFile, port, "src", ""}, + {hostname, jarFile, port, "src", "off"}, + {hostname, jarFile, port, "src", "none"}, + }; + + //Test int port + for (Object[] arg: args) { + String urlString = ConfigurationUtil.computeCodebase( + (String)arg[0], (String)arg[1], + ((Integer)arg[2]).intValue(), + (String)arg[3], (String)arg[4]); //gen http url + testComputeCodebase_5_arg_valid_hostname_http_assert( + urlString, hostname, port, jarFile); + } + + //Test String port + for (Object[] arg: args) { + String urlString = ConfigurationUtil.computeCodebase( + (String)arg[0], (String)arg[1], + (String)arg[2].toString(), + (String)arg[3], (String)arg[4]); //gen http url + testComputeCodebase_5_arg_valid_hostname_http_assert( + urlString, hostname, port, jarFile); + } + } + + private void testComputeCodebase_5_arg_valid_hostname_http_assert( + String urlString, String hostname, Integer port, String jarFile) + throws MalformedURLException + { + URL url = new URL(urlString); + assertEquals(url.getProtocol(), "http"); + assertEquals(url.getHost(), hostname); //use IP to avoid DNS conversion + assertEquals(url.getPort(), port.intValue()); + assertEquals(url.getFile(), "/" + jarFile); + } + + @Test + public void testComputeCodebase_5_arg_invalid_hostname_http() throws IOException { + Integer port = new Integer(1234); + String hostname = UUID.randomUUID().toString(); + String jarFile = "file.jar"; + + //Test int port + try { + ConfigurationUtil.computeCodebase( + hostname, jarFile, port.intValue(), "", ""); + fail("Successfully called computeCodebase() with invalid hostname: " + + hostname); + + } catch (UnknownHostException e) { + //ignore -- expected + } + + //Test String port + try { + ConfigurationUtil.computeCodebase( + hostname, jarFile, port.toString(), "", ""); + fail("Successfully called computeCodebase() with invalid hostname: " + + hostname); + + } catch (UnknownHostException e) { + //ignore -- expected + } + } + + @Test + /** + * Note: this test assumes a well-known jar file under Sun's JDK and as + * such is JDK specific. + */ + public void testComputeCodebase_5_arg_valid_hostname_httpmd_md5() + throws IOException, URISyntaxException + { + Integer port = new Integer(1234); + String hostname = InetAddress.getLocalHost().getHostAddress(); + String jarFile = "rt.jar"; + String jarSrcPath = System.getProperty("java.home") + + File.separator + "lib"; + //Test int port + String urlString = ConfigurationUtil.computeCodebase( + hostname, jarFile, port.intValue(), jarSrcPath, "md5"); //gen httpmd url + testComputeCodebase_5_arg_valid_hostname_httpmd_md5_assert( + urlString, hostname, port, jarFile); + //Test String port + urlString = ConfigurationUtil.computeCodebase( + hostname, jarFile, port.toString(), jarSrcPath, "md5"); //gen httpmd url + testComputeCodebase_5_arg_valid_hostname_httpmd_md5_assert( + urlString, hostname, port, jarFile); + } + + private void testComputeCodebase_5_arg_valid_hostname_httpmd_md5_assert( + String urlString, String hostname, Integer port, String jarFile) + throws MalformedURLException, URISyntaxException + { + URI uri = new URI(urlString); + assertEquals(uri.getScheme(), "httpmd"); + assertEquals(uri.getHost(), hostname); //use IP to avoid DNS conversion + assertEquals(uri.getPort(), port.intValue()); + assertTrue(uri.getPath().startsWith("/" + jarFile + ";md5=")); + } + + @Test + /** + * Note: this test assumes a well-known jar file under Sun's JDK and as + * such is JDK specific. + */ + public void testComputeCodebase_5_arg_valid_hostname_httpmd_sha() + throws IOException, URISyntaxException + { + Integer port = new Integer(1234); + String hostname = InetAddress.getLocalHost().getHostAddress(); + String jarFile = "rt.jar"; + String jarSrcPath = System.getProperty("java.home") + + File.separator + "lib"; + //Test int port + String urlString = ConfigurationUtil.computeCodebase( + hostname, jarFile, port.intValue(), jarSrcPath, "sha"); //gen httpmd url + testComputeCodebase_5_arg_valid_hostname_httpmd_sha_assert( + urlString, hostname, port, jarFile); + //Test String port + urlString = ConfigurationUtil.computeCodebase( + hostname, jarFile, port.toString(), jarSrcPath, "sha"); //gen httpmd url + testComputeCodebase_5_arg_valid_hostname_httpmd_sha_assert( + urlString, hostname, port, jarFile); + } + + private void testComputeCodebase_5_arg_valid_hostname_httpmd_sha_assert( + String urlString, String hostname, Integer port, String jarFile) + throws MalformedURLException, URISyntaxException + { + URI uri = new URI(urlString); + assertEquals(uri.getScheme(), "httpmd"); + assertEquals(uri.getHost(), hostname); //use IP to avoid DNS conversion + assertEquals(uri.getPort(), port.intValue()); + assertTrue(uri.getPath().startsWith("/" + jarFile + ";sha=")); + } + + @Test + public void testComputeCodebase_3_arg_null() throws IOException { + Integer port = new Integer(5); + String jarf = "bogus_jar_file.jar"; + String name = "bogus_name"; + Object[][] args = { + {null, null, port}, + {null, jarf, port}, + {name, null, port}, + }; + //Test int port method + for (Object[] arg: args) { + try { + ConfigurationUtil.computeCodebase( + (String)arg[0], (String)arg[1], + ((Integer)arg[2]).intValue()); + fail("Successfully called computeCodebase() with null arg: " + + Arrays.asList(arg)); + } catch (NullPointerException e) { + // ignore -- expected. + } + } + //Test String port method + for (Object[] arg: args) { + try { + ConfigurationUtil.computeCodebase( + (String)arg[0], (String)arg[1], + ((Integer)arg[2]).toString()); + fail("Successfully called computeCodebase() with null arg: " + + Arrays.asList(arg)); + } catch (NullPointerException e) { + // ignore -- expected. + } + } + } + + @Test + public void testComputeCodebase_3_arg_invalid_port() throws IOException { + Integer port = new Integer(-1); + //Try int port + try { + ConfigurationUtil.computeCodebase( + "bogus_name", "bogus_jar_file.jar", port.intValue()); + fail("Successfully called computeCodebase() with negative port: " + + port); + } catch (IllegalArgumentException e) { + // ignore -- expected. + } + //Try String port + try { + ConfigurationUtil.computeCodebase( + "bogus_name", "bogus_jar_file.jar", port.toString()); + fail("Successfully called computeCodebase() with negative port: " + + port); + } catch (IllegalArgumentException e) { + // ignore -- expected. + } + } + + @Test + public void testComputeCodebase_3_arg_valid_hostname_http() throws IOException { + Integer port = new Integer(1234); + String hostname = InetAddress.getLocalHost().getHostAddress(); + String jarFile = "file.jar"; + + String urlString = ConfigurationUtil.computeCodebase( + hostname, jarFile, port.intValue()); //gen http url + testComputeCodebase_3_arg_valid_hostname_http_assert( + urlString, hostname, port, jarFile); + + + urlString = ConfigurationUtil.computeCodebase( + hostname, jarFile, port.toString()); //gen http url + testComputeCodebase_3_arg_valid_hostname_http_assert( + urlString, hostname, port, jarFile); + } + + private void testComputeCodebase_3_arg_valid_hostname_http_assert( + String urlString, String hostname, Integer port, String jarFile) + throws MalformedURLException + { + URL url = new URL(urlString); + assertEquals(url.getProtocol(), "http"); + assertEquals(url.getHost(), hostname); //use IP to avoid DNS conversion + assertEquals(url.getPort(), port.intValue()); + assertEquals(url.getFile(), "/" + jarFile); + } + + @Test + public void testComputeCodebase_3_arg_invalid_hostname_http() throws IOException { + Integer port = new Integer(1234); + String hostname = UUID.randomUUID().toString(); + String jarFile = "file.jar"; + + //Test int port + try { + ConfigurationUtil.computeCodebase( + hostname, jarFile, port.intValue()); + fail("Successfully called computeCodebase() with invalid hostname: " + + hostname); + } catch (UnknownHostException e) { + //ignore -- expected + } + + //Test String port + try { + ConfigurationUtil.computeCodebase( + hostname, jarFile, port.toString()); + fail("Successfully called computeCodebase() with invalid hostname: " + + hostname); + } catch (UnknownHostException e) { + //ignore -- expected + } + } + + @Test + /** + * Note: this test assumes a well-known jar file under Sun's JDK and as + * such is JDK specific. + */ + public void testComputeCodebase_port_not_number() + throws IOException, URISyntaxException + { + String port = "cafebabe"; + String hostname = InetAddress.getLocalHost().getHostAddress(); + String jarFile = "rt.jar"; + String jarSrcPath = System.getProperty("java.home") + + File.separator + "lib"; + // 5 arg method + try { + ConfigurationUtil.computeCodebase( + hostname, jarFile, port, jarSrcPath, "md5"); //gen httpmd url + fail("Successfully called computeCodebase() with invalid port: " + + port); + } catch (NumberFormatException e) { + //ignore -- expected + } + // 3 arg method + try { + ConfigurationUtil.computeCodebase( + hostname, jarFile, port); //gen httpmd url + fail("Successfully called computeCodebase() with invalid port: " + + port); + } catch (NumberFormatException e) { + //ignore -- expected + } + } + + /** + * Helper method that joins the given collection of <code>String</code> using the + * ASCII record separator (RS) character(\036). + * @param s the collection of strings to join + * @return String containing the collection's elements separated by + * the RS delimiter. + */ + private static String join(Collection<String> s) { + if (s == null || s.isEmpty()) return ""; + Iterator<String> iter = s.iterator(); + StringBuilder builder = new StringBuilder(iter.next()); + while( iter.hasNext() ) + { + builder.append('\036').append(iter.next()); + } + return builder.toString(); + } + + /** + * Helper method that combines two <code>String</code> arrays in order in which + * they are provided. + * @param s the collection of strings to join + * @return String containing the collection's elements separated by + * the RS delimiter. + */ + private static String[] combine(String[]a, String[]b) { + List<String> l = new ArrayList<String>(); + l.addAll(Arrays.asList(a)); + l.addAll(Arrays.asList(b)); + return l.toArray(new String[0]); + } +} Added: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/Log4jLoggingHandlerTest.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/Log4jLoggingHandlerTest.java (rev 0) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/Log4jLoggingHandlerTest.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -0,0 +1,52 @@ +package com.bigdata.util.config; + +import static org.junit.Assert.*; + +import java.util.logging.Level; +import java.util.logging.LogRecord; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class Log4jLoggingHandlerTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testFlush() { + Log4jLoggingHandler l = new Log4jLoggingHandler(); + l.flush(); + } + + @Test + public void testClose() { + Log4jLoggingHandler l = new Log4jLoggingHandler(); + l.close(); + } + + @Test + public void testPublishLogRecord() { + Log4jLoggingHandler l = new Log4jLoggingHandler(); + LogRecord rec = new LogRecord(Level.SEVERE, "Log message"); + l.publish(rec); + } + + //TODO - test static logic +} Added: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/LogUtilTest.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/LogUtilTest.java (rev 0) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/LogUtilTest.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -0,0 +1,52 @@ +package com.bigdata.util.config; + +import static org.junit.Assert.*; + +import org.apache.log4j.Logger; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class LogUtilTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetLog4jLoggerString() { + Logger l = LogUtil.getLog4jLogger(LogUtilTest.class); + assertNotNull(l); + assertEquals(LogUtilTest.class.getName(), l.getName()); + } + + @Test + public void testGetLog4jLoggerClass() { + Logger l = LogUtil.getLog4jLogger(LogUtilTest.class.getName()); + assertNotNull(l); + assertEquals(LogUtilTest.class.getName(), l.getName()); + } + + @Test + public void testGetLog4jRootLogger() { + Logger l = LogUtil.getLog4jRootLogger(); + assertNotNull(l); + assertEquals("root", l.getName()); + } + + //TODO - Still need to test static property logic +} Added: branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/NicUtilTest.java =================================================================== --- branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/NicUtilTest.java (rev 0) +++ branches/bbb_cleanup/bigdata-core/src/test/java/com/bigdata/util/config/NicUtilTest.java 2010-10-11 18:33:47 UTC (rev 3768) @@ -0,0 +1,1293 @@ +package com.bigdata.util.config; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.InterfaceAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.net.UnknownHostException; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Random; +import java.util.UUID; + +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class NicUtilTest { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + } + + @Before + public void setUp() throws Exception { + } + + @After + public void tearDown() throws Exception { + } + + @Test + public void testGetNetworkInterface_null() throws SocketException { + try { + NicUtil.getNetworkInterface(null); + fail("Successfully called getNetworkInterface() with null."); + } catch (NullPointerException e) { + //ignore -- expected + } + } + + @Test + public void testGetNetworkInterface_by_if_name() throws SocketException { + Enumeration<NetworkInterface> ifs = + NetworkInterface.getNetworkInterfaces(); + while(ifs.hasMoreElements()) { + NetworkInterface i = ifs.nextElement(); + assertEquals(NicUtil.getNetworkInterface(i.getName()), i); + } + } + + @Test + public void testGetNetworkInterface_by_host_name() throws SocketException { + boolean found = false; + for (Enumeration<NetworkInterface> ifs = + NetworkInterface.getNetworkInterfaces(); + ifs.hasMoreElements();) + { + NetworkInterface i = ifs.nextElement(); + for (Enumeration<InetAddress> ips = i.getInetAddresses(); + ips.hasMoreElements();) + { + InetAddress ip = ips.nextElement(); + NetworkInterface x = NicUtil.getNetworkInterface(ip.getHostAddress()); + assertNotNull(x); + assertEquals(i, x); + found = true; + } + } + + if (!found) fail("Could not find any IP address for testing."); + } + + @Test + public void testGetNetworkInterface_by_unknown_host_name() throws SocketException { + String unknownHost = getBogusHostname(); + NetworkInterface x = NicUtil.getNetworkInterface(unknownHost); + assertNull(x); + } + + @Test + public void testGetNetworkInterface_by_empty_host_name() throws SocketException { + NetworkInterface x = NicUtil.getNetworkInterface(""); + assertNotNull(x); + assertTrue(x.isLoopback()); + } + + @Test + public void testGetInetAddressMap() throws SocketException { + Map<InetAddress, String> expected = new HashMap<InetAddress, String>(); + for (Enumeration<NetworkInterface> ifs = + NetworkInterface.getNetworkInterfaces(); + ifs.hasMoreElements();) + { + NetworkInterface i = ifs.nextElement(); + for (Enumeration<InetAddress> ips = i.getInetAddresses(); + ips.hasMoreElements();) + { + InetAddress ip = ips.nextElement(); + expected.put(ip, i.getName()); + } + } + Map<InetAddress, String> actual = NicUtil.getInetAddressMap(); + assertEquals(actual, expected); + } + + @Test + public void testGetNetworkInterfaceArray_null() throws SocketException { + try { + NicUtil.getNetworkInterfaceArray(null); + fail("Successfully called getNetworkInterfaceArray with null."); + } catch (NullPointerException e) { + // ignore -- expected + } + } + + @Test + public void testGetNetworkInterfaceArray_unknown_host_name() throws SocketException { + String unknownHost = getBogusHostname(); + NetworkInterface[] x = NicUtil.getNetworkInterfaceArray(unknownHost); + assertEquals(x.length, 0); + } + + @Test + public void testGetNetworkInterfaceArray_known_host_name() throws SocketException { + Enumeration<NetworkInterface> ns = NetworkInterface.getNetworkInterfaces(); + List<NetworkInterface> ls = Collections.list(ns); + assert(ls.size()>0); + //Pick random entry from list. + Random r = new Random(); + //Note: size() must > 0, here. + String ifName = ls.get(r.nextInt(ls.size())).getName(); + NetworkInterface[] nifs = NicUtil.getNetworkInterfaceArray(ifName); + assertTrue(nifs.length > 0); + assertEquals(nifs[0].getName(), ifName); + } + + @Test + public void testGetNetworkInterfaceArray_known_ip_address() throws SocketException { + Result<String, NetworkInterface> res = + getNonLoopbackNetworkInterfaceWithInet4Address(); + NetworkInterface[] nifs = NicUtil.getNetworkInterfaceArray(res.getValue1()); + assertEquals(nifs.length, 1); + assertEquals(nifs[0], res.getValue2()); + } + + @Test + public void testGetNetworkInterfaceArray_all() throws SocketException { + List<NetworkInterface> expected = + Collections.list(NetworkInterface.getNetworkInterfaces()); + assertTrue(expected.size() > 0); + List<NetworkInterface> actual = + Arrays.asList(NicUtil.getNetworkInterfaceArray("all")); + assertEquals(actual.size(), expected.size()); + //Remove actual from expected -- should leave an empty list + expected.removeAll(actual); + assertTrue(expected.isEmpty()); + } + + @Test + public void testGetInetAddress_npe_args() { + String name = null; + int index = 0; + String host = null; + boolean localHost = false; + try{ + NicUtil.getInetAddress(name, index, host, localHost); + fail("Successfully called getInetAddress with invalid arguments."); + } catch (NullPointerException e) { + //ignore -- expected + } + } + + @Test + public void testGetInetAddress_negative_index() { + String name = null; + int index = -1; + String host = null; + boolean localHost = true; + try{ + NicUtil.getInetAddress(name, index, host, localHost); + fail("Successfully called getInetAddress with negative index."); + } catch (IllegalArgumentException e) { + //ignore -- expected + } + } + + @Test + public void testGetInetAddress_get_localhost_only() throws UnknownHostException { + String name = null; + int index = 0; + String host = null; + boolean localHost = true; + InetAddress expected = InetAddress.getLocalHost(); + InetAddress actual = NicUtil.getInetAddress(name, index, host, localHost); + assertEquals(expected, actual); + } + + @Test + public void testGetInetAddress_get_unknown_host_no_localhost() + throws UnknownHostException + { + String name = null; + int index = 0; + String host = getBogusHostname(); + boolean localHost = false; + InetAddress expected = null; + InetAddress actual = NicUtil.getInetAddress(name, index, host, localHost); + assertEquals(expected, actual); + } + + @Test + public void testGetInetAddress_get_unknown_host_with_localhost() + throws UnknownHostException + { + String name = null; + int index = 0; + String host = getBogusHostname(); + boolean localHost = true; + InetAddress expected = InetAddress.getLocalHost(); + InetAddress actual = NicUtil.getInetAddress(name, index, host, localHost); + assertEquals(expected, actual); + } + + @Test + public void testGetInetAddress_get_known_host_no_localhost() + throws UnknownHostException + { + String name = null; + int index = 0; + String host = InetAddress.getLocalHost().getHostName(); + boolean localHost = false; + InetAddress expected = InetAddress.getLocalHost(); + InetAddress actual = NicUtil.getInetAddress(name, index, host, localHost); + assertEquals(expected, actual); + } + + @Test + public void testGetInetAddress_get_known_host_with_localhost() + throws UnknownHostException + { + String name = null; + int index = 0; + String host = InetAddress.getLocalHost().getHostName(); + boolean localHost = true; + InetAddress expected = InetAddress.getLocalHost(); + InetAddress actual = NicUtil.getInetAddress(name, index, host, localHost); + assertEquals(expected, actual); + } + + @Test + public void testGetInetAddress_get_known_name_no_host_or_localhost() + throws UnknownHostException, SocketException + { + Result<String, NetworkInterface> res = + getNonLoopbackNetworkInterfaceWithInet4Address(); + String name = res.getValue2().getName(); + int index = 0; + String host = null; + boolean localHost = false; + List<InetAddress> expectedAddresses = + Collections.list(res.getValue2().getInetAddresses()); + assertFalse(expectedAddresses.isEmpty()); + InetAddress actual = NicUtil.getInetAddress(name, index, host, localHost); + assertNotNull(actual); + assertTrue(expectedAddresses.contains(actual)); + } + + @Test + public void testGetInetAddress_get_known_name_with_host_no_localhost() + throws UnknownHostException, SocketException + { + Result<String, NetworkInterface> res = + getNonLoopbackNetworkInterfaceWithInet4Address(); + String name = res.getValue2().getName(); + int index = 0; + String host = InetAddress.getLocalHost().getHostName(); + boolean localHost = false; + List<InetAddress> expectedAddresses = + Collections.list(res.getValue2().getInetAddresses()); + assertFalse(expectedAddresses.isEmpty()); + InetAddress actual = NicUtil.getInetAddress(name, index, host, localHost); + assertNotNull(actual); + assertTrue(expectedAddresses.contains(actual)); + } + + //TODO - Another test which ensures host != result.NetworkInterface + + @Test + public void testGetInetAddress_get_known_name_with_host_with_localhost() + throws UnknownHostException, SocketException + { + Result<String, NetworkInterface> res = + getNonLoopbackNetworkInterfaceWithInet4Address(); + String name = res.getValue1(); + int index = 0; + String host = InetAddress.getLocalHost().getHostName(); + boolean localHost = true; + List<InetAddress> expectedAddresses = + Collections.list(res.getValue2().getInetAddresses()); + assertFalse(expectedAddresses.isEmpty()); + InetAddress actual = NicUtil.getInetAddress(name, index, host, localHost); + assertNotNull(actual); + assertTrue(expectedAddresses.contains(actual)); + } + + //TODO - Another test which ensures host != result.NetworkInterface + + //TODO - test for multi-IP interface (e.g. index arg > 0) + + @Test + public void testGetMacAddress_null() throws SocketException { + try { + NicUtil.getMacAddress(null); + fail("Successfully called getMacAddress with null."); + } catch (NullPointerException e) { + //ignore -- exception + } + } + + @Test + public void testGetMacAddress_unknown() throws SocketException { + String mac = NicUtil.getMacAddress(UUID.randomUUID().toString()); + assertNull(mac); + } + + @Test + public void testGetMacAddress_known() throws SocketException, ParseException { + Result<byte[], NetworkInterface> res = + getFirstNetworkInterfaceWithMacAddress(); + String mac = NicUtil.getMacAddress(res.getValue2().getName()); + assertNotNull(mac); + //Note: not verifying MAC address via string representation + } + + + @Test + public void testGetIpAddressString_npe() { + try { + NicUtil.getIpAddress(null); + fail("Successfully called getIpAddress with null."); + } catch (NullPointerException e) { + //ignore -- expected + } + } + + @Test + public void testGetIpAddressString_unknown_host() { + assertNull(NicUtil.getIpAddress(getBogusHostname())); + } + + @Test + public void testGetIpAddressString_known_host() throws SocketException { + Result<String, NetworkInterface> res = + getNonLoopbackNetworkInterfaceWithInet4Address(); + String actual = + NicUtil.getIpAddress(res.getValue2().getName()); + assertEquals(res.getValue1(), actual); + } + + @Test + public void testGetIpAddressStringInt_npe() { + try { + NicUtil.getIpAddress(null, 0); + fail("Successfully called getIpAddress with null."); + } catch (NullPointerException e) { + //ignore -- expected + } + } + + @Test + public void testGetIpAddressStringInt_negative_index() { + try { + NicUtil.getIpAddress("", -1); + fail("Successfully called getIpAddress with" + + "a negative index."); + } catch (IllegalArgumentException e) { + //ignore -- expected + } + } + + @Test + public void testGetIpAddressStringInt_unknown_host() { + assertNull(NicUtil.getIpAddress(getBogusHostname(), 0)); + } + + @Test + public void testGetIpAddressStringInt_known_host() throws SocketException { + Result<String, NetworkInterface> res = + getNonLoopbackNetworkInterfaceWithInet4Address(); + String actual = + NicUtil.getIpAddress(res.getValue2().getName(), 0); + assertEquals(res.getValue1(), actual); + } + + @Test + public void testGetIpAddressStringString_npe() { + try { + NicUtil.getIpAddress((String)null, (String)null); + fail("Succcessfully called getIpAddress with null."); + }catch (NullPointerException e... [truncated message content] |