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  
45          //scmManager = getScmManager();
46      }
47  
48      public void testGetSettings()
49          throws IOException
50      {
51          /*
52           * If we have a genuine settings file, take a copy of this first Create
53           * the test settings file Check getSettings from file Delete the test
54           * settings file Check getSettings without file Check getSettings with
55           * system property Re-instate original settings file, if existing
56           *
57           */
58  
59          final String vssInstallPath = "c:\\wherever";
60          final String vssInstallPathAlt = "c:\\somewhere";
61          final String settingsXml =
62              "<vss-settings><Settings><vssDirectory>" + vssInstallPath + "</vssDirectory></Settings></vss-settings>";
63          final String settingsFilename = "vss-settings.xml";
64          final String backupFilename = settingsFilename + ".backup";
65          boolean preExistingScmFolder = false;
66          boolean preExistingSettings = false;
67  
68          /*
69           * Create a backup of the current settings file, if one exists
70           */
71          File scmUserHome = new File( getTestFile( "target/vssdir" ), ".scm" );
72          if ( scmUserHome.exists() )
73          {
74              preExistingScmFolder = true;
75              File settingsFile = new File( scmUserHome, settingsFilename );
76              if ( settingsFile.exists() )
77              {
78                  preExistingSettings = true;
79                  settingsFile.renameTo( new File( scmUserHome, backupFilename ) );
80              }
81          }
82          else
83          {
84              scmUserHome.mkdirs();
85          }
86  
87          /*
88           * Create the test settings file
89           */
90          File testSettings = new File( scmUserHome, settingsFilename );
91          FileOutputStream fos = new FileOutputStream( testSettings );
92          fos.write( settingsXml.getBytes() );
93          fos.flush();
94          fos.close();
95          fos = null;
96  
97          /*
98           * Validate that setting from settings file is returned correctly
99           */
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 }