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.tck.command.branch;
020
021import java.io.File;
022import java.io.Writer;
023import java.nio.file.Files;
024import java.nio.file.Path;
025
026import org.apache.maven.scm.CommandParameter;
027import org.apache.maven.scm.CommandParameters;
028import org.apache.maven.scm.ScmBranch;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.ScmTckTestCase;
031import org.apache.maven.scm.command.branch.BranchScmResult;
032import org.apache.maven.scm.command.checkin.CheckInScmResult;
033import org.apache.maven.scm.command.checkout.CheckOutScmResult;
034import org.codehaus.plexus.util.FileUtils;
035import org.junit.Test;
036
037import static org.junit.Assert.assertEquals;
038import static org.junit.Assert.assertFalse;
039
040/**
041 * This test tests the branch command.
042 *
043 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
044 *
045 */
046public abstract class BranchCommandTckTest extends ScmTckTestCase {
047
048    protected String getBranch() {
049        return "test-branch";
050    }
051
052    @Test
053    public void testBranchCommandTest() throws Exception {
054        String branch = getBranch();
055
056        @SuppressWarnings("deprecation")
057        BranchScmResult branchResult = getScmManager()
058                .getProviderByUrl(getScmUrl())
059                .branch(getScmRepository(), new ScmFileSet(getWorkingCopy()), branch);
060
061        assertResultIsSuccess(branchResult);
062
063        // see https://issues.apache.org/jira/browse/SCM-754
064        // assertEquals( "check all 4 files branched", 4, branchResult.getBranchedFiles().size() );
065
066        File readmeTxt = new File(getWorkingCopy(), "readme.txt");
067
068        assertEquals("check readme.txt contents", "/readme.txt", FileUtils.fileRead(readmeTxt));
069
070        this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
071        changeReadmeTxt(readmeTxt.toPath());
072
073        CommandParameters commandParameters = new CommandParameters();
074        commandParameters.setString(CommandParameter.MESSAGE, "commit message");
075        commandParameters.setString(CommandParameter.SCM_COMMIT_SIGN, "false");
076
077        CheckInScmResult checkinResult =
078                getScmManager().checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy()), commandParameters);
079
080        assertResultIsSuccess(checkinResult);
081
082        CheckOutScmResult checkoutResult =
083                getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
084
085        assertResultIsSuccess(checkoutResult);
086
087        readmeTxt = new File(getAssertionCopy(), "readme.txt");
088
089        assertEquals("check readme.txt contents", "changed file", FileUtils.fileRead(readmeTxt));
090
091        deleteDirectory(getAssertionCopy());
092
093        assertFalse("check previous assertion copy deleted", getAssertionCopy().exists());
094
095        checkoutResult = getScmManager()
096                .getProviderByUrl(getScmUrl())
097                .checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()), new ScmBranch(branch));
098
099        assertResultIsSuccess(checkoutResult);
100
101        assertEquals(
102                "check readme.txt contents is from branched version", "/readme.txt", FileUtils.fileRead(readmeTxt));
103    }
104
105    private void changeReadmeTxt(Path readmeTxt) throws Exception {
106        try (Writer output = Files.newBufferedWriter(readmeTxt)) {
107            output.write("changed file");
108        }
109    }
110}