1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.javagen.funcgen.visitor;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.javagen.agile.core.model.Model;
22 import org.javagen.agile.core.visitor.TypeLookupVisitor;
23 import org.javagen.agile.core.visitor.Visit;
24 import org.javagen.funcgen.model.Permutations;
25 import org.javagen.funcgen.model.Suite;
26 import org.javagen.funcgen.model.Test;
27
28 /***
29 * Expand Suites with permutations declarations into a set of tests, one for
30 * each combination of permutations.
31 *
32 * @author Richard Easterling
33 */
34 public class TestExpanderVisitor extends TypeLookupVisitor {
35
36 /***
37 * Setup Suite visitor for expansion.
38 */
39 public TestExpanderVisitor() {
40 super();
41 this.put(Suite.class, new Visit() {
42 public void visit(Model model) {
43 Suite suite = (Suite)model;
44 suite.put("suiteName", suite.getName());
45 if (suite.getPermutations()!=null) {
46 expandPermutations(suite);
47 }
48 }
49 });
50 }
51
52 /***
53 * Create a unique test for each combination of permutations.
54 *
55 * @param test that holds permutations
56 */
57 protected void expandPermutations(Suite suite) {
58 List<Test> expanedTests = new ArrayList<Test>();
59 for(Permutations permutations : suite.getPermutations()) {
60 if (expanedTests.isEmpty()) {
61 for(String mutation : permutations.getValue()) {
62
63 Test newTest = new Test(mutation.toString());
64
65 newTest.put(permutations.getName(), mutation);
66 expanedTests.add(newTest);
67 }
68 } else {
69 List<Test> multiplyTests = new ArrayList<Test>(expanedTests.size()*suite.getPermutations().size());
70 for(String mutation : permutations.getValue()) {
71 for(Test baseTest : expanedTests) {
72
73 Test newTest = new Test(baseTest.getName()+"-"+mutation.toString());
74
75 newTest.put(permutations.getName(), mutation);
76 for(String key: baseTest.getContext().keySet()) {
77 newTest.put(key, baseTest.get(key));
78 }
79 multiplyTests.add(newTest);
80 }
81 }
82 expanedTests = multiplyTests;
83 }
84 }
85 for(Test newTest : expanedTests) {
86 suite.addTest(newTest);
87 }
88 }
89
90 }