Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutEventProviderImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutEventProviderImpl.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutEventProviderImpl.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutEventProviderImpl.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.donut; + +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.Random; + +import org.apache.felix.ipojo.handlers.event.publisher.Publisher; +import org.apache.felix.ipojo.test.util.EahTestUtils; + +/** + * Implementation of a donut vendor that send raw events instead of sending + * Donut objects. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + * + */ +public class DonutEventProviderImpl implements DonutProvider { + + /** + * The donut current serial number. + */ + private long m_serial = 0L; + + /** + * The name of the donut vendor. + */ + private String m_name; + + /** + * A random generator. + */ + private Random m_random; + + /** + * The event publisher of the donut vendor. + */ + private Publisher m_publisher; + + /** + * Construct a new donut provider. The initial serial number is randomly + * generated. + */ + public DonutEventProviderImpl() { + m_random = new Random(System.currentTimeMillis()); + } + + /** + * Sell a donut with a random flavour. + * + * @return the sold donut + */ + public Donut sellDonut() { + Dictionary rawEvent = new Hashtable(); + String flavour = Donut.FLAVOURS[m_random.nextInt(Donut.FLAVOURS.length)]; + Donut donut = new Donut(m_serial++, m_name, flavour); + rawEvent.put("food", donut); + rawEvent.put("flavour", flavour); + m_publisher.send(rawEvent); + if (EahTestUtils.TRACE) { + System.err.println("[" + this.getClass().getSimpleName() + ":" + + m_name + "] Selling event donut " + donut); + } + return donut; + } +}
Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutProvider.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutProvider.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutProvider.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutProvider.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.donut; + +/** + * Specification of a donut vendor. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + * + */ +public interface DonutProvider { + + /** + * Sell a donut with a random flavour. + * + * @return the sold donut + */ + Donut sellDonut(); +} Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutProviderImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutProviderImpl.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutProviderImpl.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/DonutProviderImpl.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.donut; + +import java.util.Random; + +import org.apache.felix.ipojo.handlers.event.publisher.Publisher; +import org.apache.felix.ipojo.test.util.EahTestUtils; + +/** + * The standard implementation of a donut vendor. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + * + */ +public class DonutProviderImpl implements DonutProvider { + + + + /** + * The donut current serial number. + */ + private long m_serial = 0L; + + /** + * The name of the donut vendor. + */ + private String m_name; + + /** + * A random generator. + */ + private Random m_random; + + /** + * The donut publisher of the vendor. + */ + private Publisher m_publisher; + + /** + * Construct a new donut provider. The initial serial number is randomly + * generated. + */ + public DonutProviderImpl() { + m_random = new Random(System.currentTimeMillis()); + } + + /** + * Sell a donut with a random flavour. + * + * @return the sold donut + */ + public Donut sellDonut() { + Donut donut = new Donut(m_serial++, m_name, Donut.FLAVOURS[m_random + .nextInt(Donut.FLAVOURS.length)]); + m_publisher.sendData(donut); + if (EahTestUtils.TRACE) { + System.err.println("[" + this.getClass().getSimpleName() + ":" + + m_name + "] Selling donut " + donut); + } + return donut; + } +} Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventConsumerImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventConsumerImpl.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventConsumerImpl.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventConsumerImpl.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,217 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.donut; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.felix.ipojo.test.util.EahTestUtils; +import org.osgi.service.event.Event; +import org.osgi.service.event.EventHandler; + +/** + * Implementation of a donut consumer. + * + * @see Homer Simpson + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + */ +public class EventConsumerImpl implements DonutConsumer, EventHandler { + + /** + * The name of the donut consumer. + */ + private String m_name; + + /** + * The list of eaten donuts. + */ + private List m_donuts = new ArrayList(); + + /** + * Is this consumer a slow eater ? + */ + private boolean m_isSlow; + + /** + * Process incoming donuts. This method is called by the receiveDonut + * callback. + * + * @param donut + * the received donut + */ + private void doReceiveDonut(Donut donut) { + synchronized (m_donuts) { + m_donuts.add(donut); + m_donuts.notify(); + if (EahTestUtils.TRACE) { + System.err.println("[" + this.getClass().getSimpleName() + ":" + + m_name + "] Eating donut " + donut); + } + } + } + + /** + * Utility method that causes the current thread to sleep. + * + * @param millis + * the number of milliseconds to wait + */ + public static void sleep(long millis) { + long past = System.currentTimeMillis(); + long future = past + millis; + long now = past; + while (now < future) { + try { + Thread.sleep(future - now); + } catch (Exception e) { + } + now = System.currentTimeMillis(); + } + } + + /** + * Donut receiver callback. This method is called when a donut is received + * on the listened topic. + * + * @param donut + * the received donut + */ + public void receiveDonut(Donut donut) { + final Donut myDonut = donut; + if (m_isSlow) { + new Thread(new Runnable() { + public void run() { + sleep(EahTestUtils.BLACK_LIST_TIME); + doReceiveDonut(myDonut); + } + }, m_name + " eating " + donut).start(); + } else { + doReceiveDonut(donut); + } + } + + /** + * Event donut receiver callback. This method is called when an event is + * received on the listened topic. + * + * @param event + * the received event + */ + public void receiveEvent(Event event) { + Object thing = event.getProperty("food"); + if (Donut.class.isInstance(thing)) { + receiveDonut((Donut) thing); + } else { + if (EahTestUtils.TRACE) { + System.err.println("[" + this.getClass().getSimpleName() + ":" + + m_name + "] D'oh ! Received an uneatable thing : " + + thing); + throw new ClassCastException("I want DONUTS !"); + } + } + } + + /** + * Event receiver callback. This method is called by the event admin service + * when a event is received. + * + * @param event + * the received event + */ + public void handleEvent(Event event) { + receiveEvent(event); + } + + /** + * Clear the eaten donuts list. (Useful before tests) + */ + public void clearDonuts() { + synchronized (m_donuts) { + m_donuts.clear(); + } + } + + /** + * Get the first received donut and remove it from the eaten donut list. + * + * @return the first received donut or null if no donut is available + */ + public Donut getDonut() { + Donut donut = null; + synchronized (m_donuts) { + if (!m_donuts.isEmpty()) { + donut = (Donut) m_donuts.remove(0); + } + } + return donut; + } + + /** + * Get the whole list of eaten donuts. + * + * @return the array containing all eaten donuts + */ + public Donut[] getAllDonuts() { + Donut[] donuts = new Donut[0]; + synchronized (m_donuts) { + donuts = (Donut[]) m_donuts.toArray(donuts); + m_donuts.clear(); + } + return donuts; + } + + /** + * Get the first donut if available or wait for an incoming donut. The + * returned donut is removed from the eaten donut list. + * + * @return the first available donut. + */ + public Donut waitForDonut() { + Donut donut = null; + synchronized (m_donuts) { + while (donut == null) { + if (m_donuts.isEmpty()) { + try { + m_donuts.wait(); + } catch (InterruptedException e) { + // Thanks Checkstyle to forbid empty catch statements + // ;-( + } + } else { + donut = (Donut) m_donuts.remove(0); + } + } + } + return donut; + } + + /** + * Return the size of the eaten donut list. + * + * @return the size of the eaten donut list + */ + public int getNumberOfDonuts() { + int length; + synchronized (m_donuts) { + length = m_donuts.size(); + } + return length; + } +} Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventTracker.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventTracker.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventTracker.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventTracker.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.donut; + +import org.osgi.service.event.Event; + +/** + * Specification of an event tracker. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + */ +public interface EventTracker { + + /** + * Clear the received events list. (Useful before tests) + */ + void clearEvents(); + + /** + * Get the first received event and remove it from the events list. + * + * @return the first received event or null if no event is available + */ + Event getEvent(); + + /** + * Get the whole list of received events. + * + * @return the array containing all received events + */ + Event[] getAllEvents(); + + /** + * Get the first event if available or wait for an incoming event. The + * returned event is removed from the eaten event list. + * + * @return the first available event. + */ + Event waitForEvent(); + + /** + * Return the size of the events list. + * + * @return the size of the events list + */ + int getNumberOfEvents(); +} Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventTrackerImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventTrackerImpl.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventTrackerImpl.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/EventTrackerImpl.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.donut; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.felix.ipojo.test.util.EahTestUtils; +import org.osgi.service.event.Event; +import org.osgi.service.event.EventHandler; + +/** + * Implementation of an event tracker. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + */ +public class EventTrackerImpl implements EventTracker, EventHandler { + + /** + * The name of the event tracker. + */ + private String m_name; + + /** + * The list of received events + */ + private List m_events = new ArrayList(); + + /** + * Event receiver callback. This method is called by the event admin service + * when a event is received. + * + * @param event + * the received event + */ + public void handleEvent(Event event) { + synchronized (m_events) { + m_events.add(event); + m_events.notify(); + if (EahTestUtils.TRACE) { + System.err.println("[" + this.getClass().getSimpleName() + ":" + + m_name + "] Event received : " + event); + } + } + } + + /** + * Clear the received events list. (Useful before tests) + */ + public void clearEvents() { + synchronized (m_events) { + m_events.clear(); + } + } + + /** + * Get the first received event and remove it from the events list. + * + * @return the first received event or null if no event is available + */ + public Event getEvent() { + Event event = null; + synchronized (m_events) { + if (!m_events.isEmpty()) { + event = (Event) m_events.remove(0); + } + } + return event; + } + + /** + * Get the whole list of received events. + * + * @return the array containing all received events + */ + public Event[] getAllEvents() { + Event[] events = new Event[0]; + synchronized (m_events) { + events = (Event[]) m_events.toArray(events); + m_events.clear(); + } + return events; + } + + /** + * Get the first event if available or wait for an incoming event. The + * returned event is removed from the eaten event list. + * + * @return the first available event. + */ + public Event waitForEvent() { + Event event = null; + synchronized (m_events) { + while (event == null) { + if (m_events.isEmpty()) { + try { + m_events.wait(); + } catch (InterruptedException e) { + // Thanks Checkstyle to forbid empty catch statements + // ;-( + } + } else { + event = (Event) m_events.remove(0); + } + } + } + return event; + } + + /** + * Return the size of the events list. + * + * @return the size of the events list + */ + public int getNumberOfEvents() { + int length; + synchronized (m_events) { + length = m_events.size(); + } + return length; + } + + /** + * Return the string representation of a given event. + * + * @return the string representation of a given event + */ + public static String eventToString(Event e) { + StringBuilder buf = new StringBuilder(); + buf.append("[" + e.getTopic() + "] {"); + + String[] properties = e.getPropertyNames(); + int n = properties.length - 1; + for (int i = 0; i <= n; i++) { + String name = properties[i]; + buf.append(name + "=" + e.getProperty(name)); + if (i != n) + buf.append(", "); + } + buf.append("}"); + return buf.toString(); + } +} Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/SyncEventProviderImpl.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/SyncEventProviderImpl.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/SyncEventProviderImpl.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/donut/SyncEventProviderImpl.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.donut; + +import java.util.Dictionary; +import java.util.Hashtable; +import java.util.Random; + +import org.apache.felix.ipojo.test.util.EahTestUtils; +import org.osgi.service.event.Event; +import org.osgi.service.event.EventAdmin; + +/** + * Implementation of an event vendor that directly uses the Event Admin service + * to send (synchronously) raw events. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + * + */ +public class SyncEventProviderImpl implements DonutProvider { + + /** + * The donut current serial number. + */ + private long m_serial = 0L; + + /** + * The name of the donut vendor. + */ + private String m_name; + + /** + * A random generator. + */ + private Random m_random; + + /** + * The Event Admin service reference. + */ + private EventAdmin m_ea; + + /** + * Construct a new donut provider. The initial serial number is randomly + * generated. + */ + public SyncEventProviderImpl() { + m_random = new Random(System.currentTimeMillis()); + } + + /** + * Sell a donut with a random flavour. + * + * @return the sold donut + */ + public Donut sellDonut() { + Dictionary rawEvent = new Hashtable(); + Donut donut = new Donut(m_serial++, m_name, Donut.FLAVOURS[m_random + .nextInt(Donut.FLAVOURS.length)]); + rawEvent.put("food", donut); + m_ea.sendEvent(new Event("food/donuts", rawEvent)); + if (EahTestUtils.TRACE) { + System.err.println("[" + this.getClass().getSimpleName() + ":" + + m_name + "] Selling donut " + donut); + } + return donut; + } +} Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/util/EahTestUtils.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/util/EahTestUtils.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/util/EahTestUtils.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/util/EahTestUtils.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,197 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.util; + +import org.apache.felix.ipojo.Factory; +import org.apache.felix.ipojo.test.donut.Donut; +import org.osgi.framework.BundleContext; + +/** + * Useful variables used for the tests of the Event Admin Handler. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + */ +public class EahTestUtils { + + /** + * Enable debug messages ? + */ + public static final boolean TRACE = false; + + /** + * The number of tests to execute. + */ + public static final int NUMBER_OF_TESTS = 50; + + /** + * The time that is normally necessary to cause a blacklist from the event + * admin service. + */ + public static final long BLACK_LIST_TIME = 5000L; + + /** + * The long amount of time. + */ + public static final long A_LONG_TIME = 1000L; + + /** + * The bundle context. + */ + private BundleContext m_context; + + /** + * Construct a new Event Admin Handler Tests utility instance. + * + * @param context + * the bundle context + */ + public EahTestUtils(BundleContext context) { + m_context = context; + } + + /** + * Return the (asynchronous) donut provider factory. + * + * @return the (asynchronous) donut provider factory + */ + public Factory getDonutProviderFactory() { + return IPojoTestUtils.getFactoryByName(m_context, "donut-provider"); + } + + /** + * Return the synchronous donut provider factory. + * + * @return the synchronous donut provider factory + */ + public Factory getSynchronousDonutProviderFactory() { + return IPojoTestUtils.getFactoryByName(m_context, + "synchronous-donut-provider"); + } + + /** + * Return the (asynchronous) donut event provider factory. + * + * @return the (asynchronous) donut event provider factory + */ + public Factory getDonutEventProviderFactory() { + return IPojoTestUtils.getFactoryByName(m_context, + "donut-event-provider"); + } + + /** + * Return the synchronous donut event provider factory. + * + * @return the synchronous donut event provider factory + */ + public Factory getSynchronousDonutEventProviderFactory() { + return IPojoTestUtils.getFactoryByName(m_context, + "synchronous-donut-event-provider"); + } + + /** + * Return the event provider factory. + * + * @return the event provider factory + */ + public Factory getEventProviderFactory() { + return IPojoTestUtils.getFactoryByName(m_context, "event-provider"); + } + + /** + * Return the synchronous event provider factory. + * + * @return the synchronous event provider factory + */ + public Factory getSynchronousEventProviderFactory() { + return IPojoTestUtils.getFactoryByName(m_context, + "synchronous-event-provider"); + } + + /** + * Return the donut consumer factory. + * + * @return the donut consumer factory + */ + public Factory getDonutConsumerFactory() { + return IPojoTestUtils.getFactoryByName(m_context, "donut-consumer"); + } + + /** + * Return the donut event consumer factory. + * + * @return the donut event consumer factory + */ + public Factory getDonutEventConsumerFactory() { + return IPojoTestUtils.getFactoryByName(m_context, + "donut-event-consumer"); + } + + /** + * Return the event consumer factory. + * + * @return the event consumer factory + */ + public Factory getEventConsumerFactory() { + return IPojoTestUtils.getFactoryByName(m_context, "event-consumer"); + } + + /** + * Return the event tracker. + * + * @return the event consumer factory + */ + public Factory getEventTrackerFactory() { + return IPojoTestUtils.getFactoryByName(m_context, "event-tracker"); + } + + /** + * Utility method that causes the current thread to sleep. + * + * @param millis + * the number of milliseconds to wait + */ + public static void sleep(long millis) { + long past = System.currentTimeMillis(); + long future = past + millis; + long now = past; + if (TRACE) { + System.err.println("Sleeping for " + millis + "ms"); + } + while (now < future) { + try { + Thread.sleep(future - now); + } catch (Exception e) { + } + now = System.currentTimeMillis(); + } + } + + /** + * Return the index of the given donut flavour in the flavour array. + * + * @return the index of the given flavour or -1 if not found + */ + public static int flavourIndex(String flavour) { + for (int i = 0; i < Donut.FLAVOURS.length; i++) { + if (Donut.FLAVOURS[i].equals(flavour)) + return i; + } + return -1; + } +} Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/util/IPojoTestUtils.java URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/util/IPojoTestUtils.java?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/util/IPojoTestUtils.java (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/java/org/apache/felix/ipojo/test/util/IPojoTestUtils.java Wed Jul 30 11:05:29 2008 @@ -0,0 +1,307 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.ipojo.test.util; + +import java.util.Dictionary; +import java.util.Properties; + +import org.apache.felix.ipojo.ComponentInstance; +import org.apache.felix.ipojo.Factory; +import org.apache.felix.ipojo.Handler; +import org.apache.felix.ipojo.HandlerFactory; +import org.apache.felix.ipojo.ServiceContext; +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; +import org.osgi.service.cm.ManagedServiceFactory; + +/** + * Useful iPOJO methods used for the tests of the Event Admin Handler. + * + * @author <a href="mailto:[EMAIL PROTECTED]">Felix Project Team</a> + */ +public class IPojoTestUtils { + + public static ComponentInstance getComponentInstance(BundleContext bc, + String factoryName, Dictionary configuration) { + Factory fact = getFactoryByName(bc, factoryName); + + if (fact == null) { + System.err.println("Factory " + factoryName + " not found"); + return null; + } + + try { + return fact.createComponentInstance(configuration); + } catch (Exception e) { + System.err.println("Cannot create the instance from " + factoryName + + " : " + e.getMessage()); + e.printStackTrace(); + return null; + } + } + + public static ComponentInstance getComponentInstance(ServiceContext bc, + String factoryName, Dictionary configuration) { + Factory fact = getFactoryByName(bc, factoryName); + + if (fact == null) { + return null; + } + + if (fact.isAcceptable(configuration)) { + try { + return fact.createComponentInstance(configuration); + } catch (Exception e) { + System.err.println(e.getMessage()); + e.printStackTrace(); + return null; + } + } else { + System.err + .println("Configuration not accepted by : " + factoryName); + return null; + } + } + + public static ComponentInstance getComponentInstanceByName( + BundleContext bc, String factoryName, String name) { + Factory fact = getFactoryByName(bc, factoryName); + + if (fact == null) { + System.err.println("Factory " + factoryName + " not found"); + return null; + } + + try { + Properties props = new Properties(); + props.put("name", name); + return fact.createComponentInstance(props); + } catch (Exception e) { + System.err.println("Cannot create the instance from " + factoryName + + " : " + e.getMessage()); + e.printStackTrace(); + return null; + } + } + + public static Factory getFactoryByName(BundleContext bc, String factoryName) { + ServiceReference[] refs; + try { + refs = bc.getServiceReferences(Factory.class.getName(), + "(factory.name=" + factoryName + ")"); + if (refs == null) { + System.err.println("Cannot get the factory " + factoryName); + return null; + } + return (Factory) bc.getService(refs[0]); + } catch (InvalidSyntaxException e) { + System.err.println("Cannot get the factory " + factoryName + " : " + + e.getMessage()); + return null; + } + } + + public static Factory getFactoryByName(ServiceContext bc, String factoryName) { + ServiceReference[] refs; + try { + refs = bc.getServiceReferences(Factory.class.getName(), + "(factory.name=" + factoryName + ")"); + if (refs == null) { + return null; + } + return (Factory) bc.getService(refs[0]); + } catch (InvalidSyntaxException e) { + System.err.println("Cannot get the factory " + factoryName + " : " + + e.getMessage()); + return null; + } + } + + public static HandlerFactory getHandlerFactoryByName(BundleContext bc, + String factoryName) { + ServiceReference[] refs; + try { + refs = bc.getServiceReferences(Factory.class.getName(), "(" + + Handler.HANDLER_NAME_PROPERTY + "=" + factoryName + ")"); + if (refs == null) { + System.err.println("Cannot get the factory " + factoryName); + return null; + } + return (HandlerFactory) bc.getService(refs[0]); + } catch (InvalidSyntaxException e) { + System.err.println("Cannot get the factory " + factoryName + " : " + + e.getMessage()); + return null; + } + } + + public static Object getServiceObject(BundleContext bc, String itf, + String filter) { + ServiceReference ref = getServiceReference(bc, itf, filter); + if (ref != null) { + return bc.getService(ref); + } else { + return null; + } + } + + public static Object getServiceObject(ServiceContext bc, String itf, + String filter) { + ServiceReference ref = getServiceReference(bc, itf, filter); + if (ref != null) { + return bc.getService(ref); + } else { + return null; + } + } + + public static Object[] getServiceObjects(BundleContext bc, String itf, + String filter) { + ServiceReference[] refs = getServiceReferences(bc, itf, filter); + if (refs != null) { + Object[] list = new Object[refs.length]; + for (int i = 0; i < refs.length; i++) { + list[i] = bc.getService(refs[i]); + } + return list; + } else { + return new Object[0]; + } + } + + public static Object[] getServiceObjects(ServiceContext bc, String itf, + String filter) { + ServiceReference[] refs = getServiceReferences(bc, itf, filter); + if (refs != null) { + Object[] list = new Object[refs.length]; + for (int i = 0; i < refs.length; i++) { + list[i] = bc.getService(refs[i]); + } + return list; + } else { + return new Object[0]; + } + } + + public static ServiceReference getServiceReference(BundleContext bc, + String itf, String filter) { + ServiceReference[] refs = null; + try { + refs = bc.getServiceReferences(itf, filter); + } catch (InvalidSyntaxException e) { + System.err.println("Invalid Filter : " + filter); + } + if (refs == null) { + return null; + } else { + return refs[0]; + } + } + + public static ServiceReference getServiceReference(ServiceContext bc, + String itf, String filter) { + ServiceReference[] refs = null; + try { + refs = bc.getServiceReferences(itf, filter); + } catch (InvalidSyntaxException e) { + System.err.println("Invalid Filter : " + filter); + } + if (refs == null) { + return null; + } else { + return refs[0]; + } + } + + public static ServiceReference getServiceReferenceByName(BundleContext bc, + String itf, String name) { + ServiceReference[] refs = null; + String filter = null; + if (itf.equals(Factory.class.getName()) + || itf.equals(ManagedServiceFactory.class.getName())) { + filter = "(" + "factory.name" + "=" + name + ")"; + } else { + filter = "(" + "instance.name" + "=" + name + ")"; + } + try { + refs = bc.getServiceReferences(itf, filter); + } catch (InvalidSyntaxException e) { + System.err.println("Invalid Filter : " + filter); + } + if (refs == null) { + return null; + } else { + return refs[0]; + } + } + + public static ServiceReference getServiceReferenceByName(ServiceContext bc, + String itf, String name) { + ServiceReference[] refs = null; + String filter = null; + if (itf.equals(Factory.class.getName()) + || itf.equals(ManagedServiceFactory.class.getName())) { + filter = "(" + "factory.name" + "=" + name + ")"; + } else { + filter = "(" + "instance.name" + "=" + name + ")"; + } + try { + refs = bc.getServiceReferences(itf, filter); + } catch (InvalidSyntaxException e) { + System.err.println("Invalid Filter : " + filter); + } + if (refs == null) { + return null; + } else { + return refs[0]; + } + } + + public static ServiceReference[] getServiceReferences(BundleContext bc, + String itf, String filter) { + ServiceReference[] refs = null; + try { + refs = bc.getServiceReferences(itf, filter); + } catch (InvalidSyntaxException e) { + System.err.println("Invalid Filter : " + filter); + } + if (refs == null) { + return new ServiceReference[0]; + } else { + return refs; + } + } + + public static ServiceReference[] getServiceReferences(ServiceContext bc, + String itf, String filter) { + ServiceReference[] refs = null; + try { + refs = bc.getServiceReferences(itf, filter); + } catch (InvalidSyntaxException e) { + System.err.println("Invalid Filter : " + filter); + } + if (refs == null) { + return new ServiceReference[0]; + } else { + return refs; + } + } + +} Added: felix/trunk/ipojo/tests/handler/eventadmin/src/main/resources/metadata.xml URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/eventadmin/src/main/resources/metadata.xml?rev=681155&view=auto ============================================================================== --- felix/trunk/ipojo/tests/handler/eventadmin/src/main/resources/metadata.xml (added) +++ felix/trunk/ipojo/tests/handler/eventadmin/src/main/resources/metadata.xml Wed Jul 30 11:05:29 2008 @@ -0,0 +1,138 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ipojo xmlns:ev="org.apache.felix.ipojo.handlers.event.EventAdminHandler"> + + <!-- The (asynchronous) donut provider --> + <component className="org.apache.felix.ipojo.test.donut.DonutProviderImpl" + name="donut-provider"> + <!-- Expose the donut provider service --> + <provides interface="org.apache.felix.ipojo.test.donut.DonutProvider"> + <property name="name" field="m_name" value="Unknown donut vendor"/> + </provides> + <!-- Donut publisher --> + <ev:publisher name="donut-publisher" field="m_publisher" + topics="food/donuts" data-key="food" synchronous="false"/> + </component> + + <!-- The synchronous donut provider --> + <component className="org.apache.felix.ipojo.test.donut.DonutProviderImpl" + name="synchronous-donut-provider"> + <!-- Expose the donut provider service --> + <provides interface="org.apache.felix.ipojo.test.donut.DonutProvider"> + <property name="name" field="m_name" value="Unknown donut vendor"/> + </provides> + <!-- Donut publisher --> + <ev:publisher name="donut-publisher" field="m_publisher" + topics="food/donuts" data-key="food" synchronous="true"/> + </component> + + <!-- The (asynchronous) donut event provider --> + <component + className="org.apache.felix.ipojo.test.donut.DonutEventProviderImpl" + name="donut-event-provider"> + <!-- Expose the donut provider service --> + <provides interface="org.apache.felix.ipojo.test.donut.DonutProvider"> + <property name="name" field="m_name" value="Unknown donut vendor"/> + </provides> + <!-- Raw events publisher --> + <ev:publisher name="event-publisher" field="m_publisher" + topics="food/donuts" synchronous="false"/> + </component> + + <!-- The synchronous donut event provider --> + <component + className="org.apache.felix.ipojo.test.donut.DonutEventProviderImpl" + name="synchronous-donut-event-provider"> + <!-- Expose the donut provider service --> + <provides interface="org.apache.felix.ipojo.test.donut.DonutProvider"> + <property name="name" field="m_name" value="Unknown donut vendor"/> + </provides> + <!-- Raw events publisher --> + <ev:publisher name="event-publisher" field="m_publisher" + topics="food/donuts" synchronous="true"/> + </component> + + <!-- The (asynchronous) event provider --> + <component + className="org.apache.felix.ipojo.test.donut.AsyncEventProviderImpl" + name="event-provider"> + <!-- Expose the donut provider service --> + <provides interface="org.apache.felix.ipojo.test.donut.DonutProvider"> + <property name="name" field="m_name" value="Unknown donut vendor"/> + </provides> + <!-- Direcly interacts with the Event Admin service --> + <requires field="m_ea"/> + </component> + + <!-- The synchronous event provider --> + <component + className="org.apache.felix.ipojo.test.donut.SyncEventProviderImpl" + name="synchronous-event-provider"> + <!-- Expose the donut provider service --> + <provides interface="org.apache.felix.ipojo.test.donut.DonutProvider"> + <property name="name" field="m_name" value="Unknown donut vendor"/> + </provides> + <!-- Direcly interacts with the Event Admin service --> + <requires field="m_ea"/> + </component> + + <!-- The donut consumer --> + <component className="org.apache.felix.ipojo.test.donut.DonutConsumerImpl" + name="donut-consumer"> + <!-- Expose the donut consumer service --> + <provides interface="org.apache.felix.ipojo.test.donut.DonutConsumer"> + <property name="name" field="m_name" value="Unknown donut consumer"/> + <property name="slow" field="m_isSlow" value="false"/> + </provides> + <!-- Donut events subscriber --> + <ev:subscriber name="donut-subscriber" callback="receiveDonut" + topics="food/donuts" data-key="food" + data-type="org.apache.felix.ipojo.test.donut.Donut"/> + </component> + + <!-- The donut event consumer --> + <component className="org.apache.felix.ipojo.test.donut.DonutConsumerImpl" + name="donut-event-consumer"> + <!-- Expose the donut consumer service --> + <provides interface="org.apache.felix.ipojo.test.donut.DonutConsumer"> + <property name="name" field="m_name" value="Unknown donut consumer"/> + <property name="slow" field="m_isSlow" value="false"/> + </provides> + <!-- Raw events subscriber --> + <ev:subscriber name="donut-event-subscriber" callback="receiveEvent" + topics="food/donuts"/> + </component> + + <!-- The event consumer --> + <component className="org.apache.felix.ipojo.test.donut.EventConsumerImpl" + name="event-consumer"> + <!-- Expose the donut consumer service --> + <provides + interface="{org.apache.felix.ipojo.test.donut.DonutConsumer,org.osgi.service.event.EventHandler}"> + <property name="name" field="m_name" value="Unknown event consumer"/> + <property name="slow" field="m_isSlow" value="false"/> + <property name="event.topics" type="String" value="food/donuts"/> + </provides> + </component> + + <!-- The event tracker --> + <component className="org.apache.felix.ipojo.test.donut.EventTrackerImpl" + name="event-tracker"> + <!-- Expose the donut consumer service --> + <provides + interface="{org.apache.felix.ipojo.test.donut.EventTracker,org.osgi.service.event.EventHandler}"> + <property name="name" field="m_name" value="Unknown event tracker"/> + <property name="event.topics" type="String" value="food/donuts"/> + </provides> + </component> + + + <!-- Example instances --> + <instance component="donut-provider" name="zeifhgbzre"> + <property name="name" value="zeifhgbzre donuts"/> + </instance> + <instance component="donut-consumer" name="zeifhgbzre simpson"> + <property name="name" value="zeifhgbzre simpson"/> + <property name="slow" value="false"/> + </instance> + +</ipojo> \ No newline at end of file Modified: felix/trunk/ipojo/tests/handler/whiteboard/pom.xml URL: http://svn.apache.org/viewvc/felix/trunk/ipojo/tests/handler/whiteboard/pom.xml?rev=681155&r1=681154&r2=681155&view=diff ============================================================================== --- felix/trunk/ipojo/tests/handler/whiteboard/pom.xml (original) +++ felix/trunk/ipojo/tests/handler/whiteboard/pom.xml Wed Jul 30 11:05:29 2008 @@ -1,5 +1,5 @@ <project> - <groupId>ipojo.test</groupId> + <groupId>ipojo.tests</groupId> <version>0.9.0-SNAPSHOT</version> <modelVersion>4.0.0</modelVersion> <packaging>bundle</packaging>
