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.hg;
020
021import org.apache.commons.lang3.StringUtils;
022import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
023import org.codehaus.plexus.util.cli.Commandline;
024import org.junit.Test;
025
026import static org.junit.Assert.assertEquals;
027import static org.junit.Assert.assertNull;
028
029public class HgUtilsTest {
030
031    @Test
032    public void testNullWorkingDirectory() throws Exception {
033        Commandline cmd = HgUtils.buildCmd(null, new String[] {});
034        assertNull(cmd.getWorkingDirectory());
035    }
036
037    @Test
038    public void testCryptPassword() throws Exception {
039        Commandline cmdHttps = HgUtils.buildCmd(
040                null, new String[] {HgCommandConstants.PUSH_CMD, null, "https://username:password@example.com/foobar"});
041        Commandline cmd = new Commandline(HgUtils.maskPassword(cmdHttps));
042
043        String[] shellArgs = cmd.getShell().getShellArgs();
044        // Watch it: Shell would return null, whereas BourneShell would return an empty array
045        if (shellArgs != null && shellArgs.length > 0) {
046            // [/C, hg push https://username:*****@example.com/foobar]
047            // [/X, /C, hg push https://username:*****@example.com/foobar]
048            assertEquals(
049                    "https://username:*****@example.com/foobar",
050                    StringUtils.split(cmd.getArguments()[shellArgs.length])[2]);
051        } else {
052            assertEquals("https://username:*****@example.com/foobar", cmd.getArguments()[3]);
053        }
054    }
055}