Browse Source

test

develop
Robin Thoni 7 years ago
parent
commit
b60c7b9fa3

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

26
         Object obj = server.getObject(id);
26
         Object obj = server.getObject(id);
27
         Class objClass = obj.getClass();
27
         Class objClass = obj.getClass();
28
         Field field = objClass.getDeclaredField(fieldName);
28
         Field field = objClass.getDeclaredField(fieldName);
29
-        Object typedValue = ReflectionUtil.toObject(field.getDeclaringClass(), value);
29
+        Object typedValue = ReflectionUtil.toObject(field.getType(), value);
30
 
30
 
31
         try {
31
         try {
32
             field.set(obj, typedValue);
32
             field.set(obj, typedValue);
33
         } catch (IllegalAccessException e) {
33
         } catch (IllegalAccessException e) {
34
-            Method method = objClass.getDeclaredMethod(methodName, typedValue.getClass());
34
+            Method method = objClass.getDeclaredMethod(methodName, field.getType());
35
             method.invoke(obj, typedValue);
35
             method.invoke(obj, typedValue);
36
         }
36
         }
37
 
37
 

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

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
+
6
+/**
7
+ * Created by robin on 9/16/16.
8
+ */
9
+public abstract class AbstractTest {
10
+
11
+    public String runCommand(ServerApplication app, String str) throws Exception {
12
+        Command command = Command.fromString(str);
13
+        AbstractCommandExecutor executor = app.getExecutor(command);
14
+        return executor.run(command, app);
15
+    }
16
+
17
+    public String runCommands(ServerApplication app, String ...str) throws Exception {
18
+        StringBuilder stringBuilder = new StringBuilder();
19
+        for (String s : str) {
20
+            String res = runCommand(app, s);
21
+            if (res != null) {
22
+                stringBuilder.append(res);
23
+            }
24
+        }
25
+        return stringBuilder.toString();
26
+    }
27
+
28
+    public ServerApplication getServer() throws Exception {
29
+        ServerApplication app = new ServerApplication();
30
+        app.loadExecutors();
31
+        return app;
32
+    }
33
+}

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

4
 import com.uqac.rthoni.java_rmi.server.ServerApplication;
4
 import com.uqac.rthoni.java_rmi.server.ServerApplication;
5
 import org.junit.Test;
5
 import org.junit.Test;
6
 
6
 
7
+import java.util.Arrays;
8
+
7
 import static org.junit.Assert.*;
9
 import static org.junit.Assert.*;
8
 
10
 
9
 /**
11
 /**
10
  * Created by robin on 9/16/16.
12
  * Created by robin on 9/16/16.
11
  */
13
  */
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
-    }
14
+public class NewInstanceExecutorTest extends AbstractTest {
25
 
15
 
26
     @Test
16
     @Test
27
     public void test1() throws Exception
17
     public void test1() throws Exception
28
     {
18
     {
29
         ServerApplication app = getServer();
19
         ServerApplication app = getServer();
30
-        String res = runCommand("creation#com.uqac.rthoni.java_rmi.server.TestDbo#test", app);
20
+        String res = runCommand(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test");
31
         assertNull(res);
21
         assertNull(res);
32
         Object obj = app.getObject("test");
22
         Object obj = app.getObject("test");
33
         assertNotNull(obj);
23
         assertNotNull(obj);
24
+        assertEquals(obj.toString(), "privateString=default_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true");
34
     }
25
     }
35
 
26
 
36
     @Test
27
     @Test
37
     public void test2() throws Exception
28
     public void test2() throws Exception
38
     {
29
     {
39
         ServerApplication app = getServer();
30
         ServerApplication app = getServer();
40
-        String res = runCommand("creation#java.lang.String#mystr", app);
31
+        String res = runCommand(app, "creation#java.lang.String#mystr");
41
         assertNull(res);
32
         assertNull(res);
42
         Object obj = app.getObject("mystr");
33
         Object obj = app.getObject("mystr");
43
         assertNotNull(obj);
34
         assertNotNull(obj);
47
     @Test(expected = ClassNotFoundException.class)
38
     @Test(expected = ClassNotFoundException.class)
48
     public void test3() throws Exception
39
     public void test3() throws Exception
49
     {
40
     {
50
-        runCommand("creation#not_existing_class#myobj", getServer());
41
+        ServerApplication app = getServer();
42
+        runCommand(app, "creation#not_existing_class#myobj");
51
     }
43
     }
52
 
44
 
53
 }
45
 }

+ 82
- 0
server/src/test/java/com/uqac/rthoni/java_rmi/server/executors/ReadExecutorTest.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 ReadExecutorTest 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
+                "lecture#test#publicInt");
19
+        assertEquals(res, "42");
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
+                "lecture#test#privateInt");
28
+        assertEquals(res, "24");
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
+                "lecture#test#privateString");
37
+        assertEquals(res, "default_value");
38
+    }
39
+
40
+    @Test(expected = NoSuchMethodException.class)
41
+    public void test4() throws Exception
42
+    {
43
+        ServerApplication app = getServer();
44
+        runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
45
+                "lecture#test#aPrivateField");
46
+    }
47
+
48
+    @Test(expected = NullPointerException.class)
49
+    public void test5() throws Exception
50
+    {
51
+        ServerApplication app = getServer();
52
+        runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
53
+                "lecture#test2#privateString");
54
+    }
55
+
56
+    @Test
57
+    public void test6() throws Exception
58
+    {
59
+        ServerApplication app = getServer();
60
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
61
+                "lecture#test#publicFloat");
62
+        assertEquals(res, "0.42");
63
+    }
64
+
65
+    @Test
66
+    public void test7() throws Exception
67
+    {
68
+        ServerApplication app = getServer();
69
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
70
+                "lecture#test#publicDouble");
71
+        assertEquals(res, "0.125");
72
+    }
73
+
74
+    @Test
75
+    public void test8() throws Exception
76
+    {
77
+        ServerApplication app = getServer();
78
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
79
+                "lecture#test#publicBool");
80
+        assertEquals(res, "true");
81
+    }
82
+}

server/src/main/java/com/uqac/rthoni/java_rmi/server/TestDbo.java → server/src/test/java/com/uqac/rthoni/java_rmi/server/executors/TestDbo.java View File

1
-package com.uqac.rthoni.java_rmi.server;
1
+package com.uqac.rthoni.java_rmi.server.executors;
2
 
2
 
3
 /**
3
 /**
4
  * Created by robin on 9/16/16.
4
  * Created by robin on 9/16/16.
10
 
10
 
11
     public int publicInt = 42;
11
     public int publicInt = 42;
12
 
12
 
13
+    private int aPrivateField = 0;
14
+
15
+    public float publicFloat = 0.42f;
16
+
17
+    public double publicDouble = 0.125f;
18
+
19
+    public boolean publicBool = true;
20
+
13
     public String getPrivateString() {
21
     public String getPrivateString() {
14
         return privateString;
22
         return privateString;
15
     }
23
     }
29
     public void setPrivateInteger(Integer privateInt) {
37
     public void setPrivateInteger(Integer privateInt) {
30
         this.privateInt = privateInt;
38
         this.privateInt = privateInt;
31
     }
39
     }
40
+
41
+    public String getMyself(String str)
42
+    {
43
+        return str;
44
+    }
45
+
46
+    public String toString()
47
+    {
48
+        return String.format("privateString=%s, privateInt=%d, publicInt=%d, aPrivateField=%d, publicFloat=%f, publicDouble=%f, publicBool=%b",
49
+                privateString, privateInt, publicInt, aPrivateField, publicFloat, publicDouble, publicBool);
50
+    }
32
 }
51
 }

+ 77
- 0
server/src/test/java/com/uqac/rthoni/java_rmi/server/executors/WriteExecutorTest.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 WriteExecutorTest 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
+                "ecriture#test#publicInt#24",
19
+                "lecture#test#publicInt");
20
+        assertEquals(res, "24");
21
+        Object obj = app.getObject("test");
22
+        assertNotNull(obj);
23
+        assertEquals(obj.toString(), "privateString=default_value, privateInt=24, publicInt=24, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true");
24
+    }
25
+
26
+    @Test
27
+    public void test2() throws Exception
28
+    {
29
+        ServerApplication app = getServer();
30
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
31
+                "ecriture#test#privateInt#42",
32
+                "lecture#test#privateInt");
33
+        assertEquals(res, "42");
34
+        Object obj = app.getObject("test");
35
+        assertNotNull(obj);
36
+        assertEquals(obj.toString(), "privateString=default_value, privateInt=42, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true");
37
+    }
38
+
39
+    @Test
40
+    public void test3() throws Exception
41
+    {
42
+        ServerApplication app = getServer();
43
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
44
+                "ecriture#test#privateString#a_value",
45
+                "lecture#test#privateString");
46
+        assertEquals(res, "a_value");
47
+        Object obj = app.getObject("test");
48
+        assertNotNull(obj);
49
+        assertEquals(obj.toString(), "privateString=a_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=true");
50
+    }
51
+
52
+    @Test
53
+    public void test4() throws Exception
54
+    {
55
+        ServerApplication app = getServer();
56
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
57
+                "ecriture#test#publicFloat#4242.5",
58
+                "lecture#test#publicFloat");
59
+        assertEquals(res, "4242.5");
60
+        Object obj = app.getObject("test");
61
+        assertNotNull(obj);
62
+        assertEquals(obj.toString(), "privateString=default_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=4242.500000, publicDouble=0.125000, publicBool=true");
63
+    }
64
+
65
+    @Test
66
+    public void test5() throws Exception
67
+    {
68
+        ServerApplication app = getServer();
69
+        String res = runCommands(app, "creation#com.uqac.rthoni.java_rmi.server.executors.TestDbo#test",
70
+                "ecriture#test#publicBool#false",
71
+                "lecture#test#publicBool");
72
+        assertEquals(res, "false");
73
+        Object obj = app.getObject("test");
74
+        assertNotNull(obj);
75
+        assertEquals(obj.toString(), "privateString=default_value, privateInt=24, publicInt=42, aPrivateField=0, publicFloat=0.420000, publicDouble=0.125000, publicBool=false");
76
+    }
77
+}

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

9
       <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
9
       <sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10
       <excludeFolder url="file://$MODULE_DIR$/target" />
10
       <excludeFolder url="file://$MODULE_DIR$/target" />
11
     </content>
11
     </content>
12
+    <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
13
+    <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
12
     <orderEntry type="inheritedJdk" />
14
     <orderEntry type="inheritedJdk" />
13
     <orderEntry type="sourceFolder" forTests="false" />
15
     <orderEntry type="sourceFolder" forTests="false" />
16
+    <orderEntry type="module" module-name="server" />
14
     <orderEntry type="module" module-name="client" />
17
     <orderEntry type="module" module-name="client" />
15
     <orderEntry type="module" module-name="common" />
18
     <orderEntry type="module" module-name="common" />
16
-    <orderEntry type="module" module-name="server" />
17
   </component>
19
   </component>
18
 </module>
20
 </module>

Loading…
Cancel
Save