ruper2/src/test/core/org/krysalis/ruper2/util/select SelectionTests.java,NONE,1.1 MockSelector.java,NONE,1.1 CompositeSelectorTests.java,NONE,1.1

[email protected]
Newsgroups gmane.comp.krysalis.metamorphosis.cvs
Message-ID <[email protected]>
Update of /cvsroot/metamorphosis/ruper2/src/test/core/org/krysalis/ruper2/util/select
In directory sc8-pr-cvs1:/tmp/cvs-serv30481/src/test/core/org/krysalis/ruper2/util/select

Added Files:
	SelectionTests.java MockSelector.java 
	CompositeSelectorTests.java 
Log Message:
Avoid clash with existing Ruper...

--- NEW FILE: SelectionTests.java ---
/*
 * Created on Aug 27, 2003
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package org.krysalis.ruper2.util.select;

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

import junit.framework.TestCase;

import org.krysalis.ruper2.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, Selector selector) {
		return 0 < selectionCount(stuff, selector);
	}

	public int selectionCount(List stuff, Selector 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 ---
package org.krysalis.ruper2.util.select;

/**
 * Mock selector that can be used for tests.
 * Useful cause the boolean selector doesn't allow multiple
 * instances to compare not equal
 * @author Ben Sommerville
 */
public class MockSelector implements Selector {
    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 ---
package org.krysalis.ruper2.util.select;

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

import java.util.Iterator;

/**
 * Tests for CompositeSelector
 *
 * @author Ben Sommerville
 */
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() {
        Selector 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( NullPointerException e ) {
            // expect this
        }
    }


    /**
     * Test that children are returned in the order that
     * they are accessed.
     */
    public void testChildOrder() {
        Selector children[] = new Selector[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()) {
            Selector selector = (Selector) 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++  ) {
            Selector 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++  ) {
            Selector 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++  ) {
            Selector 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++  ) {
            Selector 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());
    }
}






-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.