001/*
002 *   Copyright (C) Christian Schulte, 2011-293
003 *   All rights reserved.
004 *
005 *   Redistribution and use in source and binary forms, with or without
006 *   modification, are permitted provided that the following conditions
007 *   are met:
008 *
009 *     o Redistributions of source code must retain the above copyright
010 *       notice, this list of conditions and the following disclaimer.
011 *
012 *     o Redistributions in binary form must reproduce the above copyright
013 *       notice, this list of conditions and the following disclaimer in
014 *       the documentation and/or other materials provided with the
015 *       distribution.
016 *
017 *   THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
018 *   INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
019 *   AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
020 *   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY DIRECT, INDIRECT,
021 *   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022 *   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023 *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024 *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025 *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026 *   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027 *
028 *   $JOMC: KeyValueTypeTest.java 4613 2012-09-22 10:07:08Z schulte $
029 *
030 */
031package org.jomc.ant.types.test;
032
033import java.net.URL;
034import org.apache.tools.ant.BuildException;
035import org.apache.tools.ant.Location;
036import org.jomc.ant.types.KeyValueType;
037import org.junit.Test;
038import static org.junit.Assert.assertEquals;
039import static org.junit.Assert.assertNotNull;
040import static org.junit.Assert.assertNull;
041import static org.junit.Assert.fail;
042
043/**
044 * Test cases for class {@code org.jomc.ant.types.KeyValueType}.
045 *
046 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a>
047 * @version $JOMC: KeyValueTypeTest.java 4613 2012-09-22 10:07:08Z schulte $
048 */
049public class KeyValueTypeTest
050{
051
052    /** Creates a new {@code KeyValueTypeTest} instance. */
053    public KeyValueTypeTest()
054    {
055        super();
056    }
057
058    @Test
059    public void testGetValue() throws Exception
060    {
061        final KeyValueType keyValueType = new KeyValueType();
062
063        try
064        {
065            keyValueType.getObject( null );
066            fail( "Expected NullPointerException not thrown." );
067        }
068        catch ( final NullPointerException e )
069        {
070            assertNotNull( e.getMessage() );
071            System.out.println( e.toString() );
072        }
073
074        assertNull( keyValueType.getObject( new Location( "TEST" ) ) );
075
076        keyValueType.setValue( "TEST" );
077        assertEquals( "TEST", keyValueType.getObject( new Location( "TEST" ) ) );
078
079        keyValueType.setValue( null );
080        keyValueType.setType( Object.class );
081        assertEquals( Object.class, keyValueType.getObject( new Location( "TEST" ) ).getClass() );
082
083        keyValueType.setValue( "file:///" );
084        keyValueType.setType( URL.class );
085        assertEquals( URL.class, keyValueType.getObject( new Location( "TEST" ) ).getClass() );
086
087        keyValueType.setValue( "TEST" );
088        keyValueType.setType( Object.class );
089
090        try
091        {
092            keyValueType.getObject( new Location( "TEST" ) );
093            fail( "Expected BuildException not thrown." );
094        }
095        catch ( final BuildException e )
096        {
097            assertNotNull( e.getMessage() );
098            System.out.println( e.toString() );
099        }
100    }
101
102}