View Javadoc
1   package org.apache.maven.scm.provider.vss.commands;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmTestCase;
23  
24  import java.io.File;
25  import java.io.FileOutputStream;
26  import java.io.IOException;
27  
28  /**
29   * @author Allan Lang
30   *
31   */
32  public class VssCommandLineUtilsTest
33      extends ScmTestCase
34  {
35  
36      private static final String VSS_DIRECTORY_PROPERTY = "vssDirectory";
37  
38      //private ScmManager scmManager;
39  
40      public void setUp()
41          throws Exception
42      {
43          super.setUp();
44          System.setProperty( VSS_DIRECTORY_PROPERTY, "" );
45  
46          //scmManager = getScmManager();
47      }
48  
49      public void testGetSettings()
50          throws IOException
51      {
52          /*
53           * If we have a genuine settings file, take a copy of this first Create
54           * the test settings file Check getSettings from file Delete the test
55           * settings file Check getSettings without file Check getSettings with
56           * system property Re-instate original settings file, if existing
57           *
58           */
59  
60          final String vssInstallPath = "c:\\wherever";
61          final String vssInstallPathAlt = "c:\\somewhere";
62          final String settingsXml =
63              "<vss-settings><vssDirectory>" + vssInstallPath + "</vssDirectory></vss-settings>";
64          final String settingsFilename = "vss-settings.xml";
65          final String backupFilename = settingsFilename + ".backup";
66          boolean preExistingScmFolder = false;
67          boolean preExistingSettings = false;
68  
69          /*
70           * Create a backup of the current settings file, if one exists
71           */
72          File scmUserHome = new File( getTestFile( "target/vssdir" ), ".scm" );
73          if ( scmUserHome.exists() )
74          {
75              preExistingScmFolder = true;
76              File settingsFile = new File( scmUserHome, settingsFilename );
77              if ( settingsFile.exists() )
78              {
79                  preExistingSettings = true;
80                  settingsFile.renameTo( new File( scmUserHome, backupFilename ) );
81              }
82          }
83          else
84          {
85              scmUserHome.mkdirs();
86          }
87  
88          /*
89           * Create the test settings file
90           */
91          File testSettings = new File( scmUserHome, settingsFilename );
92          FileOutputStream fos = new FileOutputStream( testSettings );
93          fos.write( settingsXml.getBytes() );
94          fos.flush();
95          fos.close();
96          fos = null;
97  
98          /*
99           * Validate that setting from settings file is returned correctly
100          */
101         VssCommandLineUtils.setScmConfDir( scmUserHome );
102         assertEquals( vssInstallPath, VssCommandLineUtils.getSettings().getVssDirectory() );
103 
104         /*
105          * Validate that setting is overridden by system property
106          */
107         System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathAlt );
108         VssCommandLineUtils.setScmConfDir( new File( vssInstallPathAlt ) );
109         assertEquals( vssInstallPathAlt, VssCommandLineUtils.getSettings().getVssDirectory() );
110 
111         /*
112          * Delete the test settings file
113          */
114         testSettings.delete();
115 
116         /*
117          * Validate that setting is still equal to system property
118          */
119         assertEquals( vssInstallPathAlt, VssCommandLineUtils.getSettings().getVssDirectory() );
120 
121         /*
122          * Re-instate the original settings file, if one existed
123          */
124         if ( preExistingSettings )
125         {
126             File backup = new File( scmUserHome, backupFilename );
127             backup.renameTo( new File( scmUserHome, settingsFilename ) );
128         }
129 
130         if ( !preExistingScmFolder )
131         {
132             scmUserHome.delete();
133         }
134 
135     }
136 
137     public void testGetSsDir()
138     {
139         final String vssInstallPathWindowsStyle = "c:\\vss\\bin";
140         final String vssInstallPathUnixStyle = "c:/vss/bin";
141         final String targetValue = "c:/vss/bin/";
142 
143         // Windows style test
144         System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathWindowsStyle );
145         VssCommandLineUtils.setScmConfDir( new File( vssInstallPathWindowsStyle ) );
146         assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
147 
148         // Unix style test
149         System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathUnixStyle );
150         VssCommandLineUtils.setScmConfDir( new File( vssInstallPathUnixStyle ) );
151         assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
152 
153         // Windows style with folder indicator
154         System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathWindowsStyle + "\\" );
155         VssCommandLineUtils.setScmConfDir( new File( vssInstallPathWindowsStyle ) );
156         assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
157 
158         // Unix style with folder indicator
159         System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathUnixStyle + "/" );
160         VssCommandLineUtils.setScmConfDir( new File( vssInstallPathUnixStyle ) );
161         assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
162 
163         // Unix style with Windows style folder indicator
164         System.setProperty( VSS_DIRECTORY_PROPERTY, vssInstallPathUnixStyle + "\\" );
165         VssCommandLineUtils.setScmConfDir( new File( vssInstallPathUnixStyle ) );
166         assertEquals( targetValue, VssCommandLineUtils.getSsDir() );
167 
168     }
169 }