All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.evosuite.runtime.javaee.TestDataJavaEE Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
/**
 * Copyright (C) 2010-2016 Gordon Fraser, Andrea Arcuri and EvoSuite
 * contributors
 *
 * This file is part of EvoSuite.
 *
 * EvoSuite is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3.0 of the License, or
 * (at your option) any later version.
 *
 * EvoSuite is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with EvoSuite. If not, see .
 */
package org.evosuite.runtime.javaee;

import org.evosuite.runtime.util.Inputs;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * Singleton class used to keep track what part of the JavaEE container have been accessed
 * and that can be useful to generate proper test data
 */
public class TestDataJavaEE {

    public static final String HTTP_REQUEST_CONTENT_TYPE= "javaee.servlet.http.contentType";
    public static final String HTTP_REQUEST_PARAM = "javaee.servlet.http.param";
    public static final String HTTP_REQUEST_PART = "javaee.servlet.http.part";
    public static final String HTTP_REQUEST_PRINCIPAL = "javaee.servlet.http.principal";

    private static final TestDataJavaEE singleton = new TestDataJavaEE();

    /**
     * Keep track of all parameters that have been checked in a http servlet
     */
    private final Set httpRequestParameters;

    /**
     * Keep track of all http request dispatchers
     */
    private final Set dispatchers;

    /**
     * Did the SUT check the content type of the request or of any of its parts?
     */
    private volatile boolean readContentType;


    /**
     * Keep track of which parts were asked for.
     * The set is {@code null} if no part was ever read, not even with a get all.
     */
    private volatile Set partNames;

    /**
     * Check if there was any servlet that was initialized.
     * Note: servlet initialization is automatically added every time
     * a new servlet is instantiated with "new"
     *
     */
    private volatile boolean wasAServletInitialized;

    private final Set lookedUpContextNames;


    private TestDataJavaEE(){
        httpRequestParameters = new CopyOnWriteArraySet<>();
        dispatchers = new CopyOnWriteArraySet<>();
        lookedUpContextNames = new CopyOnWriteArraySet<>();
        readContentType = false;
        partNames = null;
    }

    public static TestDataJavaEE getInstance(){
        return singleton;
    }

    public void reset(){
        httpRequestParameters.clear();
        dispatchers.clear();
        lookedUpContextNames.clear();
        readContentType = false;
        partNames = null;
        wasAServletInitialized = false;
    }

    public JeeData getJeeData(){
        return new JeeData(httpRequestParameters,dispatchers,readContentType,partNames,wasAServletInitialized,lookedUpContextNames);
    }

    public Set getViewOfLookedUpContextNames(){
        return Collections.unmodifiableSet(lookedUpContextNames);
    }

    public void accessLookUpContextName(String name){
        Inputs.checkNull(name);
        lookedUpContextNames.add(name);
    }

    public boolean isWasAServletInitialized() {
        return wasAServletInitialized;
    }

    public void setWasAServletInitialized(boolean wasAServletInitialized) {
        this.wasAServletInitialized = wasAServletInitialized;
    }

    public Set getViewOfHttpRequestParameters(){
        return Collections.unmodifiableSet(httpRequestParameters);
    }

    public Set getViewOfDispatchers(){
        return Collections.unmodifiableSet(dispatchers);
    }

    public Set getViewOfParts(){
        if(partNames==null){
            return null;
        }
        return Collections.unmodifiableSet(partNames);
    }

    public boolean wasContentTypeRead(){
        return readContentType;
    }

    public void accessPart(String name){
        if(partNames==null){
            partNames = new CopyOnWriteArraySet<>();
        }
        if(name != null){
            partNames.add(name);
        }
    }

    public void accessContentType(){
        readContentType = true;
    }

    public void accessedDispatcher(String dispatcherName) throws IllegalArgumentException{
        Inputs.checkNull(dispatcherName);
        dispatchers.add(dispatcherName);
    }

    public void accessedHttpRequestParameter(String param) throws IllegalArgumentException{
        Inputs.checkNull(param);
        httpRequestParameters.add(param);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy