View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugins.assembly.utils;
20  
21  import java.util.ArrayList;
22  import java.util.List;
23  
24  import org.apache.maven.plugins.assembly.format.AssemblyFormattingException;
25  import org.junit.Test;
26  import org.mockito.ArgumentCaptor;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertTrue;
32  import static org.junit.Assert.fail;
33  import static org.mockito.Mockito.mock;
34  import static org.mockito.Mockito.verify;
35  
36  public class TypeConversionUtilsTest {
37      private final Logger logger = LoggerFactory.getLogger(getClass());
38  
39      @Test
40      public void testModeToInt_InterpretAsOctalWithoutLeadingZero() throws AssemblyFormattingException {
41          final int check = Integer.decode("0777");
42          final int test = TypeConversionUtils.modeToInt("777", logger);
43  
44          assertEquals(check, test);
45      }
46  
47      @Test
48      public void testModeToInt_InterpretValuesWithLeadingZeroAsOctal() throws AssemblyFormattingException {
49          final int check = Integer.decode("0777");
50          final int test = TypeConversionUtils.modeToInt("0777", logger);
51  
52          assertEquals(check, test);
53      }
54  
55      @Test
56      public void testModeToInt_FailOnInvalidOctalValue() {
57          try {
58              TypeConversionUtils.modeToInt("493", logger);
59  
60              fail("'493' is an invalid mode and should trigger an exception.");
61          } catch (final AssemblyFormattingException e) {
62              // expected.
63          }
64      }
65  
66      @Test
67      public void testVerifyModeSanity_WarnOnNonsensicalOctalValue_002() {
68          final List<String> messages = new ArrayList<>(2);
69          messages.add("World has write access, but user does not.");
70          messages.add("World has write access, but group does not.");
71  
72          checkFileModeSanity("002", false, messages);
73      }
74  
75      @Test
76      public void testVerifyModeSanity_WarnOnNonsensicalOctalValue_020() {
77          final List<String> messages = new ArrayList<>(1);
78          messages.add("Group has write access, but user does not.");
79  
80          checkFileModeSanity("020", false, messages);
81      }
82  
83      @Test
84      public void testVerifyModeSanity_ReturnTrueForValidOctalValue_775() {
85          checkFileModeSanity("775", true, null);
86      }
87  
88      private void checkFileModeSanity(
89              final String mode, final boolean isSane, final List<String> messagesToCheckIfInsane) {
90          Logger logger = mock(Logger.class);
91          assertEquals(
92                  "Mode sanity should be: " + isSane,
93                  isSane,
94                  TypeConversionUtils.verifyModeSanity(Integer.parseInt(mode, 8), logger));
95  
96          if (!isSane && messagesToCheckIfInsane != null && !messagesToCheckIfInsane.isEmpty()) {
97              ArgumentCaptor<String> warnings = ArgumentCaptor.forClass(String.class);
98              verify(logger).warn(warnings.capture());
99              System.out.println(warnings.getAllValues());
100             final String message = warnings.getAllValues().toString();
101 
102             for (final String checkMessage : messagesToCheckIfInsane) {
103                 assertTrue("\'" + checkMessage + "\' is not present in output.", message.contains(checkMessage));
104             }
105         }
106     }
107 }