View Javadoc
1   package org.codehaus.plexus.util.cli;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import junit.framework.TestCase;
20  import org.codehaus.plexus.util.Os;
21  
22  import java.util.Arrays;
23  import java.util.Locale;
24  import java.util.Properties;
25  
26  @SuppressWarnings( { "JavaDoc", "deprecation" } )
27  public class CommandLineUtilsTest
28      extends TestCase
29  {
30  
31      public void testQuoteArguments()
32      {
33          try
34          {
35              String result = CommandLineUtils.quote( "Hello" );
36              System.out.println( result );
37              assertEquals( "Hello", result );
38              result = CommandLineUtils.quote( "Hello World" );
39              System.out.println( result );
40              assertEquals( "\"Hello World\"", result );
41              result = CommandLineUtils.quote( "\"Hello World\"" );
42              System.out.println( result );
43              assertEquals( "\'\"Hello World\"\'", result );
44          }
45          catch ( Exception e )
46          {
47              fail( e.getMessage() );
48          }
49          try
50          {
51              CommandLineUtils.quote( "\"Hello \'World\'\'" );
52              fail();
53          }
54          catch ( Exception e )
55          {
56          }
57      }
58  
59      /**
60       * Tests that case-insensitive environment variables are normalized to upper case.
61       */
62      public void testGetSystemEnvVarsCaseInsensitive()
63          throws Exception
64      {
65          Properties vars = CommandLineUtils.getSystemEnvVars( false );
66          for ( Object o : vars.keySet() )
67          {
68              String variable = (String) o;
69              assertEquals( variable.toUpperCase( Locale.ENGLISH ), variable );
70          }
71      }
72  
73      /**
74       * Tests that environment variables on Windows are normalized to upper case. Does nothing on Unix platforms.
75       */
76      public void testGetSystemEnvVarsWindows()
77          throws Exception
78      {
79          if ( !Os.isFamily( Os.FAMILY_WINDOWS ) )
80          {
81              return;
82          }
83          Properties vars = CommandLineUtils.getSystemEnvVars();
84          for ( Object o : vars.keySet() )
85          {
86              String variable = (String) o;
87              assertEquals( variable.toUpperCase( Locale.ENGLISH ), variable );
88          }
89      }
90  
91      /**
92       * Tests the splitting of a command line into distinct arguments.
93       */
94      public void testTranslateCommandline()
95          throws Exception
96      {
97          assertCmdLineArgs( new String[] {}, null );
98          assertCmdLineArgs( new String[] {}, "" );
99  
100         assertCmdLineArgs( new String[] { "foo", "bar" }, "foo bar" );
101         assertCmdLineArgs( new String[] { "foo", "bar" }, "   foo   bar   " );
102 
103         assertCmdLineArgs( new String[] { "foo", " double quotes ", "bar" }, "foo \" double quotes \" bar" );
104         assertCmdLineArgs( new String[] { "foo", " single quotes ", "bar" }, "foo ' single quotes ' bar" );
105 
106         assertCmdLineArgs( new String[] { "foo", " \" ", "bar" }, "foo ' \" ' bar" );
107         assertCmdLineArgs( new String[] { "foo", " ' ", "bar" }, "foo \" ' \" bar" );
108     }
109 
110     private void assertCmdLineArgs( String[] expected, String cmdLine )
111         throws Exception
112     {
113         String[] actual = CommandLineUtils.translateCommandline( cmdLine );
114         assertNotNull( actual );
115         assertEquals( expected.length, actual.length );
116         assertEquals( Arrays.asList( expected ), Arrays.asList( actual ) );
117     }
118 
119 }