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

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