1 package org.apache.maven.plugin.assembly.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.plugin.assembly.format.AssemblyFormattingException;
23 import org.codehaus.plexus.logging.Logger;
24 import org.codehaus.plexus.logging.console.ConsoleLogger;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.PrintStream;
28 import java.util.ArrayList;
29 import java.util.Iterator;
30 import java.util.List;
31
32 import junit.framework.TestCase;
33
34 public class TypeConversionUtilsTest
35 extends TestCase
36 {
37
38 public void testModeToInt_InterpretAsOctalWithoutLeadingZero() throws AssemblyFormattingException
39 {
40 final int check = Integer.decode( "0777" )
41 .intValue();
42 final int test = TypeConversionUtils.modeToInt( "777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
43
44 assertEquals( check, test );
45 }
46
47 public void testModeToInt_InterpretValuesWithLeadingZeroAsOctal() throws AssemblyFormattingException
48 {
49 final int check = Integer.decode( "0777" )
50 .intValue();
51 final int test = TypeConversionUtils.modeToInt( "0777", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
52
53 assertEquals( check, test );
54 }
55
56 public void testModeToInt_FailOnInvalidOctalValue()
57 {
58 try
59 {
60 TypeConversionUtils.modeToInt( "493", new ConsoleLogger( Logger.LEVEL_DEBUG, "test" ) );
61
62 fail( "'493' is an invalid mode and should trigger an exception." );
63 }
64 catch ( final AssemblyFormattingException e )
65 {
66
67 }
68 }
69
70 public void testVerifyModeSanity_WarnOnNonsensicalOctalValue_002()
71 {
72 final List<String> messages = new ArrayList<String>( 2 );
73 messages.add( "World has write access, but user does not." );
74 messages.add( "World has write access, but group does not." );
75
76 checkFileModeSanity( "002", false, messages );
77 }
78
79 public void testVerifyModeSanity_WarnOnNonsensicalOctalValue_020()
80 {
81 final List<String> messages = new ArrayList<String>( 1 );
82 messages.add( "Group has write access, but user does not." );
83
84 checkFileModeSanity( "020", false, messages );
85 }
86
87 public void testVerifyModeSanity_ReturnTrueForValidOctalValue_775()
88 {
89 checkFileModeSanity( "775", true, null );
90 }
91
92 private void checkFileModeSanity( final String mode, final boolean isSane,
93 final List<String> messagesToCheckIfInsane )
94 {
95 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
96 final PrintStream ps = new PrintStream( baos );
97
98 final PrintStream oldOut = System.out;
99 final PrintStream oldErr = System.err;
100
101 try
102 {
103 System.setOut( ps );
104 System.setErr( ps );
105
106 assertEquals( "Mode sanity should be: " + isSane, isSane,
107 TypeConversionUtils.verifyModeSanity( Integer.parseInt( mode, 8 ),
108 new ConsoleLogger( Logger.LEVEL_WARN, "test" ) ) );
109 }
110 finally
111 {
112 System.setOut( oldOut );
113 System.setErr( oldErr );
114 }
115
116 if ( !isSane && messagesToCheckIfInsane != null && !messagesToCheckIfInsane.isEmpty() )
117 {
118 final String message = new String( baos.toByteArray() );
119
120 for ( final Iterator<String> it = messagesToCheckIfInsane.iterator(); it.hasNext(); )
121 {
122 final String checkMessage = it.next();
123
124 assertTrue( "\'" + checkMessage + "\' is not present in output.", message.indexOf( checkMessage ) > -1 );
125 }
126 }
127 }
128
129 }