Last active
August 29, 2015 14:11
-
-
Save olim7t/7cc16d8f193aadab27c2 to your computer and use it in GitHub Desktop.
Swing client to expose Cassandra java driver internals in tests
This file contains hidden or 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
package com.datastax.driver.core; | |
import java.awt.Component; | |
import java.awt.Container; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.util.Iterator; | |
import java.util.Set; | |
import javax.swing.*; | |
import com.datastax.driver.core.policies.LoadBalancingPolicy; | |
/** | |
* To use from your test class: | |
* | |
* 1) build your cluster as usual | |
* 2) new SwingClient(cluster); | |
*/ | |
public class SwingClient { | |
private static final String DEFAULT_QUERY = "SELECT release_version FROM system.local"; | |
private final Cluster cluster; | |
private final Session session; | |
private final Gui gui; | |
public SwingClient(Cluster cluster) { | |
this.cluster = cluster; | |
session = cluster.connect(); | |
gui = new Gui(); | |
} | |
private void executeQuery() { | |
ResultSet rs = session.execute(gui.getQuery()); | |
for (Row row : rs) { | |
gui.output("%s%n", row.toString()); | |
} | |
} | |
private void getNodesStatuses() { | |
Set<Host> hosts = cluster.getMetadata().getAllHosts(); | |
for (Host host : hosts) { | |
HostDistance distance = cluster.getConfiguration().getPolicies().getLoadBalancingPolicy().distance(host); | |
boolean isReconnectingFromSuspected = host.getInitialReconnectionAttemptFuture() != null && !host.getInitialReconnectionAttemptFuture().isDone(); | |
boolean isReconnectingFromDown = host.getReconnectionAttemptFuture() != null && !host.getReconnectionAttemptFuture().isDone(); | |
gui.output("[%s:%s:%s] version=%s, state=%s, dist=%s, reconnectFromSuspected=%s, reconnectFromDown=%s%n", | |
host.getDatacenter(), host.getRack(), host.getAddress(), | |
host.getCassandraVersion(), | |
host.state, | |
distance, | |
isReconnectingFromSuspected, | |
isReconnectingFromDown); | |
} | |
} | |
private void computeQueryPlan() { | |
gui.output("Query plan: "); | |
LoadBalancingPolicy lbp = cluster.getConfiguration().getPolicies().getLoadBalancingPolicy(); | |
Iterator<Host> iter = lbp.newQueryPlan(null, new SimpleStatement("")); | |
while (iter.hasNext()) { | |
Host host = iter.next(); | |
gui.output("%s ", host); | |
} | |
gui.output("%n"); | |
} | |
private void checkControlConnection() { | |
ControlConnection connection = cluster.manager.controlConnection; | |
if (connection.isOpen()) { | |
gui.output("Control connection is open to %s%n", connection.connectedHost()); | |
} else { | |
gui.output("Control connection is closed%n"); | |
} | |
} | |
class Gui { | |
private JTextArea queryField; | |
private JTextArea outputField; | |
Gui() { | |
SwingUtilities.invokeLater(new Runnable() { | |
public void run() { | |
createAndShow(); | |
} | |
}); | |
} | |
void output(String format, Object... args) { | |
outputField.append(String.format(format, args)); | |
} | |
String getQuery() { | |
return queryField.getText(); | |
} | |
private void createAndShow() { | |
JFrame frame = new JFrame("Cassandra Java driver"); | |
Container pane = frame.getContentPane(); | |
pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS)); | |
createQueryPanel(pane); | |
createCommandsPanel(pane); | |
createOutputPanel(pane); | |
frame.pack(); | |
frame.setVisible(true); | |
} | |
private void createQueryPanel(Container pane) { | |
JPanel panel = new JPanel(); | |
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); | |
panel.setBorder(BorderFactory.createTitledBorder("Query")); | |
pane.add(panel); | |
queryField = new JTextArea(4, 50); | |
queryField.setText(DEFAULT_QUERY); | |
queryField.setLineWrap(true); | |
JScrollPane scrollPane = new JScrollPane(queryField); | |
panel.add(scrollPane); | |
createButton(panel, "Run", new ActionListener() { | |
public void actionPerformed(ActionEvent event) { | |
executeQuery(); | |
} | |
}).setAlignmentX(Component.RIGHT_ALIGNMENT); | |
} | |
private void createCommandsPanel(Container pane) { | |
JPanel panel = new JPanel(); | |
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS)); | |
createButton(panel, "Query plan", new ActionListener() { | |
public void actionPerformed(ActionEvent event) { | |
computeQueryPlan(); | |
} | |
}); | |
createButton(panel, "Hosts", new ActionListener() { | |
public void actionPerformed(ActionEvent event) { | |
getNodesStatuses(); | |
} | |
}); | |
pane.add(panel); | |
createButton(panel, "Control Cnx", new ActionListener() { | |
public void actionPerformed(ActionEvent event) { | |
checkControlConnection(); | |
} | |
}); | |
pane.add(panel); | |
} | |
private void createOutputPanel(Container pane) { | |
JPanel panel = new JPanel(); | |
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); | |
panel.setBorder(BorderFactory.createTitledBorder("Output")); | |
pane.add(panel); | |
outputField = new JTextArea(8, 100); | |
outputField.setLineWrap(true); | |
outputField.setEditable(false); | |
JScrollPane scrollPane = new JScrollPane(outputField); | |
panel.add(scrollPane); | |
createButton(panel, "Clear", new ActionListener() { | |
public void actionPerformed(ActionEvent event) { | |
outputField.setText(""); | |
} | |
}).setAlignmentX(Component.RIGHT_ALIGNMENT); | |
} | |
private JButton createButton(Container container, String label, ActionListener action) { | |
JButton button = new JButton(label); | |
button.addActionListener(action); | |
container.add(button); | |
return button; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment