View Javadoc
1   /*
2    * Copyright 2014 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.archiva.webdav;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.nio.file.Files;
21  import java.nio.file.Path;
22  import org.apache.commons.io.FileUtils;
23  import org.junit.rules.TestRule;
24  import org.junit.runner.Description;
25  import org.junit.runners.model.Statement;
26  
27  /**
28   * Rule to help creating folder for repository based on testmethod name
29   * @author Eric
30   */
31  public class ArchivaTemporaryFolderRule implements TestRule {
32      private File d;
33      private Description desc = Description.EMPTY;
34  
35      public void before() throws IOException {
36          // hard coded maven target file
37          File f1 = new File("target" + File.separator + "archivarepo" + File.separator + ArchivaTemporaryFolderRule.resumepackage(desc.getClassName()) + File.separator + desc.getMethodName());
38          f1.mkdirs();
39          Path p = Files.createDirectories(f1.toPath());
40          d = p.toFile();
41      }
42  
43      public File getRoot() {
44          return d;
45      }
46  
47      public void after() throws IOException {
48          FileUtils.deleteDirectory(getRoot());
49      }
50  
51      @Override
52      public Statement apply(Statement base, Description description) {
53          desc = description;
54          return statement(base);
55      }
56  
57      private Statement statement(final Statement base) {
58          return new Statement() {
59              @Override
60              public void evaluate() throws Throwable {
61                  before();
62                  try {
63                      base.evaluate();
64                  } finally {
65                      after();
66                  }
67              }
68          };
69      }
70      /**
71       * Return a filepath from FQN class name with only first char of package and classname
72       * @param packagename
73       * @return 
74       */ 
75      public static String resumepackage(String packagename) {
76          StringBuilder sb = new StringBuilder();
77          String[] p = packagename.split("\\.");
78          for (int i = 0; i < p.length - 2; i++) 
79          {
80              sb.append(p[i].charAt(0)).append(File.separator);
81          }
82          sb.append(p[p.length - 1]);
83          return sb.toString();
84      }
85      
86  }