1 package org.apache.maven.plugins.assembly.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 }