[90] | 1 | package com.framsticks.params; |
---|
| 2 | |
---|
[96] | 3 | import java.util.Arrays; |
---|
[90] | 4 | |
---|
| 5 | import org.testng.annotations.BeforeClass; |
---|
| 6 | import org.testng.annotations.Test; |
---|
| 7 | |
---|
[99] | 8 | import com.framsticks.params.types.EventParam; |
---|
[90] | 9 | import com.framsticks.params.types.ProcedureParam; |
---|
| 10 | import com.framsticks.params.types.StringParam; |
---|
[96] | 11 | import com.framsticks.parsers.Savers; |
---|
[105] | 12 | import com.framsticks.structure.messages.ValueChange; |
---|
[90] | 13 | import com.framsticks.test.TestClass; |
---|
| 14 | import com.framsticks.test.TestConfiguration; |
---|
[99] | 15 | import com.framsticks.util.lang.Holder; |
---|
| 16 | |
---|
[90] | 17 | import static org.fest.assertions.Assertions.*; |
---|
[105] | 18 | import static com.framsticks.params.ParamsUtil.arguments; |
---|
| 19 | import static com.framsticks.params.ParamsUtil.getParam; |
---|
[90] | 20 | |
---|
| 21 | @Test |
---|
| 22 | public class FramsClassBuilderTest extends TestConfiguration { |
---|
| 23 | |
---|
| 24 | FramsClass framsClass; |
---|
| 25 | ReflectionAccess access; |
---|
| 26 | TestClass test; |
---|
| 27 | |
---|
| 28 | @BeforeClass |
---|
| 29 | public void setUp() { |
---|
| 30 | test = new TestClass(); |
---|
| 31 | test.setName("test"); |
---|
| 32 | test.appendHistory("first"); |
---|
| 33 | framsClass = FramsClass.build().forClass(TestClass.class); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | @Test |
---|
| 37 | public void checkProcedureParams() { |
---|
[100] | 38 | assertThat(framsClass.getParamCount()).isEqualTo(8); |
---|
[90] | 39 | |
---|
| 40 | assertThat(framsClass.getParam("name")).isInstanceOf(StringParam.class); |
---|
| 41 | assertThat(framsClass.getParam("history")).isInstanceOf(StringParam.class); |
---|
[99] | 42 | assertThat(framsClass.getParam("history_changed")).isInstanceOf(EventParam.class); |
---|
[90] | 43 | |
---|
| 44 | assertThat(framsClass.getParam("appendHistory")).isInstanceOf(ProcedureParam.class); |
---|
| 45 | assertThat(framsClass.getParam("resetHistory")).isInstanceOf(ProcedureParam.class); |
---|
[96] | 46 | |
---|
[105] | 47 | |
---|
| 48 | ProcedureParam appendHistory = getParam(framsClass, "appendHistory", ProcedureParam.class); |
---|
[90] | 49 | assertThat(appendHistory.getArgumentsType().size()).isEqualTo(1); |
---|
| 50 | assertThat(appendHistory.getArgumentsType().get(0).getId()).isEqualTo("arg0"); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | @Test(dependsOnMethods = "checkProcedureParams") |
---|
[96] | 54 | public void print() { |
---|
| 55 | assertThat(Savers.saveFramsClass(new ListSink(), framsClass).getOut()).isEqualTo( |
---|
| 56 | Arrays.asList( |
---|
| 57 | "class:", |
---|
| 58 | "name:TestClass", |
---|
| 59 | "id:TestClass", |
---|
| 60 | "", |
---|
| 61 | "prop:", |
---|
| 62 | "id:name", |
---|
| 63 | "name:Name", |
---|
| 64 | "type:s", |
---|
| 65 | "", |
---|
| 66 | "prop:", |
---|
| 67 | "id:history", |
---|
| 68 | "name:History", |
---|
| 69 | "type:s", |
---|
| 70 | "", |
---|
| 71 | "prop:", |
---|
[99] | 72 | "id:history_changed", |
---|
| 73 | "name:HistoryListener", |
---|
[103] | 74 | "type:e ValueChange", |
---|
[99] | 75 | "", |
---|
| 76 | "prop:", |
---|
[96] | 77 | "id:appendHistory", |
---|
| 78 | "name:AppendHistory", |
---|
| 79 | "type:p d(s arg0)", |
---|
| 80 | "", |
---|
| 81 | "prop:", |
---|
| 82 | "id:resetHistory", |
---|
| 83 | "name:ResetHistory", |
---|
| 84 | "type:p()", |
---|
[100] | 85 | "", |
---|
| 86 | "prop:", |
---|
| 87 | "id:children", |
---|
| 88 | "name:Children", |
---|
| 89 | "type:l TestChild uid", |
---|
| 90 | "flags:1", |
---|
| 91 | "", |
---|
| 92 | "prop:", |
---|
| 93 | "id:createChild", |
---|
| 94 | "name:CreateChild", |
---|
| 95 | "type:p(s arg0)", |
---|
| 96 | "", |
---|
| 97 | "prop:", |
---|
| 98 | "id:children_changed", |
---|
| 99 | "name:ChildrenListener", |
---|
| 100 | "type:e ListChange", |
---|
[96] | 101 | "" |
---|
| 102 | ) |
---|
| 103 | ); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | @Test(dependsOnMethods = "print") |
---|
[99] | 107 | public void createAccess() { |
---|
[90] | 108 | access = new ReflectionAccess(TestClass.class, framsClass); |
---|
| 109 | access.select(test); |
---|
[99] | 110 | } |
---|
[90] | 111 | |
---|
[99] | 112 | @Test(dependsOnMethods = "createAccess") |
---|
| 113 | public void callProcedures() { |
---|
| 114 | |
---|
[97] | 115 | assertThat(access.get("history", String.class)).isEqualTo("initial|first|"); |
---|
[105] | 116 | Object result = access.call("appendHistory", arguments("second")); |
---|
[90] | 117 | |
---|
| 118 | assertThat(result).isInstanceOf(Integer.class); |
---|
[97] | 119 | assertThat(result).isEqualTo(21); |
---|
| 120 | assertThat(access.get("history", String.class)).isEqualTo("initial|first|second|"); |
---|
[90] | 121 | |
---|
[105] | 122 | result = access.call("resetHistory"); |
---|
[90] | 123 | assertThat(result).isNull(); |
---|
| 124 | |
---|
| 125 | assertThat(access.get("history", String.class)).isEqualTo(""); |
---|
[99] | 126 | } |
---|
[90] | 127 | |
---|
[99] | 128 | @Test(dependsOnMethods = "callProcedures") |
---|
| 129 | public void listeners() { |
---|
| 130 | |
---|
| 131 | final Holder<String> called = new Holder<>(); |
---|
| 132 | |
---|
[103] | 133 | final EventListener<ValueChange> listener = new EventListener<ValueChange>() { |
---|
[99] | 134 | |
---|
| 135 | @Override |
---|
[103] | 136 | public void action(ValueChange argument) { |
---|
| 137 | called.set(argument.value.toString()); |
---|
[99] | 138 | } |
---|
| 139 | }; |
---|
| 140 | |
---|
[105] | 141 | final EventParam eventParam = getParam(access, "history_changed", EventParam.class); |
---|
[99] | 142 | access.reg(eventParam, listener); |
---|
| 143 | |
---|
| 144 | final String currentHistory = access.get("history", String.class); |
---|
| 145 | final String addition = "test"; |
---|
| 146 | |
---|
[105] | 147 | access.call("appendHistory", arguments(addition)); |
---|
[99] | 148 | |
---|
| 149 | String expected = currentHistory + addition + "|"; |
---|
| 150 | assertThat(access.get("history", String.class)).isEqualTo(expected); |
---|
| 151 | assertThat(called.get()).isEqualTo(expected); |
---|
| 152 | access.regRemove(eventParam, listener); |
---|
[90] | 153 | } |
---|
| 154 | |
---|
| 155 | } |
---|