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.svn.svnexe.command.update;
020
021import java.io.File;
022
023import org.apache.maven.scm.ScmBranch;
024import org.apache.maven.scm.ScmRevision;
025import org.apache.maven.scm.ScmTag;
026import org.apache.maven.scm.ScmTestCase;
027import org.apache.maven.scm.ScmVersion;
028import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
029import org.apache.maven.scm.provider.svn.util.SvnUtil;
030import org.apache.maven.scm.repository.ScmRepository;
031import org.codehaus.plexus.util.Os;
032import org.codehaus.plexus.util.cli.Commandline;
033import org.junit.Test;
034
035import static org.junit.Assert.assertTrue;
036
037/**
038 * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
039 *
040 */
041public class SvnUpdateCommandTest extends ScmTestCase {
042    @Test
043    public void testCommandLineWithEmptyTag() throws Exception {
044        testCommandLine(
045                "scm:svn:http://foo.com/svn/trunk",
046                new ScmTag(""),
047                "svn --non-interactive update " + getUpdateTestFile().getAbsolutePath() + "@");
048    }
049
050    @Test
051    public void testCommandLineWithEmptyBranch() throws Exception {
052        testCommandLine(
053                "scm:svn:http://foo.com/svn/trunk",
054                new ScmBranch(""),
055                "svn --non-interactive update " + getUpdateTestFile().getAbsolutePath() + "@");
056    }
057
058    @Test
059    public void testCommandLineWithEmptyVersion() throws Exception {
060        testCommandLine(
061                "scm:svn:http://foo.com/svn/trunk",
062                new ScmRevision(""),
063                "svn --non-interactive update " + getUpdateTestFile().getAbsolutePath() + "@");
064    }
065
066    @Test
067    public void testCommandLineWithWhitespaceTag() throws Exception {
068        testCommandLine(
069                "scm:svn:http://foo.com/svn/trunk",
070                new ScmTag("  "),
071                "svn --non-interactive update " + getUpdateTestFile().getAbsolutePath() + "@");
072    }
073
074    @Test
075    public void testCommandLineWithWhitespaceBranch() throws Exception {
076        testCommandLine(
077                "scm:svn:http://foo.com/svn/trunk",
078                new ScmBranch("  "),
079                "svn --non-interactive update " + getUpdateTestFile().getAbsolutePath() + "@");
080    }
081
082    @Test
083    public void testCommandLineWithWhitespaceRevision() throws Exception {
084        testCommandLine(
085                "scm:svn:http://foo.com/svn/trunk",
086                new ScmRevision("  "),
087                "svn --non-interactive update " + getUpdateTestFile().getAbsolutePath() + "@");
088    }
089
090    @Test
091    public void testCommandLineWithoutTag() throws Exception {
092        testCommandLine(
093                "scm:svn:http://foo.com/svn/trunk",
094                null,
095                "svn --non-interactive update " + getUpdateTestFile().getAbsolutePath() + "@");
096    }
097
098    @Test
099    public void testCommandLineTag() throws Exception {
100        testCommandLine(
101                "scm:svn:http://anonymous@foo.com/svn/trunk",
102                new ScmRevision("10"),
103                "svn --username anonymous --no-auth-cache --non-interactive update -r 10 "
104                        + getUpdateTestFile().getAbsolutePath() + "@");
105    }
106
107    @Test
108    public void testCommandLineWithUsernameAndTag() throws Exception {
109        testCommandLine(
110                "scm:svn:http://anonymous@foo.com/svn/trunk",
111                new ScmRevision("10"),
112                "svn --username anonymous --no-auth-cache --non-interactive update -r 10 "
113                        + getUpdateTestFile().getAbsolutePath() + "@");
114    }
115
116    @Test
117    public void testCommandLineWithCygwinProperty() throws Exception {
118        if (Os.isFamily(Os.FAMILY_WINDOWS)) {
119            SvnUtil.setSettingsDirectory(getTestFile("src/test/resources/svn/update/cygwin"));
120            try {
121                assertTrue(SvnUtil.getSettings().isUseCygwinPath());
122                testCommandLine(
123                        "scm:svn:http://foo.com/svn/trunk",
124                        null,
125                        "svn --non-interactive update /mnt/c/my_working_directory@",
126                        new File("c:\\my_working_directory"));
127            } finally {
128                SvnUtil.setSettingsDirectory(SvnUtil.DEFAULT_SETTINGS_DIRECTORY);
129            }
130        }
131    }
132
133    @Test
134    public void testCommandLineWithRelativeURLTag() throws Exception {
135        testCommandLine(
136                "scm:svn:http://foo.com/svn/trunk",
137                new ScmBranch("branches/my-test-branch"),
138                "svn --non-interactive switch http://foo.com/svn/branches/my-test-branch@ "
139                        + getUpdateTestFile().getAbsolutePath() + "@");
140    }
141
142    @Test
143    public void testCommandLineWithAbsoluteURLTag() throws Exception {
144        testCommandLine(
145                "scm:svn:http://foo.com/svn/trunk",
146                new ScmBranch("http://foo.com/svn/branches/my-test-branch"),
147                "svn --non-interactive switch http://foo.com/svn/branches/my-test-branch@ "
148                        + getUpdateTestFile().getAbsolutePath() + "@");
149    }
150
151    @Test
152    public void testCommandLineWithNonDeterminantBase() throws Exception {
153        testCommandLine(
154                "scm:svn:http://foo.com/svn/some-project",
155                new ScmBranch("branches/my-test-branch"),
156                "svn --non-interactive switch http://foo.com/svn/some-project/branches/my-test-branch@ "
157                        + getUpdateTestFile().getAbsolutePath() + "@");
158    }
159
160    @Test
161    public void testCommandLineWithNonDeterminantBaseTrailingSlash() throws Exception {
162        testCommandLine(
163                "scm:svn:http://foo.com/svn/some-project/",
164                new ScmBranch("branches/my-test-branch"),
165                "svn --non-interactive switch http://foo.com/svn/some-project/branches/my-test-branch@ "
166                        + getUpdateTestFile().getAbsolutePath() + "@");
167    }
168
169    @Test
170    public void testCommandLineWithBranchSameAsBase() throws Exception {
171        testCommandLine(
172                "scm:svn:http://foo.com/svn/tags/my-tag",
173                new ScmTag("tags/my-tag"),
174                "svn --non-interactive switch http://foo.com/svn/tags/my-tag@ "
175                        + getUpdateTestFile().getAbsolutePath() + "@");
176    }
177
178    // ----------------------------------------------------------------------
179    //
180    // ----------------------------------------------------------------------
181
182    private File getUpdateTestFile() {
183        return getTestFile("target/svn-update-command-test");
184    }
185
186    private SvnScmProviderRepository getSvnRepository(String scmUrl) throws Exception {
187        ScmRepository repository = getScmManager().makeScmRepository(scmUrl);
188
189        return (SvnScmProviderRepository) repository.getProviderRepository();
190    }
191
192    private void testCommandLine(String scmUrl, ScmVersion version, String commandLine) throws Exception {
193        File workingDirectory = getUpdateTestFile();
194
195        testCommandLine(scmUrl, version, commandLine, workingDirectory);
196    }
197
198    private void testCommandLine(String scmUrl, ScmVersion version, String commandLine, File workingDirectory)
199            throws Exception {
200        Commandline cl = SvnUpdateCommand.createCommandLine(getSvnRepository(scmUrl), workingDirectory, version);
201
202        assertCommandLine(commandLine, workingDirectory, cl);
203    }
204}