001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.hg;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.maven.scm.ScmFileSet;
027import org.apache.maven.scm.ScmResult;
028import org.apache.maven.scm.ScmTestCase;
029import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
030import org.codehaus.plexus.util.FileUtils;
031
032/**
033 * Common code used in all tests.
034 *
035 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
036 */
037public class HgRepoUtils extends ScmTestCase {
038    /** 'hg' command line */
039    public static final String HG_COMMAND_LINE = "hg";
040
041    public static final String[] filesInTestBranch =
042            new String[] {"pom.xml", "readme.txt", "src/main/java/Application.java", "src/test/java/Test.java"};
043
044    public static final String TCK_FILE_CONSTANT = "/";
045
046    public static final String BRANCH_NAME = "target" + File.separator + "test-branch";
047
048    public static final File WORKING_DIR = new File(getBasedir(), BRANCH_NAME);
049
050    public static final String COMMIT_MESSAGE = "Add files to test branch";
051
052    public static String getScmUrl() throws Exception {
053        return "scm:hg:" + WORKING_DIR.getAbsolutePath();
054    }
055
056    public static void initRepo() throws Exception {
057        // Prepare tmp directory
058        if (WORKING_DIR.exists()) {
059            FileUtils.deleteDirectory(WORKING_DIR);
060
061            if (WORKING_DIR.exists()) {
062                throw new IOException(WORKING_DIR.getAbsolutePath() + " wasn't deleted.");
063            }
064        }
065
066        boolean workingDirReady = WORKING_DIR.mkdirs();
067
068        if (!workingDirReady) {
069            throw new IOException("Could not initiate test branch at: " + WORKING_DIR);
070        }
071
072        // Init repository
073        String[] init_cmd = new String[] {HgCommandConstants.INIT_CMD};
074        HgUtils.execute(WORKING_DIR, init_cmd);
075
076        // Create and add files to repository
077        List<File> files = new ArrayList<>();
078        for (int i = 0; i < filesInTestBranch.length; i++) {
079            File file = new File(WORKING_DIR.getAbsolutePath(), filesInTestBranch[i]);
080            if (file.getParentFile() != null && !file.getParentFile().exists()) {
081                boolean success = file.getParentFile().mkdirs();
082                if (!success) {
083                    throw new IOException("Could not create directories in branch for: " + file);
084                }
085            }
086            file.createNewFile();
087
088            FileUtils.fileWrite(file.getAbsolutePath(), TCK_FILE_CONSTANT + filesInTestBranch[i]);
089
090            files.add(file);
091        }
092
093        // Add to repository
094        String[] add_cmd = new String[] {HgCommandConstants.ADD_CMD};
095        ScmFileSet filesToAdd = new ScmFileSet(new File(""), files);
096        add_cmd = HgUtils.expandCommandLine(add_cmd, filesToAdd);
097        ScmResult result = HgUtils.execute(WORKING_DIR, add_cmd);
098        if (!result.isSuccess()) {
099            String message =
100                    "Provider message: " + result.getProviderMessage() + "\n" + "Output: " + result.getCommandOutput();
101            throw new Exception(message);
102        }
103
104        // Commit the initial repository
105        String[] commit_cmd =
106                new String[] {HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, COMMIT_MESSAGE};
107        result = HgUtils.execute(WORKING_DIR, commit_cmd);
108        if (!result.isSuccess()) {
109            String message =
110                    "Provider message: " + result.getProviderMessage() + "\n" + "Output: " + result.getCommandOutput();
111            throw new Exception(message);
112        }
113    }
114}