001package org.apache.maven.scm.tck.command.branch;
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.ScmBranch;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmTckTestCase;
025import org.apache.maven.scm.command.branch.BranchScmResult;
026import org.apache.maven.scm.command.checkin.CheckInScmResult;
027import org.apache.maven.scm.command.checkout.CheckOutScmResult;
028import org.codehaus.plexus.util.FileUtils;
029import org.codehaus.plexus.util.IOUtil;
030import org.junit.Test;
031
032import java.io.File;
033import java.io.FileWriter;
034
035import static org.junit.Assert.assertEquals;
036import static org.junit.Assert.assertFalse;
037
038/**
039 * This test tests the branch command.
040 *
041 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
042 *
043 */
044public abstract class BranchCommandTckTest
045    extends ScmTckTestCase
046{
047
048    protected String getBranch()
049    {
050        return "test-branch";
051    }
052
053    @Test
054    public void testBranchCommandTest()
055        throws Exception
056    {
057        String branch = getBranch();
058
059        @SuppressWarnings( "deprecation" ) BranchScmResult branchResult =
060            getScmManager().getProviderByUrl( getScmUrl() ).branch( getScmRepository(),
061                                                                    new ScmFileSet( getWorkingCopy() ), branch );
062
063        assertResultIsSuccess( branchResult );
064
065        // see https://issues.apache.org/jira/browse/SCM-754
066        //assertEquals( "check all 4 files branched", 4, branchResult.getBranchedFiles().size() );
067
068        File readmeTxt = new File( getWorkingCopy(), "readme.txt" );
069
070        assertEquals( "check readme.txt contents", "/readme.txt", FileUtils.fileRead( readmeTxt ) );
071
072        this.edit( getWorkingCopy(), "readme.txt", null, getScmRepository() );
073        changeReadmeTxt( readmeTxt );
074
075        CheckInScmResult checkinResult =
076            getScmManager().checkIn( getScmRepository(), new ScmFileSet( getWorkingCopy() ), "commit message" );
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().getProviderByUrl( getScmUrl() ).checkOut( getScmRepository(),
094                                                                                   new ScmFileSet( getAssertionCopy() ),
095                                                                                   new ScmBranch( branch ) );
096
097        assertResultIsSuccess( checkoutResult );
098
099        assertEquals( "check readme.txt contents is from branched version", "/readme.txt",
100                      FileUtils.fileRead( readmeTxt ) );
101    }
102
103    private void changeReadmeTxt( File readmeTxt )
104        throws Exception
105    {
106        FileWriter output = new FileWriter( readmeTxt );
107        try
108        {
109            output.write( "changed file" );
110        }
111        finally
112        {
113            IOUtil.close( output );
114        }
115    }
116}