001package org.apache.maven.scm.tck.command.tag;
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.ScmTag;
024import org.apache.maven.scm.ScmTckTestCase;
025import org.apache.maven.scm.command.checkin.CheckInScmResult;
026import org.apache.maven.scm.command.checkout.CheckOutScmResult;
027import org.apache.maven.scm.command.tag.TagScmResult;
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 tag command.
040 *
041 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
042 *
043 */
044public abstract class TagCommandTckTest
045    extends ScmTckTestCase
046{
047
048    protected String getTagName()
049    {
050        return "test-tag";
051    }
052
053    @Test
054    public void testTagCommandTest()
055        throws Exception
056    {
057        String tag = getTagName();
058
059        @SuppressWarnings( "deprecation" ) TagScmResult tagResult =
060            getScmManager().getProviderByUrl( getScmUrl() ).tag( getScmRepository(), new ScmFileSet( getWorkingCopy() ),
061                                                                 tag );
062
063        assertResultIsSuccess( tagResult );
064
065        // see https://issues.apache.org/jira/browse/SCM-754
066        // assertEquals( "check all 4 files tagged", 4, tagResult.getTaggedFiles().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 ScmTag( tag ) );
096
097        assertResultIsSuccess( checkoutResult );
098
099        assertEquals( "check readme.txt contents is from tagged 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    }
117}