Browse Source

tests

develop
Robin Thoni 7 years ago
parent
commit
0d1f813f77

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

69
         return _arguments.size();
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
         return _arguments.get(i);
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
     public void setResult(String result)
88
     public void setResult(String result)

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

96
         assertNotNull(c);
96
         assertNotNull(c);
97
         assertEquals(c.getCommandName(), "Test");
97
         assertEquals(c.getCommandName(), "Test");
98
         assertEquals(c.getArgumentCount(), 1);
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
     @Test
102
     @Test
106
         assertNotNull(c);
106
         assertNotNull(c);
107
         assertEquals(c.getCommandName(), "Test");
107
         assertEquals(c.getCommandName(), "Test");
108
         assertEquals(c.getArgumentCount(), 2);
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
 
20
 
21
     @Override
21
     @Override
22
     public String run(Command command, ServerApplication server) throws Exception {
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
         Object obj = server.getObject(id);
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
             String[] split = v.split(":");
27
             String[] split = v.split(":");
28
             try {
28
             try {
29
                 return new Pair<>(ReflectionUtil.getClass(split[0]), split[1]);
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
 
14
 
15
     @Override
15
     @Override
16
     public String run(Command command, ServerApplication server) throws Exception {
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
         Class c = Class.forName(className);
19
         Class c = Class.forName(className);
20
         Object obj = c.newInstance();
20
         Object obj = c.newInstance();
21
         server.addObject(id, obj);
21
         server.addObject(id, obj);

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

17
 
17
 
18
     @Override
18
     @Override
19
     public String run(Command command, ServerApplication server) throws Exception {
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
         String methodName = String.format("get%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
22
         String methodName = String.format("get%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
23
 
23
 
24
         Object obj = server.getObject(id);
24
         Object obj = server.getObject(id);

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

18
 
18
 
19
     @Override
19
     @Override
20
     public String run(Command command, ServerApplication server) throws Exception {
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
         String methodName = String.format("set%s%s", fieldName.substring(0, 1).toUpperCase(), fieldName.substring(1));
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
         Object obj = server.getObject(id);
26
         Object obj = server.getObject(id);
27
         Class objClass = obj.getClass();
27
         Class objClass = obj.getClass();

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

20
             String res = runCommand(app, s);
20
             String res = runCommand(app, s);
21
             if (res != null) {
21
             if (res != null) {
22
                 stringBuilder.append(res);
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
     public ServerApplication getServer() throws Exception {
30
     public ServerApplication getServer() throws Exception {

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

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
         ServerApplication app = getServer();
16
         ServerApplication app = getServer();
17
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
17
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
18
                 "lecture#test#publicInt");
18
                 "lecture#test#publicInt");
19
-        assertEquals(res, "42");
19
+        assertEquals("42", res);
20
     }
20
     }
21
 
21
 
22
     @Test
22
     @Test
25
         ServerApplication app = getServer();
25
         ServerApplication app = getServer();
26
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
26
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
27
                 "lecture#test#privateInt");
27
                 "lecture#test#privateInt");
28
-        assertEquals(res, "24");
28
+        assertEquals("24", res);
29
     }
29
     }
30
 
30
 
31
     @Test
31
     @Test
34
         ServerApplication app = getServer();
34
         ServerApplication app = getServer();
35
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
35
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
36
                 "lecture#test#privateString");
36
                 "lecture#test#privateString");
37
-        assertEquals(res, "default_value");
37
+        assertEquals("default_value", res);
38
     }
38
     }
39
 
39
 
40
     @Test(expected = NoSuchMethodException.class)
40
     @Test(expected = NoSuchMethodException.class)
59
         ServerApplication app = getServer();
59
         ServerApplication app = getServer();
60
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
60
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
61
                 "lecture#test#publicFloat");
61
                 "lecture#test#publicFloat");
62
-        assertEquals(res, "0.42");
62
+        assertEquals("0.42", res);
63
     }
63
     }
64
 
64
 
65
     @Test
65
     @Test
68
         ServerApplication app = getServer();
68
         ServerApplication app = getServer();
69
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
69
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
70
                 "lecture#test#publicDouble");
70
                 "lecture#test#publicDouble");
71
-        assertEquals(res, "0.125");
71
+        assertEquals("0.125", res);
72
     }
72
     }
73
 
73
 
74
     @Test
74
     @Test
77
         ServerApplication app = getServer();
77
         ServerApplication app = getServer();
78
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
78
         String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
79
                 "lecture#test#publicBool");
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
         return str;
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
     public String toString()
55
     public String toString()
47
     {
56
     {
48
         return String.format("privateString=%s, privateInt=%d, publicInt=%d, aPrivateField=%d, publicFloat=%f, publicDouble=%f, publicBool=%b",
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
         assertEquals(res, "24");
20
         assertEquals(res, "24");
21
         Object obj = app.getObject("test");
21
         Object obj = app.getObject("test");
22
         assertNotNull(obj);
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
     @Test
26
     @Test
33
         assertEquals(res, "42");
33
         assertEquals(res, "42");
34
         Object obj = app.getObject("test");
34
         Object obj = app.getObject("test");
35
         assertNotNull(obj);
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
     @Test
39
     @Test
46
         assertEquals(res, "a_value");
46
         assertEquals(res, "a_value");
47
         Object obj = app.getObject("test");
47
         Object obj = app.getObject("test");
48
         assertNotNull(obj);
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
     @Test
52
     @Test
59
         assertEquals(res, "4242.5");
59
         assertEquals(res, "4242.5");
60
         Object obj = app.getObject("test");
60
         Object obj = app.getObject("test");
61
         assertNotNull(obj);
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
     @Test
65
     @Test
72
         assertEquals(res, "false");
72
         assertEquals(res, "false");
73
         Object obj = app.getObject("test");
73
         Object obj = app.getObject("test");
74
         assertNotNull(obj);
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