001package org.apache.maven.scm.provider.starteam.command.changelog;
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 org.apache.maven.scm.ChangeSet;
023import org.apache.maven.scm.ScmTestCase;
024import org.apache.maven.scm.log.DefaultLog;
025
026import java.io.BufferedReader;
027import java.io.File;
028import java.io.FileInputStream;
029import java.io.InputStreamReader;
030import java.util.Iterator;
031import java.util.List;
032import java.util.Locale;
033
034/**
035 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
036 *
037 */
038public class StarteamChangeLogConsumerTest
039    extends ScmTestCase
040{
041    private File testFile;
042
043    public void setUp()
044        throws Exception
045    {
046        super.setUp();
047
048        String language = Locale.getDefault().getLanguage();
049
050        testFile = getTestFile( "/src/test/resources/starteam/changelog/starteamlog_" + language + ".txt" );
051
052        if ( !testFile.exists() )
053        {
054            testFile = getTestFile( "/src/test/resources/starteam/changelog/starteamlog_en.txt" );
055        }
056    }
057
058    private List<ChangeSet> parseTestFile()
059        throws Exception
060    {
061        /* must match the working directory in the text test file */
062
063        File basedir = new File( "C:/Test" );
064
065        FileInputStream fis = new FileInputStream( testFile );
066
067        BufferedReader in = new BufferedReader( new InputStreamReader( fis ) );
068
069        String s = in.readLine();
070
071        StarteamChangeLogConsumer consumer =
072            new StarteamChangeLogConsumer( basedir, new DefaultLog(), null, null, null );
073
074        while ( s != null )
075        {
076            consumer.consumeLine( s );
077
078            s = in.readLine();
079        }
080
081        return consumer.getModifications();
082    }
083
084    public void testNumberOfModifications()
085        throws Exception
086    {
087        List<ChangeSet> entries = parseTestFile();
088
089        assertEquals( "Wrong number of entries returned", 6, entries.size() );
090
091        for ( Iterator<ChangeSet> i = entries.iterator(); i.hasNext(); )
092        {
093            assertTrue( "ChangeLogEntry erroneously picked up",
094                        i.next().toString().indexOf( "ChangeLogEntry.java" ) == -1 );
095        }
096    }
097
098    public void testRelativeFilePath()
099        throws Exception
100    {
101        List<ChangeSet> entries = parseTestFile();
102
103        // ensure the filename in the first ChangeSet has correct relative path
104        ChangeSet entry = (ChangeSet) entries.get( 1 );
105
106        assertTrue( entry.containsFilename( "./maven/src/File2.java" ));
107    }
108
109    public void testLocales()
110        throws Exception
111    {
112        Locale currentLocale = Locale.getDefault();
113
114        Locale[] availableLocales = Locale.getAvailableLocales();
115        try
116        {
117            for ( int i = 0; i < availableLocales.length; i++ )
118            {
119                Locale.setDefault( availableLocales[i] );
120
121                String language = availableLocales[i].getLanguage();
122
123                testFile = getTestFile( "/src/test/resources/starteam/changelog/starteamlog_" + language + ".txt" );
124
125                if ( !testFile.exists() )
126                {
127                    testFile = getTestFile( "/src/test/resources/starteam/changelog/starteamlog_en.txt" );
128                }
129
130                parseTestFile();
131            }
132        }
133        finally
134        {
135            Locale.setDefault( currentLocale );
136        }
137    }
138}