krysalis-update/src/test/java/org/apache/depot/update/util/select MockSelector.java,NONE,1.1 SelectionTests.java,NONE,1.1 CompositeSelectorTests.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:37 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/test/java/org/apache/depot/update/util/select
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/test/java/org/apache/depot/update/util/select

Added Files:
	MockSelector.java SelectionTests.java 
	CompositeSelectorTests.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: SelectionTests.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed 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.depot.update.util.select;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.apache.depot.update.util.regexp.RegularExpressionSelector;


/**
 * @author arb_jack
 */
public class SelectionTests extends TestCase {

	private List m_list = null;

	public SelectionTests(String name) {
		super(name);
	}

	public void setUp() {
		m_list = new ArrayList();

		m_list.add(new String("A"));
		m_list.add(new Integer(1));
		m_list.add(new String("V"));
		m_list.add("VV");
		m_list.add("XYZ");
	}

	public void testAllSelector() {
		int selected = selectionCount(m_list, new AllSelector());
		assertEquals("Selected All", selected, m_list.size());
	}

	public void testAllExpressionSelector() {
		int selected =
			selectionCount(m_list, new RegularExpressionSelector(".*"));
		assertEquals("Selected All", selected, m_list.size());
	}

	public void testXYZExpressionSelector() {
		int selected =
			selectionCount(m_list, new RegularExpressionSelector("XYZ"));
		assertTrue("Selected 1", selected == 1);
	}

	public void testVExpressionSelector() {
		int selected =
			selectionCount(m_list, new RegularExpressionSelector(".*V.*"));
		assertTrue("Selected Some", selected > 1);
	}

	public boolean selectedSomethingFrom(List stuff, ISelector selector) {
		return 0 < selectionCount(stuff, selector);
	}

	public int selectionCount(List stuff, ISelector selector) {
		List selected = SelectionHelper.select(stuff, selector);
		return selected.size();
	}

	public static void main(String[] args) {
		junit.textui.TestRunner.run(SelectionTests.class);
	}
}

--- NEW FILE: MockSelector.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed 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.depot.update.util.select;

public class MockSelector implements ISelector {
    boolean m_result = false;

    public MockSelector() {
    }

    public MockSelector( boolean result ) {
        m_result = result;
    }

    public boolean select(Object o) throws Exception {
        return m_result;
    }
}

--- NEW FILE: CompositeSelectorTests.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed 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.depot.update.util.select;

import java.util.Iterator;

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

public class CompositeSelectorTests extends TestCase {

    static class ConcreteCompositeSelector extends CompositeSelector {
        public boolean select(Object o) throws Exception {
            return false;  //To change body of implemented methods use Options | File Templates.
        }
    }

    static class OtherCompositeSelector extends CompositeSelector {
        public boolean select(Object o) throws Exception {
            return false;  //To change body of implemented methods use Options | File Templates.
        }
    }

    CompositeSelector m_selector = new ConcreteCompositeSelector();


    public void testAddSelector() {
        m_selector.addSelector( new MockSelector() );
        assertEquals("Checking children", 1, m_selector.childCount());
    }

    /**
     * Test that adding a duplicate selector will return false
     * Duplicate means that the selectors are equal (according to
     * equals method)
     */
    public void testAddDuplicate() {
        ISelector child = new MockSelector();
        boolean result = m_selector.addSelector( child );
        assertTrue("Add should succeed", result);
        assertEquals("Checking children", 1, m_selector.childCount());

        result = m_selector.addSelector( child );
        assertFalse("Add should fail", result);
        assertEquals("Checking children", 1, m_selector.childCount());
    }

    public void testAddNull() {
        try{
            m_selector.addSelector( null );
            fail("Should not be able to add null selector");
        } catch( IllegalArgumentException e ) {
            // expect this
        }
    }


    /**
     * Test that children are returned in the order that
     * they are accessed.
     */
    public void testChildOrder() {
        ISelector children[] = new ISelector[3];
        for (int i = 0; i < children.length; i++) {
            children[i] = new MockSelector();
            m_selector.addSelector( children[i] );
        }

        int count = 0;
        Iterator childIter = m_selector.childIterator();
        while (childIter.hasNext()) {
            ISelector selector = (ISelector) childIter.next();
            assertEquals( "Child " + Integer.toString( count ) + " selector should be in same order", children[count], selector );
            count++;
        }
        assertEquals("Iteration count should be same as child count", children.length, count );
    }


    /**
     * Two composite selectors of the same type, with the same contents should be equal
     */
    public void testEquals() {
        CompositeSelector testSelector = new ConcreteCompositeSelector();

        assertTrue("Empty composites should be equal", m_selector.equals( testSelector ));

        for( int i=0; i<4; i++  ) {
            ISelector child = new MockSelector();
            m_selector.addSelector( child );
            testSelector.addSelector( child );
        }
        assertTrue("Selectors should be equals", m_selector.equals( testSelector ));
        assertTrue("Selectors should be equals", testSelector.equals( m_selector ));

        m_selector.addSelector( new MockSelector());
        assertFalse("Selectors should not be equal", m_selector.equals( testSelector ));
        assertFalse("Selectors should not be equal", testSelector.equals( m_selector ));

        testSelector.addSelector( new MockSelector( ));
        assertFalse("Selectors should not be equal", m_selector.equals( testSelector ));
        assertFalse("Selectors should not be equal", testSelector.equals( m_selector ));
    }

    /**
     * Two composite selectors of the same type, with the same contents should have the
     * same hash code
     */
    public void testHashCode() {
        CompositeSelector testSelector = new ConcreteCompositeSelector();

        assertEquals("Empty composites should have the same hash", m_selector.hashCode(),
                                                                   testSelector.hashCode() );

        for( int i=0; i<4; i++  ) {
            ISelector child = new MockSelector();
            m_selector.addSelector( child );
            testSelector.addSelector( child );
        }
        assertEquals("Selectors should have the same hash",  m_selector.hashCode(),
                                                             testSelector.hashCode() );

        m_selector.addSelector( new MockSelector());
        assertTrue("Selectors should have different hashes", m_selector.hashCode() != testSelector.hashCode()  );

        testSelector.addSelector( new MockSelector( ));
        assertTrue("Selectors should have different hashes", m_selector.hashCode() != testSelector.hashCode()  );
    }

    /**
     * Different types of selectors should not be equal
     */
    public void testEqualsWithDifferentTypes() {
        CompositeSelector testSelector = new OtherCompositeSelector();

        assertFalse("Different selector types should not be equal", m_selector.equals( testSelector ));

        for( int i=0; i<4; i++  ) {
            ISelector child = new MockSelector();
            m_selector.addSelector( child );
            testSelector.addSelector( child );
        }
        assertFalse("Different selector types should not be equal", m_selector.equals( testSelector ));
    }

    /**
     * Different types of selectors should not have same hash code
     */
    public void testHashCodeWithDifferentTypes() {
        CompositeSelector testSelector = new OtherCompositeSelector();

        assertTrue("Different selector types should not have same hash",
                        m_selector.hashCode() != testSelector.hashCode());

        for( int i=0; i<4; i++  ) {
            ISelector child = new MockSelector();
            m_selector.addSelector( child );
            testSelector.addSelector( child );
        }
        assertTrue("Different selector types should mpt have same hash",
                        m_selector.hashCode() != testSelector.hashCode());
    }

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

    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}





-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/