Skip to content

Commit

Permalink
Merge pull request #520 from agoncal/FORGE-2091
Browse files Browse the repository at this point in the history
FORGE-2091: Being able to create a new Java exception
  • Loading branch information
gastaldi committed Oct 31, 2014
2 parents 79490af + 64b81d7 commit 7bcd2dd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* 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.parser.java.ui;

import org.jboss.forge.addon.ui.command.UICommand;

/**
* @author <a href="mailto:antonio.goncalves@gmail.com">Antonio Goncalves</a>
*/
public interface JavaExceptionCommand extends UICommand
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* 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.parser.java.ui;

import org.jboss.forge.addon.projects.Project;
import org.jboss.forge.addon.ui.context.UIExecutionContext;
import org.jboss.forge.roaster.model.source.JavaClassSource;

/**
* @author <a href="mailto:antonio.goncalves@gmail.com">Antonio Goncalves</a>
*/
public class JavaExceptionCommandImpl extends AbstractJavaSourceCommand<JavaClassSource> implements
JavaExceptionCommand
{

@Override
public JavaClassSource decorateSource(UIExecutionContext context, Project project, JavaClassSource source)
throws Exception
{
source.setSuperType(RuntimeException.class);
source.addMethod().setPublic().setConstructor(true).setBody("super();");
source.addMethod().setPublic().setConstructor(true).setParameters("String message").setBody("super(message);");

return source;
}

@Override
protected Class<JavaClassSource> getSourceType()
{
return JavaClassSource.class;
}

@Override
protected String getType()
{
return "Exception";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ public void testCreateClass() throws Exception
Assert.assertThat(result, is(not(instanceOf(Failed.class))));
}

@Test
public void testCreateException() throws Exception
{
Project project = projectFactory.createTempProject();
facetFactory.install(project, JavaSourceFacet.class);

CommandController controller = testHarness.createCommandController(JavaExceptionCommand.class, project.getRoot());
controller.initialize();
controller.setValueFor("named", "MyException");
controller.setValueFor("targetPackage", "org.jboss.forge.test");

Assert.assertTrue(controller.isValid());
Assert.assertTrue(controller.canExecute());
Result result = controller.execute();
Assert.assertThat(result, is(not(instanceOf(Failed.class))));

JavaSourceFacet facet = project.getFacet(JavaSourceFacet.class);
JavaResource javaResource = facet.getJavaResource("org.jboss.forge.test.MyException");
Assert.assertNotNull(javaResource);
Assert.assertThat(javaResource.getJavaType(), is(instanceOf(JavaClass.class)));
}

@Test
public void testCreateInterface() throws Exception
{
Expand Down

0 comments on commit 7bcd2dd

Please sign in to comment.