001package com.ganteater.ae.processor;
002
003import static org.junit.Assert.*;
004
005import java.io.File;
006import java.io.FileWriter;
007import java.util.ArrayList;
008import java.util.List;
009
010import org.junit.Test;
011
012public class ParameterIteratorTest {
013
014        private static class StubProcessor extends Processor {
015                private final File baseDir;
016                private final java.util.Map<String, Object> vars = new java.util.HashMap<String, Object>();
017
018                StubProcessor(File baseDir) {
019                        this.baseDir = baseDir;
020                }
021
022                @Override
023                public File getBaseDir() {
024                        return baseDir;
025                }
026
027                @Override
028                public void setVariableValue(String name, Object value) {
029                        vars.put(name, value);
030                }
031
032                @Override
033                public Object getVariableValue(String name) {
034                        return vars.get(name);
035                }
036
037                @Override
038                public String replaceProperties(String value) {
039                        return value;
040                }
041        }
042
043        @Test
044        public void addRule_value_shouldAddSingleValue() throws Exception {
045                // Arrange
046                StubProcessor processor = new StubProcessor(new File("."));
047                ParameterIterator it = new ParameterIterator(processor, "X", 0);
048
049                // Act
050                it.addRule("value", "abc");
051
052                // Assert
053                assertEquals(1, it.count());
054                List<String> set = new ArrayList<String>();
055                it.setType(ParameterIterator.SINGLE);
056                it.applyValue(0, processor, set);
057                assertEquals("abc", processor.getVariableValue("X"));
058        }
059
060        @Test
061        public void addRule_enum_shouldAddAllTokens() throws Exception {
062                // Arrange
063                StubProcessor processor = new StubProcessor(new File("."));
064                ParameterIterator it = new ParameterIterator(processor, "X", 0);
065
066                // Act
067                it.addRule("enum", "a;b;c");
068
069                // Assert
070                assertEquals(3, it.count());
071                it.setType(ParameterIterator.SINGLE);
072                List<String> set = new ArrayList<String>();
073                it.applyValue(0, processor, set);
074                assertEquals("a", processor.getVariableValue("X"));
075                it.applyValue(1, processor, set);
076                assertEquals("b", processor.getVariableValue("X"));
077                it.applyValue(2, processor, set);
078                assertEquals("c", processor.getVariableValue("X"));
079        }
080
081        @Test
082        public void addRule_inc_positive_shouldGenerateIncreasingSequence() throws Exception {
083                // Arrange
084                StubProcessor processor = new StubProcessor(new File("."));
085                ParameterIterator it = new ParameterIterator(processor, "X", 0);
086                it.setType(ParameterIterator.SINGLE);
087
088                // Act
089                it.addRule("inc", "1;3");
090
091                // Assert
092                assertEquals(3, it.count());
093                List<String> set = new ArrayList<String>();
094                it.applyValue(0, processor, set);
095                assertEquals("1", processor.getVariableValue("X"));
096                it.applyValue(1, processor, set);
097                assertEquals("2", processor.getVariableValue("X"));
098                it.applyValue(2, processor, set);
099                assertEquals("3", processor.getVariableValue("X"));
100        }
101
102        @Test
103        public void addRule_inc_negative_shouldGenerateDecreasingSequence() throws Exception {
104                // Arrange
105                StubProcessor processor = new StubProcessor(new File("."));
106                ParameterIterator it = new ParameterIterator(processor, "X", 0);
107                it.setType(ParameterIterator.SINGLE);
108
109                // Act
110                it.addRule("inc", "3;-2");
111
112                // Assert
113                assertEquals(2, it.count());
114                List<String> set = new ArrayList<String>();
115                it.applyValue(0, processor, set);
116                assertEquals("3", processor.getVariableValue("X"));
117                it.applyValue(1, processor, set);
118                assertEquals("2", processor.getVariableValue("X"));
119        }
120
121        @Test
122        public void addRule_file_shouldLoadValuesFromFile() throws Exception {
123                // Arrange
124                File dir = new File("target/test-tmp/parameter-iterator");
125                assertTrue(dir.mkdirs() || dir.isDirectory());
126                File file = new File(dir, "values.txt");
127                FileWriter fw = new FileWriter(file);
128                try {
129                        fw.write("a\n");
130                        fw.write("b\n");
131                } finally {
132                        fw.close();
133                }
134
135                StubProcessor processor = new StubProcessor(dir);
136                ParameterIterator it = new ParameterIterator(processor, "X", 0);
137                it.setType(ParameterIterator.SINGLE);
138
139                // Act
140                it.addRule("file", "values.txt");
141
142                // Assert
143                assertEquals(2, it.count());
144                List<String> set = new ArrayList<String>();
145                it.applyValue(0, processor, set);
146                assertEquals("a", processor.getVariableValue("X"));
147                it.applyValue(1, processor, set);
148                assertEquals("b", processor.getVariableValue("X"));
149        }
150
151        @Test
152        public void applyValue_single_shouldApplyDefaultBeforeStartStepThenCycle() throws Exception {
153                // Arrange
154                StubProcessor processor = new StubProcessor(new File("."));
155                ParameterIterator it = new ParameterIterator(processor, "X", 2);
156                it.addRule("enum", "a;b");
157                it.setType(ParameterIterator.SINGLE);
158
159                List<String> set = new ArrayList<String>();
160
161                // Act
162                it.applyValue(0, processor, set);
163                Object v0 = processor.getVariableValue("X");
164                set.clear();
165                it.applyValue(2, processor, set);
166                Object v2 = processor.getVariableValue("X");
167                set.clear();
168                it.applyValue(3, processor, set);
169                Object v3 = processor.getVariableValue("X");
170                set.clear();
171                it.applyValue(5, processor, set);
172                Object v5 = processor.getVariableValue("X");
173
174                // Assert
175                assertEquals("a", v0);
176                assertEquals("a", v2);
177                assertEquals("b", v3);
178                assertEquals("a", v5);
179        }
180
181        @Test
182        public void applyValue_independent_shouldApplyDefaultBeforeStartStepAndThenStayAtCurrentIterationIndex() throws Exception {
183                // Arrange
184                StubProcessor processor = new StubProcessor(new File("."));
185                ParameterIterator it = new ParameterIterator(processor, "X", 2);
186                it.addRule("enum", "a;b");
187                it.setType(ParameterIterator.INDEPENDENT);
188
189                List<String> set = new ArrayList<String>();
190
191                // Act
192                it.applyValue(0, processor, set);
193                Object v0 = processor.getVariableValue("X");
194                set.clear();
195                it.applyValue(2, processor, set);
196                Object v2 = processor.getVariableValue("X");
197                set.clear();
198                it.applyValue(10, processor, set);
199                Object v10 = processor.getVariableValue("X");
200
201                // Assert
202                assertEquals("a", v0);
203                assertEquals("b", v2);
204                assertEquals("b", v10);
205        }
206
207        @Test
208        public void applyChieldValue_multiple_shouldCombineParentAndChildUsingInterval() throws Exception {
209                // Arrange
210                StubProcessor processor = new StubProcessor(new File("."));
211
212                ParameterIterator parent = new ParameterIterator(processor, "A", 0);
213                parent.addRule("enum", "a1;a2");
214                parent.setType(ParameterIterator.MULTIPLE);
215                parent.setParent(true);
216
217                ParameterIterator child = new ParameterIterator(processor, "B", 0);
218                child.addRule("enum", "b1;b2;b3");
219                child.setType(ParameterIterator.MULTIPLE);
220                parent.setChield(child);
221
222                List<String> set = new ArrayList<String>();
223
224                // Act
225                parent.applyValue(0, processor, set);
226                Object a0 = processor.getVariableValue("A");
227                Object b0 = processor.getVariableValue("B");
228                set.clear();
229                parent.applyValue(2, processor, set);
230                Object a2 = processor.getVariableValue("A");
231                Object b2 = processor.getVariableValue("B");
232
233                // Assert
234                assertEquals("a1", a0);
235                assertEquals("b1", b0);
236                assertEquals("a1", a2);
237                assertEquals("b2", b2);
238        }
239
240        @Test(expected = IllegalArgumentException.class)
241        public void addRule_unknownRule_shouldThrowIllegalArgumentException() throws Exception {
242                // Arrange
243                StubProcessor processor = new StubProcessor(new File("."));
244                ParameterIterator it = new ParameterIterator(processor, "X", 0);
245
246                // Act
247                it.addRule("unknown", "x");
248        }
249}