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 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
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 }