Example usage for org.apache.commons.collections CollectionUtils size

List of usage examples for org.apache.commons.collections CollectionUtils size

Introduction

In this page you can find the example usage for org.apache.commons.collections CollectionUtils size.

Prototype

public static int size(Object object) 

Source Link

Document

Gets the size of the collection/iterator specified.

Usage

From source file:org.localmatters.serializer.config.SerializationElementHandlerTest.java

/**
 * Tests the <code>handleType</code> method when complex and parent
 *//* www . j a v  a  2s .co  m*/
public void testHandleTypeWhenComplexAndParent() {
    expect(element.getName()).andReturn(TYPE_COMPLEX);
    expect(element.attributeValue(ATTRIBUTE_PARENT)).andReturn("parent");
    expect(element.elements()).andReturn(null);
    expect(element.attributeValue(ATTRIBUTE_DISPLAY_EMPTY)).andReturn("true");
    expect(element.attributes()).andReturn(Arrays.asList("parent", "true"));

    replay(element);
    Serialization result = handler.handleType(element, attributes);
    verify(element);

    assertTrue(result instanceof ComplexSerialization);
    ComplexSerialization ser = (ComplexSerialization) result;
    assertTrue(ser.isWriteEmpty());
    assertTrue(CollectionUtils.isEmpty(ser.getAttributes()));
    assertTrue(CollectionUtils.isEmpty(ser.getElements()));
    assertEquals(1, CollectionUtils.size(handler.getExtensions()));
    assertEquals("parent", handler.getExtensions().get(ser));
}

From source file:org.localmatters.serializer.config.SerializationElementHandlerTest.java

/**
 * Tests the resolution of the references when some are invalid
 *///  w  w  w  .j  a va 2 s.  co  m
public void testResolveInvalidReferences() {
    Map<ReferenceSerialization, String> references = new HashMap<ReferenceSerialization, String>();
    Map<String, Serialization> serializations = new HashMap<String, Serialization>();
    references.put(new ReferenceSerialization(), "12345");
    references.put(new ReferenceSerialization(), "6789");

    replay(element);
    Set<String> results = SerializationElementHandler.resolveReferences(references, serializations);
    verify(element);

    assertEquals(2, CollectionUtils.size(results));
    assertTrue(results.containsAll(Arrays.asList("12345", "6789")));
}

From source file:org.localmatters.serializer.config.SerializationElementHandlerTest.java

/**
 * Tests the resolution of the extensions when some are invalid
 *///from  w  ww .  j  a v  a 2 s.  c o  m
public void testResolveInvalidExtensions() {
    Map<ComplexSerialization, String> extensions = new HashMap<ComplexSerialization, String>();
    Map<String, ComplexSerialization> parents = new HashMap<String, ComplexSerialization>();
    extensions.put(new ComplexSerialization(), "12345");
    extensions.put(new ComplexSerialization(), "6789");

    replay(element);
    Set<String> results = SerializationElementHandler.resolveExtensions(extensions, parents);
    verify(element);

    assertEquals(2, CollectionUtils.size(results));
    assertTrue(results.containsAll(Arrays.asList("12345", "6789")));
}

From source file:org.localmatters.serializer.serialization.MapSerializationTest.java

/**
 * Tests the serialization of an array/*from   w ww.  j  av  a 2  s  .  c  om*/
 */
@SuppressWarnings("unchecked")
public void testHandleArray() throws Exception {
    String[] array = new String[] { "hello", "world" };
    writer.writeMap(same(parentSer), eq("addresses"), isA(Collection.class), eq("key"), same(value),
            same(comments), same(ctx));
    expectLastCall().andAnswer(new IAnswer<Object>() {
        public Object answer() throws Throwable {
            Collection<SameKeyValueEntry<String>> entries = (Collection<SameKeyValueEntry<String>>) getCurrentArguments()[2];
            assertEquals(2, CollectionUtils.size(entries));
            Iterator<SameKeyValueEntry<String>> itr = entries.iterator();
            SameKeyValueEntry<String> entry = itr.next();
            assertEquals("hello", entry.getEntry());
            assertEquals("hello", entry.getKey());
            assertEquals("hello", entry.getValue());
            entry = itr.next();
            assertEquals("world", entry.getEntry());
            assertEquals("world", entry.getKey());
            assertEquals("world", entry.getValue());
            return null;
        }
    });
    replay(writer, value, parentSer);
    ser.serialize(parentSer, "addresses", array, ctx);
    verify(writer, value, parentSer);
}

From source file:org.localmatters.serializer.serialization.MapSerializationTest.java

/**
 * Tests the serialization of an iterable
 *//*from  www . j a  v a 2 s . c  o m*/
@SuppressWarnings("unchecked")
public void testHandleIterable() throws Exception {
    Iterable<String> iterable = Arrays.asList("hello", "world");
    writer.writeMap(same(parentSer), eq("addresses"), isA(Collection.class), eq("key"), same(value),
            same(comments), same(ctx));
    expectLastCall().andAnswer(new IAnswer<Object>() {
        public Object answer() throws Throwable {
            Collection<SameKeyValueEntry<String>> entries = (Collection<SameKeyValueEntry<String>>) getCurrentArguments()[2];
            assertEquals(2, CollectionUtils.size(entries));
            Iterator<SameKeyValueEntry<String>> itr = entries.iterator();
            SameKeyValueEntry<String> entry = itr.next();
            assertEquals("hello", entry.getEntry());
            assertEquals("hello", entry.getKey());
            assertEquals("hello", entry.getValue());
            entry = itr.next();
            assertEquals("world", entry.getEntry());
            assertEquals("world", entry.getKey());
            assertEquals("world", entry.getValue());
            return null;
        }
    });
    replay(writer, value, parentSer);
    ser.serialize(parentSer, "addresses", iterable, ctx);
    verify(writer, value, parentSer);
}

From source file:org.localmatters.serializer.SerializationContext.java

/**
 * @return The deepness of this context
 */
public int getDeepness() {
    return CollectionUtils.size(levels);
}

From source file:org.localmatters.serializer.tool.ConfigWriterFromClassTest.java

/**
 * Tests handling the iterator when the type array is invalid
 *///from   www. j  av  a  2  s.com
public void testHandleIteratorWithInvalidType() {
    ConfigWriterFromClass writer = new ConfigWriterFromClass(Object.class);
    AttributeSerialization attribute = new AttributeSerialization();
    NameSerialization name = writer.handleIterator("addresses", List.class, new Type[] {}, attribute);
    assertEquals("list", name.getName());
    assertTrue(name.getDelegate() instanceof ComplexSerialization);
    ComplexSerialization list = (ComplexSerialization) name.getDelegate();
    assertEquals(1, CollectionUtils.size(list.getAttributes()));
    assertSame(attribute, list.getAttributes().get(0));
    assertEquals(1, CollectionUtils.size(list.getElements()));
    assertEquals(2, CollectionUtils.size(list.getComments()));
    assertEquals("Unable to identify the types for the [addresses] iteration!", list.getComments().get(0));
    assertEquals("The configuration of its elements must be written manually.", list.getComments().get(1));
}

From source file:org.localmatters.serializer.tool.ConfigWriterFromClassTest.java

/**
 * Tests handling the map when the type array is invalid
 *///from w  ww . j  a v  a  2  s. c o  m
public void testHandleMapWithInvalidType() {
    ConfigWriterFromClass writer = new ConfigWriterFromClass(Object.class);
    AttributeSerialization attribute = new AttributeSerialization();
    NameSerialization name = writer.handleMap("addresses", Map.class, new Type[] {}, attribute);
    assertEquals("map", name.getName());
    assertTrue(name.getDelegate() instanceof ComplexSerialization);
    ComplexSerialization list = (ComplexSerialization) name.getDelegate();
    assertEquals(1, CollectionUtils.size(list.getAttributes()));
    assertSame(attribute, list.getAttributes().get(0));
    assertTrue(CollectionUtils.isEmpty(list.getElements()));
    assertEquals(2, CollectionUtils.size(list.getComments()));
    assertEquals("Unable to identify the types for the [addresses] map!", list.getComments().get(0));
    assertEquals("The configuration of its entries must be written manually.", list.getComments().get(1));
}

From source file:org.localmatters.serializer.tool.ConfigWriterFromClassTest.java

/**
 * Tests handling the map when the second entry in the type array is not a
 * class and the first one is a simple//  ww w . ja v a  2 s. c o  m
 */
public void testHandleMapWithNonClassSecondType() {
    ConfigWriterFromClass writer = new ConfigWriterFromClass(Object.class);
    AttributeSerialization attribute = new AttributeSerialization();
    NameSerialization name = writer.handleMap("addresses", Map.class,
            new Type[] { String.class, new DummyType() }, attribute);
    assertEquals("map", name.getName());
    assertTrue(name.getDelegate() instanceof ComplexSerialization);
    ComplexSerialization list = (ComplexSerialization) name.getDelegate();
    assertEquals(1, CollectionUtils.size(list.getAttributes()));
    assertSame(attribute, list.getAttributes().get(0));
    assertEquals(1, CollectionUtils.size(list.getElements()));
    assertEquals(1, CollectionUtils.size(list.getComments()));
    assertEquals("map of [class java.lang.String] and [DummyType]", list.getComments().get(0));
}

From source file:org.localmatters.serializer.tool.ConfigWriterFromClassTest.java

/**
 * Tests handling a parameterized type when the raw type is not a class 
 *//*from  w ww. j av a  2  s  . c o  m*/
public void testHandleParametrizedTypeWhenRawNotClass() {
    ConfigWriterFromClass writer = new ConfigWriterFromClass(Object.class);
    AttributeSerialization attribute = new AttributeSerialization();
    Serialization ser = writer.handleType("invalid", new ParameterizedType() {
        public Type getRawType() {
            return new Type() {
            };
        }

        public Type getOwnerType() {
            return null;
        }

        public Type[] getActualTypeArguments() {
            return null;
        }
    }, attribute);
    assertTrue(ser instanceof NameSerialization);
    NameSerialization name = (NameSerialization) ser;
    assertEquals("value", name.getName());
    assertTrue(name.getDelegate() instanceof ComplexSerialization);
    ComplexSerialization complex = (ComplexSerialization) name.getDelegate();
    assertEquals(1, CollectionUtils.size(complex.getAttributes()));
    assertSame(attribute, complex.getAttributes().get(0));
    assertTrue(CollectionUtils.isEmpty(complex.getElements()));
    assertEquals(2, CollectionUtils.size(complex.getComments()));
    assertEquals("Unable to resolve the class for the element [invalid]!", complex.getComments().get(0));
    assertEquals("Its configuration must be written manually.", complex.getComments().get(1));
}