⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testlinkedlist.java

📁 初级java程序员如果想要更深一步提高自己的实力
💻 JAVA
字号:
/*
 *  Copyright 2001-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.commons.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;

import org.apache.commons.collections.list.AbstractTestList;

/**
 * Tests base {@link java.util.LinkedList} methods and contracts.
 * <p>
 * To use, simply extend this class, and implement
 * the {@link #makeLinkedList} method.
 * <p>
 * If your {@link LinkedList} fails one of these tests by design,
 * you may still use this base set of cases.  Simply override the
 * test case (method) your {@link List} fails.
 * 
 * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
 *
 * @author Rich Dougherty
 */
public abstract class TestLinkedList extends AbstractTestList {

    public TestLinkedList(String testName) {
        super(testName);
    }

    public List makeEmptyList() {
        return makeEmptyLinkedList();
    }

    public List makeFullList() {
        return makeFullLinkedList();
    }

    /**
     *  Return a new, empty {@link LinkedList} to be used for testing.
     *
     *  @return an empty list for testing.
     */
    protected abstract LinkedList makeEmptyLinkedList();

    /**
     *  Return a new, full {@link List} to be used for testing.
     *
     *  @return a full list for testing
     */
    protected LinkedList makeFullLinkedList() {
        // only works if list supports optional "addAll(Collection)" 
        LinkedList list = makeEmptyLinkedList();
        list.addAll(Arrays.asList(getFullElements()));
        return list;
    }

    /**
     *  Returns the {@link #collection} field cast to a {@link LinkedList}.
     *
     *  @return the collection field as a List
     */
    protected LinkedList getLinkedList() {
        return (LinkedList)collection;
    }

    /**
     *  Returns the {@link #confirmed} field cast to a {@link LinkedList}.
     *
     *  @return the confirmed field as a List
     */
    protected LinkedList getConfirmedLinkedList() {
        return (LinkedList)confirmed;
    }

    /**
     *  Tests {@link LinkedList#addFirst(Object)}.
     */
    public void testLinkedListAddFirst() {
        if (!isAddSupported()) return;
        Object o = "hello";

        resetEmpty();
        getLinkedList().addFirst(o);
        getConfirmedLinkedList().addFirst(o);
        verify();

        resetFull();
        getLinkedList().addFirst(o);
        getConfirmedLinkedList().addFirst(o);
        verify();
    }

    /**
     *  Tests {@link LinkedList#addLast(Object)}.
     */
    public void testLinkedListAddLast() {
        if (!isAddSupported()) return;
        Object o = "hello";

        resetEmpty();
        getLinkedList().addLast(o);
        getConfirmedLinkedList().addLast(o);
        verify();

        resetFull();
        getLinkedList().addLast(o);
        getConfirmedLinkedList().addLast(o);
        verify();
    }

    /**
     *  Tests {@link LinkedList#getFirst(Object)}.
     */
    public void testLinkedListGetFirst() {
        resetEmpty();
        try {
            getLinkedList().getFirst();
            fail("getFirst() should throw a NoSuchElementException for an " +
                    "empty list.");
        } catch (NoSuchElementException e) {
            // This is correct
        }
        verify();

        resetFull();
        Object first = getLinkedList().getFirst();
        Object confirmedFirst = getConfirmedLinkedList().getFirst();
        assertEquals("Result returned by getFirst() was wrong.",
                confirmedFirst, first);
        verify();
    }

    /**
     *  Tests {@link LinkedList#getLast(Object)}.
     */
    public void testLinkedListGetLast() {
        resetEmpty();
        try {
            getLinkedList().getLast();
            fail("getLast() should throw a NoSuchElementException for an " +
                    "empty list.");
        } catch (NoSuchElementException e) {
            // This is correct
        }
        verify();
        
        resetFull();
        Object last = getLinkedList().getLast();
        Object confirmedLast = getConfirmedLinkedList().getLast();
        assertEquals("Result returned by getLast() was wrong.",
                confirmedLast, last);
        verify();
    }

    /**
     *  Tests {@link LinkedList#removeFirst(Object)}.
     */
    public void testLinkedListRemoveFirst() {
        if (!isRemoveSupported()) return;

        resetEmpty();
        try {
            getLinkedList().removeFirst();
            fail("removeFirst() should throw a NoSuchElementException for " +
                    "an empty list.");
        } catch (NoSuchElementException e) {
            // This is correct
        }
        verify();
        
        resetFull();
        Object first = getLinkedList().removeFirst();
        Object confirmedFirst = getConfirmedLinkedList().removeFirst();
        assertEquals("Result returned by removeFirst() was wrong.",
                confirmedFirst, first);
        verify();
    }

    /**
     *  Tests {@link LinkedList#removeLast(Object)}.
     */
    public void testLinkedListRemoveLast() {
        if (!isRemoveSupported()) return;

        resetEmpty();
        try {
            getLinkedList().removeLast();
            fail("removeLast() should throw a NoSuchElementException for " +
                    "an empty list.");
        } catch (NoSuchElementException e) {
            // This is correct
        }
        verify();

        resetFull();
        Object last = getLinkedList().removeLast();
        Object confirmedLast = getConfirmedLinkedList().removeLast();
        assertEquals("Result returned by removeLast() was wrong.",
                confirmedLast, last);
        verify();
    }

    /**
     *  Returns an empty {@link ArrayList}.
     */
    public Collection makeConfirmedCollection() {
        return new LinkedList();
    }

    /**
     *  Returns a full {@link ArrayList}.
     */
    public Collection makeConfirmedFullCollection() {
        List list = new LinkedList();
        list.addAll(Arrays.asList(getFullElements()));
        return list;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -