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

com.github.rmannibucau.featuredmock.mock.FeaturedMock Maven / Gradle / Ivy

package com.github.rmannibucau.featuredmock.mock;

import com.github.rmannibucau.featuredmock.mock.unmarshaller.Unmarshaller;
import com.github.rmannibucau.featuredmock.util.Extensions;
import com.github.rmannibucau.featuredmock.util.IOs;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Collection;
import java.util.LinkedList;

public final class FeaturedMock {
    private static final Unmarshaller[] DEFAULT_UNMARSHALLERS;
    static {
        final Collection list = new LinkedList();
        final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        for (final String name : new String[]{
            "com.github.rmannibucau.featuredmock.mock.unmarshaller.jackson.JacksonUnmarshaller",
            "com.github.rmannibucau.featuredmock.mock.unmarshaller.jaxb.JaxbUnmarshaller"
        }) {
            try {
                list.add(Unmarshaller.class.cast(Class.forName(name, true, classLoader).newInstance()));
            } catch (final Throwable th) {
                // no-op
            }
        }
        DEFAULT_UNMARSHALLERS = list.toArray(new Unmarshaller[list.size()]);
    }

    public static  T mock(final Class mock) {
        return mock(mock, DEFAULT_UNMARSHALLERS);
    }

    public static  T mock(final Class mock, final Unmarshaller... unmarshallers) {
        return mock.cast(Proxy.newProxyInstance(mock.getClassLoader(), new Class[] { mock }, new FeaturedMockHandler(unmarshallers)));
    }

    private FeaturedMock() {
        // no-op
    }

    private static class FeaturedMockHandler implements InvocationHandler, Extensions {
        private final Unmarshaller[] unmarshallers;

        public FeaturedMockHandler(final Unmarshaller[] unmarshallers) {
            this.unmarshallers = unmarshallers;
        }

        @Override
        public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
            final ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
            final String resource = method.getDeclaringClass().getName().replace(".", "/") + "/" + method.getName();

            for (final String extension : EXTENSIONS) {
                final InputStream is = contextClassLoader.getResourceAsStream(resource + extension);
                if (is == null) {
                    continue;
                }

                final byte[] slurp;
                try {
                    slurp = IOs.slurp(is);
                } finally {
                    is.close();
                }

                for (final Unmarshaller unmarshaller : unmarshallers) {
                    if (unmarshaller.accept(extension)) {
                        return unmarshaller.unmarshall(method.getReturnType(), new ByteArrayInputStream(slurp));
                    }
                }
            }

            return null; // null is the default, let it be called, it is just not yet mocked
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy