Package org.springmodules.xt.model.introductor.collections

Source Code of org.springmodules.xt.model.introductor.collections.IntroductorCollectionTest

/*
* Copyright 2006 the original author or authors.
*
* 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.springmodules.xt.model.introductor.collections;

import java.util.LinkedList;
import java.util.Collection;
import java.util.Iterator;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.springmodules.xt.test.domain.Employee;
import org.springmodules.xt.test.domain.IEmployee;
import org.springmodules.xt.test.domain.Office;
import org.springmodules.xt.test.domain.EmployeeView;
import org.springmodules.xt.model.introductor.bean.DynamicBeanIntroductor;

/**
*
* @author Sergio Bossa
*/
public class IntroductorCollectionTest extends TestCase {
   
    private Collection target;
   
    public IntroductorCollectionTest(String testName) {
        super(testName);
    }
   
    protected void setUp() throws Exception {
        target = new LinkedList();
        Employee emp1 = new Employee();
        Employee emp2 = new Employee();
       
        emp1.setMatriculationCode("1");
        emp2.setMatriculationCode("2");
       
        target.add(emp1);
        target.add(emp2);
    }
   
    protected void tearDown() throws Exception {
    }
   
    /**
     * Test of adding and removing a new object.
     */
    public void testAddAndRemoveObjectPartOne() {
        IntroductorCollection introductor = introductor = new IntroductorCollection(target, new Class[]{EmployeeView.class}, new DynamicBeanIntroductor());
       
        this.internalTestAddAndRemove(introductor);
    }
   
    /**
     * Test of adding and removing a new object.
     */
    public void testAddAndRemoveObjectPartTwo() {
        IntroductorCollection introductor = new IntroductorCollection(target, new Class[]{EmployeeView.class}, new Class[]{IEmployee.class}, new DynamicBeanIntroductor());
       
        this.internalTestAddAndRemove(introductor);
    }
   
    /**
     * Test of iterating and removing objects.
     */
    public void testIterateAndRemovePartOne() {
        IntroductorCollection introductor = introductor = new IntroductorCollection(target, new Class[]{EmployeeView.class}, new DynamicBeanIntroductor());
       
        this.internalTestIterateAndRemove(introductor);
    }
   
    /**
     * Test of iterating and removing objects.
     */
    public void testIterateAndRemovePartTwo() {
        IntroductorCollection introductor = new IntroductorCollection(target, new Class[]{EmployeeView.class}, new Class[]{IEmployee.class}, new DynamicBeanIntroductor());
       
        this.internalTestIterateAndRemove(introductor);
    }
   
    /**
     * Test of modifying and iterating objects.
     */
    public void testModifyAndIteratePartOne() {
        IntroductorCollection introductor = new IntroductorCollection(target, new Class[]{EmployeeView.class}, new DynamicBeanIntroductor());
       
        this.internalTestModifyAndIterate(introductor);
    }
   
    /**
     * Test of modifying and iterating objects.
     */
    public void testModifyAndIteratePartTwo() {
        IntroductorCollection introductor = new IntroductorCollection(target, new Class[]{EmployeeView.class}, new Class[]{IEmployee.class}, new DynamicBeanIntroductor());
       
        this.internalTestModifyAndIterate(introductor);
    }
   
    private void internalTestAddAndRemove(IntroductorCollection introductor) {
        assertEquals(2, target.size());
        assertEquals(2, introductor.size());
       
        Employee emp3 = new Employee();
        emp3.setMatriculationCode("3");
       
        // Add and remove from introductor ....
       
        introductor.add(emp3);
       
        assertEquals(3, target.size());
        assertEquals(3, introductor.size());
        assertTrue(target.contains(emp3));
        assertTrue(introductor.contains(emp3));
       
        introductor.remove(emp3);
       
        assertEquals(2, target.size());
        assertEquals(2, introductor.size());
       
        // Add from introductor and remove from target ....
       
        introductor.add(emp3);
       
        assertEquals(3, target.size());
        assertEquals(3, introductor.size());
        assertTrue(target.contains(emp3));
        assertTrue(introductor.contains(emp3));
       
        target.remove(emp3);
       
        assertEquals(2, target.size());
        assertEquals(2, introductor.size());
    }
   
    private void internalTestIterateAndRemove(IntroductorCollection introductor) {
        for (Iterator it = introductor.iterator(); it.hasNext();) {
            Object o = it.next();
            assertTrue(o instanceof  IEmployee);
            assertTrue(o instanceof EmployeeView);
            it.remove();
        }
       
        assertEquals(0, target.size());
        assertEquals(0, introductor.size());
    }
   
    private void internalTestModifyAndIterate(IntroductorCollection introductor) {
        int n = 0;
        for (Iterator it = introductor.iterator(); it.hasNext();) {
            EmployeeView v = (EmployeeView) it.next();
            Office o = new Office();
            o.setOfficeId("" + ++n);
            v.setOffice(o);
        }
       
        n = 0;
        for (Iterator it = introductor.iterator(); it.hasNext();) {
            EmployeeView v = (EmployeeView) it.next();
            assertEquals("" + ++n, v.getOffice().getOfficeId());
        }
    }
}
TOP

Related Classes of org.springmodules.xt.model.introductor.collections.IntroductorCollectionTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.