shallowMap() should behave like a map. testcase added

This commit is contained in:
Noble Paul 2016-09-16 17:08:55 +05:30
parent f65c114e1a
commit 84e01eb84b
2 changed files with 28 additions and 3 deletions

View File

@ -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

View File

@ -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"));
}
}