Karl Heinz Marbaise created EXEC-80:
---------------------------------------

             Summary: NPE in EnvironmentUtils.toString(map)
                 Key: EXEC-80
                 URL: https://issues.apache.org/jira/browse/EXEC-80
             Project: Commons Exec
          Issue Type: Bug
    Affects Versions: 1.2
         Environment: all
            Reporter: Karl Heinz Marbaise
            Priority: Critical


I have drilled down a problem in the above class method which can be checked by 
using the following unit test:
{code}
        @Test
        public void toStringTest() {
                Map<String,String> map = new HashMap<String, String>();
                map.put("key", null);
                String[] result = EnvironmentUtils.toStrings(map);
                assertThat(result.length).isEqualTo(1);
        }
{code}
The above test will fail with an NullPointerException..as expected, cause the 
method accesses the key as well as the value by using the following code line:
{code}
result[i] = entry.getKey().toString() + "=" + entry.getValue().toString();
{code}
It should be checked if key and/or value are null. If you would use the 
following code:
{code}
    public static String[] toStrings(final Map environment) {
        if (environment == null) {
            return null;
        }
        final String[] result = new String[environment.size()];
        int i = 0;
        for (final Iterator iter = environment.entrySet().iterator(); 
iter.hasNext();) {
            final Map.Entry entry = (Map.Entry) iter.next();

            String key  = entry.getKey() == null ? "" : 
entry.getKey().toString();
            String value = entry.getValue() == null ? "" : 
entry.getValue().toString();
            result[i] = key + "=" + value;
            i++;
        }
        return result;
    }
{code}
it will solve the issue.



--
This message was sent by Atlassian JIRA
(v6.1.5#6160)

Reply via email to