use of com.linkedin.data.DataComplex in project rest.li by linkedin.
the class TestArrayTemplate method testArray.
public static <ArrayTemplate extends AbstractArrayTemplate<E>, E> void testArray(Class<ArrayTemplate> templateClass, ArrayDataSchema schema, List<E> input, List<E> adds) {
try {
// constructors and addAll
ArrayTemplate array1 = templateClass.newInstance();
array1.addAll(input);
assertEquals(input, array1);
/*
Constructor[] constructors = templateClass.getConstructors();
for (Constructor c : constructors)
{
out.println(c);
}
*/
try {
int size = input.size();
// constructor(int capacity)
Constructor<ArrayTemplate> capacityConstructor = templateClass.getConstructor(int.class);
ArrayTemplate array = capacityConstructor.newInstance(input.size());
assertEquals(array, Collections.emptyList());
array.addAll(input);
assertEquals(input, array);
array.clear();
assertEquals(size, input.size());
// constructor(Collection<E>)
Constructor<ArrayTemplate> collectionConstructor = templateClass.getConstructor(Collection.class);
array = collectionConstructor.newInstance(input);
assertEquals(input, array);
array.clear();
assertEquals(size, input.size());
// constructor(DataList)
Constructor<ArrayTemplate> dataListConstructor = templateClass.getConstructor(DataList.class);
array = dataListConstructor.newInstance(array1.data());
assertEquals(array1, array);
assertEquals(input, array);
array.clear();
assertEquals(array1, array);
} catch (Exception e) {
assertSame(e, null);
}
// test wrapping
array1.clear();
array1.addAll(input);
DataList dataList2 = new DataList();
// with schema arg
ArrayTemplate array2 = DataTemplateUtil.wrap(dataList2, schema, templateClass);
for (E e : input) {
if (e instanceof DataTemplate) {
dataList2.add(((DataTemplate<?>) e).data());
} else if (e instanceof Enum) {
dataList2.add(e.toString());
} else {
dataList2.add(e);
}
}
assertEquals(array1, array2);
// without schema arg
ArrayTemplate array2a = DataTemplateUtil.wrap(dataList2, templateClass);
assertEquals(array1, array2a);
assertSame(array2.data(), array2a.data());
// schema()
ArrayDataSchema schema1 = array1.schema();
assertTrue(schema1 != null);
assertEquals(schema1.getType(), DataSchema.Type.ARRAY);
assertEquals(schema1, schema);
// add(E element), get(int index)
ArrayTemplate array3 = templateClass.newInstance();
for (int i = 0; i < adds.size(); ++i) {
E value = adds.get(i);
assertTrue(array3.add(value));
assertEquals(array3.get(i), value);
assertSame(array3.get(i), value);
assertTrue(array3.toString().contains(value.toString()));
}
assertEquals(array3, adds);
// add(int index, E element), get(int index)
ArrayTemplate array4 = templateClass.newInstance();
for (int i = 0; i < adds.size(); ++i) {
E value = adds.get(adds.size() - i - 1);
array4.add(0, value);
assertEquals(array4.get(0), value);
assertSame(array4.get(0), value);
}
assertEquals(array4, adds);
// clear(), isEmpty(), size()
assertEquals(array4.size(), adds.size());
assertFalse(array4.isEmpty());
array4.clear();
assertTrue(array4.isEmpty());
assertEquals(array4.size(), 0);
// equals()
array4.clear();
array4.addAll(input);
assertTrue(array4.equals(array4));
assertTrue(array4.equals(input));
assertFalse(array4.equals(null));
assertFalse(array4.equals(adds));
for (int i = 0; i <= input.size(); ++i) {
List<E> subList = input.subList(0, i);
ArrayTemplate a = templateClass.newInstance();
a.addAll(subList);
if (i == input.size()) {
assertTrue(array4.equals(subList));
assertTrue(array4.equals(a));
} else {
assertFalse(array4.equals(subList));
assertFalse(array4.equals(a));
}
}
// hashcode()
ArrayTemplate array5 = templateClass.newInstance();
array5.addAll(input);
assertEquals(array5.hashCode(), array5.data().hashCode());
array5.addAll(adds);
assertEquals(array5.hashCode(), array5.data().hashCode());
array5.clear();
int lastHash = 0;
for (int i = 0; i < input.size(); ++i) {
array5.add(input.get(i));
int newHash = array5.hashCode();
if (i > 0) {
assertFalse(newHash == lastHash);
}
lastHash = newHash;
}
// indexOf(Object o), lastIndexOf(Object o)
ArrayTemplate array6 = templateClass.newInstance();
array6.addAll(adds);
for (E e : adds) {
assertEquals(array6.indexOf(e), adds.indexOf(e));
assertEquals(array6.lastIndexOf(e), adds.lastIndexOf(e));
}
// remove(int index), subList(int fromIndex, int toIndex)
ArrayTemplate array7 = templateClass.newInstance();
array7.addAll(input);
ArrayTemplate array8 = templateClass.newInstance();
array8.addAll(input);
for (int i = 0; i < input.size(); ++i) {
array7.remove(0);
assertEquals(array7, input.subList(i + 1, input.size()));
assertEquals(array7, array8.subList(i + 1, input.size()));
}
// removeRange(int fromIndex, int toIndex), subList(int fromIndex, int toIndex)
for (int from = 0; from < input.size(); ++from) {
for (int to = from + 1; to <= input.size(); ++to) {
ArrayTemplate arrayRemove = templateClass.newInstance();
arrayRemove.addAll(input);
InternalList<E> reference = new InternalList<E>(input);
arrayRemove.removeRange(from, to);
reference.removeRange(from, to);
assertEquals(reference, arrayRemove);
}
}
// set(int index, E element)
ArrayTemplate array9 = templateClass.newInstance();
array9.addAll(input);
InternalList<E> reference9 = new InternalList<E>(input);
for (int i = 0; i < input.size() / 2; ++i) {
int k = input.size() - i - 1;
E lo = array9.get(i);
E hi = array9.get(k);
E hiPrev = array9.set(k, lo);
E loPrev = array9.set(i, hi);
E refHiPrev = reference9.set(k, lo);
E refLoPrev = reference9.set(i, hi);
assertEquals(hiPrev, refHiPrev);
assertEquals(loPrev, refLoPrev);
assertEquals(array9.get(i), reference9.get(i));
assertEquals(array9.get(k), reference9.get(k));
}
// clone and copy return types
TestDataTemplateUtil.assertCloneAndCopyReturnType(templateClass);
// clone
Exception exc = null;
ArrayTemplate array10 = templateClass.newInstance();
array10.addAll(input);
try {
@SuppressWarnings("unchecked") ArrayTemplate array10Clone = (ArrayTemplate) array10.clone();
assertTrue(array10Clone.getClass() == templateClass);
assertEquals(array10Clone, array10);
assertTrue(array10Clone != array10);
for (int i = 0; i < array10.size(); i++) {
assertSame(array10Clone.data().get(i), array10.data().get(i));
}
array10Clone.remove(0);
assertEquals(array10Clone.size(), array10.size() - 1);
assertFalse(array10Clone.equals(array10));
assertTrue(array10.containsAll(array10Clone));
array10.remove(0);
assertEquals(array10Clone, array10);
} catch (CloneNotSupportedException e) {
exc = e;
}
assert (exc == null);
// copy
ArrayTemplate array10a = templateClass.newInstance();
array10a.addAll(input);
try {
@SuppressWarnings("unchecked") ArrayTemplate array10aCopy = (ArrayTemplate) array10a.copy();
assertTrue(array10aCopy.getClass() == templateClass);
assertEquals(array10a, array10aCopy);
boolean hasComplex = false;
for (int i = 0; i < array10a.size(); i++) {
if (array10a.data().get(i) instanceof DataComplex) {
assertNotSame(array10aCopy.data().get(i), array10a.data().get(i));
hasComplex = true;
}
}
assertTrue(DataTemplate.class.isAssignableFrom(array10a._elementClass) == false || hasComplex);
assertTrue(noCommonDataComplex(array10a.data(), array10aCopy.data()));
boolean mutated = false;
for (Object items : array10aCopy.data()) {
mutated |= TestUtil.mutateChild(items);
}
assertEquals(mutated, hasComplex);
if (mutated) {
assertNotEquals(array10aCopy, array10a);
} else {
assertEquals(array10aCopy, array10a);
array10aCopy.remove(0);
assertNotEquals(array10aCopy, array10a);
}
} catch (CloneNotSupportedException e) {
exc = e;
}
assert (exc == null);
// contains
for (int i = 0; i < input.size(); ++i) {
ArrayTemplate array = templateClass.newInstance();
E v = input.get(i);
array.add(v);
for (int k = 0; k < input.size(); ++k) {
if (k == i)
assertTrue(array.contains(v));
else
assertFalse(array.contains(input.get(k)));
}
}
// containsAll
ArrayTemplate arrayContainsAll = templateClass.newInstance();
arrayContainsAll.addAll(input);
arrayContainsAll.addAll(adds);
InternalList<E> referenceContainsAll = new InternalList<E>(input);
referenceContainsAll.addAll(adds);
for (int from = 0; from < arrayContainsAll.size(); ++from) {
for (int to = from + 1; to <= arrayContainsAll.size(); ++to) {
boolean testResult = arrayContainsAll.containsAll(referenceContainsAll.subList(from, to));
boolean referenceResult = referenceContainsAll.containsAll(referenceContainsAll.subList(from, to));
assertEquals(testResult, referenceResult);
assertTrue(testResult);
assertTrue(referenceResult);
}
boolean testResult2 = arrayContainsAll.subList(from, arrayContainsAll.size()).containsAll(referenceContainsAll);
boolean referenceResult2 = referenceContainsAll.subList(from, arrayContainsAll.size()).containsAll(referenceContainsAll);
// out.println("from " + from + " test " + testResult2 + " ref " + referenceResult2);
assertEquals(testResult2, referenceResult2);
}
// removeAll
InternalList<E> referenceListRemoveAll = new InternalList<E>(input);
referenceListRemoveAll.addAll(adds);
for (int from = 0; from < referenceListRemoveAll.size(); ++from) {
for (int to = from + 1; to <= referenceListRemoveAll.size(); ++to) {
ArrayTemplate arrayRemoveAll = templateClass.newInstance();
arrayRemoveAll.addAll(referenceListRemoveAll);
InternalList<E> referenceRemoveAll = new InternalList<E>(referenceListRemoveAll);
boolean testResult = arrayRemoveAll.removeAll(referenceListRemoveAll.subList(from, to));
boolean referenceResult = referenceRemoveAll.removeAll(referenceListRemoveAll.subList(from, to));
// out.println("from " + from + " to " + to + " test " + testResult + " " + arrayRemoveAll + " ref " + referenceResult + " " + referenceRemoveAll);
assertEquals(arrayRemoveAll, referenceRemoveAll);
assertEquals(testResult, referenceResult);
assertTrue(testResult);
assertTrue(referenceResult);
}
}
// retainAll
InternalList<E> referenceListRetainAll = new InternalList<E>(input);
referenceListRetainAll.addAll(adds);
for (int from = 0; from < referenceListRetainAll.size(); ++from) {
for (int to = from + 1; to <= referenceListRetainAll.size(); ++to) {
ArrayTemplate arrayRetainAll = templateClass.newInstance();
arrayRetainAll.addAll(referenceListRetainAll);
InternalList<E> referenceRetainAll = new InternalList<E>(referenceListRetainAll);
boolean testResult = arrayRetainAll.removeAll(referenceListRetainAll.subList(from, to));
boolean referenceResult = referenceRetainAll.removeAll(referenceListRetainAll.subList(from, to));
// out.println("from " + from + " to " + to + " test " + testResult + " " + arrayRetainAll + " ref " + referenceResult + " " + referenceRetainAll);
assertEquals(arrayRetainAll, referenceRetainAll);
assertEquals(testResult, referenceResult);
assertTrue(testResult);
assertTrue(referenceResult);
}
}
// Iterator
ArrayTemplate arrayIt = templateClass.newInstance();
arrayIt.addAll(input);
arrayIt.addAll(adds);
for (Iterator<E> it = arrayIt.iterator(); it.hasNext(); ) {
it.next();
it.remove();
}
assertTrue(arrayIt.isEmpty());
// ListIterator hasNext, hasPrevious, next, previous
ArrayTemplate arrayListIt = templateClass.newInstance();
arrayListIt.addAll(input);
arrayListIt.addAll(adds);
for (int i = 0; i <= arrayListIt.size(); ++i) {
ListIterator<E> it = arrayListIt.listIterator(i);
if (i > 0) {
int save = it.nextIndex();
assertTrue(it.hasPrevious());
assertEquals(it.previous(), arrayListIt.get(i - 1));
it.next();
assertEquals(it.nextIndex(), save);
} else {
assertFalse(it.hasPrevious());
}
if (i < arrayListIt.size()) {
int save = it.previousIndex();
assertTrue(it.hasNext());
assertEquals(it.next(), arrayListIt.get(i));
it.previous();
assertEquals(it.previousIndex(), save);
} else {
assertFalse(it.hasNext());
}
assertEquals(it.nextIndex(), i);
assertEquals(it.previousIndex(), i - 1);
}
// ListIterator remove
for (ListIterator<E> it = arrayListIt.listIterator(); it.hasNext(); ) {
it.next();
it.remove();
}
assertTrue(arrayListIt.isEmpty());
// ListIterator add
arrayListIt.clear();
{
ListIterator<E> it = arrayListIt.listIterator();
for (E e : adds) {
it.add(e);
}
}
assertEquals(arrayListIt, adds);
// ListIterator set
for (int i = 0; i < adds.size(); ++i) {
ListIterator<E> it = arrayListIt.listIterator(i);
it.next();
E value = adds.get(adds.size() - i - 1);
it.set(value);
}
for (int i = 0; i < adds.size(); ++i) {
E value = adds.get(adds.size() - i - 1);
assertEquals(arrayListIt.get(i), value);
}
} catch (InstantiationException exc) {
fail("Unexpected exception", exc);
} catch (IllegalAccessException exc) {
fail("Unexpected exception", exc);
}
}
use of com.linkedin.data.DataComplex in project rest.li by linkedin.
the class TestMapTemplate method testMap.
public static <MapTemplate extends AbstractMapTemplate<E>, E> void testMap(Class<MapTemplate> templateClass, MapDataSchema schema, Map<String, E> input, Map<String, E> adds) {
try {
Exception exc = null;
// constructor and putall
MapTemplate map1 = templateClass.newInstance();
map1.putAll(input);
assertEquals(map1, input);
/*
Constructor[] constructors = templateClass.getConstructors();
for (Constructor c : constructors)
{
out.println(c);
}
*/
try {
int size = input.size();
// constructor(int capacity)
Constructor<MapTemplate> capacityConstructor = templateClass.getConstructor(int.class);
MapTemplate map = capacityConstructor.newInstance(input.size());
assertEquals(map, Collections.emptyMap());
map.putAll(input);
assertEquals(input, map);
map.clear();
assertEquals(size, input.size());
// constructor(int capacity, float loadFactor)
Constructor<MapTemplate> loadFactorConstructor = templateClass.getConstructor(int.class, float.class);
map = loadFactorConstructor.newInstance(input.size(), input.size() * 1.4f);
assertEquals(map, Collections.emptyMap());
map.putAll(input);
assertEquals(input, map);
map.clear();
assertEquals(size, input.size());
// constructor(Map<String, E>)
Constructor<MapTemplate> mapConstructor = templateClass.getConstructor(Map.class);
map = mapConstructor.newInstance(input);
assertEquals(input, map);
map.clear();
assertEquals(size, input.size());
// constructor(DataMap)
Constructor<MapTemplate> dataMapConstructor = templateClass.getConstructor(DataMap.class);
map = dataMapConstructor.newInstance(map1.data());
assertEquals(map1, map);
assertEquals(input, map);
map.clear();
assertEquals(map1, map);
} catch (Exception e) {
assertSame(e, null);
}
// test wrapping
map1.clear();
map1.putAll(input);
DataMap dataMap2 = new DataMap();
MapTemplate map2 = DataTemplateUtil.wrap(dataMap2, schema, templateClass);
for (Map.Entry<String, E> e : input.entrySet()) {
E v = e.getValue();
Object o;
if (v instanceof DataTemplate) {
o = ((DataTemplate<?>) v).data();
} else if (v instanceof Enum) {
o = v.toString();
} else {
o = v;
}
dataMap2.put(e.getKey(), o);
assertEquals(map2.hashCode(), dataMap2.hashCode());
}
assertEquals(map1, map2);
MapTemplate map2a = DataTemplateUtil.wrap(dataMap2, templateClass);
assertEquals(map1, map2a);
assertSame(map2a.data(), map2.data());
// valueClass()
assertSame(map1.valueClass(), input.values().iterator().next().getClass());
// equals(), hashCode()
map1.clear();
map1.putAll(input);
assertTrue(map1.equals(map1));
assertTrue(map1.equals(input));
assertFalse(map1.equals(null));
assertFalse(map1.equals(adds));
assertFalse(map1.equals(new HashMap<String, E>()));
map2.clear();
Map<String, E> hashMap2 = new HashMap<String, E>();
List<Map.Entry<String, E>> inputList = new ArrayList<Map.Entry<String, E>>(input.entrySet());
int lastHash = 0;
for (int i = 0; i < inputList.size(); ++i) {
Map.Entry<String, E> entry = inputList.get(i);
map2.put(entry.getKey(), entry.getValue());
hashMap2.put(entry.getKey(), entry.getValue());
if (i == (inputList.size() - 1)) {
assertTrue(map1.equals(map2));
assertTrue(map1.equals(hashMap2));
} else {
assertFalse(map1.equals(map2));
assertFalse(map1.equals(hashMap2));
}
int newHash = map2.hashCode();
if (i > 0) {
assertFalse(lastHash == newHash);
}
lastHash = newHash;
}
// schema()
MapDataSchema schema1 = map1.schema();
assertTrue(schema1 != null);
assertEquals(schema1.getType(), DataSchema.Type.MAP);
assertEquals(schema1, schema);
// get(Object key), put(K key, V value, containsKey(Object key), containsValue(Object value), toString
MapTemplate map3 = templateClass.newInstance();
for (Map.Entry<String, E> e : input.entrySet()) {
String key = e.getKey();
E value = e.getValue();
E replaced = map3.put(key, value);
assertTrue(replaced == null);
E got = map3.get(key);
assertTrue(got != null);
assertEquals(value, got);
assertSame(value, got);
assertTrue(map3.containsKey(key));
assertTrue(map3.containsValue(value));
assertTrue(map3.toString().contains(key + "=" + value));
}
assertNull(map3.get(null));
assertNull(map3.get(1));
assertNull(map3.get(new Object()));
assertNull(map3.get("not found"));
assertEquals(map3, input);
Iterator<Map.Entry<String, E>> e2 = adds.entrySet().iterator();
for (Map.Entry<String, E> e : input.entrySet()) {
if (e2.hasNext() == false)
break;
E newValue = e2.next().getValue();
String key = e.getKey();
E value = e.getValue();
assertTrue(map3.containsKey(key));
assertTrue(map3.containsValue(value));
E replaced = map3.put(key, newValue);
assertTrue(replaced != null);
assertEquals(replaced, value);
assertSame(replaced, value);
assertTrue(map3.containsKey(key));
assertTrue(map3.containsValue(newValue));
assertTrue(map3.toString().contains(key));
assertTrue(map3.toString().contains(newValue.toString()));
}
// put(String key, E value) with replacement of existing value
map3.clear();
String testKey = "testKey";
E lastValue = null;
for (Map.Entry<String, E> e : input.entrySet()) {
E putResult = map3.put(testKey, e.getValue());
if (lastValue != null) {
assertEquals(putResult, lastValue);
}
lastValue = e.getValue();
}
// remove(Object key), containsKey(Object key), containsValue(Object value)
MapTemplate map4 = templateClass.newInstance();
map4.putAll(input);
int map4Size = map4.size();
for (Map.Entry<String, E> e : input.entrySet()) {
String key = e.getKey();
E value = e.getValue();
assertTrue(map4.containsKey(key));
assertTrue(map4.containsValue(value));
E removed = map4.remove(key);
assertTrue(removed != null);
assertEquals(value, removed);
assertSame(value, removed);
assertFalse(map4.containsKey(key));
assertFalse(map4.containsValue(value));
map4Size--;
assertEquals(map4Size, map4.size());
}
assertTrue(map4.isEmpty());
assertTrue(map4.size() == 0);
assertFalse(map4.containsValue(null));
assertFalse(map4.containsValue(new StringArray()));
// clone and copy return types
TestDataTemplateUtil.assertCloneAndCopyReturnType(templateClass);
// clone
exc = null;
map4 = templateClass.newInstance();
map4.putAll(input);
try {
exc = null;
@SuppressWarnings("unchecked") MapTemplate map4Clone = (MapTemplate) map4.clone();
assertTrue(map4Clone.getClass() == templateClass);
assertEquals(map4Clone, map4);
assertTrue(map4Clone != map4);
for (Map.Entry<String, Object> entry : map4.data().entrySet()) {
if (entry.getValue() instanceof DataComplex) {
assertSame(map4Clone.data().get(entry.getKey()), entry.getValue());
}
}
String key = map4Clone.keySet().iterator().next();
map4Clone.remove(key);
assertEquals(map4Clone.size(), map4.size() - 1);
assertFalse(map4Clone.equals(map4));
assertTrue(map4.entrySet().containsAll(map4Clone.entrySet()));
map4.remove(key);
assertEquals(map4Clone, map4);
} catch (CloneNotSupportedException e) {
exc = e;
}
assert (exc == null);
//copy
MapTemplate map4a = templateClass.newInstance();
map4a.putAll(input);
try {
@SuppressWarnings("unchecked") MapTemplate map4aCopy = (MapTemplate) map4a.copy();
assertTrue(map4aCopy.getClass() == templateClass);
assertEquals(map4a, map4aCopy);
boolean hasComplex = false;
for (Map.Entry<String, Object> entry : map4a.data().entrySet()) {
if (entry.getValue() instanceof DataComplex) {
assertNotSame(map4aCopy.data().get(entry.getKey()), entry.getValue());
hasComplex = true;
}
}
assertTrue(DataTemplate.class.isAssignableFrom(map4a._valueClass) == false || hasComplex);
assertTrue(noCommonDataComplex(map4a.data(), map4aCopy.data()));
boolean mutated = false;
for (Object value : map4aCopy.data().values()) {
if (value instanceof DataComplex) {
mutated |= TestUtil.mutateDataComplex((DataComplex) value);
}
}
assertEquals(mutated, hasComplex);
if (mutated) {
assertNotEquals(map4aCopy, map4a);
} else {
assertEquals(map4aCopy, map4a);
String key = map4aCopy.keySet().iterator().next();
map4aCopy.remove(key);
assertFalse(map4aCopy.containsKey(key));
assertTrue(map4a.containsKey(key));
}
} catch (CloneNotSupportedException e) {
exc = e;
}
assert (exc == null);
// entrySet, keySet, values, clear
MapTemplate map5 = templateClass.newInstance();
map5.putAll(input);
assertEquals(map5.entrySet(), input.entrySet());
assertCollectionEquals(map5.entrySet(), input.entrySet());
assertEquals(map5.keySet(), input.keySet());
assertCollectionEquals(map5.keySet(), input.keySet());
assertCollectionEquals(map5.values(), input.values());
assertEquals(map5.size(), input.size());
assertFalse(map5.isEmpty());
map5.clear();
assertEquals(map5.size(), 0);
assertTrue(map5.isEmpty());
map5.putAll(adds);
assertEquals(map5.entrySet(), adds.entrySet());
assertEquals(map5.keySet(), adds.keySet());
assertCollectionEquals(map5.values(), adds.values());
assertEquals(map5.size(), adds.size());
assertFalse(map5.isEmpty());
map5.clear();
assertEquals(map5.size(), 0);
assertTrue(map5.isEmpty());
// entrySet contains
MapTemplate map6 = templateClass.newInstance();
Set<Map.Entry<String, E>> entrySet6 = map6.entrySet();
for (Map.Entry<String, E> e : input.entrySet()) {
assertFalse(entrySet6.contains(e));
map6.put(e.getKey(), e.getValue());
assertTrue(entrySet6.contains(e));
}
for (Map.Entry<String, E> e : adds.entrySet()) {
assertFalse(entrySet6.contains(e));
}
assertFalse(entrySet6.contains(null));
assertFalse(entrySet6.contains(1));
assertFalse(entrySet6.contains(new Object()));
assertFalse(entrySet6.contains(new AbstractMap.SimpleEntry<String, Object>(null, null)));
assertFalse(entrySet6.contains(new AbstractMap.SimpleEntry<String, Object>("xxxx", null)));
assertFalse(entrySet6.contains(new AbstractMap.SimpleEntry<String, Object>("xxxx", "xxxx")));
assertFalse(entrySet6.contains(new AbstractMap.SimpleEntry<String, Object>("xxxx", new Object())));
// entrySet iterator
for (Map.Entry<String, E> e : map6.entrySet()) {
assertTrue(map6.containsKey(e.getKey()));
assertTrue(map6.containsValue(e.getValue()));
}
try {
exc = null;
map6.entrySet().iterator().remove();
} catch (Exception e) {
exc = e;
}
assertTrue(exc != null);
assertTrue(exc instanceof UnsupportedOperationException);
// entrySet containsAll
assertTrue(map6.entrySet().containsAll(input.entrySet()));
assertTrue(input.entrySet().containsAll(map6.entrySet()));
// entrySet add
for (Map.Entry<String, E> e : input.entrySet()) {
try {
exc = null;
entrySet6.add(e);
} catch (Exception ex) {
exc = ex;
}
assertTrue(exc != null);
assertTrue(exc instanceof UnsupportedOperationException);
}
// entrySet remove
for (Map.Entry<String, E> e : input.entrySet()) {
try {
exc = null;
entrySet6.remove(e);
} catch (Exception ex) {
exc = ex;
}
assertTrue(exc != null);
assertTrue(exc instanceof UnsupportedOperationException);
}
Object[] invalidEntries = { null, 1, new Object() };
for (Object e : invalidEntries) {
try {
exc = null;
entrySet6.remove(e);
} catch (Exception ex) {
exc = ex;
}
assertTrue(exc != null);
assertTrue(exc instanceof UnsupportedOperationException);
}
// entrySet addAll
try {
exc = null;
entrySet6.addAll(adds.entrySet());
} catch (Exception ex) {
exc = ex;
}
assertTrue(exc != null);
assertTrue(exc instanceof UnsupportedOperationException);
// entrySet clear
try {
exc = null;
entrySet6.clear();
} catch (Exception ex) {
exc = ex;
}
assertTrue(exc != null);
assertTrue(exc instanceof UnsupportedOperationException);
// entrySet removeAll
Collection<?>[] collectionsToRemove = { adds.entrySet(), Collections.emptySet() };
for (Collection<?> c : collectionsToRemove) {
try {
exc = null;
entrySet6.removeAll(c);
} catch (Exception ex) {
exc = ex;
}
assertTrue(exc != null);
assertTrue(exc instanceof UnsupportedOperationException);
}
// entrySet retainAll
try {
exc = null;
entrySet6.retainAll(adds.entrySet());
} catch (Exception ex) {
exc = ex;
}
assertTrue(exc != null);
assertTrue(exc instanceof UnsupportedOperationException);
// entrySet equals, isEmpty
MapTemplate map7 = templateClass.newInstance();
MapTemplate map8 = templateClass.newInstance();
map8.putAll(input);
Set<Map.Entry<String, E>> entrySet7 = map7.entrySet();
assertTrue(entrySet7.isEmpty());
Map<String, E> hashMap7 = new HashMap<String, E>();
lastHash = 0;
for (int i = 0; i < inputList.size(); ++i) {
Map.Entry<String, E> entry = inputList.get(i);
map7.put(entry.getKey(), entry.getValue());
hashMap7.put(entry.getKey(), entry.getValue());
if (i == (inputList.size() - 1)) {
assertTrue(entrySet7.equals(map8.entrySet()));
} else {
assertFalse(entrySet7.equals(map8.entrySet()));
assertTrue(entrySet7.equals(hashMap7.entrySet()));
}
assertTrue(entrySet7.toString().contains(entry.getKey()));
assertTrue(entrySet7.toString().contains(entry.getValue().toString()));
int newHash = entrySet7.hashCode();
if (i > 0) {
assertFalse(lastHash == newHash);
}
lastHash = newHash;
assertFalse(entrySet7.isEmpty());
}
assertTrue(entrySet7.equals(input.entrySet()));
assertFalse(map7.entrySet().equals(null));
assertFalse(map7.entrySet().equals(new Object()));
// test Map.Entry.set()
MapTemplate map9 = templateClass.newInstance();
map9.putAll(input);
lastValue = null;
for (Map.Entry<String, E> e : map9.entrySet()) {
E value = e.getValue();
if (lastValue != null) {
exc = null;
try {
E ret = e.setValue(lastValue);
/* CowMap entrySet() returns unmodifiable view.
This may change if don't use CowMap to back DataMap.
assertEquals(ret, value);
assertEquals(e.getValue(), lastValue);
assertEquals(map9.get(e.getKey()), lastValue);
*/
} catch (Exception ex) {
exc = ex;
}
assertTrue(exc instanceof UnsupportedOperationException);
}
lastValue = value;
}
} catch (IllegalAccessException exc) {
fail("Unexpected exception", exc);
} catch (InstantiationException exc) {
fail("Unexpected exception", exc);
}
}
use of com.linkedin.data.DataComplex in project rest.li by linkedin.
the class QueryParamsDataMap method iterate.
private static void iterate(String keyPrefix, DataComplex dataComplex, Map<String, List<String>> result) {
String separator = ("".equals(keyPrefix)) ? "" : PathSegment.PATH_SEPARATOR;
if (dataComplex instanceof DataMap) {
DataMap dataMap = (DataMap) dataComplex;
for (Entry<String, Object> entry : dataMap.entrySet()) {
String escapedKeyPrefix = keyPrefix + separator + PathSegment.CODEC.encode(entry.getKey());
Object object = entry.getValue();
if (object instanceof DataComplex) {
iterate(escapedKeyPrefix, (DataComplex) object, result);
} else {
// If the current key designates a location in a datamap - the key should be unique - create a new
// list containing the value and put it in the map.
String objStr;
if (object instanceof ByteString) {
objStr = Data.bytesToString(((ByteString) object).copyBytes());
} else {
objStr = object.toString();
}
result.put(escapedKeyPrefix, Collections.singletonList(objStr));
}
}
} else if (dataComplex instanceof DataList) {
DataList dataList = (DataList) dataComplex;
for (int i = 0; i < dataList.size(); i++) {
Object object = dataList.get(i);
if (object instanceof DataComplex) {
iterate(keyPrefix + "[" + i + "]", (DataComplex) object, result);
} else {
addListValue(keyPrefix, i, object, result);
}
}
}
}
use of com.linkedin.data.DataComplex in project rest.li by linkedin.
the class URIParamUtils method encodeDataObject.
private static void encodeDataObject(Object obj, URLEscaper.Escaping escaping, UriComponent.Type componentType, StringBuilder stringBuilder) {
if (obj instanceof DataComplex) {
if (obj instanceof DataMap) {
DataMap dataMap = (DataMap) obj;
stringBuilder.append(URIConstants.OBJ_START);
if (!dataMap.isEmpty()) {
List<String> keys = new ArrayList<String>(dataMap.keySet());
Collections.sort(keys);
ListIterator<String> iterator = keys.listIterator();
String currentKey = iterator.next();
mapEncodingHelper(currentKey, dataMap.get(currentKey), escaping, componentType, stringBuilder);
while (iterator.hasNext()) {
stringBuilder.append(URIConstants.ITEM_SEP);
currentKey = iterator.next();
mapEncodingHelper(currentKey, dataMap.get(currentKey), escaping, componentType, stringBuilder);
}
}
stringBuilder.append(URIConstants.OBJ_END);
} else if (obj instanceof DataList) {
DataList dataList = (DataList) obj;
stringBuilder.append(URIConstants.LIST_PREFIX);
stringBuilder.append(URIConstants.OBJ_START);
if (!dataList.isEmpty()) {
ListIterator<Object> iterator = dataList.listIterator();
encodeDataObject(iterator.next(), escaping, componentType, stringBuilder);
while (iterator.hasNext()) {
stringBuilder.append(URIConstants.ITEM_SEP);
encodeDataObject(iterator.next(), escaping, componentType, stringBuilder);
}
}
stringBuilder.append(URIConstants.OBJ_END);
} else {
throw new IllegalArgumentException(obj.getClass() + " is an unknown subtype of dataComplex.");
}
} else {
stringBuilder.append(encodeString(DataTemplateUtil.stringify(obj), escaping, componentType));
}
}
use of com.linkedin.data.DataComplex in project rest.li by linkedin.
the class TestDataIterator method traverse.
public List<String> traverse(DataElement element, IterationOrder order, boolean usePath) {
List<String> traversalList = new ArrayList<String>();
DataIterator it = Builder.create(element, order).dataIterator();
DataElement current;
while ((current = it.next()) != null) {
StringBuilder s = new StringBuilder();
if (usePath) {
s.append("path=").append(current.pathAsString());
} else {
s.append("name=").append(current.getName());
}
s.append(", class=").append(current.getValue().getClass().getName());
Object value = current.getValue();
if ((value instanceof DataComplex) == false) {
s.append(", value=");
if (value instanceof ByteString) {
s.append(((ByteString) value).asAvroString());
} else {
s.append(value.toString());
}
}
traversalList.add(s.toString());
}
return traversalList;
}
Aggregations