kazurayam / selenium-webdriver-java

Examples of the O'Reilly book "Hands-On Selenium WebDriver with Java"
https://oreil.ly/1E7CX
Apache License 2.0
0 stars 0 forks source link

ProjectDirectoryResolver should accept arbitray sub-directory pattern #20

Closed kazurayam closed 10 months ago

kazurayam commented 10 months ago

In the kazurayam8a branch, I developed com.kazurayam.unittest.ProjectDirectoryResolver class.

It has the following fragment:

    public static Path getProjectDirViaClasspath(Class clazz) {
        Path codeSourcePath = ProjectDirectoryResolver.getCodeSourceAsPath(clazz);
        // e.g. "/Users/myname/oreilly/selenium-webdriver-java/selenium-webdriver-junit4/build/classes/java/test/com/kazurayam/webdriver/TestHelper.class"
        List<String> nameElements = toNameElements(codeSourcePath);
        StringSequence ss = new StringSequence(nameElements);
        int indexOfBuildDir =
                ss.indexOfSubsequence(Arrays.asList("build", "classes", "java", "test"));
        int indexOfTargetDir =
                ss.indexOfSubsequence(Arrays.asList("target", "test-classes"));
        ...

Here I assumed 2 constant values: Arrays.asList("build", "classes", "java", "test") as Gradle's sub-directory pattern, Arrays.asList("target", "test-classes") as Maven's sub-driectory pattern.

The current version supports only these 2 subdirectory structure. I should introduce one more abstraction. The class should enable users to set more subdirectory patterns.

kazurayam commented 10 months ago

https://github.com/kazurayam/unittest-helper/blob/develop/lib/src/test/java/com/kazurayam/unittest/TestHelperTest.java

supported this. See the following for how to use it

    @Test
    public void test_resolveOutput_into_custom_location() throws Exception {
        TestHelper th =
                new TestHelper(this.getClass())
                        .setOutputDirPath(Paths.get("build/tmp/testOutput"));
        Path p = th.resolveOutput("hello.txt");
        Files.write(p, "Hello, world!".getBytes("utf-8"));
        assertThat(p.getParent()                   // expecting testOutput
                .getFileName().toString())
                .isEqualTo("testOutput");
        assertThat(p.getParent()                   // expecting testOutput
                .getParent()                       // expecting tmp
                .getFileName().toString())
                .isEqualTo("tmp");
        assertThat(p.getParent()                   // expecting testOutput
                .getParent()                       // expecting tmp
                .getParent()                       // expecting build
                .getFileName().toString())
                .isEqualTo("build");
    }