org.jboss.forge.furnace.addons.AddonRegistry Java Examples

The following examples show how to use org.jboss.forge.furnace.addons.AddonRegistry. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExportedInstanceApiTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testExportedInstanceExposesServiceTypeAndSourceAddon() throws Exception
{
   Furnace furnace = LocalServices.getFurnace(getClass().getClassLoader());
   Assert.assertNotNull(furnace);
   AddonRegistry registry = furnace.getAddonRegistry();
   boolean found = false;
   for (Addon addon : registry.getAddons())
   {
      ExportedInstance<ExportedInstanceApiTest> instance = addon.getServiceRegistry()
               .getExportedInstance(ExportedInstanceApiTest.class);
      if (instance != null)
      {
         found = true;
         Assert.assertEquals(ExportedInstanceApiTest.class, instance.getActualType());
         Assert.assertEquals(addon, instance.getSourceAddon());
         break;
      }
   }
   Assert.assertTrue("Could not locate service in any addon.", found);
}
 
Example #2
Source File: SidewaysProxyAnonymousCollisionTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testSidewaysCollision() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader A = this.getClass().getClassLoader();
   ClassLoader B = registry.getAddon(AddonId.from("B", "1")).getClassLoader();
   ClassLoader C = registry.getAddon(AddonId.from("C", "1")).getClassLoader();

   Class<?> typeAction1 = B.loadClass(Action1.class.getName());
   Action action1 = getProxiedInstance(A, B, typeAction1);

   Class<?> typePayload1 = C.loadClass(Payload1.class.getName());
   Payload payload1 = getProxiedInstance(A, C, typePayload1);

   Context context = new ContextImpl();
   ContextValue<Payload> value = new ContextValueImpl<Payload>();
   value.set(payload1);
   context.set(value);

   action1.handle(context);
}
 
Example #3
Source File: ClassLoaderAdapterCollisionsTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testReturnTypeNativeAccessAfterParameterTypeEnhancementFromEnhancedClass() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderAdapterCollisionsTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep1", "1")).getClassLoader();
   ClassLoader dep2Loader = registry.getAddon(AddonId.from("dep2", "2")).getClassLoader();

   ClassImplementsInterfaceWithArrayParameterModification modifier = (ClassImplementsInterfaceWithArrayParameterModification) ClassLoaderAdapterBuilder
            .callingLoader(thisLoader)
            .delegateLoader(dep1Loader)
            .enhance(dep1Loader.loadClass(ClassImplementsInterfaceWithArrayParameterModification.class.getName())
                     .newInstance());

   modifier.setValueClassLoader(dep2Loader);

   List<InterfaceValue> values = new ArrayList<>();
   modifier.modifyParameter(new ClassImplementsInterfaceModifiableContext(values));
   InterfaceValue result = values.get(0);
   ClassImplementsInterfaceExtendsInterfaceValue value = (ClassImplementsInterfaceExtendsInterfaceValue) result;
   Assert.assertNotNull(value);
}
 
Example #4
Source File: ClassLoaderAdapterWhitelistLoaderPassthroughTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testProxyNotPropagatedIfClassLoadersBothInWhitelist() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderAdapterWhitelistLoaderPassthroughTest.class.getClassLoader();
   ClassLoader loader1 = registry.getAddon(AddonId.from("dep1", "1")).getClassLoader();
   ClassLoader loader2 = registry.getAddon(AddonId.from("dep2", "1")).getClassLoader();
   ClassLoader loader3 = registry.getAddon(AddonId.from("dep3", "1")).getClassLoader();

   MockContext context = new MockContext();

   Object delegate = loader1.loadClass(MockContextConsumer.class.getName()).newInstance();
   MockContextConsumer enhancedConsumer = (MockContextConsumer) ClassLoaderAdapterBuilder
            .callingLoader(thisLoader).delegateLoader(loader1)
            .whitelist(new HashSet<>(Arrays.asList(loader1, loader2, loader3)))
            .enhance(delegate);

   Object payload = loader2.loadClass(MockContextPayloadImpl.class.getName()).newInstance();
   context.getAttributes().put(MockContextPayload.class.getName(), payload);

   enhancedConsumer.processContext(context);

   Object object = context.getAttributes().get(MockContextPayload.class.getName());
   Assert.assertFalse(Proxies.isForgeProxy(object));
}
 
Example #5
Source File: ClassLoaderAdapterWhitelistLoaderLookupTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testWhitelistLookupConvertsClassReturnTypes() throws Exception
{
   ClassLoader thisLoader = ClassLoaderAdapterWhitelistLoaderLookupTest.class.getClassLoader();

   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();

   ClassLoader loader1 = registry.getAddon(AddonId.from("dep1", "1")).getClassLoader();
   ClassLoader loader2 = registry.getAddon(AddonId.from("dep2", "1")).getClassLoader();
   ClassLoader loader3 = registry.getAddon(AddonId.from("dep3", "1")).getClassLoader();

   AddonRegistry enhancedRegistry = ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(loader2)
            .whitelist(new HashSet<>(Arrays.asList(loader1, loader3))).enhance(registry);

   Assert.assertNotSame(MockContextConsumer.class, registry.getServices(MockContextConsumer.class.getName()).get()
            .getClass());

   Assert.assertNotSame(MockContextConsumer.class, enhancedRegistry.getServices(MockContextConsumer.class)
            .get().getClass());

   Assert.assertSame(MockContextConsumer.class, enhancedRegistry.getServices(MockContextConsumer.class)
            .get().getNativeClass());
}
 
Example #6
Source File: ClassLoaderAdapterJavaIOTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testSimpleFileProxy() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderAdapterJavaIOTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();

   Class<?> foreignType = dep1Loader.loadClass(JavaIOFactory.class.getName());
   File file = (File) foreignType.getMethod("getFile")
            .invoke(foreignType.newInstance());

   Assert.assertNotNull(file);
   Assert.assertTrue(file.getClass().equals(File.class));

   Object delegate = foreignType.newInstance();
   JavaIOFactory enhancedFactory = (JavaIOFactory) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).enhance(delegate);

   Assert.assertTrue(Proxies.isForgeProxy(enhancedFactory));
   File result = enhancedFactory.getFile();
   Assert.assertFalse(Proxies.isForgeProxy(result));

   enhancedFactory.useFile(new File("foo"));
}
 
Example #7
Source File: CLACProxiedCollectionsTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testIterableTypesAreProxied() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = CLACProxiedCollectionsTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();

   Class<?> foreignType = dep1Loader.loadClass(ProfileCommand.class.getName());

   Object delegate = foreignType.newInstance();
   ProfileCommand enhanced = (ProfileCommand) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).enhance(delegate);

   ProfileManagerImpl manager = new ProfileManagerImpl();
   enhanced.setManager(manager);
   enhanced.configureProfile();
   Assert.assertTrue(Proxies.isForgeProxy(enhanced));
}
 
Example #8
Source File: CLACProxiedCollectionsReturnTypeUnwrappingTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testCollectionsReturnUnwrappedResultsIfUnwrappedTypeIsCompatible() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = CLACProxiedCollectionsReturnTypeUnwrappingTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep1", "1")).getClassLoader();
   ClassLoader dep2Loader = registry.getAddon(AddonId.from("dep2", "2")).getClassLoader();

   Class<?> foreignProfileType = dep1Loader.loadClass(ProfileFactory.class.getName());
   Object delegate = foreignProfileType.newInstance();
   ProfileFactory factory = (ProfileFactory) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).whitelist(Sets.toSet(Arrays.asList(dep1Loader, dep2Loader)))
            .enhance(delegate);

   Profile profile = factory.createProfile();

   ProfileManager manager = factory.createProfileManager();

   manager.setProfileListCallGet(Arrays.asList(profile));
}
 
Example #9
Source File: CLACProxiedIterableTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testCustomIterableTypesAreProxied() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = CLACProxiedIterableTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();

   Class<?> foreignType = dep1Loader.loadClass(IterableFactory.class.getName());
   Iterable<?> proxy = (Iterable<?>) foreignType.getMethod("getIterable")
            .invoke(foreignType.newInstance());
   Assert.assertFalse(Proxies.isForgeProxy(proxy));

   Object delegate = foreignType.newInstance();
   IterableFactory enhancedFactory = (IterableFactory) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).enhance(delegate);

   Assert.assertTrue(Proxies.isForgeProxy(enhancedFactory));
   Iterable<?> enhancedInstance = enhancedFactory.getCustomIterable();
   Assert.assertTrue(Proxies.isForgeProxy(enhancedInstance));

   Iterator<?> iterator = enhancedInstance.iterator();
   Assert.assertNotNull(iterator);
}
 
Example #10
Source File: CLACProxiedIterableTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testIterableTypesAreProxied() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = CLACProxiedIterableTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();

   Class<?> foreignType = dep1Loader.loadClass(IterableFactory.class.getName());
   Iterable<?> proxy = (Iterable<?>) foreignType.getMethod("getIterable")
            .invoke(foreignType.newInstance());
   Assert.assertFalse(Proxies.isForgeProxy(proxy));

   Object delegate = foreignType.newInstance();
   IterableFactory enhancedFactory = (IterableFactory) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).enhance(delegate);

   Assert.assertTrue(Proxies.isForgeProxy(enhancedFactory));
   Iterable<?> enhancedInstance = enhancedFactory.getIterable();
   Assert.assertTrue(Proxies.isForgeProxy(enhancedInstance));

   Iterator<?> iterator = enhancedInstance.iterator();
   Assert.assertNotNull(iterator);
}
 
Example #11
Source File: ClassLoaderAdapterJavaOptionalTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testOptionalProxyAsParameter() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderAdapterJavaOptionalTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();
   Class<?> loadedType = dep1Loader.loadClass(MockOptionalService.class.getName());
   Object delegate = loadedType.newInstance();
   MockOptionalService enhanced = (MockOptionalService) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).enhance(delegate);
   MockParentInterface1 mpi = new MockParentInterface1()
   {
      @Override
      public Object getResult()
      {
         return "My Result";
      }
   };
   Assert.assertThat(enhanced.getResult(Optional.empty()), nullValue());
   Assert.assertThat(enhanced.getResult(Optional.of(mpi)), equalTo("My Result"));
}
 
Example #12
Source File: ClassLoaderAdapterPassthroughTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testParameterPassthroughIfTypeIsShared() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderAdapterPassthroughTest.class.getClassLoader();
   ClassLoader loader1 = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();

   ClassWithGetterAndSetter local = new ClassWithGetterAndSetter();
   local.setPassthrough((ClassWithPassthroughMethod) loader1
            .loadClass(ClassWithPassthroughMethod.class.getName())
            .newInstance());

   Object delegate = loader1.loadClass(ClassWithGetterAndSetter.class.getName()).newInstance();
   ClassWithGetterAndSetter enhanced = (ClassWithGetterAndSetter) ClassLoaderAdapterBuilder
            .callingLoader(thisLoader).delegateLoader(loader1).enhance(delegate);

   enhanced.setPassthrough(new ClassWithPassthroughMethod());

   Assert.assertNotNull(enhanced);
   Assert.assertNotNull(enhanced.getPassthrough());
   Assert.assertFalse(Proxies.isForgeProxy(enhanced.getPassthrough()));
   Assert.assertFalse(enhanced.assertPassthroughNotProxied());
}
 
Example #13
Source File: ClassLoaderAdapterJavaUtilLoggingTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testLogRecordProxy() throws Exception
{
    AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
    ClassLoader thisLoader = ClassLoaderAdapterJavaUtilLoggingTest.class.getClassLoader();
    ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();

    Class<?> foreignType = dep1Loader.loadClass(JavaUtilLoggingFactory.class.getName());
    LogRecord logRecord = (LogRecord) foreignType.getMethod("getLogRecord")
            .invoke(foreignType.newInstance());

    Assert.assertNotNull(logRecord);
    Assert.assertTrue(logRecord.getClass().equals(LogRecord.class));

    Object delegate = foreignType.newInstance();
    JavaUtilLoggingFactory enhancedFactory = (JavaUtilLoggingFactory) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).enhance(delegate);

    Assert.assertTrue(Proxies.isForgeProxy(enhancedFactory));
    LogRecord result = enhancedFactory.getLogRecord();
    Assert.assertFalse(Proxies.isForgeProxy(result));
}
 
Example #14
Source File: ProxyMethodHandlerDispatchTest.java    From furnace with Eclipse Public License 1.0 6 votes vote down vote up
@Test
@Ignore
public void testProxyCallsDelegateAppropriately() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();

   Imported<ConcreteC> imported = registry.getServices(ConcreteC.class);
   ConcreteC c = imported.get();
   Assert.assertNotNull(c);

   String payload = "PAYLOAD";
   c.setPayload(payload);

   Assert.assertEquals(payload, c.getPayload());
   Assert.assertEquals(payload.toString(), c.toString());
}
 
Example #15
Source File: FurnaceProducer.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
public void setup(File repoDir) {
	furnace = FurnaceFactory.getInstance(Thread.currentThread()
			.getContextClassLoader(), Thread.currentThread()
			.getContextClassLoader());
	furnace.addRepository(AddonRepositoryMode.IMMUTABLE, repoDir);
	Future<Furnace> future = furnace.startAsync();

	try {
		future.get();
	} catch (InterruptedException | ExecutionException e) {
		throw new RuntimeException("Furnace failed to start.", e);
	}

	AddonRegistry addonRegistry = furnace.getAddonRegistry();
	commandFactory = addonRegistry.getServices(CommandFactory.class).get();
	controllerFactory = (CommandControllerFactory) addonRegistry
			.getServices(CommandControllerFactory.class.getName()).get();
       dependencyResolver = (DependencyResolver) addonRegistry
               .getServices(DependencyResolver.class.getName()).get();
}
 
Example #16
Source File: AddonRepositoryLoadingTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testAddonRepositoryIsCorrectInMultiViewEnvironment() throws Exception
{
   Furnace furnace = LocalServices.getFurnace(getClass().getClassLoader());
   Assert.assertNotNull(furnace);
   AddonRegistry registry = furnace.getAddonRegistry();
   AddonRepository rep1 = registry.getAddon(AddonId.from("dep1", "1")).getRepository();
   AddonRepository rep2 = registry.getAddon(AddonId.from("dep2", "2")).getRepository();
   AddonRepository rep3 = registry.getAddon(AddonId.from("dep3", "3")).getRepository();
   AddonRepository rep4 = registry.getAddon(AddonId.from("dep4", "4")).getRepository();
   AddonRepository rep5 = registry.getAddon(AddonId.from("dep5", "5")).getRepository();
   Assert.assertEquals(rep1, rep2);
   Assert.assertEquals(rep3, rep4);
   Assert.assertEquals(rep4, rep5);
}
 
Example #17
Source File: CommandsResource.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected ResourceFactory getResourceFactory() {
    AddonRegistry addonRegistry = furnace.getAddonRegistry();
    Imported<ResourceFactory> resourceFactoryImport = addonRegistry.getServices(ResourceFactory.class);
    ResourceFactory resourceFactory = null;
    try {
        resourceFactory = resourceFactoryImport.get();
    } catch (Exception e) {
        LOG.warn("Failed to get ResourceFactory injected: " + e, e);
    }
    if (resourceFactory == null) {
        // lets try one more time - might work this time?
        resourceFactory = resourceFactoryImport.get();
    }
    return resourceFactory;
}
 
Example #18
Source File: ImportedLookupTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test(expected = ContainerException.class)
public void testDoesNotResolveNonService() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();

   Imported<MockServiceConsumer> importedByName = registry.getServices(MockServiceConsumer.class.getName());
   Assert.assertTrue(importedByName.isUnsatisfied());
   importedByName.get();
}
 
Example #19
Source File: ServiceLookupTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void shouldResolveImpls() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   Imported<MockInterface> imported = registry.getServices(MockInterface.class);
   Assert.assertTrue(imported.isAmbiguous());
   Assert.assertEquals(3, Iterators.asList(imported).size());
   Assert.assertThat(registry.getExportedTypes(MockInterface.class).size(), equalTo(3));
   Assert.assertThat(imported, CoreMatchers.<MockInterface> hasItems(
            instanceOf(MockImpl1.class),
            instanceOf(MockImpl2.class),
            instanceOf(SubMockImpl1.class)));
}
 
Example #20
Source File: PreShutdownEventTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test(timeout = 5000)
public void testPreShutdownIsCalled() throws Exception
{
   Furnace furnace = LocalServices.getFurnace(getClass().getClassLoader());
   AddonRegistry registry = furnace.getAddonRegistry();
   Addon dep2 = registry.getAddon(AddonId.from("dep2", "2"));
   RecordingEventManager manager = registry.getServices(RecordingEventManager.class).get();
   Assert.assertEquals(3, manager.getPostStartupCount());
   MutableAddonRepository repository = (MutableAddonRepository) furnace.getRepositories().get(0);
   repository.disable(dep2.getId());
   Addons.waitUntilStopped(dep2);
   Assert.assertEquals(1, manager.getPreShutdownCount());
}
 
Example #21
Source File: ImportedTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testIsAmbiguous() throws Exception
{
   Furnace furnace = LocalServices.getFurnace(getClass().getClassLoader());
   AddonRegistry registry = furnace.getAddonRegistry();
   Imported<MockInterface> services = registry.getServices(MockInterface.class);
   Assert.assertFalse(services.isUnsatisfied());
   Assert.assertTrue(services.isAmbiguous());
}
 
Example #22
Source File: ImportedTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testIsAmbiguousUsingClassName() throws Exception
{
   Furnace furnace = LocalServices.getFurnace(getClass().getClassLoader());
   AddonRegistry registry = furnace.getAddonRegistry();
   Imported<MockInterface> services = registry.getServices(MockInterface.class.getName());
   Assert.assertFalse(services.isUnsatisfied());
   Assert.assertTrue(services.isAmbiguous());
}
 
Example #23
Source File: ImportedImpl.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
public ImportedImpl(AddonRegistry addonRegistry, LockManager lock, Class<T> type)
{
   this.addonRegistry = addonRegistry;
   this.lock = lock;
   this.type = type;
   this.typeName = type.getName();
}
 
Example #24
Source File: PostStartupEventTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testPostStartupIsCalled() throws Exception
{
   Furnace furnace = LocalServices.getFurnace(getClass().getClassLoader());
   AddonRegistry registry = furnace.getAddonRegistry();
   RecordingEventManager manager = registry.getServices(RecordingEventManager.class).get();
   Assert.assertEquals(2, manager.getPostStartupCount());
}
 
Example #25
Source File: BootstrapClassLoaderTestCase.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void shouldBeAbleToUseFactoryDelegateTypesafely() throws Exception
{
   Furnace instance = FurnaceFactory.getInstance();
   Assert.assertNotNull(instance);
   AddonRegistry registry = instance.getAddonRegistry();
   Assert.assertNotNull(registry);
}
 
Example #26
Source File: ClassLoaderParameterUnwrappedTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testUnwrapUnknownClassParameter() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderParameterUnwrappedTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep1", "1")).getClassLoader();
   ClassLoader dep2Loader = registry.getAddon(AddonId.from("dep2", "2")).getClassLoader();

   Class<?> foreignType = dep2Loader.loadClass(MockResult.class.getName());
   Object delegate = foreignType.newInstance();
   MockResult enhancedResult = (MockResult) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep2Loader).enhance(delegate);
   Assert.assertTrue(Proxies.isForgeProxy(enhancedResult));

   Object foreignInstance = dep1Loader
            .loadClass(ClassWithClassAsParameter.class.getName())
            .getConstructor(Class.class)
            .newInstance(foreignType);

   ClassLoaderAdapterBuilderDelegateLoader builder = ClassLoaderAdapterBuilder
            .callingLoader(thisLoader)
            .delegateLoader(dep1Loader);

   Object enhancedFilter = builder.enhance(foreignInstance);

   ClassWithClassAsParameter classFilter = (ClassWithClassAsParameter) enhancedFilter;

   Assert.assertTrue(Proxies.isForgeProxy(classFilter));

   Class<? extends MockResult> enhancedResultType = enhancedResult.getClass();
   Assert.assertTrue(classFilter.verify(enhancedResultType));
   Assert.assertFalse(classFilter.isProxyType(enhancedResultType));
   Assert.assertTrue(classFilter.verify(delegate.getClass()));
   Assert.assertFalse(classFilter.isProxyType(delegate.getClass()));
   Assert.assertTrue(classFilter.verify(foreignType));
   Assert.assertFalse(classFilter.isProxyType(foreignType));

}
 
Example #27
Source File: ClassLoaderParameterUnwrappedTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testUnwrapParameterIfDelegateClassLoaderIsShared() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderParameterUnwrappedTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep1", "1")).getClassLoader();
   ClassLoader dep2Loader = registry.getAddon(AddonId.from("dep2", "2")).getClassLoader();

   Class<?> foreignType = dep2Loader.loadClass(MockResult.class.getName());
   Object delegate = foreignType.newInstance();
   MockResult enhancedValue = (MockResult) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep2Loader).enhance(delegate);
   Assert.assertTrue(Proxies.isForgeProxy(enhancedValue));

   Object foreignInstance = dep1Loader
            .loadClass(ClassWithClassAsParameter.class.getName())
            .getConstructor(Class.class)
            .newInstance(foreignType);

   ClassLoaderAdapterBuilderDelegateLoader builder = ClassLoaderAdapterBuilder
            .callingLoader(thisLoader)
            .delegateLoader(dep1Loader);

   Object enhancedFilter = builder.enhance(foreignInstance);

   ClassWithClassAsParameter classFilter = (ClassWithClassAsParameter) enhancedFilter;

   Assert.assertTrue(Proxies.isForgeProxy(classFilter));

   Assert.assertEquals(foreignInstance.hashCode(), classFilter.hashCode());
   Assert.assertTrue(Proxies.isForgeProxy(enhancedValue));
   Assert.assertFalse(classFilter.isProxyObject(enhancedValue));
   Assert.assertTrue(classFilter.equals(classFilter));
   Assert.assertTrue(Proxies.isForgeProxy(classFilter));
   Assert.assertFalse(classFilter.isProxyObject(classFilter));
}
 
Example #28
Source File: ClassLoaderParameterUnwrappedTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void testUnwrapClassParameter() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderParameterUnwrappedTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep1", "1")).getClassLoader();

   Class<?> foreignType = dep1Loader.loadClass(IterableFactory.class.getName());
   Object delegate = foreignType.newInstance();
   IterableFactory enhancedFactory = (IterableFactory) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).enhance(delegate);
   Assert.assertTrue(Proxies.isForgeProxy(enhancedFactory));

   Object foreignInstance = dep1Loader
            .loadClass(ClassWithClassAsParameter.class.getName())
            .getConstructor(Class.class)
            .newInstance(foreignType);

   ClassLoaderAdapterBuilderDelegateLoader builder = ClassLoaderAdapterBuilder
            .callingLoader(thisLoader)
            .delegateLoader(dep1Loader);

   Object enhancedFilter = builder.enhance(foreignInstance);

   ClassWithClassAsParameter classFilter = (ClassWithClassAsParameter) enhancedFilter;

   Assert.assertTrue(Proxies.isForgeProxy(classFilter));

   Assert.assertTrue(classFilter.verify(foreignType));
   Assert.assertTrue(classFilter.verify(delegate.getClass()));
   Assert.assertTrue(classFilter.verify(enhancedFactory.getClass()));

}
 
Example #29
Source File: CommandsResource.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
public ConverterFactory getConverterFactory() {
    if (converterFactory == null) {
        AddonRegistry addonRegistry = furnace.getAddonRegistry();
        Imported<ConverterFactory> converterFactoryImport = addonRegistry.getServices(ConverterFactory.class);
        converterFactory = converterFactoryImport.get();
    }
    return converterFactory;
}
 
Example #30
Source File: ClassLoaderListParameterProxiedTest.java    From furnace with Eclipse Public License 1.0 5 votes vote down vote up
@Test
public void test() throws Exception
{
   AddonRegistry registry = LocalServices.getFurnace(getClass().getClassLoader())
            .getAddonRegistry();
   ClassLoader thisLoader = ClassLoaderListParameterProxiedTest.class.getClassLoader();
   ClassLoader dep1Loader = registry.getAddon(AddonId.from("dep", "1")).getClassLoader();

   Class<?> foreignType = dep1Loader.loadClass(MockResult.class.getName());
   Object delegateResult = foreignType.newInstance();
   MockResult enhancedResult = (MockResult) ClassLoaderAdapterBuilder.callingLoader(thisLoader)
            .delegateLoader(dep1Loader).enhance(delegateResult);
   Assert.assertTrue(Proxies.isForgeProxy(enhancedResult));

   Object foreignInstance = dep1Loader
            .loadClass(ClassWithListAsParameter.class.getName())
            .newInstance();

   ClassLoaderAdapterBuilderDelegateLoader builder = ClassLoaderAdapterBuilder
            .callingLoader(thisLoader)
            .delegateLoader(dep1Loader);

   Object enhancedFilter = builder.enhance(foreignInstance);

   ClassWithListAsParameter classFilter = (ClassWithListAsParameter) enhancedFilter;

   Assert.assertTrue(Proxies.isForgeProxy(classFilter));

   List<Object> list = new ArrayList<Object>();
   list.add(enhancedResult);
   list.add(delegateResult);
   Assert.assertTrue(classFilter.verify(list));

}