Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files

fix unit test

  • Loading branch information...
aeng committed Nov 15, 2012
1 parent a2cd02e commit 19732dcb4528d8bd0a33033544da26309e78cfe0
@@ -27,6 +27,7 @@
import org.zanata.webtrans.client.events.UserConfigChangeHandler;
import org.zanata.webtrans.client.view.OptionsDisplay;

import com.allen_sauer.gwt.log.client.Log;
import com.google.inject.Inject;

public class OptionsPresenter extends WidgetPresenter<OptionsDisplay> implements OptionsDisplay.Listener, UserConfigChangeHandler
@@ -63,7 +64,6 @@ public void setOptionsView(MainView view)
display.setOptions(documentListOptionsPresenter.getDisplay().asWidget());
break;
}

}

@Override
@@ -42,9 +42,9 @@
import org.zanata.webtrans.shared.rpc.LoadOptionsResult;
import org.zanata.webtrans.shared.rpc.NavOption;


/**
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
* @author Carlos Munoz <a
* href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
@Name("webtrans.gwt.LoadOptionsHandler")
@Scope(ScopeType.STATELESS)
@@ -57,28 +57,31 @@
@In
private AccountDAO accountDAO;


@Override
public LoadOptionsResult execute(LoadOptionsAction action, ExecutionContext context) throws ActionException
{
UserConfigHolder configHolder = new UserConfigHolder();
HAccount account = accountDAO.findById(authenticatedAccount.getId(), true);
Map<String,HAccountOption> options = account.getEditorOptions();
Map<String, HAccountOption> options = account.getEditorOptions();
HashMap<String, HAccountOption> filteredOptions = new HashMap<String, HAccountOption>();

if (action.getPrefixes() != null && !action.getPrefixes().isEmpty())
for (Entry<String, HAccountOption> entry : options.entrySet())
{
for (Entry<String, HAccountOption> entry : options.entrySet())
// filter config according to prefix
if (action.getPrefixes() != null && !action.getPrefixes().isEmpty())
{
for (String prefix : action.getPrefixes())
{
if (entry.getKey().startsWith(prefix))
{
// filter config according to prefix
filteredOptions.put(entry.getKey(), entry.getValue());
}
}
}
else
{
filteredOptions.put(entry.getKey(), entry.getValue());
}
}

if (filteredOptions.containsKey(UserOptions.DisplayButtons.getPersistentName()))
@@ -127,7 +130,7 @@ public LoadOptionsResult execute(LoadOptionsAction action, ExecutionContext cont
}

LoadOptionsResult result = new LoadOptionsResult();
result.setConfiguration( configHolder.getState() );
result.setConfiguration(configHolder.getState());
return result;
}

@@ -0,0 +1,236 @@
package org.zanata.webtrans.client.presenter;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import net.customware.gwt.presenter.client.EventBus;
import static org.mockito.Mockito.mock;

import org.hamcrest.Matchers;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.zanata.webtrans.client.events.EditorPageSizeChangeEvent;
import org.zanata.webtrans.client.events.FilterViewEvent;
import org.zanata.webtrans.client.events.NotificationEvent;
import org.zanata.webtrans.client.events.UserConfigChangeEvent;
import org.zanata.webtrans.client.events.WorkspaceContextUpdateEvent;
import org.zanata.webtrans.client.rpc.CachingDispatchAsync;
import org.zanata.webtrans.client.view.DocumentListOptionsDisplay;
import org.zanata.webtrans.client.view.EditorOptionsDisplay;
import org.zanata.webtrans.client.view.OptionsDisplay;
import org.zanata.webtrans.shared.model.UserOptions;
import org.zanata.webtrans.shared.model.UserWorkspaceContext;
import org.zanata.webtrans.shared.rpc.HasWorkspaceContextUpdateData;
import org.zanata.webtrans.shared.rpc.LoadOptionsAction;
import org.zanata.webtrans.shared.rpc.LoadOptionsResult;
import org.zanata.webtrans.shared.rpc.NavOption;
import org.zanata.webtrans.shared.rpc.SaveOptionsAction;
import org.zanata.webtrans.shared.rpc.SaveOptionsResult;

import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasValue;

@Test(groups = { "unit-tests" })
public class OptionsPresenterTest
{
private OptionsPresenter presenter;

@Mock
private EditorOptionsPresenter editorOptionsPresenter;
@Mock
private DocumentListOptionsPresenter documentListOptionsPresenter;
@Mock
private OptionsDisplay display;
@Mock
private EventBus eventBus;

private UserConfigHolder configHolder = new UserConfigHolder();

@Captor
private ArgumentCaptor<UserConfigChangeEvent> eventCaptor;

@BeforeMethod
public void beforeMethod()
{
MockitoAnnotations.initMocks(this);

presenter = new OptionsPresenter(display, eventBus, editorOptionsPresenter, documentListOptionsPresenter, configHolder);
verify(display).setListener(presenter);
}

@Test
public void onBind()
{
// When:
presenter.onBind();

// Then:
verify(editorOptionsPresenter).onBind();
verify(documentListOptionsPresenter).onBind();

verify(eventBus).addHandler(UserConfigChangeEvent.TYPE, presenter);
verify(display).setShowErrorChk(configHolder.isShowError());
}

@Test
public void onUnbind()
{
presenter.onUnbind();

verify(editorOptionsPresenter).unbind();
verify(documentListOptionsPresenter).unbind();
}

@Test
public void setOptionsViewEditor()
{
EditorOptionsDisplay widget = mock(EditorOptionsDisplay.class);
when(editorOptionsPresenter.getDisplay()).thenReturn(widget);
presenter.onBind();
presenter.setOptionsView(MainView.Editor);

verify(display).setOptions(editorOptionsPresenter.getDisplay().asWidget());
}

@Test
public void setOptionsViewDocumentList()
{
DocumentListOptionsDisplay widget = mock(DocumentListOptionsDisplay.class);
when(documentListOptionsPresenter.getDisplay()).thenReturn(widget);
presenter.onBind();
presenter.setOptionsView(MainView.Documents);

verify(display).setOptions(documentListOptionsPresenter.getDisplay().asWidget());
}

@Test
public void setOptionsViewSearch()
{
presenter.onBind();
presenter.setOptionsView(MainView.Search);

verify(display).setOptions(null);
}

@Test
public void onShowErrorsOptionChanged()
{
presenter.onBind();
presenter.onShowErrorsOptionChanged(true);

assertThat(configHolder.isShowError(), Matchers.equalTo(true));
}

@Test
public void onLoadDefaultOptionsEditor()
{
EditorOptionsDisplay widget = mock(EditorOptionsDisplay.class);
when(editorOptionsPresenter.getDisplay()).thenReturn(widget);

presenter.onBind();
presenter.setOptionsView(MainView.Editor);
presenter.loadDefaultOptions();

verify(editorOptionsPresenter).loadDefaultOptions();
}

@Test
public void onLoadDefaultOptionsDocument()
{
DocumentListOptionsDisplay widget = mock(DocumentListOptionsDisplay.class);
when(documentListOptionsPresenter.getDisplay()).thenReturn(widget);

presenter.onBind();
presenter.setOptionsView(MainView.Documents);
presenter.loadDefaultOptions();

verify(documentListOptionsPresenter).loadDefaultOptions();
}

@Test
public void persistOptionChangeEditor()
{
EditorOptionsDisplay widget = mock(EditorOptionsDisplay.class);
when(editorOptionsPresenter.getDisplay()).thenReturn(widget);

presenter.onBind();
presenter.setOptionsView(MainView.Editor);
presenter.persistOptionChange();

verify(editorOptionsPresenter).persistOptionChange();
}

@Test
public void persistOptionChangeDocument()
{
DocumentListOptionsDisplay widget = mock(DocumentListOptionsDisplay.class);
when(documentListOptionsPresenter.getDisplay()).thenReturn(widget);

presenter.onBind();
presenter.setOptionsView(MainView.Documents);
presenter.persistOptionChange();

verify(documentListOptionsPresenter).persistOptionChange();
}

@Test
public void loadOptionsEditor()
{
EditorOptionsDisplay widget = mock(EditorOptionsDisplay.class);
when(editorOptionsPresenter.getDisplay()).thenReturn(widget);

presenter.onBind();
presenter.setOptionsView(MainView.Editor);
presenter.loadOptions();

verify(editorOptionsPresenter).loadOptions();
}

@Test
public void loadOptionsDocument()
{
DocumentListOptionsDisplay widget = mock(DocumentListOptionsDisplay.class);
when(documentListOptionsPresenter.getDisplay()).thenReturn(widget);

presenter.onBind();
presenter.setOptionsView(MainView.Documents);
presenter.loadOptions();

verify(documentListOptionsPresenter).loadOptions();
}

@Test
public void loadDefaultOptionsEditor()
{
EditorOptionsDisplay widget = mock(EditorOptionsDisplay.class);
when(editorOptionsPresenter.getDisplay()).thenReturn(widget);

presenter.onBind();
presenter.setOptionsView(MainView.Editor);
presenter.loadDefaultOptions();

verify(editorOptionsPresenter).loadDefaultOptions();
}

@Test
public void loadDefaultOptionsDocument()
{
DocumentListOptionsDisplay widget = mock(DocumentListOptionsDisplay.class);
when(documentListOptionsPresenter.getDisplay()).thenReturn(widget);

presenter.onBind();
presenter.setOptionsView(MainView.Documents);
presenter.loadDefaultOptions();

verify(documentListOptionsPresenter).loadDefaultOptions();
}
}

0 comments on commit 19732dc

Please sign in to comment.
You can’t perform that action at this time.