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.model;
17  
18  import java.util.ArrayList;
19  import java.util.Collections;
20  import java.util.List;
21  
22  import javax.xml.bind.annotation.XmlTransient;
23  
24  import org.javagen.agile.core.model.AbstractModel;
25  import org.javagen.agile.core.model.Model;
26  import org.javagen.agile.core.visitor.DefaultVisitor;
27  
28  /***
29   * Abstract Test container class.
30   *
31   * @author Richard Easterling
32   */
33  public abstract class TestHolder extends AbstractModel {
34  
35      public TestHolder() {
36          super();
37      }
38      
39      public void addTest(Test test) {
40          super.addChildModel(test);
41      }
42      
43      /***
44       * Returns immediate child Tests instances.
45       */
46      @XmlTransient
47      public List<Test> getChildTests() {
48          List<Test> tests = new ArrayList<Test>();
49          for(Model child : getChildModels()) {
50              if (child instanceof Test)
51                  tests.add( (Test)child );
52          }
53          return tests;
54      }
55  
56      /***
57       * Collect all Test instances in tree including root node.
58       */
59      @XmlTransient
60      public List<Test> getAllTests() {
61          TestVisitor visitor = new TestVisitor();
62          visitor.gen(this);
63          return visitor.getTests();
64      }
65  
66      /*** 
67       * Visitor to collect all Test instances in tree including root node. 
68       */
69      static class TestVisitor extends DefaultVisitor {
70          
71          List<Test> tests = new ArrayList<Test>();
72          
73          public TestVisitor() {
74              this.setItinerary( Collections.singleton(Test.DEFAULT_MODEL_TYPE) );
75          }
76          
77          public void visit(Model model) {
78              tests.add( (Test)model );
79          }
80  
81          public List<Test> getTests() {
82              return tests;
83          }
84      }
85  }