commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Gary Gregory (JIRA)" <j...@apache.org>
Subject [jira] [Resolved] (EXEC-80) NPE in EnvironmentUtils.toString(map)
Date Tue, 04 Feb 2014 05:22:09 GMT

     [ https://issues.apache.org/jira/browse/EXEC-80?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Gary Gregory resolved EXEC-80.
------------------------------

       Resolution: Fixed
    Fix Version/s: 1.3
         Assignee: Gary Gregory

{noformat}
commit -m "[EXEC-80] NPE in EnvironmentUtils.toString(map)" C:/vcs/svn/apache/commons/trunks-proper/exec/src/main/java/org/apache/commons/exec/environment/EnvironmentUtils.java
C:/vcs/svn/apache/commons/trunks-proper/exec/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilsTest.java
C:/vcs/svn/apache/commons/trunks-proper/exec/src/changes/changes.xml
    Sending        C:/vcs/svn/apache/commons/trunks-proper/exec/src/changes/changes.xml
    Sending        C:/vcs/svn/apache/commons/trunks-proper/exec/src/main/java/org/apache/commons/exec/environment/EnvironmentUtils.java
    Sending        C:/vcs/svn/apache/commons/trunks-proper/exec/src/test/java/org/apache/commons/exec/environment/EnvironmentUtilsTest.java
    Transmitting file data ...
    Committed revision 1564205.
{noformat}

> 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
>            Assignee: Gary Gregory
>             Fix For: 1.3
>
>         Attachments: EXEC-80.patch
>
>
> 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)

Mime
View raw message