Skip to content

Commit

Permalink
FORGE-2055: mocking UIProvider.isGUI for command names
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Oct 2, 2014
1 parent f3857ce commit 8901f8c
Show file tree
Hide file tree
Showing 7 changed files with 359 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
import javax.inject.Inject;
import javax.inject.Singleton;

import org.jboss.forge.addon.ui.UIProvider;
import org.jboss.forge.addon.ui.command.CommandFactory;
import org.jboss.forge.addon.ui.command.CommandProvider;
import org.jboss.forge.addon.ui.command.UICommand;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.impl.context.DelegatingUIContext;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.output.UIOutput;
import org.jboss.forge.addon.ui.util.Commands;
import org.jboss.forge.addon.ui.wizard.UIWizardStep;
import org.jboss.forge.furnace.addons.AddonRegistry;
Expand All @@ -38,9 +41,9 @@ public class CommandFactoryImpl implements CommandFactory
{
@Inject
private AddonRegistry registry;

private Set<UICommand> cache = Sets.getConcurrentSet();

private long version = -1;

private static final Logger log = Logger.getLogger(CommandFactoryImpl.class.getName());
Expand Down Expand Up @@ -111,31 +114,45 @@ public Set<String> getCommandNames(UIContext context)
@Override
public UICommand getCommandByName(UIContext context, String name)
{
for (UICommand cmd : getCommands())
{
String commandName = getCommandName(context, cmd);
if (Strings.compare(name, commandName) || Strings.compare(name, shellifyName(commandName)))
{
return cmd;
}
}
return null;
return findCommand(getCommands(), context, name);
}

@Override
public UICommand getNewCommandByName(UIContext context, String name)
{
for (UICommand cmd : getCommandsFromSource())
{
String commandName = getCommandName(context, cmd);
if (Strings.compare(name, commandName) || Strings.compare(name, shellifyName(commandName)))
return findCommand(getCommandsFromSource(), context, name);
}

private UICommand findCommand(Iterable<UICommand> commands, UIContext context, String name)
{
CommandNameUIProvider provider = new CommandNameUIProvider(context.getProvider());
final UIContext delegatingContext = new DelegatingUIContext(context, provider);
if (commands != null)
for (UICommand cmd : commands)
{
return cmd;
// Test non-gui command name
{
provider.setGUI(false);
String commandName = getCommandName(delegatingContext, cmd);
if (Strings.compare(name, commandName) || Strings.compare(name, shellifyName(commandName)))
{
return cmd;
}
}
// Test gui command name
{
provider.setGUI(true);
String commandName = getCommandName(delegatingContext, cmd);
if (Strings.compare(name, commandName) || Strings.compare(name, shellifyName(commandName)))
{
return cmd;
}
}
}
}
return null;

}

private Iterable<UICommand> getCachedCommands()
{
if (registry.getVersion() != version)
Expand All @@ -153,7 +170,7 @@ public void execute(UICommand command)
}
return cache;
}

private Iterable<UICommand> getCommandsFromSource()
{
final Set<UICommand> result = Sets.getConcurrentSet();
Expand All @@ -167,7 +184,7 @@ public void execute(UICommand command)
});
return result;
}

private void getCommands(Operation operation)
{
Imported<CommandProvider> instances = registry.getServices(CommandProvider.class);
Expand Down Expand Up @@ -199,17 +216,51 @@ private void getCommands(Operation operation)
*/
private static String shellifyName(String name)
{
return name.trim().toLowerCase().replaceAll("\\W+", "-").replaceAll("\\:", "");
return name != null ? name.trim().toLowerCase().replaceAll("\\W+", "-").replaceAll("\\:", "") : null;
}

/**
*
* Strategy for operation to be performed when iterating through Commands
* Strategy for operation to be performed when iterating through Commands
*
*/
interface Operation
{
void execute(UICommand command);
}

/**
* {@link UIProvider} implementation for querying the command name in GUI and non-GUI modes, which is a common use
* case for defining different names between GUI and CLI environments.
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
private static class CommandNameUIProvider implements UIProvider
{
private final UIProvider delegate;
private boolean gui;

public CommandNameUIProvider(UIProvider delegate)
{
this.delegate = delegate;
}

@Override
public boolean isGUI()
{
return gui;
}

public void setGUI(boolean gui)
{
this.gui = gui;
}

@Override
public UIOutput getOutput()
{
return delegate.getOutput();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright 2014 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.impl.context;

import java.util.Map;
import java.util.Set;

import org.jboss.forge.addon.ui.UIProvider;
import org.jboss.forge.addon.ui.command.CommandExecutionListener;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.context.UISelection;
import org.jboss.forge.furnace.spi.ListenerRegistration;

/**
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public class DelegatingUIContext implements UIContext
{
private final UIContext context;
private final UIProvider provider;

public DelegatingUIContext(UIContext context, UIProvider provider)
{
super();
this.context = context;
this.provider = provider;
}

@Override
public void close() throws Exception
{
context.close();
}

@Override
public Map<Object, Object> getAttributeMap()
{
return context.getAttributeMap();
}

@Override
public <SELECTIONTYPE> UISelection<SELECTIONTYPE> getInitialSelection()
{
return context.getInitialSelection();
}

@Override
public <SELECTIONTYPE> void setSelection(SELECTIONTYPE resource)
{
context.setSelection(resource);
}

@Override
public <SELECTIONTYPE> void setSelection(UISelection<SELECTIONTYPE> selection)
{
context.setSelection(selection);
}

@Override
public <SELECTIONTYPE> UISelection<SELECTIONTYPE> getSelection()
{
return context.getSelection();
}

@Override
public UIProvider getProvider()
{
return provider == null ? context.getProvider() : provider;
}

@Override
public ListenerRegistration<CommandExecutionListener> addCommandExecutionListener(CommandExecutionListener listener)
{
return context.addCommandExecutionListener(listener);
}

@Override
public Set<CommandExecutionListener> getListeners()
{
return context.getListeners();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.ui.controller.CommandController;
import org.jboss.forge.addon.ui.controller.mock.DifferentNameCommand;
import org.jboss.forge.addon.ui.controller.mock.ExampleCommand;
import org.jboss.forge.addon.ui.controller.mock.ExampleNoUICommand;
import org.jboss.forge.addon.ui.controller.mock.FlowExampleStep;
Expand Down Expand Up @@ -44,7 +45,7 @@ public static ForgeArchive getDeployment()
ForgeArchive archive = ShrinkWrap
.create(ForgeArchive.class)
.addClasses(ExampleCommand.class, ExampleNoUICommand.class, ExampleAnnotatedCommand.class,
FlowExampleStep.class)
FlowExampleStep.class, DifferentNameCommand.class)
.addPackage(MockUIRuntime.class.getPackage())
.addBeansXML()
.addAsAddonDependencies(
Expand Down Expand Up @@ -76,11 +77,12 @@ public void testGetCommands() throws Exception
Assert.assertTrue(ExampleCommand.class.equals(metadata.getType())
|| ExampleNoUICommand.class.equals(metadata.getType())
|| ExampleAnnotatedCommand.class.equals(metadata.getType())
|| DifferentNameCommand.class.equals(metadata.getType())
);
count++;
}

Assert.assertEquals(5, count);
Assert.assertEquals(6, count);
}

@Test
Expand All @@ -94,4 +96,14 @@ public void testCommandCache()
Assert.assertNotSame(command, commandFactory.getNewCommandByName(context, commandName));
}

@Test
public void testCommandByNameLookup()
{
MockUIContext context = new MockUIContext();
context.getProvider().setGUI(false);
Assert.assertNotNull(commandFactory.getCommandByName(context, "a-gui-command"));
Assert.assertNotNull(commandFactory.getCommandByName(context, "a-shell-command"));
Assert.assertNull(commandFactory.getCommandByName(context, "an-invalid-command"));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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.mock;

import javax.inject.Inject;

import org.jboss.forge.addon.ui.command.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;

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

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

@Override
public void validate(UIValidationContext context)
{
}

@Override
public Result execute(UIExecutionContext context) throws Exception
{
return Results.success("Hello, " + firstName.getValue());
}

@Override
public UICommandMetadata getMetadata(UIContext context)
{
return Metadata.forCommand(DifferentNameCommand.class).description("generic test command")
.name(context.getProvider().isGUI() ? "a-gui-command" : "a-shell-command")
.category(Categories.create("Example"));
}

@Override
public boolean isEnabled(UIContext context)
{
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
public class MockUIContext extends AbstractUIContext
{
private final UISelection<?> selection;
private MockUIProvider provider;

public MockUIContext()
{
this(Selections.emptySelection());
this.provider = new MockUIProvider(true);
}

public MockUIContext(UISelection<?> selection)
Expand All @@ -37,4 +39,10 @@ public <SELECTIONTYPE> UISelection<SELECTIONTYPE> getInitialSelection()
{
return (UISelection<SELECTIONTYPE>) selection;
}

@Override
public MockUIProvider getProvider()
{
return provider;
}
}
Loading

0 comments on commit 8901f8c

Please sign in to comment.