Revision: 16033
Author: [email protected]
Date: Thu Dec 23 22:24:29 2010
Log: Added console test
http://code.google.com/p/mobicents/source/detail?r=16033
Added:
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Console.java
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ConsoleListener.java
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/MessageFormatter.java
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/protocols/ss7/management/console
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/protocols/ss7/management/console/ConsoleTest.java
Deleted:
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ShellListener.java
Modified:
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Shell.java
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/shell/Command.java
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/protocols/ss7/management/shell/CommandTest.java
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/ShellTransceiverTest.java
=======================================
--- /dev/null
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Console.java
Thu Dec 23 22:24:29 2010
@@ -0,0 +1,54 @@
+package org.mobicents.protocols.ss7.management.console;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.util.Scanner;
+
+/**
+ *
+ * @author amit bhayani
+ *
+ */
+public class Console {
+
+ private final OutputStream out;
+ private final PrintWriter pw;
+ private final InputStream in;
+ private final Scanner scanner;
+
+ private ConsoleListener consoleListener = null;
+
+ public Console(InputStream in, OutputStream out,
+ ConsoleListener consoleListener) {
+
+ // Input
+ this.in = in;
+ this.scanner = new Scanner(in);
+
+ // Output
+ this.out = out;
+ this.pw = new PrintWriter(this.out);
+
+ // Listener
+ this.consoleListener = consoleListener;
+
+ }
+
+ public void start() {
+
+ while (scanner.hasNextLine()) {
+ String line = scanner.nextLine();
+ if (this.consoleListener != null) {
+ this.consoleListener.commandEntered(line);
+ }
+
+ }
+ }
+
+ public void write(String text) {
+ pw.write(text);
+ pw.flush();
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ConsoleListener.java
Thu Dec 23 22:24:29 2010
@@ -0,0 +1,10 @@
+package org.mobicents.protocols.ss7.management.console;
+
+
+public class ConsoleListener {
+
+ public void commandEntered(String consoleInput){
+
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/MessageFormatter.java
Thu Dec 23 22:24:29 2010
@@ -0,0 +1,22 @@
+package org.mobicents.protocols.ss7.management.console;
+
+import javolution.text.TextBuilder;
+
+import org.mobicents.protocols.ss7.management.shell.Command;
+
+public class MessageFormatter {
+
+ protected void displayCommand(TextBuilder tx, int charsPerLine,
+ Command cmd, int leftPad, int descPad) {
+ this.createPad(tx, leftPad);
+ tx.append(cmd.toString());
+ this.createPad(tx, descPad);
+
+ }
+
+ private void createPad(TextBuilder tx, int pad) {
+ for (int i = 0; i < pad; i++) {
+ tx.append(' ');
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/protocols/ss7/management/console/ConsoleTest.java
Thu Dec 23 22:24:29 2010
@@ -0,0 +1,81 @@
+package org.mobicents.protocols.ss7.management.console;
+
+import static org.junit.Assert.assertEquals;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ *
+ * @author amit bhayani
+ *
+ */
+public class ConsoleTest {
+
+ ByteArrayInputStream in = null;
+ ByteArrayOutputStream out = null;
+ Console console = null;
+ ConsoleListener listener = null;
+
+ public ConsoleTest() {
+ }
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ }
+
+ @Before
+ public void setUp() {
+
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ @Test
+ public void testConsole() throws IOException {
+
+ String message = "This is some message";
+ in = new ByteArrayInputStream(message.getBytes());
+
+ out = new ByteArrayOutputStream();
+
+ listener = new TestConsoleListener(out);
+
+ console = new Console(in, out, listener);
+ console.start();
+
+ assertEquals("Sent message should be equal to received", message,
+ new String(out.toByteArray()));
+ }
+
+ private class TestConsoleListener extends ConsoleListener {
+
+ ByteArrayOutputStream out;
+
+ TestConsoleListener(ByteArrayOutputStream out) {
+ this.out = out;
+ }
+
+ @Override
+ public void commandEntered(String consoleInput) {
+
+ System.out.println(consoleInput);
+
+ console.write(consoleInput);
+
+ }
+ }
+
+}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ShellListener.java
Thu Dec 23 19:45:11 2010
+++ /dev/null
@@ -1,11 +0,0 @@
-package org.mobicents.protocols.ss7.management.console;
-
-import java.io.OutputStream;
-
-public class ShellListener {
-
- public void commandEntered(String comand, OutputStream out){
-
- }
-
-}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Shell.java
Thu Dec 23 19:45:11 2010
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Shell.java
Thu Dec 23 22:24:29 2010
@@ -1,32 +1,13 @@
package org.mobicents.protocols.ss7.management.console;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.Scanner;
-
public class Shell {
- private static final OutputStream out = System.out;
- private static final InputStream in = System.in;
- private static final Scanner scanner = new Scanner(in);
-
- private ShellListener shellListener = null;
-
public static void main(String args[]) {
- Shell ss7Cli = new Shell();
-
+ Console ss7Cli = new Console(System.in, System.out);
+
// TODO : Oleg what about command line arguments?
-
- ss7Cli.beginCli();
- }
-
- public void beginCli() {
-
- while (true) {
- String line = scanner.nextLine();
- this.shellListener.commandEntered(line, out);
-
- }
+
+ ss7Cli.start();
}
}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/shell/Command.java
Wed Dec 22 04:16:02 2010
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/shell/Command.java
Thu Dec 23 22:24:29 2010
@@ -26,15 +26,21 @@
public class Command {
private String name;
private FastList<Parameter> parameters = new FastList();
-
- public Command(String name) {
+ private String description;
+
+ public Command(String name, String description) {
this.name = name;
+ this.description = description;
}
public String getName() {
return name;
- }
-
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
public void add(Parameter p) {
parameters.add(p);
}
=======================================
---
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/protocols/ss7/management/shell/CommandTest.java
Wed Dec 22 04:41:59 2010
+++
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/protocols/ss7/management/shell/CommandTest.java
Thu Dec 23 22:24:29 2010
@@ -42,7 +42,7 @@
*/
@Test
public void testToString() {
- Command cmd = new Command("add");
+ Command cmd = new Command("add", "Add new linkset of type dahdi |
dialogic");
Value dialogic = new Value("dialogic");
dialogic.add(new Parameter("module_id"));
=======================================
---
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/ShellTransceiverTest.java
Thu Dec 23 02:39:53 2010
+++
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/ShellTransceiverTest.java
Thu Dec 23 22:24:29 2010
@@ -49,7 +49,7 @@
public void tearDown() {
}
- private Message createMessage(ShellProvider provider, String text) {
+ private Message createMessage(ChannelProvider provider, String text) {
Message msg = provider.getMessageFactory().createMessage(text);
return msg;
}
@@ -80,17 +80,17 @@
}
private class Client implements Runnable {
- private ShellProvider provider;
+ private ChannelProvider provider;
private ShellChannel channel;
- private ShellSelector selector;
- private ShellSelectionKey skey;
+ private ChannelSelector selector;
+ private ChannelSelectionKey skey;
private volatile boolean started = false;
private String rxMessage = "";
private String[] txMessage = new String[] { "Hello", " ", "world"
};
public Client(InetAddress address, int port) throws IOException {
- provider = ShellProvider.open();
+ provider = ChannelProvider.open();
channel = provider.openChannel();
channel.bind(new InetSocketAddress(address, port));
@@ -130,11 +130,11 @@
int i = 0;
while (started) {
try {
- FastList<ShellSelectionKey> keys =
selector.selectNow();
-
- for (FastList.Node<ShellSelectionKey> n = keys.head(),
end = keys
+ FastList<ChannelSelectionKey> keys =
selector.selectNow();
+
+ for (FastList.Node<ChannelSelectionKey> n =
keys.head(), end = keys
.tail(); (n = n.getNext()) != end;) {
- ShellSelectionKey key = n.getValue();
+ ChannelSelectionKey key = n.getValue();
ShellChannel chan = (ShellChannel) key.channel();
if (key.isReadable()) {
Message msg = (Message) chan.receive();
@@ -164,11 +164,11 @@
private class Server implements Runnable {
- private ShellProvider provider;
+ private ChannelProvider provider;
private ShellServerChannel serverChannel;
private ShellChannel channel;
- private ShellSelector selector;
- private ShellSelectionKey skey;
+ private ChannelSelector selector;
+ private ChannelSelectionKey skey;
private volatile boolean started = false;
@@ -176,7 +176,7 @@
private String[] txMessage = new String[] { "Hello", " ", "world"
};
public Server(InetAddress address, int port) throws IOException {
- provider = ShellProvider.open();
+ provider = ChannelProvider.open();
serverChannel = provider.openServerChannel();
serverChannel.bind(new InetSocketAddress(address, port));
@@ -208,12 +208,12 @@
int i = 0;
while (started) {
try {
- FastList<ShellSelectionKey> keys =
selector.selectNow();
-
- for (FastList.Node<ShellSelectionKey> n = keys.head(),
end = keys
+ FastList<ChannelSelectionKey> keys =
selector.selectNow();
+
+ for (FastList.Node<ChannelSelectionKey> n =
keys.head(), end = keys
.tail(); (n = n.getNext()) != end;) {
- ShellSelectionKey key = n.getValue();
+ ChannelSelectionKey key = n.getValue();
if (key.isAcceptable()) {
ShellServerChannel chan = (ShellServerChannel)
key