Skip to content

Commit

Permalink
Wizard tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Dec 12, 2013
1 parent 5f63454 commit fbcf3a4
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ExampleWizard extends AbstractUICommand implements UIWizard
{

@Inject
@WithAttributes(label = "First Name", shortName = 'f')
@WithAttributes(label = "First Name", shortName = 'f', required = true)
private UIInput<String> firstName;

@Inject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public CommandController createSingleController(UICommand command, UIRuntime run
}

@Override
public WizardCommandController createWizardController(UIWizard wizard, UIRuntime runtime)
public WizardCommandController createWizardController(UIWizard wizard, UIRuntime runtime) throws Exception
{
return new WizardCommandControllerImpl(addonRegistry, runtime, wizard, this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ class WizardCommandControllerImpl extends AbstractCommandController implements W
private int flowPointer = 0;

public WizardCommandControllerImpl(AddonRegistry addonRegistry, UIRuntime runtime,
UIWizard initialCommand, CommandControllerFactory controllerFactory)
UIWizard initialCommand, CommandControllerFactory controllerFactory) throws Exception
{
super(addonRegistry, runtime, initialCommand);
this.controllerFactory = controllerFactory;
flow.add(createControllerFor(initialCommand));
}

@Override
Expand Down Expand Up @@ -95,6 +96,8 @@ public boolean isValid()
public CommandController setValueFor(String inputName, Object value) throws IllegalArgumentException
{
getCurrentCommandController().setValueFor(inputName, value);
// Remove subsequent pages
flow.subList(flowPointer + 1, flow.size()).clear();
return this;
}

Expand Down Expand Up @@ -153,7 +156,7 @@ public boolean canMoveToNextStep()
{
// Move only if there is a next step or if there is a subflow set
Class<? extends UICommand>[] next = getNextFrom(getCurrentCommandController().getCommand());
return (next == null || !subflow.isEmpty());
return (next != null || !subflow.isEmpty());
}
catch (Exception e)
{
Expand Down Expand Up @@ -230,6 +233,11 @@ private CommandController getCurrentCommandController()
private CommandController createControllerFor(Class<? extends UICommand> commandClass) throws Exception
{
UICommand command = addonRegistry.getServices(commandClass).get();
return createControllerFor(command);
}

private CommandController createControllerFor(UICommand command) throws Exception
{
return controllerFactory.createSingleController(command, runtime);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ public static ForgeArchive getDeployment()
@Inject
private ExampleCommand exampleCommand;

@Inject
private ExampleNoUICommand exampleNoUICommand;

@Test
public void testInjection() throws Exception
{
Expand Down Expand Up @@ -86,4 +89,16 @@ public void testInitialized() throws Exception
Assert.assertFalse(controller.getInputs().isEmpty());
}

@Test
public void testExampleNoUI() throws Exception
{
CommandController controller = controllerFactory.createSingleController(exampleNoUICommand,
new MockUIRuntime());
controller.initialize();
Assert.assertTrue(controller.getInputs().isEmpty());
Assert.assertTrue(controller.isValid());
Result result = controller.execute();
Assert.assertEquals("Executed", result.getMessage());
}

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,27 @@
/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.ui.controller;

import javax.inject.Inject;

import org.jboss.forge.addon.ui.UICommand;
import org.jboss.forge.addon.ui.context.UIBuilder;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.addon.ui.context.UIValidationContext;
import org.jboss.forge.addon.ui.input.UIInput;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.result.Result;
import org.jboss.forge.addon.ui.result.Results;
import org.jboss.forge.addon.ui.util.Categories;
import org.jboss.forge.addon.ui.util.Metadata;

/*
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

public class ExampleNoUICommand implements UICommand
{
@Inject
private UIInput<String> firstName;

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
builder.add(firstName);
}

@Override
Expand All @@ -40,7 +32,7 @@ public void validate(UIValidationContext context)
@Override
public Result execute(UIExecutionContext context) throws Exception
{
return Results.success();
return Results.success("Executed");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/

package org.jboss.forge.addon.ui.controller;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.ui.example.wizards.ExampleStepOne;
import org.jboss.forge.addon.ui.example.wizards.ExampleStepTwo;
import org.jboss.forge.addon.ui.example.wizards.ExampleWizard;
import org.jboss.forge.arquillian.AddonDependency;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.furnace.repositories.AddonDependencyEntry;
import org.jboss.forge.ui.test.UITestHarness;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@RunWith(Arquillian.class)
public class WizardCommandControllerTest
{
@Deployment
@Dependencies({
@AddonDependency(name = "org.jboss.forge.addon:ui-test-harness"),
@AddonDependency(name = "org.jboss.forge.addon:ui-example"),
@AddonDependency(name = "org.jboss.forge.addon:ui"),
@AddonDependency(name = "org.jboss.forge.furnace.container:cdi") })
public static ForgeArchive getDeployment()
{
ForgeArchive archive = ShrinkWrap
.create(ForgeArchive.class)
.addBeansXML()
.addAsAddonDependencies(
AddonDependencyEntry.create("org.jboss.forge.addon:ui-test-harness"),
AddonDependencyEntry.create("org.jboss.forge.addon:ui-example"),
AddonDependencyEntry.create("org.jboss.forge.addon:ui"),
AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi"));

return archive;
}

@Inject
UITestHarness testHarness;

@Test
public void testWizardExecution() throws Exception
{
try (WizardCommandController controller = testHarness.createWizardController(ExampleWizard.class))
{
Assert.assertFalse(controller.isInitialized());
controller.initialize();
Assert.assertTrue(controller.isInitialized());

Assert.assertFalse(controller.isValid());
controller.setValueFor("firstName", "Forge");
Assert.assertTrue(controller.isValid());

Assert.assertFalse(controller.canMoveToPreviousStep());
Assert.assertTrue(controller.canMoveToNextStep());
// Going to example Step One
controller.next();
Assert.assertThat(controller.getCommand(), is(instanceOf(ExampleStepOne.class)));
Assert.assertFalse(controller.isInitialized());
controller.initialize();
Assert.assertTrue(controller.isInitialized());

Assert.assertTrue(controller.canMoveToPreviousStep());
Assert.assertFalse(controller.isValid());
controller.setValueFor("address", "Foo street");
Assert.assertTrue(controller.isValid());
Assert.assertTrue(controller.canMoveToNextStep());

// Going back one page
controller.previous();
Assert.assertThat(controller.getCommand(), is(instanceOf(ExampleWizard.class)));
controller.setValueFor("goToLastStep", Boolean.TRUE);

// Should go to ExampleStepTwo
Assert.assertTrue(controller.canMoveToNextStep());
controller.next();
Assert.assertFalse(controller.isInitialized());
controller.initialize();
Assert.assertTrue(controller.isInitialized());
Assert.assertThat(controller.getCommand(), is(instanceOf(ExampleStepTwo.class)));

}
}
}

0 comments on commit fbcf3a4

Please sign in to comment.