Skip to content

Commit

Permalink
FORGE-1739: Handling exceptions gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Apr 7, 2014
1 parent 70f3356 commit 477a2f6
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.inject.Inject;
import javax.inject.Singleton;
Expand All @@ -18,6 +20,7 @@
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.metadata.UICommandMetadata;
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 @@ -35,6 +38,8 @@ public class CommandFactoryImpl implements CommandFactory
@Inject
private AddonRegistry registry;

private static final Logger log = Logger.getLogger(CommandFactoryImpl.class.getName());

@Override
public Iterable<UICommand> getCommands()
{
Expand Down Expand Up @@ -67,9 +72,16 @@ public Set<String> getEnabledCommandNames(UIContext context)
Iterable<UICommand> allCommands = getCommands();
for (UICommand cmd : allCommands)
{
if (Commands.isEnabled(cmd, context))
try
{
if (Commands.isEnabled(cmd, context))
{
commands.add(getCommandName(context, cmd));
}
}
catch (Exception e)
{
commands.add(getCommandName(context, cmd));
log.log(Level.SEVERE, "Error while checking if command " + cmd + " isEnabled", e);
}
}
return commands;
Expand All @@ -78,10 +90,19 @@ public Set<String> getEnabledCommandNames(UIContext context)
@Override
public String getCommandName(UIContext context, UICommand cmd)
{
String name = cmd.getMetadata(context).getName();
if (!context.getProvider().isGUI())
String name = null;
try
{
name = shellifyName(name);
UICommandMetadata metadata = cmd.getMetadata(context);
name = metadata.getName();
if (!context.getProvider().isGUI())
{
name = shellifyName(name);
}
}
catch (Exception e)
{
log.log(Level.SEVERE, "Error while getting command name for " + cmd.getClass(), e);
}
return name;
}
Expand All @@ -92,7 +113,11 @@ public Set<String> getCommandNames(UIContext context)
Set<String> commands = new TreeSet<>();
for (UICommand cmd : getCommands())
{
commands.add(getCommandName(context, cmd));
String commandName = getCommandName(context, cmd);
if (commandName != null)
{
commands.add(commandName);
}
}
return commands;
}
Expand All @@ -102,7 +127,8 @@ public UICommand getCommandByName(UIContext context, String name)
{
for (UICommand cmd : getCommands())
{
if (name.equals(getCommandName(context, cmd)))
String commandName = getCommandName(context, cmd);
if (name.equals(commandName))
{
return cmd;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* 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.command;

import java.util.List;
import java.util.Set;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.addon.ui.context.UIContext;
import org.jboss.forge.addon.ui.controller.CommandController;
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;
import org.jboss.forge.addon.ui.example.commands.ExampleAnnotatedCommand;
import org.jboss.forge.addon.ui.impl.mock.MockUIContext;
import org.jboss.forge.addon.ui.impl.mock.MockUIRuntime;
import org.jboss.forge.addon.ui.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.test.impl.UIContextImpl;
import org.jboss.forge.addon.ui.util.Selections;
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.furnace.util.Lists;
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 CommandFactoryExceptionTest
{
@Deployment
@Dependencies({ @AddonDependency(name = "org.jboss.forge.addon:ui"),
@AddonDependency(name = "org.jboss.forge.addon:ui-test-harness") })
public static ForgeArchive getDeployment()
{
ForgeArchive archive = ShrinkWrap
.create(ForgeArchive.class)
.addClasses(ExceptionCommand.class, ExampleCommand.class, ExampleNoUICommand.class,
ExampleAnnotatedCommand.class,
FlowExampleStep.class)
.addPackage(MockUIRuntime.class.getPackage())
.addBeansXML()
.addAsAddonDependencies(
AddonDependencyEntry.create("org.jboss.forge.addon:ui"),
AddonDependencyEntry.create("org.jboss.forge.addon:ui-test-harness"),
AddonDependencyEntry.create("org.jboss.forge.furnace.container:cdi"));

return archive;
}

@Inject
private CommandFactory commandFactory;

@Test
public void testInjection() throws Exception
{
Assert.assertNotNull(commandFactory);
}

@Test
public void testNoExceptionsOnEnabledCommandNames()
{
UIContext context = new UIContextImpl(true, Selections.emptySelection());
Set<String> enabledCommandNames = commandFactory.getEnabledCommandNames(context);
Assert.assertEquals(4, enabledCommandNames.size());
}

@Test
public void testNoExceptionsOnCommandsNames()
{
UIContext context = new UIContextImpl(true, Selections.emptySelection());
Set<String> commandNames = commandFactory.getCommandNames(context);
Assert.assertEquals(4, commandNames.size());

}

@Test
public void testNoExceptionsOnUICommands()
{
List<UICommand> list = Lists.toList(commandFactory.getCommands());
Assert.assertEquals(5, list.size());

}

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

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.metadata.UICommandMetadata;
import org.jboss.forge.addon.ui.result.Result;

public class ExceptionCommand extends AbstractUICommand
{
@Override
public boolean isEnabled(UIContext context)
{
throw new NullPointerException();
}

@Override
public UICommandMetadata getMetadata(UIContext context)
{
throw new NullPointerException();
}

@Override
public void initializeUI(UIBuilder builder) throws Exception
{
}

@Override
public Result execute(UIExecutionContext context) throws Exception
{
return null;
}

}

0 comments on commit 477a2f6

Please sign in to comment.