Author: abelevich
Date: 2009-03-17 12:15:49 -0400 (Tue, 17 Mar 2009)
New Revision: 12985
Modified:
trunk/test-applications/seleniumTest/richfaces/src/main/java/org/ajax4jsf/bean/AjaxPushTestBean.java
Log:
fix test
Modified:
trunk/test-applications/seleniumTest/richfaces/src/main/java/org/ajax4jsf/bean/AjaxPushTestBean.java
===================================================================
---
trunk/test-applications/seleniumTest/richfaces/src/main/java/org/ajax4jsf/bean/AjaxPushTestBean.java 2009-03-17
15:53:03 UTC (rev 12984)
+++
trunk/test-applications/seleniumTest/richfaces/src/main/java/org/ajax4jsf/bean/AjaxPushTestBean.java 2009-03-17
16:15:49 UTC (rev 12985)
@@ -23,22 +23,69 @@
import java.util.Date;
import java.util.EventListener;
import java.util.EventObject;
-import java.util.concurrent.atomic.AtomicInteger;
import org.ajax4jsf.event.PushEventListener;
-public class AjaxPushTestBean {
+public class AjaxPushTestBean implements Runnable {
private boolean enabled = false;
private PushEventListener listener;
- private EventGenerator generator;
-
private String content = "content";
+
+ private Date startDate;
+
+ private Thread thread;
+
+ private int eventsSent;
- private final Object lock = new Object();
-
+
+ public void start() {
+ if(thread == null) {
+ thread = new Thread(this);
+ thread.setDaemon(true);
+ setStartDate(new Date());
+ thread.start();
+ setEnabled(true);
+ }
+ }
+
+ public void stop() {
+ if(thread != null) {
+ thread = null;
+ setEnabled(false);
+ setStartDate(null);
+ }
+ }
+
+ public void run() {
+ while(thread != null) {
+ if (((new Date()).getTime() - getStartDate().getTime())>=10000) {
+ stop();
+ }
+
+ listener.onEvent(new EventObject(this));
+ eventsSent++;
+
+ try {
+ Thread.sleep(250);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+
+ }
+ }
+
+ public void addListener(EventListener listener) {
+ synchronized (listener) {
+ if (this.listener != listener) {
+ this.listener = (PushEventListener) listener;
+ }
+ }
+ }
+
+
/**
* Gets value of content field.
*
@@ -64,40 +111,9 @@
* @return value of eventsSent field
*/
public int getEventsSent() {
- EventGenerator generator = getGenerator();
- return generator != null ? generator.getEventsSent() : 0;
- }
+ return eventsSent;
+ }
- public void addListener(EventListener listener) {
- synchronized (lock) {
- if (this.listener != listener) {
- this.listener = (PushEventListener) listener;
- }
- }
- }
-
- public void start() {
- synchronized (lock) {
- if (generator == null && listener != null) {
- generator = new EventGenerator(listener, 60000);
- Thread thread = new Thread(generator);
- thread.setDaemon(true);
- thread.start();
- setEnabled(true);
- }
- }
- }
-
- public void stop() {
- synchronized (lock) {
- if (generator != null) {
- generator.stop();
- setEnabled(false);
- generator = null;
- }
- }
- }
-
public boolean isEnabled() {
return enabled;
}
@@ -106,59 +122,14 @@
this.enabled = enabled;
}
- public void restart() {
- stop();
- start();
- }
+ public Date getStartDate() {
+ return startDate;
+ }
- private EventGenerator getGenerator() {
- return generator;
- }
+ public void setStartDate(Date startDate) {
+ synchronized (this) {
+ this.startDate = startDate;
+ }
+ }
- private static class EventGenerator implements Runnable {
-
- private final PushEventListener listener;
-
- private final long time;
-
- private AtomicInteger eventsSent = new AtomicInteger(0);
-
- private volatile boolean stopped;
-
- public EventGenerator(PushEventListener listener, long time) {
- this.listener = listener;
- this.time = time;
- }
-
- public void stop() {
- stopped = true;
- synchronized (this) {
- notify();
- }
- }
-
- public void run() {
- Date startDate = new Date();
- while (true) {
- try {
- long left = System.currentTimeMillis() - startDate.getTime();
- if (left >= time || stopped) {
- break;
- }
- listener.onEvent(new EventObject(this));
- eventsSent.incrementAndGet();
- synchronized (this) {
- wait(500);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- public int getEventsSent() {
- return eventsSent.get();
- }
- }
-
}
Show replies by date