1 package com.jayway.maven.plugins.android.phase05compile;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.fail;
5
6 import java.io.File;
7 import java.io.IOException;
8 import java.util.ArrayList;
9 import java.util.Arrays;
10 import java.util.Collections;
11 import java.util.List;
12
13 import org.junit.Test;
14
15
16
17
18
19 public class MakefileHelperTest
20 {
21
22 private static final String FOLDER_FOO = "foo";
23 private static final String FOLDER_FOOBAR = FOLDER_FOO + File.separator + "bar";
24 private static final String FOLDER_FOOBARBAZ = FOLDER_FOOBAR + File.separator + "baz";
25 private static final String QUXFILE = "qux.file";
26
27 private static final String FOOBARBAZ_QUXFILE = FOLDER_FOOBARBAZ + File.separator + QUXFILE;
28 private static final String FOOBAR_QUXFILE = FOLDER_FOOBAR + File.separator + QUXFILE;
29
30 private static final String FOLDER_FRED = "fred";
31 private static final String FOLDER_FREDBARNY = FOLDER_FRED + File.separator + "barny";
32
33 @Test
34 public void testSplitSimpleFolder()
35 {
36
37 String[] expected = {"foo", "bar", "baz"};
38 assertEquals( Arrays.asList( expected ),
39 MakefileHelper.splitPath( new File( File.separator, FOLDER_FOOBARBAZ ) ) );
40 }
41
42 @Test
43 public void testSplitAbsoluteFolder() throws IOException
44 {
45
46 List<String> expected = getSplit( new File ( File.separator ).getCanonicalFile() );
47 expected.add( "foo" );
48 expected.add( "bar" );
49 expected.add( "baz" );
50 assertEquals( expected,
51 MakefileHelper.splitPath( new File( File.separator, FOLDER_FOOBARBAZ ).getAbsoluteFile() ) );
52 }
53
54 @Test
55 public void testSplitSimpleFile()
56 {
57 String[] expected = {"qux.file"};
58 assertEquals( Arrays.asList( expected ),
59 MakefileHelper.splitPath( new File( File.separator, QUXFILE ) ) );
60 }
61
62 @Test
63 public void testSplitSimpleFolderAndFile()
64 {
65 String[] expected = {"foo", "bar", "baz", "qux.file"};
66 assertEquals( Arrays.asList( expected ),
67 MakefileHelper.splitPath( new File( File.separator, FOOBARBAZ_QUXFILE ) ) );
68 }
69
70 @Test
71 public void testSplitRelativeFolder()
72 {
73
74 String[] expected = {"foo", "bar", "baz"};
75 assertEquals( Arrays.asList( expected ),
76 MakefileHelper.splitPath( new File( FOLDER_FOOBARBAZ ) ) );
77 }
78
79 @Test
80 public void testSplitAbsoluteFolderFromCWD() throws IOException
81 {
82
83 List<String> expected = getCWDSplit();
84 expected.add( "foo" );
85 expected.add( "bar" );
86 assertEquals( expected,
87 MakefileHelper.splitPath( new File( FOLDER_FOOBAR ).getAbsoluteFile() ) );
88 }
89
90 @Test
91 public void testSplitRelativeFile()
92 {
93 String[] expected = {"qux.file"};
94 assertEquals( Arrays.asList( expected ),
95 MakefileHelper.splitPath( new File( QUXFILE ) ) );
96 }
97
98 @Test
99 public void testSplitRoot()
100 {
101 assertEquals( Collections.EMPTY_LIST, MakefileHelper.splitPath( new File( File.separator ) ) );
102 }
103
104 @Test
105 public void testResolveRelativePathSimpleRelative() throws IOException
106 {
107 File directory = new File( System.getProperty( "user.dir" ) );
108 File file = new File( System.getProperty( "user.dir" ), QUXFILE );
109
110 assertEquals( "qux.file", MakefileHelper.resolveRelativePath( directory, file ) );
111 }
112
113 @Test
114 public void testResolveRelativePathSimpleRelative2() throws IOException
115 {
116 File directory = new File( System.getProperty( "user.dir" ) );
117 File file = new File( System.getProperty( "user.dir" ), FOOBARBAZ_QUXFILE );
118
119 assertEquals( FOOBARBAZ_QUXFILE, MakefileHelper.resolveRelativePath( directory, file ) );
120 }
121
122 @Test
123 public void testResolveRelativePathParent() throws IOException
124 {
125 File directory = new File( File.separator + FOLDER_FOOBARBAZ );
126 File file = new File( File.separator + FOOBAR_QUXFILE );
127
128 assertEquals( ".." + File.separator + "qux.file",
129 MakefileHelper.resolveRelativePath( directory, file ) );
130 }
131
132 @Test
133 public void testResolveRelativePathNointersect() throws Exception
134 {
135 File directory = new File( File.separator + FOLDER_FREDBARNY );
136 File file = new File( File.separator + FOOBARBAZ_QUXFILE );
137 assertEquals( ".." + File.separator + ".." + File.separator + FOOBARBAZ_QUXFILE,
138 MakefileHelper.resolveRelativePath( directory, file ) );
139
140
141
142
143
144 if ( MakefileHelper.IS_WINDOWS )
145 {
146
147 boolean substY = setupWindowsDrive( "Y:" );
148 boolean substZ = setupWindowsDrive( "Z:" );
149
150 directory = new File( "Y:\\" + FOLDER_FREDBARNY );
151 file = new File( "Z:\\" + FOOBARBAZ_QUXFILE );
152
153 try
154 {
155 MakefileHelper.resolveRelativePath( directory, file );
156 fail( "Expected exception not thrown (relative paths cannot cross windows drives)" );
157 }
158 catch ( IOException ioex )
159 {
160 assertEquals( "Unable to resolve relative path across windows drives", ioex.getMessage() );
161 }
162 finally
163 {
164 if ( substY )
165 {
166 clearWindowsDrive( "Y:" );
167 }
168 if ( substZ )
169 {
170 clearWindowsDrive( "Z:" );
171 }
172 }
173 }
174
175 }
176
177 @Test
178 public void testResolveRelativePathNearRootParent() throws IOException
179 {
180 File directory = new File( File.separator + FOLDER_FOO );
181 File file = new File( File.separator + QUXFILE );
182
183 assertEquals( ".." + File.separator + "qux.file",
184 MakefileHelper.resolveRelativePath( directory, file ) );
185 }
186
187
188
189
190
191
192
193 private List<String> getSplit( File toSplit )
194 {
195 List<String> result = new ArrayList<String>();
196 if ( File.separator.equals( "\\" ) )
197 {
198 result.addAll( Arrays.asList( toSplit.toString().split( "\\\\" ) ) );
199 }
200 else
201 {
202 result.addAll( Arrays.asList( toSplit.toString().split( File.separator ) ) );
203
204
205 if ( ( result.size() > 0 )
206 && ( result.get( 0 ).length() == 0 ) )
207 {
208 result.remove( 0 );
209 }
210 }
211 return result;
212 }
213
214
215
216
217
218
219 private List<String> getCWDSplit() throws IOException
220 {
221 return getSplit( new File( "." ).getCanonicalFile() );
222 }
223
224 private boolean setupWindowsDrive( String drive ) throws IOException, InterruptedException
225 {
226 boolean result = false;
227 if ( ! new File( drive ).exists() )
228 {
229 Runtime.getRuntime().exec( "subst " + drive + " C:\\" ).waitFor();
230 result = true;
231 }
232 return result;
233 }
234
235 private void clearWindowsDrive( String drive ) throws IOException, InterruptedException
236 {
237 Runtime.getRuntime().exec( "subst " + drive + " /D" ).waitFor();
238 }
239 }