/*
 * (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.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");
        if (outputFolder.equalsIgnoreCase("surefire-reports")) {
            outputFolder = "./target/surefire-reports/"
                    + context.getTestClass().get().getName();
        }
        log.debug("Output folder {}", outputFolder);

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

    private static String getProperty(String key) {
        Properties properties = new Properties();
        try {
            InputStream inputStream = WdmConfig.class.getResourceAsStream(
                    System.getProperty("sel.jup.properties",
                            "/selenium-jupiter.properties"));
            properties.load(inputStream);
        } catch (Exception e) {
            throw new WebDriverManagerException(e);
        }
        return properties.getProperty(key);
    }

}

@@ -895,16 +895,43 @@
 Stream;%0A
+import java.util.Optional;%0A
 import j
@@ -2158,24 +2158,90 @@
 t.folder%22);%0A
+        Optional%3CClass%3C?%3E%3E testInstance = context.getTestClass();%0A
         if (
@@ -2289,16 +2289,60 @@
 eports%22)
+%0A                && testInstance.isPresent()
 ) %7B%0A    
@@ -2419,46 +2419,85 @@
   + 
-context.getTestClass().get().getName()
+testInstance.get().getName();%0A        %7D else %7B%0A            outputFolder = %22.%22
 ;%0A  

Check that test instance is present before accessing