Skip to content

Apache Lucene open-source search software

shallowMap() should behave like a map. testcase added

Noble Paul84e01eb84bec
diff --git a/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java b/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
index dd5afe72087..858fec9390f 100644
--- a/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
+++ b/solr/solrj/src/java/org/apache/solr/common/util/NamedList.java
@@ -425,7 +425,12 @@ public class NamedList<T> implements Cloneable, Serializable, Iterable<Map.Entry
@Override
public T put(String key, T value) {
- NamedList.this.add(key, value);
+ int idx = NamedList.this.indexOf(key, 0);
+ if (idx == -1) {
+ NamedList.this.add(key, value);
+ } else {
+ NamedList.this.setVal(idx, value);
+ }
return null;
}
@@ -436,8 +441,10 @@ public class NamedList<T> implements Cloneable, Serializable, Iterable<Map.Entry
@Override
public void putAll(Map m) {
- NamedList.this.addAll(m);
-
+ for (Object o : m.entrySet()) {
+ Map.Entry e = (Entry) o;
+ put(e.getKey() == null ? null : e.getKey().toString(), (T) e.getValue());
+ }
}
@Override
diff --git a/solr/solrj/src/test/org/apache/solr/common/util/NamedListTest.java b/solr/solrj/src/test/org/apache/solr/common/util/NamedListTest.java
index 2625e7d7631..8605cd960dd 100644
--- a/solr/solrj/src/test/org/apache/solr/common/util/NamedListTest.java
+++ b/solr/solrj/src/test/org/apache/solr/common/util/NamedListTest.java
@@ -18,6 +18,7 @@ package org.apache.solr.common.util;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.solr.common.SolrException;
@@ -185,4 +186,21 @@ public class NamedListTest extends LuceneTestCase {
Object enltest4 = enl.findRecursive("key2");
assertNull(enltest4);
}
+
+ public void testShallowMap() {
+ NamedList nl = new NamedList();
+ nl.add("key1", "Val1");
+ Map m = nl.asShallowMap();
+ m.put("key1", "Val1_");
+ assertEquals("Val1_", nl.get("key1"));
+ assertEquals("Val1_", m.get("key1"));
+ assertEquals(0, nl.indexOf("key1", 0));
+ m.putAll(Utils.makeMap("key1", "Val1__", "key2", "Val2"));
+ assertEquals("Val1__", nl.get("key1"));
+ assertEquals("Val1__", m.get("key1"));
+ assertEquals(0, nl.indexOf("key1", 0));
+ assertEquals("Val2", nl.get("key2"));
+ assertEquals("Val2", m.get("key2"));
+
+ }
}