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.provider.local.command.checkout;
020
021import java.io.File;
022import java.io.FileReader;
023import java.io.Reader;
024import java.util.List;
025
026import org.apache.maven.scm.ScmFile;
027import org.apache.maven.scm.command.checkout.CheckOutScmResult;
028import org.apache.maven.scm.provider.local.metadata.LocalScmMetadata;
029import org.apache.maven.scm.provider.local.metadata.io.xpp3.LocalScmMetadataXpp3Reader;
030import org.apache.maven.scm.tck.command.checkout.CheckOutCommandTckTest;
031import org.codehaus.plexus.util.FileUtils;
032import org.codehaus.plexus.util.IOUtil;
033import org.junit.Test;
034
035import static org.junit.Assert.assertEquals;
036import static org.junit.Assert.assertTrue;
037
038/**
039 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
040 *
041 */
042public class LocalCheckOutCommandTckTest extends CheckOutCommandTckTest {
043    private String module = "check-out";
044
045    public String getScmUrl() throws Exception {
046        return "scm:local|" + getRepositoryRoot().getAbsolutePath() + "|" + module;
047    }
048
049    public void initRepo() throws Exception {
050        File root = new File(getRepositoryRoot() + "/" + module);
051
052        makeFile(root, "/pom.xml");
053
054        makeFile(root, "/readme.txt");
055
056        makeFile(root, "/src/main/java/Application.java");
057
058        makeFile(root, "/src/test/java/Test.java");
059
060        makeDirectory(root, "/src/test/resources");
061    }
062
063    /**
064     * Tests that the metadata file .maven-scm-local is written correctly
065     */
066    @Test
067    public void testMetadata() throws Exception {
068        FileUtils.deleteDirectory(getWorkingCopy());
069
070        CheckOutScmResult result = checkOut(getWorkingCopy(), getScmRepository());
071
072        assertResultIsSuccess(result);
073
074        List<ScmFile> checkedOutFiles = result.getCheckedOutFiles();
075
076        assertEquals(4, checkedOutFiles.size());
077
078        // ----------------------------------------------------------------------
079        // Assert metadata file
080        // ----------------------------------------------------------------------
081        File metadataFile = new File(getWorkingCopy(), ".maven-scm-local");
082        assertTrue("Expected metadata file .maven-scm-local does not exist", metadataFile.exists());
083        Reader reader = new FileReader(metadataFile);
084        LocalScmMetadata metadata;
085        try {
086            metadata = new LocalScmMetadataXpp3Reader().read(reader);
087        } finally {
088            IOUtil.close(reader);
089        }
090        File root = new File(getRepositoryRoot() + "/" + module);
091        @SuppressWarnings("unchecked")
092        List<String> fileNames = FileUtils.getFileNames(root, "**", null, false);
093        assertEquals(fileNames, metadata.getRepositoryFileNames());
094    }
095}