Skip to content

Commit

Permalink
FORGE-772: BeansPlugin += new-scope command
Browse files Browse the repository at this point in the history
  • Loading branch information
mbenson committed Jan 31, 2013
1 parent 70f3f2c commit c5b3dc4
Show file tree
Hide file tree
Showing 2 changed files with 188 additions and 2 deletions.
Expand Up @@ -24,14 +24,17 @@
import java.util.Set;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.NormalScope;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Stereotype;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Qualifier;
import javax.inject.Scope;

import org.jboss.forge.parser.JavaParser;
import org.jboss.forge.parser.java.Annotation;
import org.jboss.forge.parser.java.JavaAnnotation;
import org.jboss.forge.parser.java.JavaClass;
import org.jboss.forge.parser.java.Method;
Expand Down Expand Up @@ -352,12 +355,12 @@ public void newStereotype(
input = shell.promptMultiSelect("Select target element types", STEREOTYPE_TARGETS);
if (input.isEmpty())
{
shell.println(ShellColor.RED, "No target element types selected");
ShellMessages.error(shell, "No target element types selected");
continue;
}
if (input.contains(TYPE) && input.size() == 2)
{
shell.println(ShellColor.RED, "Invalid combination of target element types: " + input);
ShellMessages.error(shell, "Invalid combination of target element types: " + input);
continue;
}
break;
Expand All @@ -371,4 +374,50 @@ public void newStereotype(
java.saveJavaSource(stereotype);
pickup.fire(new PickupResource(java.getJavaResource(stereotype)));
}

@Command("new-scope")
public void newScope(
@Option(required = true,
name = "type") final JavaResource resource,
@Option(required = false, name = "overwrite") final boolean overwrite,
@Option(required = false, name = "pseudo", help = "mutually exclusive with 'passivating'") final boolean pseudo,
@Option(required = false, name = "passivating", help = "mutually exclusive with 'pseudo'") final boolean passivating
) throws FileNotFoundException
{
if (pseudo && passivating)
{
throw new RuntimeException("Cannot create a passivating pseudo-scope!");
}

if (resource.exists() && !overwrite)
{
throw new RuntimeException("Type already exists [" + resource.getFullyQualifiedName()
+ "] Re-run with '--overwrite' to continue.");
}

JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
JavaAnnotation scope = JavaParser.create(JavaAnnotation.class);
scope.setName(java.calculateName(resource));
scope.setPackage(java.calculatePackage(resource));

if (pseudo)
{
scope.addAnnotation(Scope.class);
}
else
{
Annotation<JavaAnnotation> normalScope = scope.addAnnotation(NormalScope.class);
if (passivating)
{
normalScope.setLiteralValue("passivating", Boolean.toString(true));
}
}
scope.addAnnotation(Retention.class).setEnumValue(RUNTIME);
scope.addAnnotation(Target.class).setEnumValue(TYPE, METHOD, FIELD);
scope.addAnnotation(Documented.class);

java.saveJavaSource(scope);
pickup.fire(new PickupResource(java.getJavaResource(scope)));
}

}
Expand Up @@ -14,11 +14,13 @@
import java.lang.annotation.Target;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.NormalScope;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Alternative;
import javax.enterprise.inject.Stereotype;
import javax.inject.Named;
import javax.inject.Qualifier;
import javax.inject.Scope;

import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.parser.JavaParser;
Expand Down Expand Up @@ -367,4 +369,139 @@ public void testStereotypeTargets() throws Exception
Assert.assertArrayEquals(new ElementType[] { ElementType.TYPE },
qualifier.getAnnotation(Target.class).getEnumArrayValue(ElementType.class));
}

@Test
public void testNewPseudoScope() throws Exception
{
final boolean pseudo = true;
final boolean passivating = false;
testNewScope(pseudo, passivating);
}

@Test
public void testNewNormalScope() throws Exception
{
final boolean pseudo = false;
final boolean passivating = false;
testNewScope(pseudo, passivating);
}

@Test
public void testNewPassivatingScope() throws Exception
{
final boolean pseudo = false;
final boolean passivating = true;
testNewScope(pseudo, passivating);
}

@Test(expected = RuntimeException.class)
public void testNewPassivatingPseudoScope() throws Exception
{
final boolean pseudo = true;
final boolean passivating = true;
testNewScope(pseudo, passivating);
}

private void testNewScope(boolean pseudo, boolean passivating) throws Exception
{
Project project = initializeJavaProject();
queueInputLines("y", "");
getShell().execute("beans setup");
String command = "beans new-scope --type foo.beans.ExampleScope";
if (pseudo)
{
command += " --pseudo";
}
if (passivating)
{
command += " --passivating";
}

getShell().execute(command);

JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
JavaResource resource = java.getJavaResource("foo.beans.ExampleScope");

Assert.assertTrue(resource.exists());
JavaSource<?> source = resource.getJavaSource();

Assert.assertNotNull(source);
Assert.assertEquals("foo.beans", source.getPackage());
Assert.assertTrue(source.isAnnotation());
JavaAnnotation scope = (JavaAnnotation) source;

if (pseudo)
{
Assert.assertFalse(scope.hasAnnotation(NormalScope.class));
Assert.assertTrue(scope.hasAnnotation(Scope.class));
Assert.assertTrue(scope.getAnnotation(Scope.class).isMarker());
}
else
{
Assert.assertFalse(scope.hasAnnotation(Scope.class));
Assert.assertTrue(scope.hasAnnotation(NormalScope.class));
if (passivating)
{
Assert.assertEquals(Boolean.toString(true), scope.getAnnotation(NormalScope.class).getLiteralValue("passivating"));
}
else
{
Assert.assertTrue(scope.getAnnotation(NormalScope.class).isMarker());
}
}

Assert.assertTrue(scope.hasAnnotation(Retention.class));
Assert.assertSame(RetentionPolicy.RUNTIME,
scope.getAnnotation(Retention.class).getEnumValue(RetentionPolicy.class));

Assert.assertTrue(scope.hasAnnotation(Target.class));
JavaAnnotation stub = JavaParser.parse(JavaAnnotation.class, "public @interface Stub {}");
stub.addAnnotation(Target.class).setEnumValue(ElementType.TYPE, ElementType.METHOD, ElementType.FIELD);
Assert.assertEquals(stub.getAnnotation(Target.class).getLiteralValue(), scope.getAnnotation(Target.class)
.getLiteralValue());

Assert.assertTrue(scope.hasAnnotation(Documented.class));
Assert.assertTrue(scope.getAnnotation(Documented.class).isMarker());

project.getFacet(CDIFacet.class).getConfig();
}

@Test(expected = RuntimeException.class)
public void testCannotOverwriteScope() throws Exception
{
Project project = initializeJavaProject();
queueInputLines("y", "");
getShell().execute("beans setup");
String command = "beans new-scope --type foo.beans.ExampleScope";
getShell().execute(command);

JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
JavaResource resource = java.getJavaResource("foo.beans.ExampleScope");

Assert.assertTrue(resource.exists());
getShell().execute(command);
}

@Test
public void testOverwriteScope() throws Exception
{
Project project = initializeJavaProject();
queueInputLines("y", "");
getShell().execute("beans setup");
String command = "beans new-scope --type foo.beans.ExampleScope";
getShell().execute(command);

JavaSourceFacet java = project.getFacet(JavaSourceFacet.class);
JavaResource resource = java.getJavaResource("foo.beans.ExampleScope");

Assert.assertTrue(resource.exists());
Assert.assertTrue(JavaAnnotation.class.cast(resource.getJavaSource()).hasAnnotation(NormalScope.class));
Assert.assertFalse(JavaAnnotation.class.cast(resource.getJavaSource()).hasAnnotation(Scope.class));

getShell().execute(command + " --overwrite --pseudo");
// reload:
resource = java.getJavaResource("foo.beans.ExampleScope");
Assert.assertFalse(JavaAnnotation.class.cast(resource.getJavaSource()).hasAnnotation(NormalScope.class));
Assert.assertTrue(JavaAnnotation.class.cast(resource.getJavaSource()).hasAnnotation(Scope.class));
}
}

0 comments on commit c5b3dc4

Please sign in to comment.