DirectoryResolverResult.java

  1. package com.soebes.itf.jupiter.extension;

  2. /*
  3.  * Licensed to the Apache Software Foundation (ASF) under one
  4.  * or more contributor license agreements.  See the NOTICE file
  5.  * distributed with this work for additional information
  6.  * regarding copyright ownership.  The ASF licenses this file
  7.  * to you under the Apache License, Version 2.0 (the
  8.  * "License"); you may not use this file except in compliance
  9.  * with the License.  You may obtain a copy of the License at
  10.  *
  11.  *  http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing,
  14.  * software distributed under the License is distributed on an
  15.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16.  * KIND, either express or implied.  See the License for the
  17.  * specific language governing permissions and limitations
  18.  * under the License.
  19.  */

  20. import org.junit.jupiter.api.extension.ExtensionContext;

  21. import java.lang.reflect.Method;
  22. import java.nio.file.Path;
  23. import java.util.Optional;

  24. /**
  25.  * The source structure looks usually like this:
  26.  * <pre>
  27.  *   src/test/java/../
  28.  *      +--- FirstIT.java
  29.  *             +--- test_case_one
  30.  *   src/test/resources-its/.../
  31.  *      +--- FirstIT/
  32.  *              +--- test_case_one
  33.  *                      +--- src/...
  34.  *                      +--- pom.xml
  35.  * </pre>
  36.  * during the execution of the integration tests the following
  37.  * will be generated:
  38.  * <pre>
  39.  *   target/                                   <-- targetDirectory
  40.  *      +-- itf-repo/                          <-- targetItfRepoDirectory
  41.  *      +-- test-classes/                      <-- targetTestClassesDirectory
  42.  *            +--- FirstIT/
  43.  *                    +--- test_case_one       <-- sourceMavenProject
  44.  *
  45.  *      +-- maven-it/                          <-- targetMavenItDirectory
  46.  *           +-- FirstIT/                      <-- mavenItTestCaseBaseDirectory
  47.  *                 +--- test_case_one/         <-- integrationTestCaseDirectory
  48.  *                       +--- .m2/repository   <-- cacheDirectory
  49.  *                       +--- project          <-- projectDirectory
  50.  *                               +--- src/
  51.  *                               +--- pom.xml
  52.  * </pre>
  53.  *
  54.  * @author Karl Heinz Marbaise
  55.  */
  56. class DirectoryResolverResult {

  57.   private final Path mavenItTestCaseBaseDirectory;

  58.   private final Path targetMavenItDirectory;

  59.   private final Path targetDirectory;

  60.   private final Path integrationTestCaseDirectory;

  61.   private final Path projectDirectory;

  62.   private final Path targetTestClassesDirectory;

  63.   private final Path sourceMavenProject;

  64.   private final Path cacheDirectory;

  65.   private final Path targetItfRepoDirectory;

  66.   private final Optional<Path> predefinedRepository;

  67.   DirectoryResolverResult(ExtensionContext context) {
  68.     StorageHelper sh = new StorageHelper(context);

  69.     this.mavenItTestCaseBaseDirectory = sh.get(Storage.MAVEN_IT_TESTCASE_BASEDIRECTORY, Path.class);
  70.     this.targetMavenItDirectory = sh.get(Storage.TARGET_MAVEN_IT_DIRECTORY, Path.class);
  71.     this.targetDirectory = sh.get(Storage.TARGET_DIRECTORY, Path.class);

  72.     Method methodName = context.getTestMethod().orElseThrow(() -> new IllegalStateException("No method given"));

  73.     Optional<Class<?>> mavenProject = AnnotationHelper.findMavenProjectAnnotation(context);
  74.     if (mavenProject.isPresent()) {
  75.       MavenProject mavenProjectAnnotation = mavenProject.get().getAnnotation(MavenProject.class);
  76.       this.integrationTestCaseDirectory = this.getMavenItTestCaseBaseDirectory().resolve( mavenProjectAnnotation.value());
  77.     } else {
  78.       this.integrationTestCaseDirectory = this.getMavenItTestCaseBaseDirectory().resolve( methodName.getName() );
  79.     }

  80.     this.projectDirectory = integrationTestCaseDirectory.resolve("project");
  81.     this.targetTestClassesDirectory = DirectoryHelper.getTargetDir().resolve("test-classes");
  82.     this.targetItfRepoDirectory = this.getTargetDirectory().resolve("itf-repo"); // Hard Coded!!

  83.     Class<?> testClass = context.getTestClass().orElseThrow(() -> new IllegalStateException("Test class not found."));
  84.     String toFullyQualifiedPath = DirectoryHelper.toFullyQualifiedPath(testClass);

  85.     Path intermediate = this.targetTestClassesDirectory.resolve(toFullyQualifiedPath);
  86.     if (mavenProject.isPresent()) {
  87.       MavenProject mavenProjectAnnotation = mavenProject.get().getAnnotation(MavenProject.class);
  88.       this.sourceMavenProject = intermediate.resolve(mavenProjectAnnotation.value());
  89.     } else {
  90.       this.sourceMavenProject = AnnotationHelper.findMavenProjectSourcesAnnotation(context)
  91.           .map(s -> targetTestClassesDirectory.resolve(s.sources()))
  92.           .orElse(intermediate.resolve(methodName.getName()));
  93.     }

  94.     Optional<Class<?>> optionalMavenRepository = AnnotationHelper.findMavenRepositoryAnnotation(context);
  95.     if (optionalMavenRepository.isPresent()) {
  96.       MavenRepository mavenRepository = optionalMavenRepository.get().getAnnotation(MavenRepository.class);
  97.       String repositoryPath = DirectoryHelper.toFullyQualifiedPath(optionalMavenRepository.get());
  98.       Path cacheDirectoryBase = this.targetMavenItDirectory.resolve(repositoryPath);
  99.       this.cacheDirectory = cacheDirectoryBase.resolve(mavenRepository.value());
  100.     } else {
  101.       //FIXME: Hard coded default. Should we get the default from the Annotation?
  102.       this.cacheDirectory = this.integrationTestCaseDirectory.resolve(".m2/repository");
  103.     }

  104.     Optional<Class<?>> optionalMavenPredefinedRepository = AnnotationHelper.findMavenPredefinedRepositoryAnnotation(context);
  105.     if (optionalMavenPredefinedRepository.isPresent()) {
  106.       MavenPredefinedRepository mavenRepository = optionalMavenPredefinedRepository.get().getAnnotation(MavenPredefinedRepository.class);
  107.       String repositoryPath = DirectoryHelper.toFullyQualifiedPath(optionalMavenPredefinedRepository.get());
  108.       Path cacheDirectoryBase = this.targetTestClassesDirectory.resolve(repositoryPath);
  109.       this.predefinedRepository = Optional.of(cacheDirectoryBase.resolve(mavenRepository.value()));
  110.     } else {
  111.       //FIXME: Hard coded default. Should we get the default from the Annotation?
  112.       this.predefinedRepository = Optional.empty();
  113.     }

  114.   }

  115.   Path getTargetItfRepoDirectory() {
  116.     return targetItfRepoDirectory;
  117.   }

  118.   Optional<Path> getPredefinedRepository() {
  119.     return predefinedRepository;
  120.   }

  121.   Path getCacheDirectory() {
  122.     return cacheDirectory;
  123.   }

  124.   Path getSourceMavenProject() {
  125.     return sourceMavenProject;
  126.   }

  127.   Path getProjectDirectory() {
  128.     return projectDirectory;
  129.   }

  130.   Path getIntegrationTestCaseDirectory() {
  131.     return integrationTestCaseDirectory;
  132.   }

  133.   Path getMavenItTestCaseBaseDirectory() {
  134.     return mavenItTestCaseBaseDirectory;
  135.   }

  136.   Path getTargetDirectory() {
  137.     return targetDirectory;
  138.   }

  139. }