View Javadoc
1   /*
2    * The akquinet maven-latex-plugin project
3    *
4    * Copyright (c) 2011 by akquinet tech@spree GmbH
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package eu.simuline.m2latex.core;
20  
21  import eu.simuline.m2latex.mojo.MavenLogWrapper;
22  
23  import eu.simuline.m2latex.core.BuildExecutionException;
24  import eu.simuline.m2latex.core.BuildFailureException;
25  
26  import org.apache.maven.plugin.logging.SystemStreamLog;
27  
28  import java.io.File;
29  import java.io.IOException;
30  
31  import static org.junit.Assert.assertEquals;
32  
33  import org.junit.Test;
34  import org.junit.Ignore;
35  import org.junit.Before;
36  import org.junit.After;
37  
38  public class TexFileUtilsTest {
39      private final static File WORKING_DIR = 
40  	new File(System.getProperty("testResourcesDir"));
41  
42      // FIXME: occurs also in other testclasses: 
43      // to be unified. 
44      private static void cleanWorkingDir() {
45  	cleanDirRec(WORKING_DIR);
46      }
47  
48      // does not work for hidden directories 
49      private static void cleanDirRec(File dir) {
50  	assert dir.isDirectory() : "Expected directory. ";
51  	File[] files = dir.listFiles();
52  	assert files != null : "Directory is not readable. ";
53  	boolean proof;
54  	for (File file : files) {
55  	    assert file.exists();
56  	    if (file.isDirectory()) {
57  		assert !file.isHidden();
58  		cleanDirRec(file);
59  		assert file.listFiles().length == 0;
60  	    }
61  	    if (!file.isHidden()) {
62  		proof = file.delete();
63  		assert proof;
64  	    }
65  	}
66       }
67  
68      @Before public void setUp() throws IOException {
69  	cleanWorkingDir();
70      }
71  
72      @After public void tearDown() throws IOException {
73  	cleanWorkingDir();
74      }
75  
76  
77   
78      @Test public void testGetTargetDir() throws BuildFailureException {
79  
80          File expected = new File(WORKING_DIR, "dir2/subdir");
81  	TexFileUtils utils = 
82  	    new TexFileUtils(new MavenLogWrapper(new SystemStreamLog()));
83  	// may throw BuildFailureException 
84          File actual = utils
85  	    .getTargetDirectory(new File(WORKING_DIR, "dir1/subdir/file"),
86  				new File(WORKING_DIR, "dir1"),
87  				new File(WORKING_DIR, "dir2"));
88          assertEquals(expected, actual);
89      }
90  }