Skip to content

Commit

Permalink
Fixed resource generator resolution and added mocked out test for the…
Browse files Browse the repository at this point in the history
… NewProjectPlugin
  • Loading branch information
lincolnthree committed Jan 17, 2013
1 parent 0b6d916 commit 9abbc40
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
Expand Down Expand Up @@ -47,8 +48,8 @@ public class ContainerServiceExtension implements Extension
private static Logger logger = Logger.getLogger(ContainerServiceExtension.class.getName());

private Map<Class<?>, AnnotatedType<?>> services = new HashMap<Class<?>, AnnotatedType<?>>();
private Map<Class<?>, InjectionPoint> requestedServices = new HashMap<Class<?>, InjectionPoint>();
private static final ServiceLiteral SERVICE_LITERAL = new ServiceLiteral();
private Map<InjectionPoint, Class<?>> requestedServices = new HashMap<InjectionPoint, Class<?>>();
private Map<InjectionPoint, ServiceLiteral> requestedServiceLiterals = new HashMap<InjectionPoint, ServiceLiteral>();

public void processRemotes(@Observes ProcessAnnotatedType<?> event) throws InstantiationException,
IllegalAccessException
Expand Down Expand Up @@ -83,8 +84,10 @@ public void processRemoteInjectionPointConsumer(@Observes ProcessInjectionPoint<
}
else
{
event.setInjectionPoint(new ExportedInstanceInjectionPoint(event.getInjectionPoint(), SERVICE_LITERAL));
requestedServices.put(injectionBeanValueType, event.getInjectionPoint());
ServiceLiteral serviceLiteral = new ServiceLiteral();
event.setInjectionPoint(new ExportedInstanceInjectionPoint(event.getInjectionPoint(), serviceLiteral));
requestedServices.put(event.getInjectionPoint(), injectionBeanValueType);
requestedServiceLiterals.put(event.getInjectionPoint(), serviceLiteral);
}
}
else if (remote != null)
Expand All @@ -106,16 +109,21 @@ public void processProducerHooks(@Observes ProcessProducer<?, ?> event, BeanMana
}
}

public void wireCrossContainerEvents(@Observes AfterBeanDiscovery event, final BeanManager manager)
public void wireCrossContainerServicesAndEvents(@Observes AfterBeanDiscovery event, final BeanManager manager)
{
event.addObserverMethod(new CrossContainerObserverMethod());

// needs to happen in the addon that is requesting the service
for (final Entry<Class<?>, InjectionPoint> entry : requestedServices.entrySet())
for (final Entry<InjectionPoint, Class<?>> entry : requestedServices.entrySet())
{
final InjectionPoint injectionPoint = entry.getKey();
Set<Type> typeClosure = injectionPoint.getAnnotated().getTypeClosure();
Class<?> beanClass = entry.getValue();
final Member member = injectionPoint.getMember();

Bean<?> serviceBean = new BeanBuilder<Object>(manager)
.beanClass(entry.getKey())
.types(entry.getValue().getAnnotated().getTypeClosure())
.beanClass(beanClass)
.types(typeClosure)
.beanLifecycle(new ContextualLifecycle<Object>()
{
@Override
Expand All @@ -127,8 +135,6 @@ public void destroy(Bean<Object> bean, Object instance, CreationalContext<Object
@Override
public Object create(Bean<Object> bean, CreationalContext<Object> creationalContext)
{
InjectionPoint injectionPoint = entry.getValue();
Member member = injectionPoint.getMember();
Class<?> serviceType = null;
if (member instanceof Method)
{
Expand All @@ -155,7 +161,7 @@ else if (member instanceof Field)
));
}
})
.qualifiers(SERVICE_LITERAL)
.qualifiers(requestedServiceLiterals.get(injectionPoint))
.create();

event.addBean(serviceBean);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
@Target({ ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD })
public @interface Service
{

int id();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@

final class ServiceLiteral implements Service
{
private static int INSTANCE_COUNT = 0;

private int id;

public ServiceLiteral()
{
this.id = uniqueId();
}

@Override
public Class<? extends Annotation> annotationType()
{
return Service.class;
}

@Override
public int id()
{
return id;
}

public static int uniqueId()
{
return INSTANCE_COUNT++;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,27 @@ public Result execute(UIContext context) throws Exception
return Result.success();
}

/*
* Getters
*/

public UIInput<String> getNamed()
{
return named;
}

public UIInput<DirectoryResource> getTargetDirectory()
{
return targetDirectory;
}

public UIInput<Boolean> getOverwrite()
{
return overwrite;
}

public UIInput<ProjectType> getType()
{
return type;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package org.jboss.forge.projects.impl;

/*
* Copyright 2012 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
*/

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.forge.arquillian.Addon;
import org.jboss.forge.arquillian.Dependencies;
import org.jboss.forge.arquillian.archive.ForgeArchive;
import org.jboss.forge.container.AddonDependency;
import org.jboss.forge.container.AddonId;
import org.jboss.forge.resource.DirectoryResource;
import org.jboss.forge.ui.UIBuilder;
import org.jboss.forge.ui.UIContext;
import org.jboss.forge.ui.UIInput;
import org.jboss.forge.ui.UIValidationContext;
import org.jboss.shrinkwrap.api.ArchivePaths;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class NewProjectCommandTest
{
@Deployment
@Dependencies({
@Addon(name = "org.jboss.forge:facets", version = "2.0.0-SNAPSHOT"),
@Addon(name = "org.jboss.forge:resources", version = "2.0.0-SNAPSHOT"),
@Addon(name = "org.jboss.forge:ui", version = "2.0.0-SNAPSHOT"),
@Addon(name = "org.jboss.forge:projects", version = "2.0.0-SNAPSHOT")
})
// FIXME We should not need to explicitly list the entire dependency graph, fix addon manager
public static ForgeArchive getDeployment()
{
ForgeArchive archive = ShrinkWrap
.create(ForgeArchive.class)
.addAsManifestResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml"))
.addAsAddonDependencies(
AddonDependency.create(AddonId.from("org.jboss.forge:resources", "2.0.0-SNAPSHOT")),
AddonDependency.create(AddonId.from("org.jboss.forge:projects", "2.0.0-SNAPSHOT")),
AddonDependency.create(AddonId.from("org.jboss.forge:ui", "2.0.0-SNAPSHOT"))
// FIXME We should not need to explicitly list the entire dependency graph, fix addon XML transitivity
);

return archive;
}

@Inject
private NewProjectCommand command;

@Test
public void testInjectionNotNull()
{
Assert.assertNotNull(command);
}

@Test
public void testInvokeCommand() throws Exception
{
final List<UIInput<?>> inputs = new ArrayList<UIInput<?>>();

final UIBuilder builder = new UIBuilder()
{
@Override
public UIBuilder add(UIInput<?> input)
{
inputs.add(input);
return this;
}
};

UIContext context = new UIContext()
{
@Override
public UIBuilder getUIBuilder()
{
return builder;
}
};

command.initializeUI(context);
command.getNamed().setValue("test");

command.validate(new UIValidationContext()
{
@Override
public UIBuilder getUIBuilder()
{
return builder;
}

@Override
public void addValidationError(UIInput<?> input, String errorMessage)
{
// TODO implement
}
});

command.execute(context);

DirectoryResource targetDirectory = command.getTargetDirectory().getValue();
Assert.assertTrue(targetDirectory.exists());
targetDirectory.delete(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
* @author Mike Brock
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
*/
public abstract class FileResource<T extends FileResource<?>> extends AbstractResource<File>
public abstract class FileResource<T extends FileResource<T>> extends AbstractResource<File>
{
protected boolean scratch;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public <E, T extends Resource<E>> T create(final Class<T> type, final E underlyi
for (ExportedInstance<ResourceGenerator> instance : getRegisteredResourceGenerators())
{
ResourceGenerator generator = instance.get();
if (generator.handles(underlyingResource))
if (generator.handles(type, underlyingResource))
{
if (type.isAssignableFrom(generator.getResourceType(underlyingResource)))
if (type.isAssignableFrom(generator.getResourceType(type, underlyingResource)))
{
Resource<?> resource = generator.getResource(this, underlyingResource);
Resource<?> resource = generator.getResource(this, type, underlyingResource);
return (T) resource;
}
}
Expand All @@ -61,9 +61,9 @@ public <E> Resource<E> create(E underlyingResource)
for (ExportedInstance<ResourceGenerator> instance : getRegisteredResourceGenerators())
{
ResourceGenerator generator = instance.get();
if (generator.handles(underlyingResource))
if (generator.handles(Resource.class, underlyingResource))
{
Resource<?> resource = generator.getResource(this, underlyingResource);
Resource<?> resource = generator.getResource(this, Resource.class, underlyingResource);
return (Resource<E>) resource;
}
instance.release(generator);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package org.jboss.forge.resource;

public interface ResourceGenerator<UNDERLYINGTYPE>
public interface ResourceGenerator<RESOURCETYPE, UNDERLYINGTYPE>
{
public Class<? extends Resource<UNDERLYINGTYPE>> getResourceType(UNDERLYINGTYPE resource);
public boolean handles(Class<?> type, final Object resource);

public <T extends Resource<UNDERLYINGTYPE>> T getResource(final ResourceFactory factory,
public <T extends Resource<UNDERLYINGTYPE>> T getResource(final ResourceFactory factory, Class<RESOURCETYPE> type,
final UNDERLYINGTYPE resource);

public boolean handles(final Object resource);
public <T extends Resource<UNDERLYINGTYPE>> Class<?> getResourceType(Class<RESOURCETYPE> type, final UNDERLYINGTYPE resource);

}
Loading

0 comments on commit 9abbc40

Please sign in to comment.