001package org.apache.maven.building;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.fail;
024
025import java.io.File;
026import java.io.InputStream;
027import java.net.URL;
028import java.util.Scanner;
029
030import org.junit.Test;
031
032public class UrlSourceTest
033{
034
035    @Test
036    public void testUrlSource()
037    {
038        try
039        {
040            new UrlSource( null );
041            fail( "Should fail, since you must specify a url" );
042        }
043        catch ( IllegalArgumentException e )
044        {
045            assertEquals( "no url specified", e.getMessage() );
046        }
047    }
048
049    @Test
050    public void testGetInputStream()
051        throws Exception
052    {
053        URL txtFile = new File( "target/test-classes/source.txt" ).toURI().toURL();
054        UrlSource source = new UrlSource( txtFile );
055
056        Scanner scanner = null;
057        InputStream is = null;
058        try
059        {
060            is = source.getInputStream();
061
062            scanner = new Scanner( is );
063            assertEquals( "Hello World!", scanner.nextLine() );
064        }
065        finally
066        {
067            if ( scanner != null )
068            {
069                scanner.close();
070            }
071            if ( is != null )
072            {
073                is.close();
074            }
075        }
076    }
077
078    @Test
079    public void testGetLocation()
080        throws Exception
081    {
082        URL txtFile = new File( "target/test-classes/source.txt" ).toURI().toURL();
083        UrlSource source = new UrlSource( txtFile );
084        assertEquals( txtFile.toExternalForm(), source.getLocation() );
085    }
086
087}