001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections.primitives.adapters;
018
019import java.util.Arrays;
020import java.util.Iterator;
021import java.util.List;
022import java.util.ListIterator;
023
024import org.apache.commons.collections.BulkTest;
025import org.apache.commons.collections.list.AbstractTestList;
026
027/**
028 * @version $Revision: 480451 $ $Date: 2006-11-29 02:45:08 -0500 (Wed, 29 Nov 2006) $
029 * @author Rodney Waldhoff
030 */
031public abstract class BaseTestList extends AbstractTestList {
032
033    // conventional
034    // ------------------------------------------------------------------------
035
036    public BaseTestList(String testName) {
037        super(testName);
038    }
039
040    // tests
041    // ------------------------------------------------------------------------
042
043    public final void testAddAllAtIndex() {
044        List source = makeFullList();
045        List dest = makeFullList();
046        
047        dest.addAll(1,source);
048         
049        Iterator iter = dest.iterator();
050        assertTrue(iter.hasNext());
051        assertEquals(source.get(0),iter.next());
052        for(int i=0;i<source.size();i++) {
053            assertTrue(iter.hasNext());
054            assertEquals(source.get(i),iter.next());
055        }
056        for(int i=1;i<source.size();i++) {
057            assertTrue(iter.hasNext());
058            assertEquals(source.get(i),iter.next());
059        }
060        assertFalse(iter.hasNext());
061    }
062
063    /**
064     * Override to change assertSame to assertEquals.
065     */
066    public void testListListIteratorPreviousRemove() {
067        if (isRemoveSupported() == false) return;
068        resetFull();
069        ListIterator it = getList().listIterator();
070        Object zero = it.next();
071        Object one = it.next();
072        Object two = it.next();
073        Object two2 = it.previous();
074        Object one2 = it.previous();
075        assertEquals(one, one2);
076        assertEquals(two, two2);
077        assertEquals(zero, getList().get(0));
078        assertEquals(one, getList().get(1));
079        assertEquals(two, getList().get(2));
080        it.remove();
081        assertEquals(zero, getList().get(0));
082        assertEquals(two, getList().get(1));
083    }
084
085    /**
086     * Override to change assertSame to assertEquals.
087     */
088    public BulkTest bulkTestSubList() {
089        if (getFullElements().length - 6 < 10) return null;
090        return new PrimitiveBulkTestSubList(this);
091    }
092
093
094    /**
095     * Whole class copied as sub list constructor was package scoped in 3.1.
096     */
097    public static class PrimitiveBulkTestSubList extends BaseTestList {
098        private BaseTestList outer;
099    
100        PrimitiveBulkTestSubList(BaseTestList outer) {
101            super("");
102            this.outer = outer;
103        }
104    
105        public Object[] getFullElements() {
106            List l = Arrays.asList(outer.getFullElements());
107            return l.subList(3, l.size() - 3).toArray();
108        }
109        public Object[] getOtherElements() {
110            return outer.getOtherElements();
111        }
112        public boolean isAddSupported() {
113            return outer.isAddSupported();
114        }
115        public boolean isSetSupported() {
116            return outer.isSetSupported();
117        }
118        public boolean isRemoveSupported() {
119            return outer.isRemoveSupported();
120        }
121    
122        public List makeEmptyList() {
123            return outer.makeFullList().subList(4, 4);
124        }
125        public List makeFullList() {
126            int size = getFullElements().length;
127            return outer.makeFullList().subList(3, size - 3);
128        }
129        public void resetEmpty() {
130            outer.resetFull();
131            this.collection = outer.getList().subList(4, 4);
132            this.confirmed = outer.getConfirmedList().subList(4, 4);
133        }
134        public void resetFull() {
135            outer.resetFull();
136            int size = outer.confirmed.size();
137            this.collection = outer.getList().subList(3, size - 3);
138            this.confirmed = outer.getConfirmedList().subList(3, size - 3);
139        }
140        public void verify() {
141            super.verify();
142            outer.verify();
143        }
144        public boolean isTestSerialization() {
145            return false;
146        }
147        /**
148         * Override to change assertSame to assertEquals.
149         */
150        public void testListListIteratorPreviousRemove() {
151            if (isRemoveSupported() == false)
152                return;
153            resetFull();
154            ListIterator it = getList().listIterator();
155            Object zero = it.next();
156            Object one = it.next();
157            Object two = it.next();
158            Object two2 = it.previous();
159            Object one2 = it.previous();
160            assertEquals(one, one2);
161            assertEquals(two, two2);
162            assertEquals(zero, getList().get(0));
163            assertEquals(one, getList().get(1));
164            assertEquals(two, getList().get(2));
165            it.remove();
166            assertEquals(zero, getList().get(0));
167            assertEquals(two, getList().get(1));
168        }
169    }
170}