/*
 * Copyright 2018, TeamDev. All rights reserved.
 *
 * Redistribution and use in source and/or binary forms, with or without
 * modification, must retain the above copyright notice and the following
 * disclaimer.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package io.spine.test;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import org.mockito.Mockito;

import java.lang.reflect.Field;

import static com.google.common.base.Throwables.getRootCause;

/**
 * Utility for injecting a Mockito spy in a private field of an object.
 *
 * <p>Although, it's a not recommended way of testing, in some cases such an approach is needed for
 * testing interactions in complex objects.
 *
 * @author Alexander Yevsyukov
 */
public class Spy<T> {

    private final Class<T> classOfSpy;

    public static <T> Spy<T> ofClass(Class<T> spyClass) {
        return new Spy<>(spyClass);
    }

    private Spy(Class<T> classOfSpy) {
        this.classOfSpy = classOfSpy;
    }

    @CanIgnoreReturnValue
    public T on(Object obj) {
         String className = classOfSpy.getSimpleName();
         String fieldName = className.substring(0, 1).toLowerCase() + className.substring(1);
        return on(obj, fieldName);
    }

    @CanIgnoreReturnValue
    public T on(Object obj, String fieldName) {
         Class<?> cls = obj.getClass();
         Object spy;
        try {
             Field field = cls.getDeclaredField(fieldName);
            field.setAccessible(true);
             Object fieldValue = field.get(obj);
            spy = Mockito.spy(fieldValue);
            field.set(obj, spy);
             T result = classOfSpy.cast(spy);
            return result;
        } catch (NoSuchFieldException | IllegalAccessException | ClassCastException e) {
            throw illegalArgumentWithCauseOf(e);
        }
    }

    private static IllegalArgumentException illegalArgumentWithCauseOf(Throwable throwable) {
         Throwable rootCause = getRootCause(throwable);
        throw new IllegalArgumentException(rootCause);
    }
}

@@ -1543,24 +1543,108 @@
 fSpy;%0A%0A    p
+rivate Spy(Class%3CT%3E classOfSpy) %7B%0A        this.classOfSpy = classOfSpy;%0A    %7D%0A%0A    p
 ublic static
@@ -1722,92 +1722,8 @@
 ass)
-;%0A    %7D%0A%0A    private Spy(Class%3CT%3E classOfSpy) %7B%0A        this.classOfSpy = classOfSpy
 ;%0A  
@@ -1779,25 +1779,24 @@
 ject obj) %7B%0A
- 
         Stri
@@ -1842,25 +1842,24 @@
 ();%0A        
- 
 String field
@@ -1890,16 +1890,53 @@
 ng(0, 1)
+%0A                                    
 .toLower
@@ -2080,25 +2080,24 @@
 ieldName) %7B%0A
- 
         Clas
@@ -2127,25 +2127,24 @@
 ();%0A        
- 
 Object spy;%0A
@@ -2157,17 +2157,16 @@
   try %7B%0A
- 
         
@@ -2251,25 +2251,24 @@
 ible(true);%0A
- 
             
@@ -2391,17 +2391,16 @@
         
- 
 T result
@@ -2696,25 +2696,24 @@
 hrowable) %7B%0A
- 
         Thro

Fix code layout