Search in sources :

Example 1 with AlreadyEmittedException

use of com.amazonaws.xray.exceptions.AlreadyEmittedException in project aws-xray-sdk-java by aws.

the class EntityTest method numberOfMutatingMethodsThatThrewException.

private MutatingMethodCount numberOfMutatingMethodsThatThrewException(Entity entity, Class klass) {
    int numberOfMutatingMethods = 0;
    int numberOfMutatingMethodsThatThrewException = 0;
    for (Method m : klass.getMethods()) {
        if (mutatingMethodPrefixes.stream().anyMatch((prefix) -> {
            return m.getName().startsWith(prefix);
        })) {
            numberOfMutatingMethods++;
            try {
                List<Parameter> parameters = Arrays.asList(m.getParameters());
                List<? extends Object> arguments = parameters.stream().map((parameter) -> {
                    try {
                        Class<?> argumentClass = parameter.getType();
                        if (boolean.class.equals(argumentClass)) {
                            return false;
                        } else if (double.class.equals(argumentClass)) {
                            return 0.0d;
                        } else {
                            return argumentClass.getConstructor().newInstance();
                        }
                    } catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
                    }
                    return null;
                }).collect(Collectors.toList());
                m.invoke(entity, arguments.toArray());
            } catch (InvocationTargetException ite) {
                if (ite.getCause() instanceof AlreadyEmittedException) {
                    numberOfMutatingMethodsThatThrewException++;
                }
            } catch (IllegalAccessException e) {
            }
        }
    }
    return new MutatingMethodCount(numberOfMutatingMethods, numberOfMutatingMethodsThatThrewException);
}
Also used : MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) AlreadyEmittedException(com.amazonaws.xray.exceptions.AlreadyEmittedException) Subsegment(com.amazonaws.xray.entities.Subsegment) JSONAssert(org.skyscreamer.jsonassert.JSONAssert) Segment(com.amazonaws.xray.entities.Segment) SegmentImpl(com.amazonaws.xray.entities.SegmentImpl) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) JSONException(org.json.JSONException) Parameter(java.lang.reflect.Parameter) Emitter(com.amazonaws.xray.emitters.Emitter) Method(java.lang.reflect.Method) Before(org.junit.Before) SubsegmentImpl(com.amazonaws.xray.entities.SubsegmentImpl) TraceID(com.amazonaws.xray.entities.TraceID) Test(org.junit.Test) Collectors(java.util.stream.Collectors) InvocationTargetException(java.lang.reflect.InvocationTargetException) Mockito(org.mockito.Mockito) List(java.util.List) JSONCompareMode(org.skyscreamer.jsonassert.JSONCompareMode) JsonNodeFactory(com.fasterxml.jackson.databind.node.JsonNodeFactory) Entity(com.amazonaws.xray.entities.Entity) Assert(org.junit.Assert) FixMethodOrder(org.junit.FixMethodOrder) Parameter(java.lang.reflect.Parameter) AlreadyEmittedException(com.amazonaws.xray.exceptions.AlreadyEmittedException) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Aggregations

Emitter (com.amazonaws.xray.emitters.Emitter)1 Entity (com.amazonaws.xray.entities.Entity)1 Segment (com.amazonaws.xray.entities.Segment)1 SegmentImpl (com.amazonaws.xray.entities.SegmentImpl)1 Subsegment (com.amazonaws.xray.entities.Subsegment)1 SubsegmentImpl (com.amazonaws.xray.entities.SubsegmentImpl)1 TraceID (com.amazonaws.xray.entities.TraceID)1 AlreadyEmittedException (com.amazonaws.xray.exceptions.AlreadyEmittedException)1 JsonNodeFactory (com.fasterxml.jackson.databind.node.JsonNodeFactory)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Parameter (java.lang.reflect.Parameter)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 JSONException (org.json.JSONException)1 Assert (org.junit.Assert)1 Before (org.junit.Before)1 FixMethodOrder (org.junit.FixMethodOrder)1