View Javadoc

1   /*
2    * #%L
3    * xcode-maven-plugin
4    * %%
5    * Copyright (C) 2012 SAP AG
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.sap.prd.mobile.ios.mios;
21  
22  import static org.junit.Assert.*;
23  
24  import java.io.ByteArrayOutputStream;
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.PrintStream;
28  
29  import junit.framework.Assert;
30  
31  import org.apache.commons.io.IOUtils;
32  import org.junit.Rule;
33  import org.junit.Test;
34  import org.junit.rules.TemporaryFolder;
35  
36  public class FileUtilsTest
37  {
38  
39    @Rule
40    public TemporaryFolder tmpFolder = new TemporaryFolder();
41  
42    @Test
43    public void testGetDelta() throws Exception
44    {
45  
46      Assert.assertEquals("source/dir",
47            FileUtils.getDelta(new File("/home/abc/def"), new File("/home/abc/def/source/dir")));
48    }
49  
50    @Test(expected = IllegalStateException.class)
51    public void testNoCommonPath() throws Exception
52    {
53      FileUtils.getDelta(new File("/home/abc"), new File("/home/def"));
54    }
55  
56    @Test
57    public void testIsChild() throws Exception
58    {
59      assertTrue(FileUtils.isChild(new File("/abc/def"), new File("/abc/def/ghi")));
60    }
61  
62    public void testIsNotAChild() throws Exception
63    {
64      assertFalse(FileUtils.isChild(new File("/abc/def"), new File("/abc/ghi/def")));
65    }
66  
67    @Test
68    public void testCreateSymbolicLink() throws Exception
69    {
70      File source = prepareFile();
71      File target = tmpFolder.newFile("target");
72  
73      FileUtils.createSymbolicLink(source, target);
74  
75      Assert.assertTrue(checkForSymbolicLink(target));
76    }
77  
78    @Test
79    public void testIsSymbolicLinkForNullFile() throws Exception
80    {
81      assertFalse(FileUtils.isSymbolicLink(null));
82    }
83  
84    @Test
85    public void testIsSymbolicLinkWithSymbolicLink() throws Exception
86    {
87      File source = prepareFile();
88      File target = tmpFolder.newFile("target");
89      FileUtils.createSymbolicLink(source, target);
90      assertTrue(FileUtils.isSymbolicLink(target));
91    }
92  
93    @Test
94    public void testIsSymbolicLinkWithRealFile() throws Exception
95    {
96      File file = prepareFile();
97      assertFalse(FileUtils.isSymbolicLink(file));
98    }
99  
100   @Test
101   public void testAddLeadingSlash()
102   {
103     assertEquals("/abc/def", FileUtils.ensureLeadingSlash("abc/def"));
104   }
105 
106   @Test
107   public void testNoDoubleLeadingSlash()
108   {
109     assertEquals("/abc/def", FileUtils.ensureLeadingSlash("/abc/def"));
110   }
111 
112   @Test(expected = NullPointerException.class)
113   public void testNullPointer()
114   {
115     FileUtils.ensureLeadingSlash(null);
116   }
117 
118   @Test
119   public void testFileAppendix()
120   {
121     assertEquals("zip", FileUtils.getAppendix(new File("MyFile.zip")));
122   }
123 
124   @Test
125   public void testNoFileAppendix()
126   {
127     assertNull(FileUtils.getAppendix(new File("MyFile")));
128   }
129 
130   private static boolean checkForSymbolicLink(final File f) throws IOException
131   {
132 
133     ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
134     PrintStream stream = new PrintStream(byteOs);
135 
136     try {
137       Forker.forkProcess(stream, null, "ls", "-l", f.getAbsolutePath());
138       stream.flush();
139       return byteOs.toString().startsWith("l");
140     }
141     finally {
142       IOUtils.closeQuietly(stream);
143     }
144   }
145 
146   private File prepareFile() throws IOException
147   {
148     File file = tmpFolder.newFile("source");
149     org.apache.commons.io.FileUtils.writeStringToFile(file, "abc");
150     return file;
151   }
152 }