-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
103 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.guowei.lv; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.ArrayList; | ||
import java.util.EventListener; | ||
import java.util.List; | ||
|
||
public class Announcer<T extends EventListener> { | ||
private final T proxy; | ||
private final List<T> listeners = new ArrayList<T>(); | ||
|
||
|
||
public Announcer(Class<? extends T> listenerType) { | ||
proxy = listenerType.cast(Proxy.newProxyInstance( | ||
listenerType.getClassLoader(), | ||
new Class<?>[]{listenerType}, | ||
new InvocationHandler() { | ||
public Object invoke(Object aProxy, Method method, Object[] args) throws Throwable { | ||
announce(method, args); | ||
return null; | ||
} | ||
})); | ||
} | ||
|
||
public void addListener(T listener) { | ||
listeners.add(listener); | ||
} | ||
|
||
public void removeListener(T listener) { | ||
listeners.remove(listener); | ||
} | ||
|
||
public T announce() { | ||
return proxy; | ||
} | ||
|
||
private void announce(Method m, Object[] args) { | ||
try { | ||
for (T listener : listeners) { | ||
m.invoke(listener, args); | ||
} | ||
} | ||
catch (IllegalAccessException e) { | ||
throw new IllegalArgumentException("could not invoke listener", e); | ||
} | ||
catch (InvocationTargetException e) { | ||
Throwable cause = e.getCause(); | ||
|
||
if (cause instanceof RuntimeException) { | ||
throw (RuntimeException)cause; | ||
} | ||
else if (cause instanceof Error) { | ||
throw (Error)cause; | ||
} | ||
else { | ||
throw new UnsupportedOperationException("listener threw exception", cause); | ||
} | ||
} | ||
} | ||
|
||
public static <T extends EventListener> Announcer<T> to(Class<? extends T> listenerType) { | ||
return new Announcer<T>(listenerType); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.guowei.lv; | ||
|
||
import com.objogate.wl.swing.probe.ValueMatcherProbe; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.CoreMatchers.equalTo; | ||
|
||
public class MainWindowTest { | ||
private final SnipersTableModel tableModel = new SnipersTableModel(); | ||
private final MainWindow mainWindow = new MainWindow(tableModel); | ||
private final AuctionSniperDriver driver = new AuctionSniperDriver(100); | ||
|
||
@Test | ||
public void makesUserRequestWhenJoinButtonClicked() { | ||
final ValueMatcherProbe<String> buttonProbe = new ValueMatcherProbe<>(equalTo("item-id"), "join request"); | ||
|
||
mainWindow.addUserRequestListener(itemId -> buttonProbe.setReceivedValue(itemId)); | ||
|
||
driver.startBiddingFor("item-id"); | ||
driver.check(buttonProbe); | ||
} | ||
} |