Browse Source

tests

develop
Robin Thoni 8 years ago
parent
commit
0d1f813f77

+ 9
- 3
common/src/main/java/com/uqac/rthoni/java_rmi/common/Command.java View File

@@ -69,14 +69,20 @@ public class Command implements Serializable {
69 69
         return _arguments.size();
70 70
     }
71 71
 
72
-    public String getArgument(int i)
72
+    public String getArgument(int i, boolean optional)
73 73
     {
74
+        if (i >= _arguments.size() && optional) {
75
+            return "";
76
+        }
74 77
         return _arguments.get(i);
75 78
     }
76 79
 
77
-    public Vector<String> getArgmumentAsList(int i)
80
+    public Vector<String> getArgumentAsList(int i, boolean optional)
78 81
     {
79
-        return new Vector<String>(Arrays.asList(_arguments.get(i).split(",")));
82
+        if (i >= _arguments.size() && optional) {
83
+            return new Vector<>();
84
+        }
85
+        return new Vector<>(Arrays.asList(_arguments.get(i).split(",")));
80 86
     }
81 87
 
82 88
     public void setResult(String result)

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

@@ -96,7 +96,7 @@ public class CommandTest {
96 96
         assertNotNull(c);
97 97
         assertEquals(c.getCommandName(), "Test");
98 98
         assertEquals(c.getArgumentCount(), 1);
99
-        assertEquals(c.getArgument(0), "an_argument");
99
+        assertEquals(c.getArgument(0, false), "an_argument");
100 100
     }
101 101
 
102 102
     @Test
@@ -106,7 +106,7 @@ public class CommandTest {
106 106
         assertNotNull(c);
107 107
         assertEquals(c.getCommandName(), "Test");
108 108
         assertEquals(c.getArgumentCount(), 2);
109
-        assertEquals(c.getArgument(0), "an_argument");
110
-        assertEquals(c.getArgument(1), "an_argument2");
109
+        assertEquals(c.getArgument(0, false), "an_argument");
110
+        assertEquals(c.getArgument(1, false), "an_argument2");
111 111
     }
112 112
 }

+ 3
- 3
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/MethodExecutor.java View File

@@ -20,10 +20,10 @@ public class MethodExecutor extends AbstractCommandExecutor {
20 20
 
21 21
     @Override
22 22
     public String run(Command command, ServerApplication server) throws Exception {
23
-        String id = command.getArgument(0);
24
-        String methodName = command.getArgument(1);
23
+        String id = command.getArgument(0, false);
24
+        String methodName = command.getArgument(1, false);
25 25
         Object obj = server.getObject(id);
26
-        List<Pair<Class, String>> argClasses = command.getArgmumentAsList(2).stream().map(v -> {
26
+        List<Pair<Class, String>> argClasses = command.getArgumentAsList(2, true).stream().map(v -> {
27 27
             String[] split = v.split(":");
28 28
             try {
29 29
                 return new Pair<>(ReflectionUtil.getClass(split[0]), split[1]);

+ 2
- 2
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/NewInstanceExecutor.java View File

@@ -14,8 +14,8 @@ public class NewInstanceExecutor extends AbstractCommandExecutor {
14 14
 
15 15
     @Override
16 16
     public String run(Command command, ServerApplication server) throws Exception {
17
-        String className = command.getArgument(0);
18
-        String id = command.getArgument(1);
17
+        String className = command.getArgument(0, false);
18
+        String id = command.getArgument(1, false);
19 19
         Class c = Class.forName(className);
20 20
         Object obj = c.newInstance();
21 21
         server.addObject(id, obj);

+ 2
- 2
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/ReadExecutor.java View File

@@ -17,8 +17,8 @@ public class ReadExecutor extends AbstractCommandExecutor {
17 17
 
18 18
     @Override
19 19
     public String run(Command command, ServerApplication server) throws Exception {
20
-        String id = command.getArgument(0);
21
-        String fieldName = command.getArgument(1);
20
+        String id = command.getArgument(0, false);
21
+        String fieldName = command.getArgument(1, false);
22 22
         String methodName = String.format("get%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
23 23
 
24 24
         Object obj = server.getObject(id);

+ 3
- 3
server/src/main/java/com/uqac/rthoni/java_rmi/server/executors/WriteExecutor.java View File

@@ -18,10 +18,10 @@ public class WriteExecutor extends AbstractCommandExecutor {
18 18
 
19 19
     @Override
20 20
     public String run(Command command, ServerApplication server) throws Exception {
21
-        String id = command.getArgument(0);
22
-        String fieldName = command.getArgument(1);
21
+        String id = command.getArgument(0, false);
22
+        String fieldName = command.getArgument(1, false);
23 23
         String methodName = String.format("set%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
24
-        String value = command.getArgument(2);
24
+        String value = command.getArgument(2, false);
25 25
 
26 26
         Object obj = server.getObject(id);
27 27
         Class objClass = obj.getClass();

+ 3
- 1
server/src/test/java/com/uqac/rthoni/java_rmi/server/executors/AbstractTest.java View File

@@ -20,9 +20,11 @@ public abstract class AbstractTest {
20 20
             String res = runCommand(app, s);
21 21
             if (res != null) {
22 22
                 stringBuilder.append(res);
23
+                stringBuilder.append("\n");
23 24
             }
24 25
         }
25
-        return stringBuilder.toString();
26
+        String res = stringBuilder.toString();
27
+        return res.substring(0, res.length() - 1);
26 28
     }
27 29
 
28 30
     public ServerApplication getServer() throws Exception {

+ 63
- 0
server/src/test/java/com/uqac/rthoni/java_rmi/server/executors/MethodExecutorTest.java View File

@@ -0,0 +1,63 @@
1
+package com.uqac.rthoni.java_rmi.server.executors;
2
+
3
+import com.uqac.rthoni.java_rmi.server.ServerApplication;
4
+import org.junit.Test;
5
+
6
+import static org.junit.Assert.*;
7
+
8
+/**
9
+ * Created by robin on 9/16/16.
10
+ */
11
+public class MethodExecutorTest extends AbstractTest {
12
+
13
+    @Test
14
+    public void test1() throws Exception
15
+    {
16
+        ServerApplication app = getServer();
17
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
18
+                "fonction#test#getMyself#java.lang.String:a_value");
19
+        assertEquals("a_value", res);
20
+    }
21
+
22
+    @Test
23
+    public void test2() throws Exception
24
+    {
25
+        ServerApplication app = getServer();
26
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
27
+                "fonction#test#uselessMethod#int:42,int:24,java.lang.String:a_value");
28
+        assertEquals("NULL", res);
29
+    }
30
+
31
+    @Test
32
+    public void test3() throws Exception
33
+    {
34
+        ServerApplication app = getServer();
35
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
36
+                "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test2",
37
+                "ecriture#test#privateString#some_value",
38
+                "fonction#test#getMyString#com.uqac.rthoni.java_rmi.server.executors.TestDbo:ID(test2)");
39
+        assertEquals("some_value", res);
40
+    }
41
+
42
+    @Test
43
+    public void test4() throws Exception
44
+    {
45
+        ServerApplication app = getServer();
46
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
47
+                "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test2",
48
+                "fonction#test#setPrivateString#java.lang.String:some_value",
49
+                "fonction#test#getMyString#com.uqac.rthoni.java_rmi.server.executors.TestDbo:ID(test2)");
50
+        assertEquals("NULL\nsome_value", res);
51
+    }
52
+
53
+    @Test
54
+    public void test5() throws Exception
55
+    {
56
+        ServerApplication app = getServer();
57
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
58
+                "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test2",
59
+                "fonction#test#setPrivateString#java.lang.String:some_value",
60
+                "fonction#test#getPrivateString");
61
+        assertEquals("NULL\nsome_value", res);
62
+    }
63
+}

+ 6
- 6
server/src/test/java/com/uqac/rthoni/java_rmi/server/executors/ReadExecutorTest.java View File

@@ -16,7 +16,7 @@ public class ReadExecutorTest extends AbstractTest {
16 16
         ServerApplication app = getServer();
17 17
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
18 18
                 "lecture#test#publicInt");
19
-        assertEquals(res, "42");
19
+        assertEquals("42", res);
20 20
     }
21 21
 
22 22
     @Test
@@ -25,7 +25,7 @@ public class ReadExecutorTest extends AbstractTest {
25 25
         ServerApplication app = getServer();
26 26
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
27 27
                 "lecture#test#privateInt");
28
-        assertEquals(res, "24");
28
+        assertEquals("24", res);
29 29
     }
30 30
 
31 31
     @Test
@@ -34,7 +34,7 @@ public class ReadExecutorTest extends AbstractTest {
34 34
         ServerApplication app = getServer();
35 35
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
36 36
                 "lecture#test#privateString");
37
-        assertEquals(res, "default_value");
37
+        assertEquals("default_value", res);
38 38
     }
39 39
 
40 40
     @Test(expected = NoSuchMethodException.class)
@@ -59,7 +59,7 @@ public class ReadExecutorTest extends AbstractTest {
59 59
         ServerApplication app = getServer();
60 60
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
61 61
                 "lecture#test#publicFloat");
62
-        assertEquals(res, "0.42");
62
+        assertEquals("0.42", res);
63 63
     }
64 64
 
65 65
     @Test
@@ -68,7 +68,7 @@ public class ReadExecutorTest extends AbstractTest {
68 68
         ServerApplication app = getServer();
69 69
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
70 70
                 "lecture#test#publicDouble");
71
-        assertEquals(res, "0.125");
71
+        assertEquals("0.125", res);
72 72
     }
73 73
 
74 74
     @Test
@@ -77,6 +77,6 @@ public class ReadExecutorTest extends AbstractTest {
77 77
         ServerApplication app = getServer();
78 78
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
79 79
                 "lecture#test#publicBool");
80
-        assertEquals(res, "true");
80
+        assertEquals("true", res);
81 81
     }
82 82
 }

+ 9
- 0
server/src/test/java/com/uqac/rthoni/java_rmi/server/executors/TestDbo.java View File

@@ -43,6 +43,15 @@ public class TestDbo {
43 43
         return str;
44 44
     }
45 45
 
46
+    public void uselessMethod(int i, int j, String str)
47
+    {
48
+    }
49
+
50
+    public String getMyString(TestDbo dbo)
51
+    {
52
+        return dbo.getPrivateString();
53
+    }
54
+
46 55
     public String toString()
47 56
     {
48 57
         return String.format("privateString=%s, privateInt=%d, publicInt=%d, aPrivateField=%d, publicFloat=%f, publicDouble=%f, publicBool=%b",

+ 5
- 5
server/src/test/java/com/uqac/rthoni/java_rmi/server/executors/WriteExecutorTest.java View File

@@ -20,7 +20,7 @@ public class WriteExecutorTest extends AbstractTest {
20 20
         assertEquals(res, "24");
21 21
         Object obj = app.getObject("test");
22 22
         assertNotNull(obj);
23
-        assertEquals(obj.toString(), "privateString=default_value, privateInt=24, publicInt=24, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true");
23
+        assertEquals("privateString=default_value, privateInt=24, publicInt=24, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true", obj.toString());
24 24
     }
25 25
 
26 26
     @Test
@@ -33,7 +33,7 @@ public class WriteExecutorTest extends AbstractTest {
33 33
         assertEquals(res, "42");
34 34
         Object obj = app.getObject("test");
35 35
         assertNotNull(obj);
36
-        assertEquals(obj.toString(), "privateString=default_value, privateInt=42, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true");
36
+        assertEquals("privateString=default_value, privateInt=42, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true", obj.toString());
37 37
     }
38 38
 
39 39
     @Test
@@ -46,7 +46,7 @@ public class WriteExecutorTest extends AbstractTest {
46 46
         assertEquals(res, "a_value");
47 47
         Object obj = app.getObject("test");
48 48
         assertNotNull(obj);
49
-        assertEquals(obj.toString(), "privateString=a_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true");
49
+        assertEquals("privateString=a_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true", obj.toString());
50 50
     }
51 51
 
52 52
     @Test
@@ -59,7 +59,7 @@ public class WriteExecutorTest extends AbstractTest {
59 59
         assertEquals(res, "4242.5");
60 60
         Object obj = app.getObject("test");
61 61
         assertNotNull(obj);
62
-        assertEquals(obj.toString(), "privateString=default_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=4242.500000, publicDouble=0.125000, publicBool=true");
62
+        assertEquals("privateString=default_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=4242.500000, publicDouble=0.125000, publicBool=true", obj.toString());
63 63
     }
64 64
 
65 65
     @Test
@@ -72,6 +72,6 @@ public class WriteExecutorTest extends AbstractTest {
72 72
         assertEquals(res, "false");
73 73
         Object obj = app.getObject("test");
74 74
         assertNotNull(obj);
75
-        assertEquals(obj.toString(), "privateString=default_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=false");
75
+        assertEquals("privateString=default_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=false", obj.toString());
76 76
     }
77 77
 }

Loading…
Cancel
Save