You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@uima.apache.org by re...@apache.org on 2021/10/22 09:12:06 UTC

[uima-uimaj] 01/01: [UIMA-6390] NPE when trying to access config names of fresh context

This is an automated email from the ASF dual-hosted git repository.

rec pushed a commit to branch bugfix/UIMA-6390-NPE-when-trying-to-access-config-names-of-fresh-context
in repository https://gitbox.apache.org/repos/asf/uima-uimaj.git

commit d1ff8b3fa9a0671c50c19a0f77068dd412579487
Author: Richard Eckart de Castilho <re...@apache.org>
AuthorDate: Fri Oct 22 11:07:52 2021 +0200

    [UIMA-6390] NPE when trying to access config names of fresh context
    
    - Add test
    - Fix NPE when no parameter declarations have been made for a context
---
 .../org/apache/uima/impl/UimaContext_ImplBase.java | 36 +++++++++++++---------
 .../apache/uima/resource/ConfigurationManager.java |  2 +-
 .../org/apache/uima/impl/UimaContext_implTest.java |  9 ++++++
 3 files changed, 32 insertions(+), 15 deletions(-)

diff --git a/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java b/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java
index 8c9d74c..af2fdfc 100644
--- a/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java
+++ b/uimaj-core/src/main/java/org/apache/uima/impl/UimaContext_ImplBase.java
@@ -56,6 +56,7 @@ import org.apache.uima.resource.ResourceConfigurationException;
 import org.apache.uima.resource.ResourceInitializationException;
 import org.apache.uima.resource.metadata.ConfigurationGroup;
 import org.apache.uima.resource.metadata.ConfigurationParameter;
+import org.apache.uima.resource.metadata.ConfigurationParameterDeclarations;
 import org.apache.uima.util.Level;
 import org.apache.uima.util.Logger;
 import org.apache.uima.util.Settings;
@@ -579,17 +580,19 @@ public abstract class UimaContext_ImplBase implements UimaContextAdmin {
    */
   @Override
   public String[] getConfigParameterNames() {
-    ConfigurationParameter[] params = getConfigurationManager()
-            .getConfigParameterDeclarations(getQualifiedContextName()).getConfigurationParameters();
-    if (params == null) {
+    ConfigurationParameterDeclarations paramDecls = getConfigurationManager()
+            .getConfigParameterDeclarations(getQualifiedContextName());
+
+    if (paramDecls == null) {
       return Constants.EMPTY_STRING_ARRAY;
-    } else {
-      String[] names = new String[params.length];
-      for (int i = 0; i < params.length; i++) {
-        names[i] = params[i].getName();
-      }
-      return names;
     }
+
+    ConfigurationParameter[] params = paramDecls.getConfigurationParameters();
+    if (params == null || params.length == 0) {
+      return Constants.EMPTY_STRING_ARRAY;
+    }
+
+    return Arrays.stream(params).map(ConfigurationParameter::getName).toArray(String[]::new);
   }
 
   /*
@@ -658,15 +661,17 @@ public abstract class UimaContext_ImplBase implements UimaContextAdmin {
     String absoluteSofaName = null;
     if (index < 0) {
       absoluteSofaName = mSofaMappings.get(nameToMap);
-      if (absoluteSofaName == null)
+      if (absoluteSofaName == null) {
         absoluteSofaName = nameToMap;
+      }
 
     } else {
       nameToMap = aSofaName.substring(0, index);
       String rest = aSofaName.substring(index);
       String absoluteRoot = mSofaMappings.get(nameToMap);
-      if (absoluteRoot == null)
+      if (absoluteRoot == null) {
         absoluteRoot = nameToMap;
+      }
       absoluteSofaName = absoluteRoot + rest;
     }
     SofaID sofaid = new SofaID_impl();
@@ -684,8 +689,9 @@ public abstract class UimaContext_ImplBase implements UimaContextAdmin {
     String componentSofaName = aSofaID;
     SofaID[] sofaArr = getSofaMappings();
     for (int i = 0; i < sofaArr.length; i++) {
-      if (aSofaID.equals(sofaArr[i].getSofaID()))
+      if (aSofaID.equals(sofaArr[i].getSofaID())) {
         return sofaArr[i].getComponentSofaName();
+      }
     }
     return componentSofaName;
   }
@@ -852,14 +858,16 @@ public abstract class UimaContext_ImplBase implements UimaContextAdmin {
       String absoluteSofaName = null;
       if (index < 0) {
         absoluteSofaName = mSofaMappings.get(nameToMap);
-        if (absoluteSofaName == null)
+        if (absoluteSofaName == null) {
           absoluteSofaName = nameToMap;
+        }
       } else {
         nameToMap = aSofaName.substring(0, index);
         String rest = aSofaName.substring(index);
         String absoluteRoot = mSofaMappings.get(nameToMap);
-        if (absoluteRoot == null)
+        if (absoluteRoot == null) {
           absoluteRoot = nameToMap;
+        }
         absoluteSofaName = absoluteRoot + rest;
       }
       return absoluteSofaName;
diff --git a/uimaj-core/src/main/java/org/apache/uima/resource/ConfigurationManager.java b/uimaj-core/src/main/java/org/apache/uima/resource/ConfigurationManager.java
index 6e242e2..ad61d8a 100644
--- a/uimaj-core/src/main/java/org/apache/uima/resource/ConfigurationManager.java
+++ b/uimaj-core/src/main/java/org/apache/uima/resource/ConfigurationManager.java
@@ -146,7 +146,7 @@ public interface ConfigurationManager {
    * @param aContextName
    *          the name for which to get the parameter declarations
    * 
-   * @return parameter declarations for the context
+   * @return parameter declarations for the context or {@code null} if there are no declarations.
    */
   ConfigurationParameterDeclarations getConfigParameterDeclarations(String aContextName);
 
diff --git a/uimaj-core/src/test/java/org/apache/uima/impl/UimaContext_implTest.java b/uimaj-core/src/test/java/org/apache/uima/impl/UimaContext_implTest.java
index bd77169..b567cbd 100644
--- a/uimaj-core/src/test/java/org/apache/uima/impl/UimaContext_implTest.java
+++ b/uimaj-core/src/test/java/org/apache/uima/impl/UimaContext_implTest.java
@@ -19,6 +19,7 @@
 
 package org.apache.uima.impl;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -295,6 +296,14 @@ public class UimaContext_implTest {
   }
 
   @Test
+  public void thatGetConfigParameterNamesWorksWhenNoParametersHaveBeenDeclared() {
+    UimaContext emptyContext = UIMAFramework.newUimaContext(UIMAFramework.getLogger(),
+            UIMAFramework.newDefaultResourceManager(), UIMAFramework.newConfigurationManager());
+
+    assertThat(emptyContext.getConfigParameterNames()).isEmpty();
+  }
+
+  @Test
   public void testGetConfigParameterNamesString() {
     String[] names = mContext2.getConfigParameterNames("en");
     Assert.assertEquals(4, names.length);