home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Commons Collections example source code file (TestTypedBag.java)

This example Commons Collections source code file (TestTypedBag.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Commons Collections tags/keywords

bag, bag, class, hashbag, hashbag, illegalargumentexception, illegalargumentexception, integer, object, object, predicatedbag, string, testtypedbag, unique, util

The Commons Collections TestTypedBag.java source code

 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
package org.apache.commons.collections.bag;

import java.util.Set;

import junit.framework.Test;
import junit.framework.TestSuite;

import org.apache.commons.collections.Bag;

 * Extension of {@link TestBag} for exercising the {@link TypedBag}
 * implementation.
 * @since Commons Collections 3.0
 * @version $Revision: 646780 $ $Date: 2008-04-10 13:48:07 +0100 (Thu, 10 Apr 2008) $
 * @author Phil Steitz
public class TestTypedBag extends AbstractTestBag {
    public TestTypedBag(String testName) {

    public static Test suite() {
        return new TestSuite(TestTypedBag.class);

    public static void main(String args[]) {
        String[] testCaseName = { TestTypedBag.class.getName()};

    protected Class stringClass = this.getName().getClass();
    private Object obj = new Object();
    protected Class objectClass = obj.getClass();
    protected Bag decorateBag(HashBag bag, Class claz) {
        return TypedBag.decorate(bag, claz);

    public Bag makeBag() {
        return decorateBag(new HashBag(), objectClass);
    protected Bag makeTestBag() {
        return decorateBag(new HashBag(), stringClass);

    public void testlegalAddRemove() {
        Bag bag = makeTestBag();
        assertEquals(0, bag.size());
        Object[] els = new Object[] {"1", "3", "5", "7", "2", "4", "1"};
        for (int i = 0; i < els.length; i++) {
            assertEquals(i + 1, bag.size());
            assertEquals(true, bag.contains(els[i]));
        Set set = ((PredicatedBag) bag).uniqueSet();
        assertTrue("Unique set contains the first element",set.contains(els[0]));
        assertEquals(true, bag.remove(els[0])); 
        set = ((PredicatedBag) bag).uniqueSet();
        assertTrue("Unique set now does not contain the first element",
    public void testIllegalAdd() {
        Bag bag = makeTestBag();
        Integer i = new Integer(3);
        try {
            fail("Integer should fail type check.");
        } catch (IllegalArgumentException e) {
            // expected
        assertTrue("Collection shouldn't contain illegal element", 

    public void testIllegalDecorate() {
        HashBag elements = new HashBag();
        elements.add(new Integer(3));
        try {
            Bag bag = decorateBag(elements, stringClass);
            fail("Bag contains an element that should fail the type test.");
        } catch (IllegalArgumentException e) {
            // expected
        try {
            Bag bag = decorateBag(new HashBag(), null);
            fail("Expectiing IllegalArgumentException for null predicate.");
        } catch (IllegalArgumentException e) {
            // expected

    protected boolean skipSerializedCanonicalTests() {
        return true;


Other Commons Collections examples (source code examples)

Here is a short list of links related to this Commons Collections TestTypedBag.java source code file:

new blog posts


Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.