getSupportedAnnotationTypes() {
45 | return singleton(MockedEndpoint.class.getCanonicalName());
46 | }
47 |
48 | @Override
49 | public SourceVersion getSupportedSourceVersion() {
50 | return latestSupported();
51 | }
52 |
53 | @Override
54 | public boolean process(Set extends TypeElement> set, RoundEnvironment roundEnvironment) {
55 | Set extends Element> elements = roundEnvironment.getElementsAnnotatedWith(MockedEndpoint.class);
56 |
57 | for (Element element : elements) {
58 | try {
59 | String path = extractPath(element);
60 |
61 | initializationBlockBuilder.add("registry.add($S);\n", path);
62 | } catch (NoAnnotationException e) {
63 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "No GET, POST, DELETE, or PUT annotation", element);
64 | return true;
65 | }
66 | }
67 |
68 | if (elements.size() != 0) {
69 | FieldSpec registryField = FieldSpec.builder(Set.class, "registry", Modifier.PRIVATE, Modifier.STATIC, Modifier.FINAL)
70 | .initializer("new $T();", HashSet.class)
71 | .build();
72 |
73 | MethodSpec getMockedEndpoints = MethodSpec.methodBuilder("getMockedEndpoints")
74 | .addModifiers(Modifier.PUBLIC, Modifier.STATIC)
75 | .returns(Set.class)
76 | .addCode("return registry;\n")
77 | .build();
78 |
79 | TypeSpec FakeRegistry = TypeSpec.classBuilder("FakeRegistry")
80 | .addModifiers(Modifier.PUBLIC, Modifier.FINAL)
81 | .addField(registryField)
82 | .addStaticBlock(initializationBlockBuilder.build())
83 | .addMethod(getMockedEndpoints)
84 | .build();
85 |
86 | try {
87 | JavaFile.builder("com.car2go.mock", FakeRegistry)
88 | .build()
89 | .writeTo(processingEnv.getFiler());
90 | } catch (IOException e) {
91 | processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "IOException: " + e);
92 | return true;
93 | }
94 | }
95 |
96 | return true;
97 | }
98 |
99 | private static String extractPath(Element element) throws NoAnnotationException {
100 | GET getAnnotation = element.getAnnotation(GET.class);
101 | if (getAnnotation != null) {
102 | return getAnnotation.value();
103 | }
104 |
105 | POST postAnnotation = element.getAnnotation(POST.class);
106 | if (postAnnotation != null) {
107 | return postAnnotation.value();
108 | }
109 |
110 | DELETE deleteAnnotation = element.getAnnotation(DELETE.class);
111 | if (deleteAnnotation != null) {
112 | return deleteAnnotation.value();
113 | }
114 |
115 | PUT putAnnotation = element.getAnnotation(PUT.class);
116 | if (putAnnotation != null) {
117 | return putAnnotation.value();
118 | }
119 |
120 | throw new NoAnnotationException();
121 | }
122 |
123 | /**
124 | * Thrown when there is no {@link GET}, {@link POST}, {@link PUT} or {@link DELETE} annotation for the method.
125 | */
126 | private static class NoAnnotationException extends Exception {
127 | }
128 |
129 | }
130 |
--------------------------------------------------------------------------------
/endpoint2mock2/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/endpoint2mock2/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'java'
2 | apply plugin: 'kotlin'
3 | apply plugin: 'maven'
4 |
5 | sourceCompatibility = JavaVersion.VERSION_1_7
6 | targetCompatibility = JavaVersion.VERSION_1_7
7 |
8 | ext {
9 | name = 'Endpoint2mock2'
10 | artifactId = 'endpoint2mock2'
11 | }
12 |
13 | dependencies {
14 | compile 'com.squareup.okhttp3:okhttp:3.8.1'
15 |
16 | testCompile 'junit:junit:4.12'
17 | testCompile 'org.mockito:mockito-core:2.8.9'
18 | }
19 |
20 |
21 |
--------------------------------------------------------------------------------
/endpoint2mock2/src/main/java/com/car2go/endpoint2mock2/BooleanFunction.java:
--------------------------------------------------------------------------------
1 | package com.car2go.endpoint2mock2;
2 |
3 | /**
4 | * Function which takes no arguments and returns boolean.
5 | */
6 | public interface BooleanFunction {
7 |
8 | /**
9 | * @return result of the function.
10 | */
11 | boolean call();
12 |
13 | /**
14 | * Always returns {@code true}.
15 | */
16 | BooleanFunction TRUE = new BooleanFunction() {
17 | @Override
18 | public boolean call() {
19 | return true;
20 | }
21 | };
22 |
23 | /**
24 | * Always returns {@code false}.
25 | */
26 | BooleanFunction FALSE = new BooleanFunction() {
27 | @Override
28 | public boolean call() {
29 | return false;
30 | }
31 | };
32 |
33 | }
--------------------------------------------------------------------------------
/endpoint2mock2/src/main/java/com/car2go/endpoint2mock2/MockableClient.java:
--------------------------------------------------------------------------------
1 | package com.car2go.endpoint2mock2;
2 |
3 | import okhttp3.Call;
4 | import okhttp3.OkHttpClient;
5 | import okhttp3.Request;
6 |
7 | /**
8 | * Client which either forwards request to a original client or if request is annotated with
9 | * {@link MockedEndpoint} calls the mock endpoint (assuming that it is hosted somewhere).
10 | */
11 | public class MockableClient extends OkHttpClient {
12 |
13 | private final Registry registry;
14 | private final OkHttpClient realClient;
15 | private final ReplaceEndpointClient mockedClient;
16 | private final BooleanFunction mockWhenFunction;
17 |
18 | MockableClient(Registry registry,
19 | OkHttpClient realClient,
20 | ReplaceEndpointClient mockedClient,
21 | BooleanFunction mockWhenFunction) {
22 | this.registry = registry;
23 | this.realClient = realClient;
24 | this.mockedClient = mockedClient;
25 | this.mockWhenFunction = mockWhenFunction;
26 | }
27 |
28 | @Override
29 | public Call newCall(Request request) {
30 | if (shouldMockRequest(request)) {
31 | return mockedClient.newCall(request);
32 | } else {
33 | return realClient.newCall(request);
34 | }
35 | }
36 |
37 | private boolean shouldMockRequest(Request request) {
38 | return registry.isInRegistry(request.url().toString()) && mockWhenFunction.call();
39 | }
40 |
41 | /**
42 | * Creates builder for {@link MockableClient} with mocked base url.
43 | *
44 | * @param mockedBaseUrl base URL of the endpoint which will be used instead of real base URL for mocked
45 | * requests.
46 | */
47 | public static MockableClient.Builder baseUrl(String mockedBaseUrl) {
48 | return new Builder(mockedBaseUrl);
49 | }
50 |
51 | /**
52 | * Builder for {@link MockableClient}.
53 | */
54 | public static class Builder {
55 | private final String mockedBaseUrl;
56 |
57 | private OkHttpClient realClient;
58 | private BooleanFunction mockWhenFunction = BooleanFunction.TRUE;
59 |
60 | private Builder(String mockedBaseUrl) {
61 | this.mockedBaseUrl = mockedBaseUrl;
62 | }
63 |
64 | /**
65 | * @param realClient the client that will be used for non-mocked requests.
66 | */
67 | public Builder realClient(OkHttpClient realClient) {
68 | this.realClient = realClient;
69 | return this;
70 | }
71 |
72 | /**
73 | * There might be cases when you do not always want to mock requests even if they are
74 | * annotated with {@link MockedEndpoint}. This method allows you to decide whether such
75 | * requests should be mocked or not by providing function which will return `true` if
76 | * request should be mocked and `false` if it should not.
77 | *
78 | * Note, this function would be still called only for endpoints annotated with
79 | * {@link BooleanFunction}.
80 | */
81 | public Builder mockWhen(BooleanFunction function) {
82 | this.mockWhenFunction = function;
83 |
84 | return this;
85 | }
86 |
87 | /**
88 | * @return new {@link MockableClient}.
89 | */
90 | public OkHttpClient build() {
91 | if (realClient == null) {
92 | realClient = new OkHttpClient();
93 | }
94 |
95 | return new MockableClient(
96 | new Registry(),
97 | realClient,
98 | new ReplaceEndpointClient(mockedBaseUrl, realClient),
99 | mockWhenFunction
100 | );
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/endpoint2mock2/src/main/java/com/car2go/endpoint2mock2/MockedEndpoint.java:
--------------------------------------------------------------------------------
1 | package com.car2go.endpoint2mock2;
2 |
3 | import java.lang.annotation.ElementType;
4 | import java.lang.annotation.Target;
5 |
6 | /**
7 | * Put this annotation on top of the Retrofit's API declaration methods (the ones which are annotated with GET, POST,
8 | * DELETE or PUT annotation) and Endpoint2mock will add it to the list of mocked endpoints.
9 | */
10 | @Target(ElementType.METHOD)
11 | public @interface MockedEndpoint {
12 | }
13 |
--------------------------------------------------------------------------------
/endpoint2mock2/src/main/java/com/car2go/endpoint2mock2/Registry.java:
--------------------------------------------------------------------------------
1 | package com.car2go.endpoint2mock2;
2 |
3 | import java.lang.reflect.Method;
4 | import java.util.Set;
5 |
6 | import static java.util.Collections.emptySet;
7 |
8 | /**
9 | * Checks whether URL should be mocked or not.
10 | */
11 | class Registry {
12 |
13 | private final Class> generatedMocksRegistryClass;
14 | private final Method getMockedEndpointsMethod;
15 |
16 | public Registry() {
17 | generatedMocksRegistryClass = loadMocksRegistryClass();
18 | getMockedEndpointsMethod = loadIsMockedMethod(generatedMocksRegistryClass);
19 | }
20 |
21 | private static Method loadIsMockedMethod(Class> registryClass) {
22 | if (registryClass == null) {
23 | return null;
24 | }
25 |
26 | try {
27 | return registryClass.getDeclaredMethod("getMockedEndpoints");
28 | } catch (NoSuchMethodException e) {
29 | return null;
30 | }
31 | }
32 |
33 | private static Class> loadMocksRegistryClass() {
34 | try {
35 | return Class.forName("com.car2go.mock.FakeRegistry");
36 | } catch (ClassNotFoundException e) {
37 | return null;
38 | }
39 | }
40 |
41 | /**
42 | * @return {@code true} if given URL is in registry. {@code false} if it is not.
43 | */
44 | public boolean isInRegistry(String url) {
45 | if (getMockedEndpointsMethod == null) {
46 | return false;
47 | }
48 |
49 | Set mockedEndpoints = getMockedEndpoints();
50 |
51 | for (String endpoint : mockedEndpoints) {
52 | if (endpointMatches(endpoint, url)) {
53 | return true;
54 | }
55 | }
56 |
57 | return false;
58 | }
59 |
60 | private static boolean endpointMatches(String mockedEndpoint, String url) {
61 | String regEx = toRegEx(mockedEndpoint);
62 |
63 | return url.matches(".*" + regEx + queryArguments());
64 | }
65 |
66 | private static String queryArguments() {
67 | return "(\\?.*)?";
68 | }
69 |
70 | private static String toRegEx(String mockedEndpoint) {
71 | return replacePathParameters(mockedEndpoint);
72 | }
73 |
74 | private static String replacePathParameters(String mockedEndpoint) {
75 | return mockedEndpoint.replaceAll("\\{.*\\}", ".*");
76 | }
77 |
78 | @SuppressWarnings("unchecked")
79 | private Set getMockedEndpoints() {
80 | try {
81 | return (Set) getMockedEndpointsMethod.invoke(generatedMocksRegistryClass);
82 | } catch (Exception e) {
83 | return emptySet();
84 | }
85 | }
86 |
87 | }
88 |
--------------------------------------------------------------------------------
/endpoint2mock2/src/main/java/com/car2go/endpoint2mock2/ReplaceEndpointClient.java:
--------------------------------------------------------------------------------
1 | package com.car2go.endpoint2mock2;
2 |
3 | import okhttp3.Call;
4 | import okhttp3.HttpUrl;
5 | import okhttp3.OkHttpClient;
6 | import okhttp3.Request;
7 |
8 | /**
9 | * Client which replaces base endpoint with another one then forwards requests to real client.
10 | */
11 | public class ReplaceEndpointClient extends OkHttpClient {
12 | private final String newEndpoint;
13 | private final OkHttpClient realClient;
14 |
15 | ReplaceEndpointClient(String newEndpoint,
16 | OkHttpClient realClient) {
17 | this.newEndpoint = newEndpoint;
18 | this.realClient = realClient;
19 | }
20 |
21 | @Override
22 | public Call newCall(Request request) {
23 | return realClient.newCall(
24 | withReplacedEndpoint(request)
25 | );
26 | }
27 |
28 | private Request withReplacedEndpoint(Request originalRequest) {
29 | return originalRequest
30 | .newBuilder()
31 | .url(replaceEndpoint(originalRequest.url().toString()))
32 | .build();
33 | }
34 |
35 | private String replaceEndpoint(String originalRequestUrl) {
36 | HttpUrl url = HttpUrl.parse(originalRequestUrl);
37 | HttpUrl newEndpointUrl = HttpUrl.parse(newEndpoint);
38 |
39 | if(url == null){
40 | throw new IllegalArgumentException("Base url is not well formatted HTTP or HTTPS");
41 | }
42 |
43 | if(newEndpointUrl == null){
44 | throw new IllegalArgumentException("Mocked base url is not well formatted HTTP or HTTPS");
45 | }
46 |
47 | return url.newBuilder()
48 | .scheme(newEndpointUrl.scheme())
49 | .host(newEndpointUrl.host())
50 | .port(newEndpointUrl.port())
51 | .build()
52 | .toString();
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/endpoint2mock2/src/test/java/com/car2go/endpoint2mock2/MockableClientTest.java:
--------------------------------------------------------------------------------
1 | package com.car2go.endpoint2mock2;
2 |
3 | import org.junit.Before;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.mockito.Mock;
7 | import org.mockito.junit.MockitoJUnitRunner;
8 |
9 | import okhttp3.OkHttpClient;
10 | import okhttp3.Request;
11 |
12 | import static org.mockito.ArgumentMatchers.any;
13 | import static org.mockito.ArgumentMatchers.anyString;
14 | import static org.mockito.BDDMockito.given;
15 | import static org.mockito.Mockito.never;
16 | import static org.mockito.Mockito.verify;
17 |
18 | @RunWith(MockitoJUnitRunner.class)
19 | public class MockableClientTest {
20 |
21 | static final Request REQUEST = new Request.Builder()
22 | .url("http://example.com")
23 | .build();
24 |
25 | @Mock
26 | Registry registry;
27 | @Mock
28 | OkHttpClient realClient;
29 | @Mock
30 | ReplaceEndpointClient mockClient;
31 | @Mock
32 | BooleanFunction lookupFunction;
33 |
34 | MockableClient testee;
35 |
36 |
37 | @Before
38 | public void setUp() throws Exception {
39 | testee = new MockableClient(
40 | registry,
41 | realClient,
42 | mockClient,
43 | lookupFunction
44 | );
45 | }
46 |
47 | @Test
48 | public void notInRegistry_CallRealEndpoint() {
49 | // Given
50 | givenWhenFunctionReturns(true);
51 | givenEndpointInRegistry(false);
52 |
53 | // When
54 | testee.newCall(REQUEST);
55 |
56 | // Then
57 | verify(realClient).newCall(REQUEST);
58 | verify(mockClient, never()).newCall(any(Request.class));
59 | }
60 |
61 | @Test
62 | public void inRegistryAndShouldMock_CallMockedEndpoint() {
63 | // Given
64 | givenWhenFunctionReturns(true);
65 | givenEndpointInRegistry(true);
66 |
67 | // When
68 | testee.newCall(REQUEST);
69 |
70 | // Then
71 | verify(mockClient).newCall(REQUEST);
72 | verify(realClient, never()).newCall(any(Request.class));
73 | }
74 |
75 | @Test
76 | public void inRegistryButShouldNotMock_CallRealEndpoint() {
77 | // Given
78 | givenWhenFunctionReturns(true);
79 | givenEndpointInRegistry(false);
80 |
81 | // When
82 | testee.newCall(REQUEST);
83 |
84 | // Then
85 | verify(realClient).newCall(REQUEST);
86 | verify(mockClient, never()).newCall(any(Request.class));
87 | }
88 |
89 | private void givenEndpointInRegistry(boolean result) {
90 | given(registry.isInRegistry(anyString()))
91 | .willReturn(result);
92 | }
93 |
94 | private void givenWhenFunctionReturns(boolean result) {
95 | given(lookupFunction.call())
96 | .willReturn(result);
97 | }
98 | }
--------------------------------------------------------------------------------
/endpoint2mock2/src/test/java/com/car2go/endpoint2mock2/RegistryTest.java:
--------------------------------------------------------------------------------
1 | package com.car2go.endpoint2mock2;
2 |
3 |
4 | import org.junit.Test;
5 |
6 | import java.util.HashSet;
7 |
8 | import com.car2go.mock.FakeRegistry;
9 |
10 | import static java.util.Arrays.asList;
11 | import static org.junit.Assert.assertFalse;
12 | import static org.junit.Assert.assertTrue;
13 |
14 | public class RegistryTest {
15 |
16 | Registry testee = new Registry();
17 |
18 | @Test
19 | public void inRegistry_Yes_PartialMatch() throws Exception {
20 | // Given
21 | FakeRegistry.setRegistry(new HashSet<>(asList(
22 | "/path3/path4",
23 | "/path1/path2"
24 | )));
25 |
26 | // When
27 | boolean result = testee.isInRegistry("http://example.com/path1/path2/path3/path4");
28 |
29 | // Then
30 | assertTrue(result);
31 | }
32 |
33 | @Test
34 | public void inRegistry_Yes_PathParameters() throws Exception {
35 | // Given
36 | FakeRegistry.setRegistry(new HashSet<>(asList(
37 | "/path3/{parameterA}/{parameterB}",
38 | "/path1/path2"
39 | )));
40 |
41 | // When
42 | boolean result = testee.isInRegistry("http://example.com/path1/path3/a/b");
43 |
44 | // Then
45 | assertTrue(result);
46 | }
47 |
48 | @Test
49 | public void inRegistry_Yes_QueryParameters() throws Exception {
50 | // Given
51 | FakeRegistry.setRegistry(new HashSet<>(asList(
52 | "/path3/path4",
53 | "/path1/path2"
54 | )));
55 |
56 | // When
57 | boolean result = testee.isInRegistry("http://example.com/path3/path4?someArg=a&otherArg=b");
58 |
59 | // Then
60 | assertTrue(result);
61 | }
62 |
63 | @Test
64 | public void inRegistry_No() throws Exception {
65 | // Given
66 | FakeRegistry.setRegistry(new HashSet<>(asList(
67 | "/path3/path4",
68 | "/path1/path2"
69 | )));
70 |
71 | // When
72 | boolean result = testee.isInRegistry("http://example.com/path3/something");
73 |
74 | // Then
75 | assertFalse(result);
76 | }
77 | }
78 |
79 |
--------------------------------------------------------------------------------
/endpoint2mock2/src/test/java/com/car2go/endpoint2mock2/ReplaceEndpointClientTest.java:
--------------------------------------------------------------------------------
1 | package com.car2go.endpoint2mock2;
2 |
3 | import org.junit.Before;
4 | import org.junit.Test;
5 | import org.junit.runner.RunWith;
6 | import org.mockito.ArgumentCaptor;
7 | import org.mockito.Mock;
8 | import org.mockito.Mockito;
9 | import org.mockito.junit.MockitoJUnitRunner;
10 |
11 | import okhttp3.OkHttpClient;
12 | import okhttp3.Request;
13 |
14 | import static org.junit.Assert.*;
15 |
16 | @RunWith(MockitoJUnitRunner.class)
17 | public class ReplaceEndpointClientTest {
18 |
19 | @Mock
20 | OkHttpClient realClient;
21 |
22 | ReplaceEndpointClient testee;
23 |
24 | private static final String REAL_BASE_URL = "http://real-server.com/";
25 | private static final String MOCK_BASE_URL = "http://mock-server.com/";
26 | private static final String PATH = "path1/path2";
27 |
28 | @Before
29 | public void setup() {
30 | testee = new ReplaceEndpointClient(
31 | MOCK_BASE_URL,
32 | realClient
33 | );
34 | }
35 |
36 | @Test
37 | public void replaceUrl() {
38 | // Given
39 | Request realRequest = new Request.Builder()
40 | .url(REAL_BASE_URL + PATH)
41 | .build();
42 |
43 | //When
44 | testee.newCall(realRequest);
45 |
46 | ArgumentCaptor requestCaptor = ArgumentCaptor.forClass(Request.class);
47 | Mockito.verify(realClient).newCall(requestCaptor.capture());
48 | Request sentRequest = requestCaptor.getValue();
49 | assertEquals(MOCK_BASE_URL + PATH, sentRequest.url().toString());
50 | }
51 | }
--------------------------------------------------------------------------------
/endpoint2mock2/src/test/java/com/car2go/mock/FakeRegistry.java:
--------------------------------------------------------------------------------
1 | package com.car2go.mock;
2 |
3 | import java.util.HashSet;
4 | import java.util.Set;
5 |
6 | /**
7 | * Example of generated fake registry. Used only for tests.
8 | */
9 | @SuppressWarnings("unchecked")
10 | public class FakeRegistry {
11 | private static final Set registry = new HashSet();
12 |
13 | public static void setRegistry(Set registry) {
14 | FakeRegistry.registry.clear();
15 | FakeRegistry.registry.addAll(registry);
16 | }
17 |
18 | public static Set getMockedEndpoints() {
19 | return registry;
20 | }
21 |
22 | }
23 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sharenowTech/Endpoint2mock2/17f4a20e16675917d1e01d3e05fdf67cafde43ac/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Wed Jun 14 15:37:27 CEST 2017
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/json-server/README.md:
--------------------------------------------------------------------------------
1 | # Sample mock server
2 |
3 | This is an example of a mock server which can be used together with sample application. It relies on awesome [json-server](https://github.com/typicode/json-server) which is a mock server which takes 30 seconds to setup and requires 0 lines of code.
4 |
5 | Of course it is possible to use Endpoint2mock2 with any other kind of server.
6 |
7 | ## How to start it
8 |
9 | Two steps. First, make sure it is installed by running:
10 |
11 | ```
12 | npm install -g json-server
13 | ```
14 |
15 | Second, run it:
16 |
17 | ```
18 | sh startServer.sh
19 | ```
20 |
21 | After that sample application will be able to use it.
22 |
--------------------------------------------------------------------------------
/json-server/db.json:
--------------------------------------------------------------------------------
1 | {
2 | "car2go": [
3 | {
4 | "name": "Mocked1"
5 | },
6 | {
7 | "name": "Mocked2"
8 | }
9 | ]
10 | }
11 |
--------------------------------------------------------------------------------
/json-server/routes.json:
--------------------------------------------------------------------------------
1 | {
2 | "/users/:user/repos": "/:user"
3 | }
4 |
--------------------------------------------------------------------------------
/json-server/startServer.sh:
--------------------------------------------------------------------------------
1 | json-server --watch --routes routes.json db.json
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':endpoint2mock2', ':endpoint2mock2-compiler'
2 |
--------------------------------------------------------------------------------