Package ar.com.dgarcia.javaspec.impl.exceptions

Examples of ar.com.dgarcia.javaspec.impl.exceptions.SpecException


    }

    public Class<T> getContextTypeFromSubclassDeclaration() {
        Class<? extends JavaSpec> subClass = getClass();
        if(!(subClass.getSuperclass().equals(JavaSpec.class))){
            throw new SpecException("A java spec class["+ subClass+ "] must inherit directly from " +JavaSpec.class);
        }
        Type generifiedJavaSpec = subClass.getGenericSuperclass();
        if(!(generifiedJavaSpec instanceof ParameterizedType)){
            throw new SpecException("JavaSpec superclass must be generified with a type of TestContext. For example JavaSpec<TestContext>");
        }
        Type contextType = ((ParameterizedType) generifiedJavaSpec).getActualTypeArguments()[0];
        if(!(contextType instanceof Class)){
            throw new SpecException("JavaSpec parameter is not a class?");
        }
        return (Class<T>) contextType;
    }
View Full Code Here


    private Map<String, Object> variableValues;

    @Override
    public void let(String variableName, Supplier<?> valueDefinition) throws SpecException {
        if(this.containsValueFor(variableName)){
            throw new SpecException("Variable [" + variableName + "] cannot be re-defined once assigned. Current value: ["+get(variableName)+"]");
        }
        this.storeDefinitionFor(variableName, valueDefinition);
    }
View Full Code Here

            return getValueFor(variableName);
        }

        Supplier<Object> variableDefinition = getDefinitionFor(variableName);
        if(variableDefinition == null){
            throw new SpecException("Variable ["+variableName+"] cannot be accessed because it lacks a definition in this context[" + this.getVariableDefinitions() + "]");
        }
        Object variableValue = null;
        try {
            variableValue = variableDefinition.get();
        } catch (SpecException e){
            throw e;
        } catch(StackOverflowError e){
            throw new SpecException("Got a Stackoverflow when evaluating variable ["+variableName+"]. Do we have a cyclic dependency on its definition?",e);
        } catch (Exception e) {
            throw new SpecException("Definition for variable ["+variableName+"] failed to execute: " + e.getMessage(),e);
        }
        storeValueFor(variableName, variableValue);
        return (T) variableValue;
    }
View Full Code Here

        throw new UnsupportedOperationException("Null context cannot define variables: " + variableName);
    }

    @Override
    public <T> T get(String variableName) {
        throw new SpecException("Variable ["+variableName+"] cannot be accessed because lacks definition");
    }
View Full Code Here

            Method[] typeMethods = currentType.getDeclaredMethods();
            for (Method typeMethod : typeMethods) {
                String variableName = TypedContextMethodInvocation.extractVariableNameFrom(typeMethod);
                if(this.seemsLikeLetter(typeMethod)){
                    if(letters.containsKey(variableName)){
                        throw new SpecException("Let operation for variable ["+variableName+"] is duplicated");
                    }
                    letters.put(variableName, typeMethod);
                } else if (this.seemsLikeGetter(typeMethod)){
                    if(getters.containsKey(variableName)){
                        throw new SpecException("Get operation for variable ["+variableName+"] is duplicated");
                    }
                    getters.put(variableName, typeMethod);
                }else{
                    throw new SpecException("Method ["+typeMethod.getName()+"] declared in ["+currentType.getSimpleName()+"] does not conform to get or let operation signatures [no arg | void, Supplier]");
                }
            }
            Class<?>[] superInterfaces = currentType.getInterfaces();
            for (Class<?> superInterface : superInterfaces) {
                pendingTypes.add(superInterface);
View Full Code Here

    private JavaSpec instantiate(Class<? extends JavaSpec> specClass) {
        try {
            JavaSpec createdInstance = specClass.newInstance();
            return createdInstance;
        } catch( SecurityException e){
            throw new SpecException("Security forbids instantiation for spec["+specClass+"]",e);
        } catch( ExceptionInInitializerError e){
            throw new SpecException("Constructor failed for new spec["+specClass+"] instance", e);
        } catch (InstantiationException e) {
            throw new SpecException("Error creating the spec["+specClass+"] instance", e);
        } catch (IllegalAccessException e) {
            throw new SpecException("Unable to access spec["+specClass+"] constructor for new instance",e);
        }
    }
View Full Code Here

        } catch(InvocationTargetException e){
            Throwable cause = e.getCause();
            if(cause instanceof SpecException){
                throw (SpecException)cause;
            }
            throw new SpecException("Invocation on proxied context failed: " + cause.getMessage(),cause);
        } catch (Exception e) {
            throw new SpecException("Unexpected error on proxied context invocation: " + e.getMessage(),e);
        }
    }
View Full Code Here

        Object firstArgument = this.args[0];
        Supplier<Object> variableDefinition = null;
        try {
            variableDefinition = (Supplier<Object>) firstArgument;
        } catch (ClassCastException e) {
            throw new SpecException("Invocation should have only a supplier argument",e);
        }
        return variableDefinition;
    }
View Full Code Here

        Set<String> allVariables = letters.keySet();
        for (String variable : allVariables) {
            Type letType = this.getLetTypeFor(variable);
            Type getType = this.getGetTypeFor(variable);
            if(this.isAMismatch(letType, getType)){
                throw new SpecException("Variable ["+variable+"] has mismatching type definitions in get ["+getType.getTypeName()+"] and let ["+letType.getTypeName()+"] operations");
            }
        }
    }
View Full Code Here

    private Type getLetTypeFor(String variable) {
        Method letMethod = letters.get(variable);
        Type supplierType = letMethod.getGenericParameterTypes()[0];
        if(!(supplierType instanceof ParameterizedType)){
            throw new SpecException("Argument of let operation ["+variable+"] is not a parameterized Supplier? " + supplierType);
        }
        ParameterizedType parameterizedSupplier = (ParameterizedType) supplierType;
        Type[] supplierArgs = parameterizedSupplier.getActualTypeArguments();
        Type letType = supplierArgs[0];
        return letType;
View Full Code Here

TOP

Related Classes of ar.com.dgarcia.javaspec.impl.exceptions.SpecException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.