パッケージ com.kazurayam.unittest

クラス ProjectDirectoryResolver

java.lang.Object
com.kazurayam.unittest.ProjectDirectoryResolver

public final class ProjectDirectoryResolver extends Object
ProjectDirectoryResolver resolves the project's directory based on the classpath of a JVM class you are developing. The following quote from https://discuss.gradle.org/t/how-do-i-set-the-working-directory-for-testng-in-a-multi-project-gradle-build/7379/7 explains what I wanted to do.

luke_daley Gradle Employee Nov '13

Loading from the filesystem using relative paths during unit tests is problematic because different environments will set a different working directory for the test process. For example, Gradle uses the projects directory while IntelliJ uses the directory of the root project.

The only really safe way to solve this problem is to load via the classpath. Is this a possibility for your scenario?

  • コンストラクタの詳細

    • ProjectDirectoryResolver

      public ProjectDirectoryResolver()
    • ProjectDirectoryResolver

      public ProjectDirectoryResolver(FileSystem fileSystem)
  • メソッドの詳細

    • addCodeSourcePathElementsUnderProjectDirectory

      public void addCodeSourcePathElementsUnderProjectDirectory(CodeSourcePathElementsUnderProjectDirectory cspeupd)
      パラメータ:
      cspeupd - e.g, CodeSourcePathElementsUnderProjectDirectory of ["build", "classes", "main", "java"]
    • getRegisteredListOfCodeSourcePathElementsUnderProjectDirectory

      public List<CodeSourcePathElementsUnderProjectDirectory> getRegisteredListOfCodeSourcePathElementsUnderProjectDirectory()
      戻り値:
      the list of Sublist Patterns including both builtin and ones you added
    • getCodeSourcePathOf

      public Path getCodeSourcePathOf(Class<?> clazz)
      returns the Path in which the class binary of `com.kazurayam.unittest.ProjectDirectoryResolver` is found on disk. e.g, "/Users/somebody/selenium-webdriver-java/selenium-webdriver-junit4/build/classes/java/test/" when built by Gradle e.g, "/Users/somebody/selenium-webdriver-java/selenium-webdriver-junit4/target/test-classes/" when built by Maven
      パラメータ:
      clazz - the Class object based on which the project dir is resolved
      戻り値:
      a java.nio.file.Path instance which is derived by clazz.getProtectionDomain().getCodeSource().getLocation()
    • resolveProjectDirectoryViaClasspath

      public Path resolveProjectDirectoryViaClasspath(Class<?> clazz)
      returns the project directory in which the class binary of `com.kazurayam.webdriver.TestOutputOrganizer` is found on disk. The same value will be returned in either case where the class was built by Gradle and Maven. e.g, "/Users/somebody/selenium-webdriver-java/selenium-webdriver-junit4/" The point is, the System property "user.dir" will return "/Users/somebody/selenium-webdriver-java". This does not include the subproject directory "selenium-webdriver-junit4", therefore it is not what we want. When the TestOutputOrganizer class is built using Gradle, the class will be stored in the directory "selenium-webdriver-java/selenium-webdriver-junit4/build". When the TestOutputOrganizer class is built using Maven, the class will be stored in the directory "selenium-webdriver-java/selenium-webdriver-junit5/target". So this method look up the file name "build" or "target" in the code source of the TestOutputOrganizer class. Then we can get the Path value of the project directory properly.
      パラメータ:
      clazz - the Class object based on which the project dir is resolved
      戻り値:
      a java.nio.file.Path instance that represents the project directory. if the project is a Gradle Multi-Project, then this method returns the path of the subproject's root dir.