View Javadoc

1   package org.apache.maven.plugin.assembly.utils;
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.plugin.assembly.format.AssemblyFormattingException;
23  import org.codehaus.plexus.logging.Logger;
24  import org.codehaus.plexus.logging.console.ConsoleLogger;
25  
26  import java.io.ByteArrayOutputStream;
27  import java.io.PrintStream;
28  import java.util.ArrayList;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  import junit.framework.TestCase;
33  
34  public class TypeConversionUtilsTest
35      extends TestCase
36  {
37  
38      public void testModeToInt_InterpretAsOctalWithoutLeadingZero() throws AssemblyFormattingException
39      {
40          final int check = Integer.decode( "0777" )
41                                   .intValue();
42          final int test = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
43  
44          assertEquals( check, test );
45      }
46  
47      public void testModeToInt_InterpretValuesWithLeadingZeroAsOctal() throws AssemblyFormattingException
48      {
49          final int check = Integer.decode( "0777" )
50                                   .intValue();
51          final int test = TypeConversionUtils.modeToInt( "0777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
52  
53          assertEquals( check, test );
54      }
55  
56      public void testModeToInt_FailOnInvalidOctalValue()
57      {
58          try
59          {
60              TypeConversionUtils.modeToInt( "493", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
61  
62              fail( "'493' is an invalid mode and should trigger an exception." );
63          }
64          catch ( final AssemblyFormattingException e )
65          {
66              // expected.
67          }
68      }
69  
70      public void testVerifyModeSanity_WarnOnNonsensicalOctalValue_002()
71      {
72          final List<String> messages = new ArrayList<String>( 2 );
73          messages.add( "World has write access, but user does not." );
74          messages.add( "World has write access, but group does not." );
75  
76          checkFileModeSanity( "002", false, messages );
77      }
78  
79      public void testVerifyModeSanity_WarnOnNonsensicalOctalValue_020()
80      {
81          final List<String> messages = new ArrayList<String>( 1 );
82          messages.add( "Group has write access, but user does not." );
83  
84          checkFileModeSanity( "020", false, messages );
85      }
86  
87      public void testVerifyModeSanity_ReturnTrueForValidOctalValue_775()
88      {
89          checkFileModeSanity( "775", true, null );
90      }
91  
92      private void checkFileModeSanity( final String mode, final boolean isSane,
93                                        final List<String> messagesToCheckIfInsane )
94      {
95          final ByteArrayOutputStream baos = new ByteArrayOutputStream();
96          final PrintStream ps = new PrintStream( baos );
97  
98          final PrintStream oldOut = System.out;
99          final PrintStream oldErr = System.err;
100 
101         try
102         {
103             System.setOut( ps );
104             System.setErr( ps );
105 
106             assertEquals( "Mode sanity should be: " + isSane, isSane,
107                           TypeConversionUtils.verifyModeSanity( Integer.parseInt( mode, 8 ),
108                                                                 new ConsoleLogger( Logger.LEVEL_WARN, "test" ) ) );
109         }
110         finally
111         {
112             System.setOut( oldOut );
113             System.setErr( oldErr );
114         }
115 
116         if ( !isSane && messagesToCheckIfInsane != null && !messagesToCheckIfInsane.isEmpty() )
117         {
118             final String message = new String( baos.toByteArray() );
119 
120             for ( final Iterator<String> it = messagesToCheckIfInsane.iterator(); it.hasNext(); )
121             {
122                 final String checkMessage = it.next();
123 
124                 assertTrue( "\'" + checkMessage + "\' is not present in output.", message.indexOf( checkMessage ) > -1 );
125             }
126         }
127     }
128 
129 }