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

📄 testabstractinstantiator.java

📁 Thinking in Java 3 中文版,经典java入门必看教程,最流行的java学习用书.
💻 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.tapestry.test;

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

import junit.framework.AssertionFailedError;
import junit.framework.TestCase;

public class TestAbstractInstantiator extends TestCase
{
    private void unreachable()
    {
        throw new AssertionFailedError("This code should be unreachable.");
    }

    public void testNonAbstract() throws Exception
    {
        AbstractInstantiator i = new AbstractInstantiator();

        Object result = i.getInstance(ArrayList.class);

        assertEquals(ArrayList.class, result.getClass());
    }

    public void testInterface() throws Exception
    {
        try
        {
            AbstractInstantiator i = new AbstractInstantiator();

            i.getInstance(List.class);
            unreachable();
        }
        catch (IllegalArgumentException ex)
        {
            assertEquals(
                ex.getMessage(),
                "Can not create instance of interface java.util.List.");
        }
    }

    public void testObjectType()
    {
        AbstractInstantiator i = new AbstractInstantiator();

        StringSubject s = (StringSubject) i.getInstance(StringSubject.class);

        s.setTitle("title");

        assertEquals("title", s.getTitle());
    }

    public void testPrimitiveType()
    {
        AbstractInstantiator i = new AbstractInstantiator();

        IntSubject s = (IntSubject) i.getInstance(IntSubject.class);

        s.setPriority(-1);

        assertEquals(-1, s.getPriority());
    }

    public void testArrayType()
    {
        AbstractInstantiator i = new AbstractInstantiator();

        ArraySubject s = (ArraySubject) i.getInstance(ArraySubject.class);

        int[] counts = new int[] { 3, 7, 9 };

        s.setCounts(counts);

        assertSame(counts, s.getCounts());
    }

    public void testInherited()
    {
        AbstractInstantiator i = new AbstractInstantiator();

        InheritedSubject s =
            (InheritedSubject) i.getInstance(InheritedSubject.class);

        s.setFlag(true);
        s.setPriority(5);

        assertEquals(true, s.getFlag());
        assertEquals(5, s.getPriority());
    }

    public void testMethodNameNotOverriden()
    {
        AbstractInstantiator i = new AbstractInstantiator();

        BooleanSubject s = (BooleanSubject) i.getInstance(BooleanSubject.class);

        s.setKnown(true);

        assertEquals(true, s.isKnown());
    }

    public void testUniqueInstances()
    {
        AbstractInstantiator i = new AbstractInstantiator();

        StringSubject s1 = (StringSubject) i.getInstance(StringSubject.class);
        StringSubject s2 = (StringSubject) i.getInstance(StringSubject.class);

        assertNotSame(s1, s2);
    }
}

⌨️ 快捷键说明

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