Browse Source

added method executor; tests

develop
Robin Thoni 7 years ago
parent
commit
c60a45e5c3

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

@@ -1,6 +1,8 @@
1 1
 package com.uqac.rthoni.java_rmi.common;
2 2
 
3 3
 import java.io.Serializable;
4
+import java.util.Arrays;
5
+import java.util.Collections;
4 6
 import java.util.Vector;
5 7
 import java.util.stream.Collectors;
6 8
 
@@ -72,6 +74,11 @@ public class Command implements Serializable {
72 74
         return _arguments.get(i);
73 75
     }
74 76
 
77
+    public Vector<String> getArgmumentAsList(int i)
78
+    {
79
+        return new Vector<String>(Arrays.asList(_arguments.get(i).split(",")));
80
+    }
81
+
75 82
     public void setResult(String result)
76 83
     {
77 84
         _result = result;

+ 51
- 0
common/src/main/java/com/uqac/rthoni/java_rmi/common/ReflectionUtil.java View File

@@ -73,4 +73,55 @@ public class ReflectionUtil {
73 73
 
74 74
         return classList;
75 75
     }
76
+
77
+    public static Object toObject(Class className, String value)
78
+    {
79
+        if (className == Boolean.class || className == boolean.class) {
80
+            return Boolean.parseBoolean(value);
81
+        }
82
+        if (className == Byte.class || className == byte.class) {
83
+            return Byte.parseByte(value);
84
+        }
85
+        if (className == Short.class || className == short.class) {
86
+            return Short.parseShort(value);
87
+        }
88
+        if (className == Integer.class || className == int.class) {
89
+            return Integer.parseInt(value);
90
+        }
91
+        if (className == Long.class || className == long.class) {
92
+            return Long.parseLong(value);
93
+        }
94
+        if (className == Float.class || className == float.class) {
95
+            return Float.parseFloat(value);
96
+        }
97
+        if (className == Double.class || className == double.class) {
98
+            return Double.parseDouble(value);
99
+        }
100
+        return value;
101
+    }
102
+
103
+    public static Class getClass(String className) throws ClassNotFoundException {
104
+        if (className.equals("boolean")) {
105
+            return boolean.class;
106
+        }
107
+        if (className.equals("byte")) {
108
+            return byte.class;
109
+        }
110
+        if (className.equals("short")) {
111
+            return short.class;
112
+        }
113
+        if (className.equals("int")) {
114
+            return int.class;
115
+        }
116
+        if (className.equals("long")) {
117
+            return long.class;
118
+        }
119
+        if (className.equals("float")) {
120
+            return float.class;
121
+        }
122
+        if (className.equals("double")) {
123
+            return double.class;
124
+        }
125
+        return Class.forName(className);
126
+    }
76 127
 }

+ 8
- 0
server/pom.xml View File

@@ -10,6 +10,14 @@
10 10
     <modelVersion>4.0.0</modelVersion>
11 11
 
12 12
     <artifactId>server</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
+    </dependencies>
13 21
 
14 22
 
15 23
 </project>

+ 2
- 0
server/server.iml View File

@@ -11,6 +11,8 @@
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
     <orderEntry type="module" module-name="common" />
15 17
   </component>
16 18
 </module>

+ 5
- 5
server/src/main/java/com/uqac/rthoni/java_rmi/server/ServerApplication.java View File

@@ -103,7 +103,7 @@ public class ServerApplication {
103 103
         }
104 104
     }
105 105
 
106
-    private void loadExecutors() throws ClassNotFoundException
106
+    public void loadExecutors() throws ClassNotFoundException
107 107
     {
108 108
         _executors = new Vector<>();
109 109
         Vector<Class> classes = AbstractCommandExecutor.getAllExecutors();
@@ -117,7 +117,7 @@ public class ServerApplication {
117 117
         }
118 118
     }
119 119
 
120
-    private void runServer(ServerSocket serverSocket)
120
+    public void runServer(ServerSocket serverSocket)
121 121
     {
122 122
         while (true) {
123 123
             Socket client = null;
@@ -144,7 +144,7 @@ public class ServerApplication {
144 144
         }
145 145
     }
146 146
 
147
-    private void handleClient(Socket client)
147
+    public void handleClient(Socket client)
148 148
     {
149 149
         String str;
150 150
         try {
@@ -165,7 +165,7 @@ public class ServerApplication {
165 165
         }
166 166
     }
167 167
 
168
-    private AbstractCommandExecutor getExecutor(Command command)
168
+    public AbstractCommandExecutor getExecutor(Command command)
169 169
     {
170 170
         for (AbstractCommandExecutor executor : _executors) {
171 171
             if (executor.getCommandName().equals(command.getCommandName())) {
@@ -175,7 +175,7 @@ public class ServerApplication {
175 175
         return null;
176 176
     }
177 177
 
178
-    private void handleCommand(Command command, Socket client)
178
+    public void handleCommand(Command command, Socket client)
179 179
     {
180 180
         String data;
181 181
         AbstractCommandExecutor executor = getExecutor(command);

+ 4
- 0
server/src/main/java/com/uqac/rthoni/java_rmi/server/TestDbo.java View File

@@ -25,4 +25,8 @@ public class TestDbo {
25 25
     public void setPrivateInt(int privateInt) {
26 26
         this.privateInt = privateInt;
27 27
     }
28
+
29
+    public void setPrivateInteger(Integer privateInt) {
30
+        this.privateInt = privateInt;
31
+    }
28 32
 }

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

@@ -0,0 +1,49 @@
1
+package com.uqac.rthoni.java_rmi.server.executors;
2
+
3
+import com.uqac.rthoni.java_rmi.common.Command;
4
+import com.uqac.rthoni.java_rmi.common.ReflectionUtil;
5
+import com.uqac.rthoni.java_rmi.server.ServerApplication;
6
+import javafx.util.Pair;
7
+
8
+import java.lang.reflect.Method;
9
+import java.util.List;
10
+import java.util.stream.Collectors;
11
+
12
+/**
13
+ * Created by robin on 9/16/16.
14
+ */
15
+public class MethodExecutor extends AbstractCommandExecutor {
16
+    @Override
17
+    public String getCommandName() {
18
+        return "fonction";
19
+    }
20
+
21
+    @Override
22
+    public String run(Command command, ServerApplication server) throws Exception {
23
+        String id = command.getArgument(0);
24
+        String methodName = command.getArgument(1);
25
+        Object obj = server.getObject(id);
26
+        List<Pair<Class, String>> argClasses = command.getArgmumentAsList(2).stream().map(v -> {
27
+            String[] split = v.split(":");
28
+            try {
29
+                return new Pair<>(ReflectionUtil.getClass(split[0]), split[1]);
30
+            } catch (ClassNotFoundException e) {
31
+                return null;
32
+            }
33
+        }).collect(Collectors.toList());
34
+        List<Object> arguments = argClasses.stream().map(p -> {
35
+            String value = p.getValue();
36
+            if (value.startsWith("ID(") && value.endsWith(")")) {
37
+                value = value.substring(3, value.length() - 2);
38
+                return server.getObject(value);
39
+            }
40
+            return ReflectionUtil.toObject(p.getKey(), value);
41
+        }).collect(Collectors.toList());
42
+        List<Class> classes = argClasses.stream().map(Pair::getKey).collect(Collectors.toList());
43
+
44
+        Method method = obj.getClass().getDeclaredMethod(methodName, classes.toArray(new Class[classes.size()]));
45
+        Object result = method.invoke(obj, arguments.toArray());
46
+
47
+        return (result == null ? "NULL" : result.toString());
48
+    }
49
+}

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

@@ -1,6 +1,7 @@
1 1
 package com.uqac.rthoni.java_rmi.server.executors;
2 2
 
3 3
 import com.uqac.rthoni.java_rmi.common.Command;
4
+import com.uqac.rthoni.java_rmi.common.ReflectionUtil;
4 5
 import com.uqac.rthoni.java_rmi.server.ServerApplication;
5 6
 
6 7
 import java.lang.reflect.Field;
@@ -25,35 +26,13 @@ public class WriteExecutor extends AbstractCommandExecutor {
25 26
         Object obj = server.getObject(id);
26 27
         Class objClass = obj.getClass();
27 28
         Field field = objClass.getDeclaredField(fieldName);
29
+        Object typedValue = ReflectionUtil.toObject(field.getDeclaringClass(), value);
28 30
 
29 31
         try {
30
-            field.set(obj, value);
32
+            field.set(obj, typedValue);
31 33
         } catch (IllegalAccessException e) {
32
-            try {
33
-                Method method = objClass.getDeclaredMethod(methodName, String.class);
34
-                method.invoke(obj, value);
35
-            }
36
-            catch (NoSuchMethodException e2) {
37
-                try {
38
-                    Method method = objClass.getDeclaredMethod(methodName, int.class);
39
-                    method.invoke(obj, Integer.parseInt(value));
40
-                }
41
-                catch (NoSuchMethodException e3) {
42
-                    try {
43
-                        Method method = objClass.getDeclaredMethod(methodName, float.class);
44
-                        method.invoke(obj, Float.parseFloat(value));
45
-                    }
46
-                    catch (NoSuchMethodException e4) {
47
-                        try {
48
-                            Method method = objClass.getDeclaredMethod(methodName, boolean.class);
49
-                            method.invoke(obj, Boolean.parseBoolean(value));
50
-                        }
51
-                        catch (NoSuchMethodException e5) {
52
-                            throw new Exception("No such setter");
53
-                        }
54
-                    }
55
-                }
56
-            }
34
+            Method method = objClass.getDeclaredMethod(methodName, typedValue.getClass());
35
+            method.invoke(obj, typedValue);
57 36
         }
58 37
 
59 38
         return null;

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

@@ -0,0 +1,53 @@
1
+package com.uqac.rthoni.java_rmi.server.executors;
2
+
3
+import com.uqac.rthoni.java_rmi.common.Command;
4
+import com.uqac.rthoni.java_rmi.server.ServerApplication;
5
+import org.junit.Test;
6
+
7
+import static org.junit.Assert.*;
8
+
9
+/**
10
+ * Created by robin on 9/16/16.
11
+ */
12
+public class NewInstanceExecutorTest {
13
+
14
+    public String runCommand(String str, ServerApplication app) throws Exception {
15
+        Command command = Command.fromString(str);
16
+        AbstractCommandExecutor executor = app.getExecutor(command);
17
+        return executor.run(command, app);
18
+    }
19
+
20
+    public ServerApplication getServer() throws Exception {
21
+        ServerApplication app = new ServerApplication();
22
+        app.loadExecutors();
23
+        return app;
24
+    }
25
+
26
+    @Test
27
+    public void test1() throws Exception
28
+    {
29
+        ServerApplication app = getServer();
30
+        String res = runCommand("creation#com.uqac.rthoni.java_rmi.server.TestDbo#test", app);
31
+        assertNull(res);
32
+        Object obj = app.getObject("test");
33
+        assertNotNull(obj);
34
+    }
35
+
36
+    @Test
37
+    public void test2() throws Exception
38
+    {
39
+        ServerApplication app = getServer();
40
+        String res = runCommand("creation#java.lang.String#mystr", app);
41
+        assertNull(res);
42
+        Object obj = app.getObject("mystr");
43
+        assertNotNull(obj);
44
+        assertEquals(obj.toString(), "");
45
+    }
46
+
47
+    @Test(expected = ClassNotFoundException.class)
48
+    public void test3() throws Exception
49
+    {
50
+        runCommand("creation#not_existing_class#myobj", getServer());
51
+    }
52
+
53
+}

Loading…
Cancel
Save