1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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 }