net.paslavsky.gwt.appload.client.internal.AccessToTheApiSteps.java Source code

Java tutorial

Introduction

Here is the source code for net.paslavsky.gwt.appload.client.internal.AccessToTheApiSteps.java

Source

/*
 * Copyright (c) 2014 Andrey Paslavsky.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.paslavsky.gwt.appload.client.internal;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.core.shared.GWTBridge;
import cucumber.api.java.Before;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import net.paslavsky.gwt.appload.client.GwtApplicationLoader;
import net.paslavsky.gwt.appload.client.Module;
import org.junit.Assert;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AccessToTheApiSteps {
    private List<String> executedModules = new ArrayList<String>();

    @Before
    public void setUp() throws Exception {
        GWTBridge mock = Mockito.mock(GWTBridge.class);
        Mockito.when(mock.create(GwtApplicationLoader.class)).then(new Answer<GwtApplicationLoader>() {
            @Override
            public GwtApplicationLoader answer(InvocationOnMock invocation) throws Throwable {
                // Lets create proxy object every time
                return new GwtApplicationLoaderProxy();
            }
        });
        GWT.setBridge(mock);
    }

    @Given("^some method that try to add some module via GWT.create\\(\\)$")
    public void some_method_that_try_to_add_some_module_via_GWT_create_() throws Throwable {
        ((GwtApplicationLoader) GWT.create(GwtApplicationLoader.class)).add(new SomeModule());
    }

    @And("^other method that makes the same logic for another module$")
    public void other_method_that_makes_the_same_logic_for_another_module() throws Throwable {
        ((GwtApplicationLoader) GWT.create(GwtApplicationLoader.class)).add(new AnotherModule());
    }

    @When("^calling GWT.create\\(GwtApplicationLoader.class\\).start\\(\\)$")
    public void calling_GWT_create_GwtApplicationLoader_class_start_() throws Throwable {
        ((GwtApplicationLoader) GWT.create(GwtApplicationLoader.class)).start();
    }

    @Then("^all modules will be loaded$")
    public void all_modules_will_be_loaded() throws Throwable {
        Assert.assertTrue(executedModules.containsAll(Arrays.asList("SomeModule", "AnotherModule")));
    }

    private class SomeModule implements Module {
        @Override
        public void load() {
            executedModules.add(getClass().getSimpleName());
        }
    }

    private class AnotherModule implements Module {
        @Override
        public void load() {
            executedModules.add(getClass().getSimpleName());
        }
    }
}