Browse Source

Command class; tests

develop
Robin Thoni 7 years ago
parent
commit
f3e65c3a74

+ 1
- 1
client/client.iml View File

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">

+ 3
- 1
common/common.iml View File

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">
@@ -11,5 +11,7 @@
11 11
     </content>
12 12
     <orderEntry type="inheritedJdk" />
13 13
     <orderEntry type="sourceFolder" forTests="false" />
14
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
15
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
14 16
   </component>
15 17
 </module>

+ 14
- 0
common/pom.xml View File

@@ -10,6 +10,20 @@
10 10
     <modelVersion>4.0.0</modelVersion>
11 11
 
12 12
     <artifactId>common</artifactId>
13
+    <dependencies>
14
+        <dependency>
15
+            <groupId>junit</groupId>
16
+            <artifactId>junit</artifactId>
17
+            <version>4.12</version>
18
+            <scope>test</scope>
19
+        </dependency>
20
+        <dependency>
21
+            <groupId>junit</groupId>
22
+            <artifactId>junit</artifactId>
23
+            <version>4.12</version>
24
+            <scope>test</scope>
25
+        </dependency>
26
+    </dependencies>
13 27
 
14 28
 
15 29
 </project>

+ 84
- 0
common/src/main/java/com/uqac/rthoni/java_rmi/common/Command.java View File

@@ -0,0 +1,84 @@
1
+package com.uqac.rthoni.java_rmi.common;
2
+
3
+import java.io.Serializable;
4
+import java.util.Vector;
5
+import java.util.stream.Collectors;
6
+
7
+/**
8
+ * Created by robin on 9/15/16.
9
+ */
10
+public class Command implements Serializable {
11
+    private String _commandName = "";
12
+    private Vector<String> _arguments = new Vector<>();
13
+    private transient String _result;
14
+
15
+    public Command(String commandName)
16
+    {
17
+        setCommandName(commandName);
18
+    }
19
+
20
+    public String toString()
21
+    {
22
+        StringBuilder stringBuilder = new StringBuilder();
23
+        stringBuilder.append(_commandName);
24
+        if (!_arguments.isEmpty()) {
25
+            stringBuilder.append("#");
26
+            stringBuilder.append(_arguments.stream().collect(Collectors.joining("#")));
27
+        }
28
+        return stringBuilder.toString();
29
+    }
30
+
31
+    public static Command fromString(String str)
32
+    {
33
+        if (str == null || str.isEmpty()) {
34
+            return null;
35
+        }
36
+        String[] parts = str.split("#");
37
+        Command command = new Command(parts[0]);
38
+        for (int i = 1; i < parts.length; ++i) {
39
+            command.addArgument(parts[i]);
40
+        }
41
+        return command;
42
+    }
43
+
44
+    public void setCommandName(String commandName)
45
+    {
46
+        if (commandName == null || commandName.isEmpty()) {
47
+            commandName = "Unknown";
48
+        }
49
+        _commandName = commandName;
50
+    }
51
+
52
+    public String getCommandName()
53
+    {
54
+        return _commandName;
55
+    }
56
+
57
+    public void addArgument(String argument)
58
+    {
59
+        if (argument == null) {
60
+            argument = "";
61
+        }
62
+        _arguments.add(argument);
63
+    }
64
+
65
+    public int getArgumentCount()
66
+    {
67
+        return _arguments.size();
68
+    }
69
+
70
+    public String getArgument(int i)
71
+    {
72
+        return _arguments.get(i);
73
+    }
74
+
75
+    public void setResult(String result)
76
+    {
77
+        _result = result;
78
+    }
79
+
80
+    public String getResult()
81
+    {
82
+        return _result;
83
+    }
84
+}

+ 112
- 0
common/src/test/java/com/uqac/rthoni/java_rmi/common/CommandTest.java View File

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

+ 1
- 1
server/server.iml View File

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">

+ 1
- 1
uqac-java-rmi.iml View File

@@ -1,6 +1,6 @@
1 1
 <?xml version="1.0" encoding="UTF-8"?>
2 2
 <module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3
-  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false">
3
+  <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false">
4 4
     <output url="file://$MODULE_DIR$/target/classes" />
5 5
     <output-test url="file://$MODULE_DIR$/target/test-classes" />
6 6
     <content url="file://$MODULE_DIR$">

Loading…
Cancel
Save