<commit_before>package com.atlassian.webdriver.product;

import com.atlassian.webdriver.AtlassianWebDriver;
import com.atlassian.webdriver.WebDriverFactory;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;

/**
 *
 */
public class TestedProductFactory
{
    private static final Logger LOG = Logger.getLogger(TestedProductFactory.class);

    public static <P extends TestedProduct> P create(Class<P> testedProductClass)
    {
        return create(testedProductClass, getDefaultInstanceId(testedProductClass));
    }

    public static <P extends TestedProduct> P create(Class<P> testedProductClass, AtlassianWebDriver webDriver)
    {
        return create(testedProductClass, getDefaultInstanceId(testedProductClass), webDriver);
    }

    public static <P extends TestedProduct> P create(Class<P> testedProductClass, String instanceId)
    {
        return create(testedProductClass, instanceId, WebDriverFactory.getDriver());
    }

    public static <P extends TestedProduct> P create(Class<P> testedProductClass, String instanceId, AtlassianWebDriver webDriver)
    {
        final String contextPath, baseUrl;
        final int httpPort;

        final String ampsBaseUrl = System.getProperty("baseurl." + instanceId);
        final ProductInstance instance;
        if (ampsBaseUrl != null)    // running within an AMPS IntegrationTestMojo invocation - read sys props for env vars
        {
            httpPort = Integer.getInteger("http." + instanceId + ".port");
            contextPath = System.getProperty("context." + instanceId + ".path");
            baseUrl = ampsBaseUrl;
        }
        else
        {
            Defaults defaults = testedProductClass.getAnnotation(Defaults.class);
            httpPort = defaults.httpPort();
            contextPath = defaults.contextPath();
            baseUrl = "http://" + getLocalHostName() + ":" + httpPort + contextPath;
        }
        instance = new ProductInstance(instanceId, httpPort, contextPath, baseUrl);
        return create(testedProductClass, instance, webDriver);
    }

    private static String getDefaultInstanceId(Class<?> testedProductClass)
    {
        return testedProductClass.getAnnotation(Defaults.class).instanceId();
    }

    private static <P extends TestedProduct> P create(Class<P> testedProductClass, ProductInstance instance, WebDriver webDriver) {
        try<commit_msg>Better warning if @Defaults is missing from the tested product<commit_after>package com.atlassian.webdriver.product;

import com.atlassian.webdriver.AtlassianWebDriver;
import com.atlassian.webdriver.WebDriverFactory;
import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.net.InetAddress;

/**
 *
 */
public class TestedProductFactory
{
    private static final Logger LOG = Logger.getLogger(TestedProductFactory.class);

    public static <P extends TestedProduct> P create(Class<P> testedProductClass)
    {
        return create(testedProductClass, getDefaultInstanceId(testedProductClass));
    }

    public static <P extends TestedProduct> P create(Class<P> testedProductClass, AtlassianWebDriver webDriver)
    {
        return create(testedProductClass, getDefaultInstanceId(testedProductClass), webDriver);
    }

    public static <P extends TestedProduct> P create(Class<P> testedProductClass, String instanceId)
    {
        return create(testedProductClass, instanceId, WebDriverFactory.getDriver());
    }

    public static <P extends TestedProduct> P create(Class<P> testedProductClass, String instanceId, AtlassianWebDriver webDriver)
    {
        final String contextPath, baseUrl;
        final int httpPort;

        final String ampsBaseUrl = System.getProperty("baseurl." + instanceId);
        final ProductInstance instance;
        if (ampsBaseUrl != null)    // running within an AMPS IntegrationTestMojo invocation - read sys props for env vars
        {
            httpPort = Integer.getInteger("http." + instanceId + ".port");
            contextPath = System.getProperty("context." + instanceId + ".path");
            baseUrl = ampsBaseUrl;
        }
        else
        {
            Defaults defaults = testedProductClass.getAnnotation(Defaults.class);
            httpPort = defaults.httpPort();
            contextPath = defaults.contextPath();
            baseUrl = "http://" + getLocalHostName() + ":" + httpPort + contextPath;
        }
        instance = new ProductInstance(instanceId, httpPort, contextPath, baseUrl);
        return create(testedProductClass, instance, webDriver);
    }

    private static String getDefaultInstanceId(Class<?> testedProductClass)
    {
        Defaults annotation = testedProductClass.getAnnotation(Defaults.class);
        if (annotation == null)
        {
            throw new IllegalArgumentException("The tested product class '" + testedProductClass.getName() + "' is missing the @Defaults annotation");
        }
        return annotation.instanceId();
    }

    private static <P extends TestedProduct> P create(Class<P> testedProductClass, ProductInstance instance, WebDriver webDriver) {
        try
{'fraction_non_alphanumeric': 0.08868846298384066, 'fraction_numerical': 0.0003757985719654265, 'mean_word_length': 3.499577345731192, 'pattern_counts': {'":': 2, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6005822', 'n_tokens_mistral': 1399, 'n_tokens_neox': 1357, 'n_words': 344}