/*
 * (C) Copyright 2017 Boni Garcia (http://bonigarcia.github.io/)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package io.github.bonigarcia;

import static java.lang.Boolean.parseBoolean;
import static java.lang.Integer.parseInt;
import static java.lang.invoke.MethodHandles.lookup;
import static org.slf4j.LoggerFactory.getLogger;

import java.io.File;
import java.io.InputStream;
import java.util.Optional;
import java.util.Properties;

import org.junit.jupiter.api.extension.ExtensionContext;
import org.slf4j.Logger;

import io.github.bonigarcia.wdm.WdmConfig;
import io.github.bonigarcia.wdm.WebDriverManagerException;

/**
 * Collection of utility features.
 *
 * @author Boni Garcia (boni.gg@gmail.com)
 * @since 1.0.0
 */
public class SeleniumJupiter {

    static final Logger log = getLogger(lookup().lookupClass());

    SeleniumJupiter() {
        throw new IllegalStateException("Utility class");
    }

    public static String getString(String key) {
        String value = "";
        if (!key.equals("")) {
            value = System.getenv(key.toUpperCase().replace(".", "_"));
            if (value == null) {
                value = System.getProperty(key);
            }
            if (value == null) {
                value = getProperty(key);
            }
        }
        return value;
    }

    public static int getInt(String key) {
        return parseInt(getString(key));
    }

    public static boolean getBoolean(String key) {
        return parseBoolean(getString(key));
    }

    public static String getOutputFolder(ExtensionContext context) {
        String outputFolder = getString("sel.jup.output.folder");
        Optional<Class<?>> testInstance = context.getTestClass();
        if (outputFolder.equalsIgnoreCase("surefire-reports")
                && testInstance.isPresent()) {
            outputFolder = "./target/surefire-reports/"
                    + testInstance.get().getName();
        } else if (outputFolder.isEmpty()) {
            outputFolder = ".";
        }
        log.trace("Output folder {}", outputFolder);

        File outputFolderFile = new File(outputFolder);
        if (!outputFolderFile.exists()) {
            outputFolderFile.mkdirs();
        }
        return outputFolder;
    }

    private static String getProperty(String key) {
        String value = null;
        Properties properties = new Properties();
        try {
            InputStream inputStream = WdmConfig.class.getResourceAsStream(
                    System.getProperty("sel.jup.properties",
                            "/selenium-jupiter.properties"));
            properties.load(inputStream);
            value = properties.getProperty(key);
        } catch (Exception e) {
            throw new WebDriverManagerException(e);
        } finally {
            if (value == null) {
                log.trace("Property key {} not found, using default value",
                        key);
                value = "";
            }
        }
        return value;
    }

}

@@ -796,16 +796,55 @@
 lookup;%0A
+import static java.util.Arrays.stream;%0A
 import s
@@ -930,24 +930,97 @@
 nputStream;%0A
+import java.lang.annotation.Annotation;%0Aimport java.lang.reflect.Method;%0A
 import java.
@@ -1064,16 +1064,59 @@
 rties;%0A%0A
+import org.junit.jupiter.api.TestTemplate;%0A
 import o
@@ -2247,24 +2247,158 @@
  context) %7B%0A
+        Optional%3CMethod%3E testMethod = context.getTestMethod();%0A        Annotation%5B%5D annotations = testMethod.get().getAnnotations();%0A%0A
         Stri
@@ -2685,30 +2685,799 @@
 ts/%22
-%0A                    +
+;%0A            boolean isTestTemplate = stream(annotations)%0A                    .map(Annotation::annotationType)%0A                    .anyMatch(a -%3E a == TestTemplate.class);%0A            log.trace(%22Is test template? %7B%7D%22, isTestTemplate);%0A            if (isTestTemplate) %7B%0A                outputFolder += testMethod.get().getName() + %22(%22;%0A%0A                Class%3C?%3E%5B%5D parameterTypes = testMethod.get()%0A                        .getParameterTypes();%0A                for (int i = 0; i %3C parameterTypes.length; i++) %7B%0A                    if (i != 0) %7B%0A                        outputFolder += %22, %22;%0A                    %7D%0A                    outputFolder += parameterTypes%5Bi%5D.getSimpleName();%0A                %7D%0A                outputFolder += %22)/%22;%0A            %7D else %7B%0A                outputFolder +=
  tes
@@ -3495,32 +3495,47 @@
 et().getName();%0A
+            %7D%0A%0A
         %7D else i

Support for Jenkins attachment plugin also with TestTemplate