You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ArchivaTemporaryFolderRule.java 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. import java.io.File;
  18. import java.io.IOException;
  19. import java.nio.file.Files;
  20. import java.nio.file.Path;
  21. import org.apache.commons.io.FileUtils;
  22. import org.junit.rules.TestRule;
  23. import org.junit.runner.Description;
  24. import org.junit.runners.model.Statement;
  25. /**
  26. * Rule to help creating folder for repository based on testmethod name
  27. * @author Eric
  28. */
  29. public class ArchivaTemporaryFolderRule implements TestRule {
  30. private File d;
  31. private Description desc = Description.EMPTY;
  32. public void before() throws IOException {
  33. // hard coded maven target file
  34. File f1 = new File("target" + File.separator + "archivarepo" + File.separator + ArchivaTemporaryFolderRule.resumepackage(desc.getClassName()) + File.separator + desc.getMethodName());
  35. f1.mkdirs();
  36. Path p = Files.createDirectories(f1.toPath());
  37. d = p.toFile();
  38. }
  39. public File getRoot() {
  40. return d;
  41. }
  42. public void after() throws IOException {
  43. FileUtils.deleteDirectory(getRoot());
  44. }
  45. @Override
  46. public Statement apply(Statement base, Description description) {
  47. desc = description;
  48. return statement(base);
  49. }
  50. private Statement statement(final Statement base) {
  51. return new Statement() {
  52. @Override
  53. public void evaluate() throws Throwable {
  54. before();
  55. try {
  56. base.evaluate();
  57. } finally {
  58. after();
  59. }
  60. }
  61. };
  62. }
  63. /**
  64. * Return a filepath from FQN class name with only first char of package and classname
  65. * @param packagename
  66. * @return
  67. */
  68. public static String resumepackage(String packagename) {
  69. StringBuilder sb = new StringBuilder();
  70. String[] p = packagename.split("\\.");
  71. for (int i = 0; i < p.length - 2; i++)
  72. {
  73. sb.append(p[i].charAt(0)).append(File.separator);
  74. }
  75. sb.append(p[p.length - 1]);
  76. return sb.toString();
  77. }
  78. }