<commit_before>/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2012, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   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 org.thymeleaf.testing.templateengine.standard.test.builder;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.thymeleaf.fragment.IFragmentSpec;
import org.thymeleaf.testing.templateengine.context.ITestContext;
import org.thymeleaf.testing.templateengine.exception.TestEngineExecutionException;
import org.thymeleaf.testing.templateengine.messages.ITestMessages;
import org.thymeleaf.testing.templateengine.messages.ITestMessagesForLocale;
import org.thymeleaf.testing.templateengine.messages.TestMessages;
import org.thymeleaf.testing.templateengine.resource.ITestResource;
import org.thymeleaf.testing.templateengine.standard.resolver.StandardTestableResolver;
import org.thymeleaf.testing.templateengine.standard.test.StandardTest;
import org.thymeleaf.testing.templateengine.standard.test.StandardTestValueType;
import org.thymeleaf.testing.templateengine.standard.test.data.StandardTestEvaluatedData;
import org.thymeleaf.testing.templateengine.standard.test.data.StandardTestEvaluatedField;
import org.thymeleaf.testing.templateengine.standard.test.data.StandardTestFieldNaming;
import org.thymeleaf.testing.templateengine.testable.ITest;
import org.thymeleaf.testing.templateengine.testable.ITestable;
import org.thymeleaf.testing.templateengine.testable.Test;
import org.thymeleaf.util.StringUtils;
import org.thymeleaf.util.Validate;





public class StandardTestBuilder implements IStandardTestBuilder {

    
    

    public StandardTestBuilder() {
        super();
    }





    @SuppressWarnings("unchecked")
    public final ITest buildTest(
            final String executionId, final StandardTestEvaluatedData data, final StandardTestableResolver resolver) {
        
        Validate.notNull(executionId, "Execution ID cannot be null");
        Validate.notNull(data, "Data cannot be null");
        Validate.notNull(resolver, "Resolver cannot be null");
        

        /*
         * Obtain general test data
         */

        final StandardTestEvaluatedField name = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_NAME);
        final StandardTestEvaluatedField cache = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_CACHE);
        final StandardTestEvaluatedField context = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_CONTEXT); 
        final Map<String,StandardTestEvaluatedField> messages = data.getValuesByQualifierForField(StandardTestFieldNaming.FIELD_NAME_MESSAGES); 
        final StandardTestEvaluatedField templateMode = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_TEMPLATE_MODE); 
        final StandardTestEvaluatedField fragmentSpec = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_FRAGMENT);
        final Map<String,StandardTestEvaluatedField> inputs = data.getValuesByQualifierForField(StandardTestFieldNaming.FIELD_NAME_INPUT);
        final StandardTestEvaluatedField output = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_OUTPUT);
        final StandardTestEvaluatedField exception = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_EXCEPTION);
        final StandardTestEvaluatedField exceptionMessagePattern = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_EXCEPTION_MESSAGE_PATTERN);
        final StandardTestEvaluatedField exactMatch = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_EXACT_MATCH);
        final StandardTestEvaluatedField extendsTest = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_EXTENDS);

        
        /*
         * Organize inputs
         */
        final StandardTestEvaluatedField mainInput = 
                (inputs != null ? inputs.get(StandardTestFieldNaming.FIELD_QUALIFIER_MAIN) : null);
        final Map<String,StandardTestEvaluatedField> additionalInputs = 
                (inputs != null ? new HashMap<String, StandardTestEvaluatedField>(inputs) : new HashMap<String, StandardTestEvaluatedField>());
        additionalInputs.remove(StandardTestFieldNaming.FIELD_QUALIFIER_MAIN);
        

        
        /*
         * Obtain parent test (if any)
         */
        ITest parentTest = null;
        if (extendsTest != null && extendsTest.hasNotNullValue()) {
            
            final String extendsValue = (String) extendsTest.getValue();
            
            final ITestable parentTestable =
                    resolver.resolveRelative(executionId, extendsValue, data.getTestResource());
            if (parentTestable == null) {
                throw new TestEngineExecutionException( 
                        "Cannot resolve \"" + StandardTestFieldNaming.FIELD_NAME_EXTENDS + "\" " +
                        "field: \"" + extendsValue + "\" resolved as null.");
            }
            if (!(parentTestable instanceof ITest)) {
                throw new TestEngineExecutionException( 
                        "Cannot resolve \"" + StandardTestFieldNaming.FIELD_NAME_EXTENDS + "\" " +
                        "field: \"" + extendsValue + "\" resolved as a " + parentTestable.getClass().getName() + 
                        " object instead of an " + ITest.class.getName() + " implementation.");
            }
            
            parentTest = (ITest) parentTestable;
            
        }

        

        /*
         * Initialize the test object
         */

        final StandardTest test =  createTestInstance();
        
        if (name != null && name.hasValue()) {
            test.setName((String)name.getValue(), name.getValueType());
        }
        
        if (templateMode != null && templateMode.hasValue()) {
            test.setTemplateMode((String)templateMode.getValue(), templateMode.getValueType());
        }
        
        if (context != null && context.hasValue()) {
            test.setContext((ITestContext)context.getValue(), context.getValueType());
        }
        
        if (messages != null) {
            final TestMessages testMessages = new TestMessages();
            for (Map.Entry<String,StandardTestEvaluatedField> entry : messages.entrySet()) {
                final Locale locale = (StringUtils.isEmptyOrWhitespace(entry.getKey())? null : new Locale(entry.getKey()));
                final StandardTestEvaluatedField field = entry.getValue();
                if (field != null && field.hasNotNullValue()) {<commit_msg>Removed minor 2.0-2.1 incompatibility point<commit_after>/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2012, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   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 org.thymeleaf.testing.templateengine.standard.test.builder;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import org.thymeleaf.fragment.IFragmentSpec;
import org.thymeleaf.testing.templateengine.context.ITestContext;
import org.thymeleaf.testing.templateengine.exception.TestEngineExecutionException;
import org.thymeleaf.testing.templateengine.messages.ITestMessages;
import org.thymeleaf.testing.templateengine.messages.ITestMessagesForLocale;
import org.thymeleaf.testing.templateengine.messages.TestMessages;
import org.thymeleaf.testing.templateengine.resource.ITestResource;
import org.thymeleaf.testing.templateengine.standard.resolver.StandardTestableResolver;
import org.thymeleaf.testing.templateengine.standard.test.StandardTest;
import org.thymeleaf.testing.templateengine.standard.test.StandardTestValueType;
import org.thymeleaf.testing.templateengine.standard.test.data.StandardTestEvaluatedData;
import org.thymeleaf.testing.templateengine.standard.test.data.StandardTestEvaluatedField;
import org.thymeleaf.testing.templateengine.standard.test.data.StandardTestFieldNaming;
import org.thymeleaf.testing.templateengine.testable.ITest;
import org.thymeleaf.testing.templateengine.testable.ITestable;
import org.thymeleaf.testing.templateengine.testable.Test;
import org.thymeleaf.util.Validate;





public class StandardTestBuilder implements IStandardTestBuilder {

    
    

    public StandardTestBuilder() {
        super();
    }





    @SuppressWarnings("unchecked")
    public final ITest buildTest(
            final String executionId, final StandardTestEvaluatedData data, final StandardTestableResolver resolver) {
        
        Validate.notNull(executionId, "Execution ID cannot be null");
        Validate.notNull(data, "Data cannot be null");
        Validate.notNull(resolver, "Resolver cannot be null");
        

        /*
         * Obtain general test data
         */

        final StandardTestEvaluatedField name = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_NAME);
        final StandardTestEvaluatedField cache = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_CACHE);
        final StandardTestEvaluatedField context = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_CONTEXT); 
        final Map<String,StandardTestEvaluatedField> messages = data.getValuesByQualifierForField(StandardTestFieldNaming.FIELD_NAME_MESSAGES); 
        final StandardTestEvaluatedField templateMode = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_TEMPLATE_MODE); 
        final StandardTestEvaluatedField fragmentSpec = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_FRAGMENT);
        final Map<String,StandardTestEvaluatedField> inputs = data.getValuesByQualifierForField(StandardTestFieldNaming.FIELD_NAME_INPUT);
        final StandardTestEvaluatedField output = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_OUTPUT);
        final StandardTestEvaluatedField exception = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_EXCEPTION);
        final StandardTestEvaluatedField exceptionMessagePattern = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_EXCEPTION_MESSAGE_PATTERN);
        final StandardTestEvaluatedField exactMatch = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_EXACT_MATCH);
        final StandardTestEvaluatedField extendsTest = getFieldValueForMainQualifier(data, StandardTestFieldNaming.FIELD_NAME_EXTENDS);

        
        /*
         * Organize inputs
         */
        final StandardTestEvaluatedField mainInput = 
                (inputs != null ? inputs.get(StandardTestFieldNaming.FIELD_QUALIFIER_MAIN) : null);
        final Map<String,StandardTestEvaluatedField> additionalInputs = 
                (inputs != null ? new HashMap<String, StandardTestEvaluatedField>(inputs) : new HashMap<String, StandardTestEvaluatedField>());
        additionalInputs.remove(StandardTestFieldNaming.FIELD_QUALIFIER_MAIN);
        

        
        /*
         * Obtain parent test (if any)
         */
        ITest parentTest = null;
        if (extendsTest != null && extendsTest.hasNotNullValue()) {
            
            final String extendsValue = (String) extendsTest.getValue();
            
            final ITestable parentTestable =
                    resolver.resolveRelative(executionId, extendsValue, data.getTestResource());
            if (parentTestable == null) {
                throw new TestEngineExecutionException( 
                        "Cannot resolve \"" + StandardTestFieldNaming.FIELD_NAME_EXTENDS + "\" " +
                        "field: \"" + extendsValue + "\" resolved as null.");
            }
            if (!(parentTestable instanceof ITest)) {
                throw new TestEngineExecutionException( 
                        "Cannot resolve \"" + StandardTestFieldNaming.FIELD_NAME_EXTENDS + "\" " +
                        "field: \"" + extendsValue + "\" resolved as a " + parentTestable.getClass().getName() + 
                        " object instead of an " + ITest.class.getName() + " implementation.");
            }
            
            parentTest = (ITest) parentTestable;
            
        }

        

        /*
         * Initialize the test object
         */

        final StandardTest test =  createTestInstance();
        
        if (name != null && name.hasValue()) {
            test.setName((String)name.getValue(), name.getValueType());
        }
        
        if (templateMode != null && templateMode.hasValue()) {
            test.setTemplateMode((String)templateMode.getValue(), templateMode.getValueType());
        }
        
        if (context != null && context.hasValue()) {
            test.setContext((ITestContext)context.getValue(), context.getValueType());
        }
        
        if (messages != null) {
            final TestMessages testMessages = new TestMessages();
            for (Map.Entry<String,StandardTestEvaluatedField> entry : messages.entrySet()) {
                final String key = entry.getKey();
                final Locale locale = (key == null || key.trim().equals("")? null : new Locale(key));
                final StandardTestEvaluatedField field = entry.getValue();
                if (field != null && field.hasNotNullValue()) {
{'fraction_non_alphanumeric': 0.10476385476385476, 'fraction_numerical': 0.001911001911001911, 'mean_word_length': 3.3377738306690348, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '6894432', 'n_tokens_mistral': 3792, 'n_tokens_neox': 3690, 'n_words': 750}