You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CommandTest.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package com.uqac.rthoni.java_rmi.common;
  2. import com.sun.corba.se.impl.orbutil.ObjectWriter;
  3. import org.junit.Test;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.ObjectOutputStream;
  6. import static org.junit.Assert.*;
  7. /**
  8. * Created by robin on 9/15/16.
  9. */
  10. public class CommandTest {
  11. private String commandToString(Command command) throws Exception
  12. {
  13. // ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  14. // ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
  15. // objectOutputStream.writeObject(command);
  16. // objectOutputStream.close();
  17. // return byteArrayOutputStream.toString("UTF-8");
  18. return command.toString();
  19. }
  20. @Test
  21. public void testCommandSerializeNull() throws Exception
  22. {
  23. Command c = new Command(null);
  24. String result = commandToString(c);
  25. assertEquals(result, "Unknown");
  26. }
  27. @Test
  28. public void testCommandSerializeEmpty() throws Exception
  29. {
  30. Command c = new Command("");
  31. String result = commandToString(c);
  32. assertEquals(result, "Unknown");
  33. }
  34. @Test
  35. public void testCommandSerializeName() throws Exception
  36. {
  37. Command c = new Command("Test");
  38. String result = commandToString(c);
  39. assertEquals(result, "Test");
  40. }
  41. @Test
  42. public void testCommandSerializeArgument() throws Exception
  43. {
  44. Command c = new Command("Test");
  45. c.addArgument("an_argument");
  46. String result = commandToString(c);
  47. assertEquals(result, "Test#an_argument");
  48. }
  49. @Test
  50. public void testCommandSerializeArguments() throws Exception
  51. {
  52. Command c = new Command("Test");
  53. c.addArgument("an_argument");
  54. c.addArgument("an_argument2");
  55. String result = commandToString(c);
  56. assertEquals(result, "Test#an_argument#an_argument2");
  57. }
  58. @Test
  59. public void testCommandDeserializeNull() throws Exception
  60. {
  61. Command c = Command.fromString(null);
  62. assertNull(c);
  63. }
  64. @Test
  65. public void testCommandDeserializeEmpty() throws Exception
  66. {
  67. Command c = Command.fromString("");
  68. assertNull(c);
  69. }
  70. @Test
  71. public void testCommandDeserializeName() throws Exception
  72. {
  73. Command c = Command.fromString("Test");
  74. assertNotNull(c);
  75. assertEquals(c.getCommandName(), "Test");
  76. assertEquals(c.getArgumentCount(), 0);
  77. }
  78. @Test
  79. public void testCommandDeserializeArgument() throws Exception
  80. {
  81. Command c = Command.fromString("Test#an_argument");
  82. assertNotNull(c);
  83. assertEquals(c.getCommandName(), "Test");
  84. assertEquals(c.getArgumentCount(), 1);
  85. assertEquals(c.getArgument(0, false), "an_argument");
  86. }
  87. @Test
  88. public void testCommandDeserializeArguments() throws Exception
  89. {
  90. Command c = Command.fromString("Test#an_argument#an_argument2");
  91. assertNotNull(c);
  92. assertEquals(c.getCommandName(), "Test");
  93. assertEquals(c.getArgumentCount(), 2);
  94. assertEquals(c.getArgument(0, false), "an_argument");
  95. assertEquals(c.getArgument(1, false), "an_argument2");
  96. }
  97. }