View Javadoc

1   /*
2    * Copyright 2006 Outsource Cafe, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the 'License')
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an 'AS IS' BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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());// this should probably go somewhere else
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                      //new test with unique name
63                      Test newTest = new Test(mutation.toString());
64                      //load context
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                          //new test with unique name
73                          Test newTest = new Test(baseTest.getName()+"-"+mutation.toString());
74                          //load context
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  }