001package org.apache.maven.scm.provider.bazaar;
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.ScmFileSet;
023import org.apache.maven.scm.ScmResult;
024import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
025import org.codehaus.plexus.PlexusTestCase;
026import org.codehaus.plexus.util.FileUtils;
027
028import java.io.File;
029import java.io.IOException;
030import java.util.ArrayList;
031import java.util.List;
032
033/**
034 * Common code used in all tests.
035 *
036 * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
037 */
038public class BazaarRepoUtils
039    extends PlexusTestCase
040{
041
042    public static final String[] filesInTestBranch =
043        new String[] { "pom.xml", "readme.txt", "src/main/java/Application.java", "src/test/java/Test.java" };
044
045    public static final String TCK_FILE_CONSTANT = "/";
046
047    public static final String BRANCH_NAME = "target" + File.separator + "test-branch";
048
049    public static final File WORKING_DIR = new File( getBasedir(), BRANCH_NAME );
050
051    public static final String COMMIT_MESSAGE = "Add files to test branch";
052
053    public static String getScmUrl()
054        throws Exception
055    {
056        return "scm:bazaar:file://" + WORKING_DIR.getAbsolutePath();
057    }
058
059    public static void initRepo()
060        throws Exception
061    {
062        // Prepare tmp directory
063        if ( WORKING_DIR.exists() )
064        {
065            FileUtils.deleteDirectory( WORKING_DIR );
066
067            if ( WORKING_DIR.exists() )
068            {
069                throw new IOException( WORKING_DIR.getAbsolutePath() + " wasn't deleted." );
070            }
071        }
072
073        boolean workingDirReady = WORKING_DIR.mkdirs();
074
075        if ( !workingDirReady )
076        {
077            throw new IOException( "Could not initiate test branch at: " + WORKING_DIR );
078        }
079
080        // Init repository
081        String[] init_cmd = new String[]{BazaarConstants.INIT_CMD};
082        BazaarUtils.execute( WORKING_DIR, init_cmd );
083
084        // Create and add files to repository
085        List<File> files = new ArrayList<File>();
086        for ( int i = 0; i < filesInTestBranch.length; i++ )
087        {
088            File file = new File( WORKING_DIR.getAbsolutePath(), filesInTestBranch[i] );
089            if ( file.getParentFile() != null && !file.getParentFile().exists() )
090            {
091                boolean success = file.getParentFile().mkdirs();
092                if ( !success )
093                {
094                    throw new IOException( "Could not create directories in branch for: " + file );
095                }
096            }
097            file.createNewFile();
098
099            FileUtils.fileWrite( file.getAbsolutePath(), TCK_FILE_CONSTANT + filesInTestBranch[i] );
100
101            files.add( file );
102        }
103
104        //Add to repository
105        String[] add_cmd = new String[] { BazaarConstants.ADD_CMD };
106        ScmFileSet filesToAdd = new ScmFileSet( new File( "" ), files );
107        add_cmd = BazaarUtils.expandCommandLine( add_cmd, filesToAdd );
108        ScmResult result = BazaarUtils.execute( WORKING_DIR, add_cmd );
109        if ( !result.isSuccess() )
110        {
111            String message =
112                "Provider message: " + result.getProviderMessage() + "\n" + "Output: " + result.getCommandOutput();
113            throw new Exception( message );
114        }
115
116        // Commit the initial repository
117        String[] commit_cmd = new String[]{BazaarConstants.COMMIT_CMD, BazaarConstants.MESSAGE_OPTION, COMMIT_MESSAGE};
118        result = BazaarUtils.execute( WORKING_DIR, commit_cmd );
119        if ( !result.isSuccess() )
120        {
121            String message =
122                "Provider message: " + result.getProviderMessage() + "\n" + "Output: " + result.getCommandOutput();
123            throw new Exception( message );
124        }
125    }
126}