Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6af0004965a42a4a7350fa3105c5ad9b0c381679 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (c) 2011, 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.emf.cdo.tests.util;

import org.eclipse.net4j.util.WrappedException;
import org.eclipse.net4j.util.event.IEvent;
import org.eclipse.net4j.util.event.IListener;

import java.util.LinkedList;
import java.util.List;

import org.junit.Assert;

/**
 * @author Caspar De Groot
 */
public class TestListener2 implements IListener
{
  private final static long DEFAULT_TIMEOUT = 3000; // 3 seconds

  private List<IEvent> events = new LinkedList<IEvent>();

  private Class<? extends IEvent> eventClass;

  private long timeout;

  private String name;

  public TestListener2(Class<? extends IEvent> eventClass)
  {
    this(eventClass, null);
  }

  public TestListener2(Class<? extends IEvent> eventClass, String name)
  {
    this.eventClass = eventClass;
    this.name = name;
    timeout = DEFAULT_TIMEOUT;
  }

  public synchronized void notifyEvent(IEvent event)
  {
    if (eventClass == null || eventClass.isAssignableFrom(event.getClass()))
    {
      events.add(event);
      notify();
    }
  }

  public List<IEvent> getEvents()
  {
    return events;
  }

  public void setTimeout(long timeout)
  {
    this.timeout = timeout;
  }

  public synchronized void waitFor(int n, long timeout)
  {
    long t = 0;

    while (events.size() < n)
    {
      if (timeout <= 0)
      {
        Assert.fail("Timed out");
      }

      try
      {
        t = System.currentTimeMillis();
        wait(timeout);
      }
      catch (InterruptedException ex)
      {
        throw WrappedException.wrap(ex);
      }

      timeout -= System.currentTimeMillis() - t;
    }
  }

  public void waitFor(int i)
  {
    waitFor(i, timeout);
  }

  @Override
  public String toString()
  {
    StringBuilder builder = new StringBuilder(TestListener2.class.getSimpleName());
    builder.append('[');
    if (name != null)
    {
      builder.append("name=\"");
      builder.append(name);
      builder.append('\"');
    }
    
    if (eventClass != null)
    {
      if (builder.charAt(builder.length() - 1) != '[')
      {
        builder.append(';');
      }
      builder.append("eventClass=");
      builder.append(eventClass.getSimpleName());
    }
    
    builder.append(']');
    return builder.toString();
  }
}

Back to the top