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.checkout;
020
021import java.util.Iterator;
022import java.util.List;
023import java.util.Optional;
024import java.util.SortedSet;
025import java.util.TreeSet;
026
027import org.apache.maven.scm.ScmFile;
028import org.apache.maven.scm.ScmTckTestCase;
029import org.apache.maven.scm.command.checkout.CheckOutScmResult;
030import org.apache.maven.scm.provider.ScmProvider;
031import org.apache.maven.scm.repository.ScmRepository;
032import org.apache.maven.scm.repository.UnknownRepositoryStructure;
033import org.junit.Test;
034
035import static org.junit.Assert.assertTrue;
036import static org.junit.Assert.fail;
037import static org.junit.Assume.assumeTrue;
038
039/**
040 * This test tests the check out command.
041 *
042 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
043 */
044public abstract class CheckOutCommandTckTest extends ScmTckTestCase {
045    @Test
046    public void testCheckOutCommandTest() throws Exception {
047        deleteDirectory(getWorkingCopy());
048
049        CheckOutScmResult result = checkOut(getWorkingCopy(), getScmRepository());
050
051        assertResultIsSuccess(result);
052
053        List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
054
055        if (checkedOutFiles.size() != 4) {
056            SortedSet<ScmFile> files = new TreeSet<>(checkedOutFiles);
057
058            int i = 0;
059
060            for (Iterator<ScmFile> it = files.iterator(); it.hasNext(); i++) {
061                ScmFile scmFile = it.next();
062
063                System.out.println(i + ": " + scmFile);
064            }
065
066            fail("Expected 4 files in the updated files list, was " + checkedOutFiles.size());
067        }
068    }
069
070    @Test
071    public void testMakeProviderScmRepositoryFromCheckoutDirectory() throws Exception {
072        assumeTrue(isMakeProviderScmRepositoryFromDirectorySupportedByProvider());
073        CheckOutScmResult result = checkOut(getWorkingCopy(), getScmRepository());
074        assertResultIsSuccess(result);
075        Optional<ScmRepository> repository = getScmManager().makeProviderScmRepository(getWorkingCopy());
076        assertTrue("Could not detect SCM repository for working copy at " + getWorkingCopy(), repository.isPresent());
077    }
078
079    private boolean isMakeProviderScmRepositoryFromDirectorySupportedByProvider() throws Exception {
080        ScmProvider provider = getScmManager().getProviderByUrl(getScmUrl());
081        try {
082            provider.makeProviderScmRepository(getWorkingCopy());
083        } catch (UnknownRepositoryStructure e) {
084            // in this case the provider does not support this operation
085            return false;
086        }
087        return true;
088    }
089}