alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

EasyMock example source code file (UsageVarargTest.java)

This example EasyMock source code file (UsageVarargTest.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - EasyMock tags/keywords

before, class, invocationhandler, invocationhandler, ivarargs, ivarargs, method, mockcontrol, object, object, reflection, test, test, throwable, usagevarargtest

The EasyMock UsageVarargTest.java source code

/*
 * Copyright (c) 2001-2007 OFFIS, Tammo Freese.
 * This program is made available under the terms of the MIT License.
 */
package org.easymock.tests;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.easymock.MockControl;
import org.junit.Before;
import org.junit.Test;

import static org.easymock.EasyMock.*;

public class UsageVarargTest {

    MockControl<IVarArgs> control;

    IVarArgs mock;

    @Before
    public void setup() {
        control = MockControl.createStrictControl(IVarArgs.class);
        mock = control.getMock();
    }

    @Test
    public void varargObjectAccepted() {
        mock.withVarargsString(1, "1");
        mock.withVarargsString(2, "1", "2");
        mock.withVarargsString(2, "1", "2");
        mock.withVarargsObject(3, "1");
        mock.withVarargsObject(4, "1", "2");

        control.replay();
        mock.withVarargsString(1, "1");
        mock.withVarargsString(2, "1", "2");
        mock.withVarargsString(2, "1", "2");
        mock.withVarargsObject(3, "1");
        mock.withVarargsObject(4, "1", "2");
        control.verify();
    }

    @Test
    public void varargBooleanAccepted() {
        mock.withVarargsBoolean(1, true);
        mock.withVarargsBoolean(2, true, false);

        control.replay();
        mock.withVarargsBoolean(1, true);
        mock.withVarargsBoolean(2, true, false);
        control.verify();
    }

    @Test
    public void varargByteAccepted() {
        mock.withVarargsByte(1, (byte) 1);
        mock.withVarargsByte(2, (byte) 1, (byte) 2);

        control.replay();
        mock.withVarargsByte(1, (byte) 1);
        mock.withVarargsByte(2, (byte) 1, (byte) 2);
        control.verify();
    }

    @Test
    public void varargCharAccepted() {
        mock.withVarargsChar(1, 'a');
        mock.withVarargsChar(1, 'a', 'b');

        control.replay();
        mock.withVarargsChar(1, 'a');
        mock.withVarargsChar(1, 'a', 'b');
        control.verify();
    }

    @Test
    public void varargDoubleAccepted() {
        mock.withVarargsDouble(1, 1.0d);
        mock.withVarargsDouble(1, 1.0d, 2.0d);

        control.replay();
        mock.withVarargsDouble(1, 1.0d);
        mock.withVarargsDouble(1, 1.0d, 2.0d);
        control.verify();
    }

    @Test
    public void varargFloatAccepted() {
        mock.withVarargsFloat(1, 1.0f);
        mock.withVarargsFloat(1, 1.0f, 2.0f);

        control.replay();
        mock.withVarargsFloat(1, 1.0f);
        mock.withVarargsFloat(1, 1.0f, 2.0f);
        control.verify();
    }

    @Test
    public void varargIntAccepted() {
        mock.withVarargsInt(1, 1);
        mock.withVarargsInt(1, 1, 2);

        control.replay();
        mock.withVarargsInt(1, 1);
        mock.withVarargsInt(1, 1, 2);
        control.verify();
    }

    @Test
    public void varargLongAccepted() {
        mock.withVarargsLong(1, (long) 1);
        mock.withVarargsLong(1, 1, 2);

        control.replay();
        mock.withVarargsLong(1, (long) 1);
        mock.withVarargsLong(1, 1, 2);
        control.verify();
    }

    @Test
    public void varargShortAccepted() {
        mock.withVarargsShort(1, (short) 1);
        mock.withVarargsShort(1, (short) 1, (short) 2);

        control.replay();
        mock.withVarargsShort(1, (short) 1);
        mock.withVarargsShort(1, (short) 1, (short) 2);
        control.verify();
    }
    
    @Test
    public void varargAcceptedIfArrayIsGiven() {
        IVarArgs object = (IVarArgs) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[] { IVarArgs.class }, new InvocationHandler() {
        
            public Object invoke(Object proxy, Method method, Object[] args)
                    throws Throwable {
                return null;
            }
        });
        object.withVarargsObject(1);
        object.withVarargsObject(1, (Object) null);
        object.withVarargsObject(1, (Object[]) null);
        object.withVarargsObject(1, (Object[]) new Object[0] );
        object.withVarargsObject(1, false);
        object.withVarargsObject(1, new boolean[] {true, false});
    }
}

Other EasyMock examples (source code examples)

Here is a short list of links related to this EasyMock UsageVarargTest.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.