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