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 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 org.apache.commons.lang.StringUtils;
  18. import org.junit.rules.TestRule;
  19. import org.junit.runner.Description;
  20. import org.junit.runners.model.Statement;
  21. import java.io.IOException;
  22. import java.nio.file.FileSystems;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.nio.file.Paths;
  26. import java.util.concurrent.atomic.AtomicReference;
  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 Path d;
  33. private Description desc = Description.EMPTY;
  34. private AtomicReference<Path> projectBase = new AtomicReference<>( );
  35. private Path getProjectBase() {
  36. if (this.projectBase.get()==null) {
  37. String pathVal = System.getProperty("mvn.project.base.dir");
  38. Path baseDir;
  39. if ( StringUtils.isEmpty(pathVal)) {
  40. baseDir= Paths.get("").toAbsolutePath();
  41. } else {
  42. baseDir = Paths.get(pathVal).toAbsolutePath();
  43. }
  44. this.projectBase.compareAndSet(null, baseDir);
  45. }
  46. return this.projectBase.get();
  47. }
  48. public void before() throws IOException {
  49. // hard coded maven target file
  50. Path f1 = getProjectBase().resolve("target/archivarepo").resolve(ArchivaTemporaryFolderRule.resumepackage(desc.getClassName())).resolve(desc.getMethodName());
  51. d = Files.createDirectories( f1 );
  52. }
  53. public Path getRoot() {
  54. return d;
  55. }
  56. public void after() throws IOException {
  57. org.apache.archiva.common.utils.FileUtils.deleteDirectory(getRoot());
  58. }
  59. @Override
  60. public Statement apply(Statement base, Description description) {
  61. desc = description;
  62. return statement(base);
  63. }
  64. private Statement statement(final Statement base) {
  65. return new Statement() {
  66. @Override
  67. public void evaluate() throws Throwable {
  68. before();
  69. try {
  70. base.evaluate();
  71. } finally {
  72. after();
  73. }
  74. }
  75. };
  76. }
  77. /**
  78. * Return a filepath from FQN class name with only first char of package and classname
  79. * @param packagename
  80. * @return
  81. */
  82. public static String resumepackage(String packagename) {
  83. StringBuilder sb = new StringBuilder();
  84. String[] p = packagename.split("\\.");
  85. for (int i = 0; i < p.length - 2; i++)
  86. {
  87. sb.append(p[i].charAt(0)).append( FileSystems.getDefault( ).getSeparator());
  88. }
  89. sb.append(p[p.length - 1]);
  90. return sb.toString();
  91. }
  92. }