Author: vishwanathk
Date: Sun Aug 26 19:19:23 2012
New Revision: 1377486

URL: http://svn.apache.org/viewvc?rev=1377486&view=rev
Log:
Avoid NPE during emptyList forEach

Added:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/CollectionsUtil.java
    
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/util/CollectionsUtilTest.java
Modified:
    
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java

Modified: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java?rev=1377486&r1=1377485&r2=1377486&view=diff
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
 (original)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/config/rules/CheckDescriptorLocation.java
 Sun Aug 26 19:19:23 2012
@@ -29,6 +29,8 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.*;
 
+import static org.apache.openejb.util.CollectionsUtil.safe;
+
 
 public class CheckDescriptorLocation extends ValidationBase {
 
@@ -40,13 +42,13 @@ public class CheckDescriptorLocation ext
 
         List<String> validated = new ArrayList<String>();
 
-        for(WebModule webModule:appModule.getWebModules())
+        for(WebModule webModule: safe(appModule.getWebModules()))
         {
             validated.add(webModule.getModuleId());
             validateWebModule(webModule);
         }
 
-        for(EjbModule ejbModule:appModule.getEjbModules())
+        for(EjbModule ejbModule: safe(appModule.getEjbModules()))
         {
             //without this check, 
CheckDescriptorLocationTest#testWarWithDescriptorInRoot() would fail
             if(!validated.contains(ejbModule.getModuleId()))
@@ -57,6 +59,9 @@ public class CheckDescriptorLocation ext
 
     }
 
+
+
+
     private void validateWebModule(DeploymentModule webModule) {
         URL baseUrl = null;
         this.module= webModule;

Added: 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/CollectionsUtil.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/CollectionsUtil.java?rev=1377486&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/CollectionsUtil.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/CollectionsUtil.java
 Sun Aug 26 19:19:23 2012
@@ -0,0 +1,32 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openejb.util;
+
+import java.util.Collections;
+import java.util.List;
+
+public class CollectionsUtil
+{
+    public static <T> List<T> safe(List<T> list)
+    {
+        if(list==null)
+        {   return Collections.emptyList();
+        }
+        return list;
+    }
+}

Added: 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/util/CollectionsUtilTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/util/CollectionsUtilTest.java?rev=1377486&view=auto
==============================================================================
--- 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/util/CollectionsUtilTest.java
 (added)
+++ 
openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/util/CollectionsUtilTest.java
 Sun Aug 26 19:19:23 2012
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.openejb.util;
+
+import org.junit.Test;
+
+import java.util.List;
+
+public class CollectionsUtilTest {
+
+    @Test
+    public void safeIterationForNullList()
+    {
+        List<String> stringList =null;
+        for(String string: CollectionsUtil.safe(stringList))
+        {
+        }
+
+        //PASS: No NPE thrown
+    }
+}


Reply via email to