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