001package org.apache.maven.scm.provider.vss.commands;
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.ScmTestCase;
023
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.IOException;
027
028/**
029 * @author Allan Lang
030 *
031 */
032public class VssCommandLineUtilsTest
033    extends ScmTestCase
034{
035
036    private static final String VSS_DIRECTORY_PROPERTY = "vssDirectory";
037
038    //private ScmManager scmManager;
039
040    public void setUp()
041        throws Exception
042    {
043        super.setUp();
044
045        //scmManager = getScmManager();
046    }
047
048    public void testGetSettings()
049        throws IOException
050    {
051        /*
052         * If we have a genuine settings file, take a copy of this first Create
053         * the test settings file Check getSettings from file Delete the test
054         * settings file Check getSettings without file Check getSettings with
055         * system property Re-instate original settings file, if existing
056         *
057         */
058
059        final String vssInstallPath = "c:\\wherever";
060        final String vssInstallPathAlt = "c:\\somewhere";
061        final String settingsXml =
062            "<vss-settings><Settings><vssDirectory>" + vssInstallPath + "</vssDirectory></Settings></vss-settings>";
063        final String settingsFilename = "vss-settings.xml";
064        final String backupFilename = settingsFilename + ".backup";
065        boolean preExistingScmFolder = false;
066        boolean preExistingSettings = false;
067
068        /*
069         * Create a backup of the current settings file, if one exists
070         */
071        File scmUserHome = new File( getTestFile( "target/vssdir" ), ".scm" );
072        if ( scmUserHome.exists() )
073        {
074            preExistingScmFolder = true;
075            File settingsFile = new File( scmUserHome, settingsFilename );
076            if ( settingsFile.exists() )
077            {
078                preExistingSettings = true;
079                settingsFile.renameTo( new File( scmUserHome, backupFilename ) );
080            }
081        }
082        else
083        {
084            scmUserHome.mkdirs();
085        }
086
087        /*
088         * Create the test settings file
089         */
090        File testSettings = new File( scmUserHome, settingsFilename );
091        FileOutputStream fos = new FileOutputStream( testSettings );
092        fos.write( settingsXml.getBytes() );
093        fos.flush();
094        fos.close();
095        fos = null;
096
097        /*
098         * Validate that setting from settings file is returned correctly
099         */
100        VssCommandLineUtils.setScmConfDir( scmUserHome );
101        assertEquals( vssInstallPath, VssCommandLineUtils.getSettings().getVssDirectory() );
102
103        /*
104         * Validate that setting is overridden by system property
105         */
106        System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathAlt );
107        VssCommandLineUtils.setScmConfDir( new File( vssInstallPathAlt ) );
108        assertEquals( vssInstallPathAlt, VssCommandLineUtils.getSettings().getVssDirectory() );
109
110        /*
111         * Delete the test settings file
112         */
113        testSettings.delete();
114
115        /*
116         * Validate that setting is still equal to system property
117         */
118        assertEquals( vssInstallPathAlt, VssCommandLineUtils.getSettings().getVssDirectory() );
119
120        /*
121         * Re-instate the original settings file, if one existed
122         */
123        if ( preExistingSettings )
124        {
125            File backup = new File( scmUserHome, backupFilename );
126            backup.renameTo( new File( scmUserHome, settingsFilename ) );
127        }
128
129        if ( !preExistingScmFolder )
130        {
131            scmUserHome.delete();
132        }
133
134    }
135
136    public void testGetSsDir()
137    {
138        final String vssInstallPathWindowsStyle = "c:\\vss\\bin";
139        final String vssInstallPathUnixStyle = "c:/vss/bin";
140        final String targetValue = "c:/vss/bin/";
141
142        // Windows style test
143        System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathWindowsStyle );
144        VssCommandLineUtils.setScmConfDir( new File( vssInstallPathWindowsStyle ) );
145        assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
146
147        // Unix style test
148        System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathUnixStyle );
149        VssCommandLineUtils.setScmConfDir( new File( vssInstallPathUnixStyle ) );
150        assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
151
152        // Windows style with folder indicator
153        System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathWindowsStyle + "\\" );
154        VssCommandLineUtils.setScmConfDir( new File( vssInstallPathWindowsStyle ) );
155        assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
156
157        // Unix style with folder indicator
158        System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathUnixStyle + "/" );
159        VssCommandLineUtils.setScmConfDir( new File( vssInstallPathUnixStyle ) );
160        assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
161
162        // Unix style with Windows style folder indicator
163        System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathUnixStyle + "\\" );
164        VssCommandLineUtils.setScmConfDir( new File( vssInstallPathUnixStyle ) );
165        assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
166
167    }
168}