From f44c07576a61f6f4915ea8fd2aa5af9fee71745f Mon Sep 17 00:00:00 2001 From: rfeng Date: Fri, 5 Dec 2008 00:48:31 +0000 Subject: Convert, clean and bring up a set of itests git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@723537 13f79535-47bb-0310-9956-ffa450edef68 --- .../itest/CallBackSeparateThreadClientImpl.java | 44 +++++------- .../tuscany/sca/itest/EventProcessorCallBack.java | 1 - .../tuscany/sca/itest/EventProcessorService.java | 1 - .../sca/itest/EventProcessorServiceImpl.java | 79 ++++++++++------------ .../sca/itest/CallBackSeparateThreadTestCase.java | 37 ++++++---- 5 files changed, 76 insertions(+), 86 deletions(-) (limited to 'java/sca/itest/callback-separatethread/src') diff --git a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java index e861f8a3d1..745240bd14 100644 --- a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java +++ b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadClientImpl.java @@ -21,8 +21,7 @@ package org.apache.tuscany.sca.itest; import java.util.concurrent.atomic.AtomicInteger; -import junit.framework.Assert; - +import org.junit.Assert; import org.osoa.sca.annotations.Reference; import org.osoa.sca.annotations.Service; @@ -58,22 +57,22 @@ public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadC public void runTests() { // Register for 1 second call back registerFor1SecondCallback(); - + // Wait for a few 1 second call backs System.out.println("Waiting for some 1 second calls"); waitForSome1SecondCallbacks(); // Register for 5 second call back registerFor5SecondCallback(); - + // Wait for a few 1 second call backs System.out.println("Waiting for some 1 second calls"); waitForSome1SecondCallbacks(); - + // Wait for a few 5 second call backs System.out.println("Waiting for some 5 second calls"); waitForSome5SecondCallbacks(); - + System.out.println("Done"); } @@ -83,7 +82,7 @@ public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadC private void waitForSome1SecondCallbacks() { // Reset the one second call back count oneSecondCallbackCount.set(0); - + // Wait until we have 10 1 second call backs or 60 seconds has passed final long start = System.currentTimeMillis(); do { @@ -91,15 +90,14 @@ public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadC System.out.println("Received enough 1 second notifications"); return; } - + try { Thread.sleep(500); } catch (InterruptedException e) { Assert.fail("Unexpeceted exception " + e); } - } - while (System.currentTimeMillis() - start < SIXTY_SECONDS); - + } while (System.currentTimeMillis() - start < SIXTY_SECONDS); + // If we get to here then we did not receive enough events Assert.fail("Did not receive enough 1 second events"); } @@ -110,31 +108,26 @@ public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadC private void waitForSome5SecondCallbacks() { // Reset the five second call back count fiveSecondCallbackCount.set(0); - + // Wait until we have 4 5 second call backs or 60 seconds has passed final long start = System.currentTimeMillis(); - do - { + do { if (fiveSecondCallbackCount.get() >= 4) { System.out.println("Received enough 5 second notifications"); return; } - - try - { + + try { Thread.sleep(500); - } - catch (InterruptedException e) - { + } catch (InterruptedException e) { Assert.fail("Unexpeceted exception " + e); } - } - while (System.currentTimeMillis() - start < SIXTY_SECONDS); - + } while (System.currentTimeMillis() - start < SIXTY_SECONDS); + // If we get to here then we did not receive enough events Assert.fail("Did not receive enough 5 second events"); } - + /** * Register to receive one second call backs */ @@ -165,8 +158,7 @@ public class CallBackSeparateThreadClientImpl implements CallBackSeparateThreadC } else if (aEventName.equals("FIVE")) { final int newValue = fiveSecondCallbackCount.incrementAndGet(); //System.out.println("Received total of " + newValue + " 5 second call backs"); - } - else + } else System.out.println("Unknown event type of " + aEventName); } } diff --git a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorCallBack.java b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorCallBack.java index ae41f596fc..3f86d14059 100644 --- a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorCallBack.java +++ b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorCallBack.java @@ -19,7 +19,6 @@ package org.apache.tuscany.sca.itest; - import org.osoa.sca.annotations.Remotable; /** diff --git a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorService.java b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorService.java index c22b8cceec..127c63eb9e 100644 --- a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorService.java +++ b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorService.java @@ -23,7 +23,6 @@ import org.osoa.sca.annotations.Callback; import org.osoa.sca.annotations.Conversational; import org.osoa.sca.annotations.Remotable; - /** * Sample Event Processor Service */ diff --git a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java index 4d672b3a8b..9b8692549a 100644 --- a/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java +++ b/java/sca/itest/callback-separatethread/src/main/java/org/apache/tuscany/sca/itest/EventProcessorServiceImpl.java @@ -43,39 +43,38 @@ public class EventProcessorServiceImpl implements EventProcessorService { */ @Callback protected CallableReference clientCallback; - + /** * This map contains the call backs for each of the registered Event names */ - private final Map> eventListeners; - + private final Map> eventListeners; + /** * The list of all Event Generators we create */ private final EventGenerator[] allEventGenerators; - + /** * Constructor. Starts the Event Generators */ public EventProcessorServiceImpl() { eventListeners = new ConcurrentHashMap>(); - + // We will simulate an Event generator allEventGenerators = new EventGenerator[2]; - allEventGenerators[0] = new EventGenerator("ONE", 1); // Generate the SECOND event every second - allEventGenerators[1] = new EventGenerator("FIVE", 5); // Generate the FIVE event every 5 seconds + allEventGenerators[0] = new EventGenerator("ONE", 1); // Generate the SECOND event every second + allEventGenerators[1] = new EventGenerator("FIVE", 5); // Generate the FIVE event every 5 seconds } - + /** * Registers the client to receive notifications for the specified event * * @param aEventName The name of the Event to register */ - public void registerForEvent(String aEventName) - { + public void registerForEvent(String aEventName) { // Register for the Event eventListeners.put(aEventName, clientCallback); - + // Send the "register" started event to the client receiveEvent(aEventName, "SameThread: Registered to receive notifications for " + aEventName); } @@ -85,89 +84,81 @@ public class EventProcessorServiceImpl implements EventProcessorService { * * @param aEventName The name of the Event to unregister */ - public void unregisterForEvent(String aEventName) - { + public void unregisterForEvent(String aEventName) { // Send the "register" started event to the client receiveEvent(aEventName, "SameThread: Unregister from receiving notifications for " + aEventName); - + eventListeners.remove(aEventName); } - + /** * This method is called whenever the EventProcessorService receives an Event * * @param aEventName The name of the Event received * @param aEventData The Event data */ - private void receiveEvent(String aEventName, Object aEventData) - { + private void receiveEvent(String aEventName, Object aEventData) { // Get the listener for the Event final CallableReference callback = eventListeners.get(aEventName); - if (callback == null) - { + if (callback == null) { //System.out.println("No registered listeners for " + aEventName); return; } - + // Trigger the call back // System.out.println("Notifying " + callback + " of event " + aEventName); callback.getService().eventNotification(aEventName, aEventData); // System.out.println("Done notify " + callback + " of event " + aEventName); } - + /** * Shuts down the Event Processor */ @Destroy - public void shutdown() - { + public void shutdown() { System.out.println("Shutting down the EventProcessor"); - + // Clear list of call back locations as we don't want to send any more notifications eventListeners.clear(); // Stop the Event Generators - for (EventGenerator generator : allEventGenerators) - { + for (EventGenerator generator : allEventGenerators) { generator.stop(); } } - + /** * Utility class for generating Events */ - private class EventGenerator - { + private class EventGenerator { /** * The Timer we are using to generate the events */ private final Timer timer = new Timer(); - + /** * Constructor * * @param aEventName The name of the Event to generate * @param frequencyInSeconds How frequently we should generate the Events */ - private EventGenerator(String aEventName, int frequencyInSeconds) - { - timer.schedule(new EventGeneratorTimerTask(aEventName), - frequencyInSeconds * 1000, frequencyInSeconds * 1000); + private EventGenerator(String aEventName, int frequencyInSeconds) { + timer.schedule(new EventGeneratorTimerTask(aEventName), + frequencyInSeconds * 1000, + frequencyInSeconds * 1000); } - + /** * Stop this Event Generator */ - private void stop() - { + private void stop() { timer.cancel(); } - + /** * The TimerTask that is invoked by the Timer for the EventGenerator */ - private class EventGeneratorTimerTask extends TimerTask - { + private class EventGeneratorTimerTask extends TimerTask { /** * The name of the Event we should generate */ @@ -178,17 +169,15 @@ public class EventProcessorServiceImpl implements EventProcessorService { * * @param aEventName The name of the Event we should generate */ - private EventGeneratorTimerTask(String aEventName) - { + private EventGeneratorTimerTask(String aEventName) { eventName = aEventName; } /** * Timer calls this method and it will generate an Event */ - @Override - public void run() - { + + public void run() { // System.out.println("Generating new event " + eventName); receiveEvent(eventName, "Separate Thread Notification: " + UUID.randomUUID().toString()); } diff --git a/java/sca/itest/callback-separatethread/src/test/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadTestCase.java b/java/sca/itest/callback-separatethread/src/test/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadTestCase.java index f7e7be2133..1dae2e1862 100644 --- a/java/sca/itest/callback-separatethread/src/test/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadTestCase.java +++ b/java/sca/itest/callback-separatethread/src/test/java/org/apache/tuscany/sca/itest/CallBackSeparateThreadTestCase.java @@ -21,7 +21,13 @@ package org.apache.tuscany.sca.itest; import junit.framework.TestCase; -import org.apache.tuscany.sca.host.embedded.SCADomain; +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.ContributionLocationHelper; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; /** * This test case will attempt to trigger a call back using a separate thread @@ -29,10 +35,10 @@ import org.apache.tuscany.sca.host.embedded.SCADomain; public class CallBackSeparateThreadTestCase extends TestCase { /** - * The SCADomain we are using + * The Node we are using */ - private SCADomain domain; - + private Node node; + /** * The client the tests should use */ @@ -41,24 +47,29 @@ public class CallBackSeparateThreadTestCase extends TestCase { /** * Run the call back in separate thread tests */ + @Test public void testCallBackSeparateThread() { - aCallBackClient.runTests(); + aCallBackClient.runTests(); } /** * Load the Call back in separate thread composite and look up the client. */ - @Override - protected void setUp() throws Exception { - domain = SCADomain.newInstance("CallBackSeparateThreadTest.composite"); - aCallBackClient = domain.getService(CallBackSeparateThreadClient.class, "CallBackSeparateThreadClient"); + + @Before + public void setUp() throws Exception { + String location = ContributionLocationHelper.getContributionLocation("CallBackSeparateThreadTest.composite"); + node = NodeFactory.newInstance().createNode("CallBackSeparateThreadTest.composite", new Contribution("c1", location)); + node.start(); + aCallBackClient = node.getService(CallBackSeparateThreadClient.class, "CallBackSeparateThreadClient"); } /** - * Shutdown the SCA domain + * Shutdown the SCA node */ - @Override - protected void tearDown() throws Exception { - domain.close(); + + @After + public void tearDown() throws Exception { + node.stop(); } } -- cgit v1.2.3