View Javadoc

1   /*
2    * This file is part of hyphenType. hyphenType is free software: you can
3    * redistribute it and/or modify it under the terms of the GNU General Public
4    * License as published by the Free Software Foundation, either version 3 of the
5    * License, or (at your option) any later version. hyphenType is distributed in
6    * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
7    * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
8    * the GNU General Public License for more details. You should have received a
9    * copy of the GNU General Public License along with hyphenType. If not, see
10   * <http://www.gnu.org/licenses/>.
11   */
12  package org.hyphenType.unittesting;
13  
14  import java.util.ArrayList;
15  
16  import org.hyphenType.input.UserInput;
17  import org.hyphenType.input.UserInputException;
18  
19  /**
20   * An user input to replace a standard user input during unit testing.
21   * 
22   * @author Aurelio Akira M. Matsui
23   */
24  public final class UnitTestingUserInput extends UserInput {
25  
26      // //////////////////////////////////////////
27      // TEXT
28      // //////////////////////////////////////////
29  
30      /**
31       * 
32       */
33      private ArrayList<Object> textUIInputs = new ArrayList<Object>();
34  
35      /**
36       * 
37       */
38      private boolean textUIAvailable = true;
39  
40      /**
41       * @param input The input to add.
42       */
43      public void addTextUIInput(final Object input) {
44          textUIInputs.add(input);
45      }
46  
47      /**
48       * Sets whether the textual user interface is available
49       * or not.
50       * 
51       * @param available The value to set.
52       */
53      public void setTextUIAvailable(final boolean available) {
54          this.textUIAvailable = available;
55      }
56  
57      @Override
58      protected boolean isTextUIAvailable() {
59          return textUIAvailable;
60      }
61  
62      @SuppressWarnings("unchecked")
63      @Override
64      protected <T> T readFromTextUI(final Class<T> type, final String message, final String regex) throws UserInputException {
65          if (textUIInputs.size() > 0) {
66              return (T) textUIInputs.remove(0);
67          } else {
68              throw new UserInputException("No input text available");
69          }
70      }
71  
72      // //////////////////////////////////////////
73      // GRAPHICAL
74      // //////////////////////////////////////////
75  
76      /**
77       * 
78       */
79      private ArrayList<Object> graphicalUIInputs = new ArrayList<Object>();
80  
81      /**
82       * 
83       */
84      private boolean graphicalUIAvailable = true;
85  
86      /**
87       * @param input The input to add.
88       */
89      public void addGraphicalUIInput(final Object input) {
90          graphicalUIInputs.add(input);
91      }
92  
93      /**
94       * Sets whether the graphical user interface is available
95       * or not.
96       * 
97       * @param available The value to set.
98       */
99      public void setGraphicalUIAvailable(final boolean available) {
100         this.graphicalUIAvailable = available;
101     }
102 
103     @Override
104     protected boolean isGraphicalUIAvailable() {
105         return graphicalUIAvailable;
106     }
107 
108     @SuppressWarnings("unchecked")
109     @Override
110     protected <T> T readFromGraphicalUI(final Class<T> type, final String message, final String regex) throws UserInputException {
111         if (graphicalUIInputs.size() > 0) {
112             return (T) graphicalUIInputs.remove(0);
113         } else {
114             throw new UserInputException("No input text available");
115         }
116     }
117 
118     // //////////////////////////////////////////
119     // BOTH
120     // //////////////////////////////////////////
121 
122     /**
123      * Adds an object to both text and graphical UI inputs.
124      * 
125      * @param input The input to add.
126      */
127     public void addBothUIInput(final Object input) {
128         addTextUIInput(input);
129         addGraphicalUIInput(input);
130     }
131 }