Skip to content

Commit

Permalink
another level of testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lvguowei committed Mar 23, 2018
1 parent 3467633 commit 14d867e
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
66 changes: 66 additions & 0 deletions src/main/java/com/guowei/lv/Announcer.java
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);
}
}
17 changes: 15 additions & 2 deletions src/main/java/com/guowei/lv/MainWindow.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.guowei.lv;

import org.jivesoftware.smackx.workgroup.agent.UserRequest;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class MainWindow extends JFrame {

Expand All @@ -11,6 +15,8 @@ class MainWindow extends JFrame {
public static final String NEW_ITEM_ID_NAME = "new item id";
public static final String JOIN_BUTTON_NAME = "join button";

private final Announcer<UserRequestListener> userRequests = Announcer.to(UserRequestListener.class);


MainWindow(SnipersTableModel snipers) {
super(APPLICATION_TITLE);
Expand All @@ -24,19 +30,22 @@ class MainWindow extends JFrame {
private JPanel makeControls() {
JPanel controls = new JPanel(new FlowLayout());
final JTextField itemIdField = new JTextField();
itemIdField.setColumns(25);
itemIdField.setName(NEW_ITEM_ID_NAME);
controls.add(itemIdField);

JButton joinAuctionButton = new JButton("Join Auction");
joinAuctionButton.setName(JOIN_BUTTON_NAME);
joinAuctionButton.addActionListener(e -> userRequests.announce().joinAuction(itemIdField.getText()));
controls.add(joinAuctionButton);

return controls;
}

private void fillContentPanel(JTable snipersTable, JPanel jPanel) {
private void fillContentPanel(JTable snipersTable, JPanel controls) {
final Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(jPanel, BorderLayout.NORTH);
contentPane.add(controls, BorderLayout.NORTH);
contentPane.add(new JScrollPane(snipersTable), BorderLayout.CENTER);
}

Expand All @@ -45,4 +54,8 @@ private JTable makeSnipersTable(SnipersTableModel snipers) {
snipersTable.setName(SNIPERS_TABLE_NAME);
return snipersTable;
}

public void addUserRequestListener(UserRequestListener userRequestListener) {
userRequests.addListener(userRequestListener);
}
}
22 changes: 22 additions & 0 deletions src/test/java/com/guowei/lv/MainWindowTest.java
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);
}
}

0 comments on commit 14d867e

Please sign in to comment.