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.util.Scanner;
028
029import org.junit.Test;
030
031public class FileSourceTest
032{
033
034    @Test
035    public void testFileSource()
036    {
037        try
038        {
039            new FileSource( null );
040            fail( "Should fail, since you must specify a file" );
041        }
042        catch ( IllegalArgumentException e )
043        {
044            assertEquals( "no file specified", e.getMessage() );
045        }
046    }
047
048    @Test
049    public void testGetInputStream()
050        throws Exception
051    {
052        File txtFile = new File( "target/test-classes/source.txt" );
053        FileSource source = new FileSource( txtFile );
054
055        Scanner scanner = null;
056        InputStream is = null;
057        try
058        {
059            is = source.getInputStream();
060
061            scanner = new Scanner( is );
062            assertEquals( "Hello World!", scanner.nextLine() );
063        }
064        finally
065        {
066            if ( scanner != null )
067            {
068                scanner.close();
069            }
070            if ( is != null )
071            {
072                is.close();
073            }
074        }
075    }
076
077    @Test
078    public void testGetLocation()
079    {
080        File txtFile = new File( "target/test-classes/source.txt" );
081        FileSource source = new FileSource( txtFile );
082        assertEquals( txtFile.getAbsolutePath(), source.getLocation() );
083    }
084
085    @Test
086    public void testGetFile()
087    {
088        File txtFile = new File( "target/test-classes/source.txt" );
089        FileSource source = new FileSource( txtFile );
090        assertEquals( txtFile.getAbsoluteFile(), source.getFile() );
091    }
092
093}