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  // mport eu.simuline.m2latex.core.BuildExecutionException;
24  // import eu.simuline.m2latex.core.BuildFailureException;
25  
26  import java.io.File;
27  import java.io.IOException;
28  
29  import static org.junit.Assert.assertEquals;
30  
31  import org.junit.Test;
32  // import org.junit.Ignore;
33  import org.junit.Before;
34  import org.junit.After;
35  
36  public class TexFileUtilsTest {
37  	private final static File WORKING_DIR =
38  			new File(System.getProperty("unitTestResourcesDir"));
39  	//private final static File KEEP_ME = 
40  	//new File(System.getProperty("keepMe"));
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
69  	public void setUp() throws IOException {
70  		cleanWorkingDir();
71  	}
72  
73  	@After
74  	public void tearDown() throws IOException {
75  		cleanWorkingDir();
76  	}
77  
78  	@Test
79  	public void testGetTargetDir() throws BuildFailureException {
80  
81  		File expected = new File(WORKING_DIR, "dir2/subdir");
82  		TexFileUtils utils =
83  			new TexFileUtils(new MavenLogWrapper(this.getClass()));
84  		// may throw BuildFailureException 
85  		File actual =
86  				utils.getTargetDirectory(new File(WORKING_DIR, "dir1/subdir/file"),
87  						new File(WORKING_DIR, "dir1"), new File(WORKING_DIR, "dir2"));
88  		assertEquals(expected, actual);
89  	}
90  }