Search in sources :

Example 1 with MockControl

use of org.easymock.MockControl in project felix by apache.

the class ServiceRegistryTest method testRegisterFindHookService.

public void testRegisterFindHookService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    FindHook hook = new FindHook() {

        @Override
        public void find(BundleContext context, String name, String filter, boolean allServices, Collection references) {
        }
    };
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { FindHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : MockControl(org.easymock.MockControl) FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 2 with MockControl

use of org.easymock.MockControl in project felix by apache.

the class ServiceRegistryTest method testRegisterEventHookService.

public void testRegisterEventHookService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    EventHook hook = new EventHook() {

        @Override
        public void event(ServiceEvent event, Collection contexts) {
        }
    };
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { EventHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertTrue(sr.getHookRegistry().getHooks(EventHook.class).iterator().next() instanceof ServiceReference);
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : EventHook(org.osgi.framework.hooks.service.EventHook) MockControl(org.easymock.MockControl) Bundle(org.osgi.framework.Bundle) ServiceEvent(org.osgi.framework.ServiceEvent) Hashtable(java.util.Hashtable) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) ServiceReference(org.osgi.framework.ServiceReference)

Example 3 with MockControl

use of org.easymock.MockControl in project felix by apache.

the class ServiceRegistryTest method testRegisterCombinedService.

public void testRegisterCombinedService() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    class CombinedService implements ListenerHook, FindHook, EventHook, Runnable {

        @Override
        public void added(Collection listeners) {
        }

        @Override
        public void removed(Collection listener) {
        }

        @Override
        public void find(BundleContext context, String name, String filter, boolean allServices, Collection references) {
        }

        @Override
        public void event(ServiceEvent event, Collection contexts) {
        }

        @Override
        public void run() {
        }
    }
    CombinedService hook = new CombinedService();
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { Runnable.class.getName(), ListenerHook.class.getName(), FindHook.class.getName(), EventHook.class.getName() }, hook, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(ListenerHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals(1, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(EventHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
    assertSame(hook, ((ServiceRegistrationImpl) reg).getService());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) ListenerHook(org.osgi.framework.hooks.service.ListenerHook) Hashtable(java.util.Hashtable) EventHook(org.osgi.framework.hooks.service.EventHook) MockControl(org.easymock.MockControl) ServiceEvent(org.osgi.framework.ServiceEvent) Collection(java.util.Collection) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 4 with MockControl

use of org.easymock.MockControl in project felix by apache.

the class ServiceRegistryTest method testRegisterFindHookServiceFactory.

public void testRegisterFindHookServiceFactory() {
    MockControl control = MockControl.createNiceControl(Bundle.class);
    Bundle b = (Bundle) control.getMock();
    control.replay();
    MockControl controlContext = MockControl.createNiceControl(BundleContext.class);
    BundleContext c = (BundleContext) controlContext.getMock();
    controlContext.expectAndReturn(c.getBundle(), b);
    controlContext.replay();
    ServiceRegistry sr = new ServiceRegistry(new Logger(), null);
    MockControl sfControl = MockControl.createNiceControl(ServiceFactory.class);
    sfControl.replay();
    ServiceFactory sf = (ServiceFactory) sfControl.getMock();
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Precondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    ServiceRegistration reg = sr.registerService(c.getBundle(), new String[] { FindHook.class.getName() }, sf, new Hashtable());
    assertEquals(1, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertSame(reg.getReference(), sr.getHookRegistry().getHooks(FindHook.class).iterator().next());
    assertSame(sf, ((ServiceRegistrationImpl) reg).getService());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Postcondition failed", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
    sr.unregisterService(b, reg);
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(EventHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(FindHook.class).size());
    assertEquals("Should be no hooks left after unregistration", 0, sr.getHookRegistry().getHooks(ListenerHook.class).size());
}
Also used : MockControl(org.easymock.MockControl) ServiceFactory(org.osgi.framework.ServiceFactory) FindHook(org.osgi.framework.hooks.service.FindHook) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 5 with MockControl

use of org.easymock.MockControl in project maven-archetype by apache.

the class DefaultArchetypeGenerationConfigurator2Test method setUp.

public void setUp() throws Exception {
    super.setUp();
    configurator = (DefaultArchetypeGenerationConfigurator) lookup(ArchetypeGenerationConfigurator.ROLE);
    ProjectBuildingRequest buildingRequest = null;
    // MavenRepositorySystemSession repositorySession = new MavenRepositorySystemSession();
    // repositorySession.setLocalRepositoryManager( new SimpleLocalRepositoryManager( localRepository.getBasedir() ) );
    // buildingRequest.setRepositorySession( repositorySession );
    // request.setProjectBuildingRequest( buildingRequest );
    MockControl control = MockControl.createControl(ArchetypeArtifactManager.class);
    control.setDefaultMatcher(MockControl.ALWAYS_MATCHER);
    ArchetypeArtifactManager manager = (ArchetypeArtifactManager) control.getMock();
    manager.exists("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
    control.setReturnValue(true);
    manager.isFileSetArchetype("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
    control.setReturnValue(true);
    manager.isOldArchetype("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
    control.setReturnValue(false);
    manager.getFileSetArchetypeDescriptor("archetypeGroupId", "archetypeArtifactId", "archetypeVersion", null, null, null, buildingRequest);
    ArchetypeDescriptor descriptor = new ArchetypeDescriptor();
    RequiredProperty groupId = new RequiredProperty();
    groupId.setKey("groupId");
    groupId.setDefaultValue("com.example.${groupName}");
    RequiredProperty artifactId = new RequiredProperty();
    artifactId.setKey("artifactId");
    artifactId.setDefaultValue("${serviceName}");
    RequiredProperty thePackage = new RequiredProperty();
    thePackage.setKey("package");
    thePackage.setDefaultValue("com.example.${groupName}");
    RequiredProperty groupName = new RequiredProperty();
    groupName.setKey("groupName");
    groupName.setDefaultValue(null);
    RequiredProperty serviceName = new RequiredProperty();
    serviceName.setKey("serviceName");
    serviceName.setDefaultValue(null);
    descriptor.addRequiredProperty(groupId);
    descriptor.addRequiredProperty(artifactId);
    descriptor.addRequiredProperty(thePackage);
    descriptor.addRequiredProperty(groupName);
    descriptor.addRequiredProperty(serviceName);
    control.setReturnValue(descriptor);
    control.replay();
    configurator.setArchetypeArtifactManager(manager);
}
Also used : ProjectBuildingRequest(org.apache.maven.project.ProjectBuildingRequest) MockControl(org.easymock.MockControl) RequiredProperty(org.apache.maven.archetype.metadata.RequiredProperty) ArchetypeDescriptor(org.apache.maven.archetype.metadata.ArchetypeDescriptor) ArchetypeArtifactManager(org.apache.maven.archetype.common.ArchetypeArtifactManager)

Aggregations

MockControl (org.easymock.MockControl)21 Hashtable (java.util.Hashtable)8 Bundle (org.osgi.framework.Bundle)8 BundleContext (org.osgi.framework.BundleContext)8 ServiceRegistration (org.osgi.framework.ServiceRegistration)8 Archetype (org.apache.maven.archetype.catalog.Archetype)7 Prompter (org.codehaus.plexus.components.interactivity.Prompter)6 ArrayList (java.util.ArrayList)5 List (java.util.List)5 ArchetypeGenerationRequest (org.apache.maven.archetype.ArchetypeGenerationRequest)5 Collection (java.util.Collection)4 ArchetypeDefinition (org.apache.maven.archetype.ui.ArchetypeDefinition)4 ServiceFactory (org.osgi.framework.ServiceFactory)3 EventHook (org.osgi.framework.hooks.service.EventHook)3 FindHook (org.osgi.framework.hooks.service.FindHook)3 ListenerHook (org.osgi.framework.hooks.service.ListenerHook)3 ArchetypeArtifactManager (org.apache.maven.archetype.common.ArchetypeArtifactManager)2 UnknownArchetype (org.apache.maven.archetype.exception.UnknownArchetype)2 ProjectBuildingRequest (org.apache.maven.project.ProjectBuildingRequest)2 ServiceEvent (org.osgi.framework.ServiceEvent)2