/
CommandControllerTest.java
180 lines (160 loc) · 6.81 KB
/
CommandControllerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* 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 javax.inject.Inject;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.ui.command.ValidateRequiredCommand;
import org.jboss.forge.addon.ui.controller.mock.*;
import org.jboss.forge.addon.ui.impl.mock.MockUIContext;
import org.jboss.forge.addon.ui.impl.mock.MockUIRuntime;
import org.jboss.forge.addon.ui.result.Result;
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.shrinkwrap.api.ShrinkWrap;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Test class for the {@link CommandController} feature
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@RunWith(Arquillian.class)
public class CommandControllerTest
{
@Deployment
@Dependencies({ @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)
.addClasses(ExampleCommand.class, ExampleNoUICommand.class, FlowExampleStep.class,
ValidateRequiredCommand.class,
FlowExampleWizard.class, MockPreStepsCommand.class)
.addPackage(MockUIRuntime.class.getPackage())
.addBeansXML()
.addAsAddonDependencies(
AddonDependencyEntry.create("org.jboss.forge.addon:ui"),
AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi"));
return archive;
}
@Inject
private CommandControllerFactory controllerFactory;
@Inject
private ExampleCommand exampleCommand;
@Inject
private ExampleNoUICommand exampleNoUICommand;
@Inject
private FlowExampleWizard flowExampleWizard;
@Inject
private MockPreStepsCommand preStepsCommand;
@Inject
private ValidateRequiredCommand validateRequiredCommand;
@Test
public void testInjection() throws Exception
{
Assert.assertNotNull(controllerFactory);
}
@Test
public void testSingleCommandController() throws Exception
{
CommandController controller = controllerFactory.createSingleController(new MockUIContext(), new MockUIRuntime(),
exampleCommand);
Assert.assertTrue(controller.isEnabled());
Assert.assertFalse(controller.isInitialized());
controller.initialize();
Assert.assertFalse(controller.getInputs().isEmpty());
Assert.assertTrue(controller.getInputs().containsKey("firstName"));
Assert.assertNotNull(controller.getInput("firstName"));
Assert.assertTrue(controller.hasInput("firstName"));
Assert.assertNull(controller.getInput("dummy"));
Assert.assertFalse(controller.hasInput("dummy"));
controller.setValueFor("firstName", "Forge");
Assert.assertEquals("Forge", controller.getValueFor("firstName"));
Assert.assertTrue(controller.isValid());
Result result = controller.execute();
Assert.assertTrue(controller.isValid());
Assert.assertEquals("Hello, Forge", result.getMessage());
}
@Test(expected = IllegalArgumentException.class)
public void testInitialized() throws Exception
{
CommandController controller = controllerFactory.createSingleController(new MockUIContext(), new MockUIRuntime(),
exampleCommand);
Assert.assertFalse(controller.getInputs().isEmpty());
}
@Test
public void testExampleNoUI() throws Exception
{
CommandController controller = controllerFactory.createSingleController(new MockUIContext(), new MockUIRuntime(),
exampleNoUICommand);
controller.initialize();
Assert.assertTrue(controller.getInputs().isEmpty());
Assert.assertEquals(0, controller.getInputs().size());
Assert.assertNull(controller.getInput("firstName"));
Assert.assertFalse(controller.hasInput("firstName"));
Assert.assertTrue(controller.isValid());
Assert.assertTrue(controller.canExecute());
Result result = controller.execute();
Assert.assertEquals("Executed", result.getMessage());
}
@Test
public void testWizardCanMoveToNextStep() throws Exception
{
try (WizardCommandController controller = controllerFactory.createWizardController(new MockUIContext(),
new MockUIRuntime(),
flowExampleWizard))
{
controller.initialize();
Assert.assertFalse(controller.canMoveToNextStep());
controller.setValueFor("hasNext", Boolean.TRUE);
Assert.assertTrue(controller.canMoveToNextStep());
controller.next().initialize();
controller.previous();
Assert.assertTrue(controller.canMoveToNextStep());
controller.setValueFor("hasNext", Boolean.FALSE);
Assert.assertFalse(controller.canMoveToNextStep());
}
}
@Test
public void testPreStepsCommand() throws Exception
{
try (CommandController controller = controllerFactory.createController(new MockUIContext(), new MockUIRuntime(),
preStepsCommand))
{
Assert.assertThat(controller, instanceOf(WizardCommandController.class));
WizardCommandController wizardController = (WizardCommandController) controller;
wizardController.initialize();
Assert.assertThat(wizardController.getCommand(), instanceOf(ExampleCommand.class));
wizardController.setValueFor("firstName", "George");
Assert.assertTrue(wizardController.canMoveToNextStep());
wizardController.next().initialize();
Assert.assertThat(wizardController.getCommand(), instanceOf(MockPreStepsCommand.class));
wizardController.setValueFor("name", "George Gastaldi");
Assert.assertFalse(wizardController.canMoveToNextStep());
Assert.assertTrue(wizardController.canExecute());
}
}
@Test
public void testControllerShouldNotCallCommandValidateWhenRequiredInputsAreMissing() throws Exception
{
try (CommandController controller = controllerFactory.createController(new MockUIContext(), new MockUIRuntime(),
validateRequiredCommand))
{
controller.initialize();
Assert.assertFalse(controller.isValid());
controller.setValueFor("input", "Any value");
Assert.assertTrue(controller.isValid());
Assert.assertTrue(controller.canExecute());
}
}
}