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 */
045public abstract class BranchCommandTckTest extends ScmTckTestCase {
046
047    protected String getBranch() {
048        return "test-branch";
049    }
050
051    @Test
052    public void testBranchCommandTest() throws Exception {
053        String branch = getBranch();
054
055        @SuppressWarnings("deprecation")
056        BranchScmResult branchResult = getScmManager()
057                .getProviderByUrl(getScmUrl())
058                .branch(getScmRepository(), new ScmFileSet(getWorkingCopy()), branch);
059
060        assertResultIsSuccess(branchResult);
061
062        // see https://issues.apache.org/jira/browse/SCM-754
063        // assertEquals( "check all 4 files branched", 4, branchResult.getBranchedFiles().size() );
064
065        File readmeTxt = new File(getWorkingCopy(), "readme.txt");
066
067        assertEquals("check readme.txt contents", "/readme.txt", FileUtils.fileRead(readmeTxt));
068
069        this.edit(getWorkingCopy(), "readme.txt", null, getScmRepository());
070        changeReadmeTxt(readmeTxt.toPath());
071
072        CommandParameters commandParameters = new CommandParameters();
073        commandParameters.setString(CommandParameter.MESSAGE, "commit message");
074
075        CheckInScmResult checkinResult =
076                getScmManager().checkIn(getScmRepository(), new ScmFileSet(getWorkingCopy()), commandParameters);
077
078        assertResultIsSuccess(checkinResult);
079
080        CheckOutScmResult checkoutResult =
081                getScmManager().checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()));
082
083        assertResultIsSuccess(checkoutResult);
084
085        readmeTxt = new File(getAssertionCopy(), "readme.txt");
086
087        assertEquals("check readme.txt contents", "changed file", FileUtils.fileRead(readmeTxt));
088
089        deleteDirectory(getAssertionCopy());
090
091        assertFalse("check previous assertion copy deleted", getAssertionCopy().exists());
092
093        checkoutResult = getScmManager()
094                .getProviderByUrl(getScmUrl())
095                .checkOut(getScmRepository(), new ScmFileSet(getAssertionCopy()), new ScmBranch(branch));
096
097        assertResultIsSuccess(checkoutResult);
098
099        assertEquals(
100                "check readme.txt contents is from branched version", "/readme.txt", FileUtils.fileRead(readmeTxt));
101    }
102
103    private void changeReadmeTxt(Path readmeTxt) throws Exception {
104        try (Writer output = Files.newBufferedWriter(readmeTxt)) {
105            output.write("changed file");
106        }
107    }
108}