Clover Coverage Report - EasyMock 3.0
Coverage timestamp: sam. mai 8 2010 14:37:27 CEST
../../img/srcFileCovDistChart10.png 0% of files have more coverage
30   140   18   3,75
12   65   0,6   8
8     2,25  
1    
3,8% of code in this file is excluded from these metrics.
 
  Capture       Line # 32 30 3,8% 18 0 100% 1.0
 
  (34)
 
1    /**
2    * Copyright 2001-2010 the original author or authors.
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   
17    package org.easymock;
18   
19    import java.io.Serializable;
20    import java.util.ArrayList;
21    import java.util.List;
22   
23    /**
24    * Will contain what was captured by the <code>capture()</code> matcher. Knows
25    * if something was captured or not (allows to capture a null value).
26    *
27    * @param <T>
28    * Type of the captured element
29    *
30    * @author Henri Tremblay
31    */
 
32    public class Capture<T> implements Serializable {
33   
34    private static final long serialVersionUID = -4214363692271370781L;
35   
36    private CaptureType type;
37   
38    private final List<T> values = new ArrayList<T>(2);
39   
40    /**
41    * Default constructor. Only the last element will be captured
42    */
 
43  44 toggle public Capture() {
44  44 this(CaptureType.LAST);
45    }
46   
47    /**
48    * Constructor allowing to select the capture type
49    *
50    * @param type
51    * capture type
52    */
 
53  60 toggle public Capture(final CaptureType type) {
54  60 this.type = type;
55    }
56   
57    /**
58    * Will reset capture to a "nothing captured yet" state
59    */
 
60  14 toggle public void reset() {
61  14 values.clear();
62    }
63   
64    /**
65    * @return true if something was captured
66    */
 
67  92 toggle public boolean hasCaptured() {
68  92 return !values.isEmpty();
69    }
70   
71    /**
72    * Return captured value
73    *
74    * @throws AssertionError
75    * if nothing was captured yet or if more than one value was
76    * captured
77    * @return The last captured value
78    */
 
79  60 toggle public T getValue() {
80  60 if (values.isEmpty()) {
81  4 throw new AssertionError("Nothing captured yet");
82    }
83  56 if (values.size() > 1) {
84  2 throw new AssertionError("More than one value captured: " + getValues());
85    }
86  54 return values.get(values.size() - 1);
87    }
88   
89    /**
90    * Return all captured values. It returns the actual list so you can modify
91    * it's content if needed
92    *
93    * @return The currently captured values
94    */
 
95  10 toggle public List<T> getValues() {
96  10 return values;
97    }
98   
99    /**
100    * Used internally by the EasyMock framework to add a new captured value
101    *
102    * @param value
103    * Value captured
104    */
 
105  112 toggle public void setValue(final T value) {
106  112 switch (type) {
107  12 case NONE:
108  12 break;
109  28 case ALL:
110  28 values.add(value);
111  28 break;
112  12 case FIRST:
113  12 if (!hasCaptured()) {
114  2 values.add(value);
115    }
116  12 break;
117  60 case LAST:
118  60 if (hasCaptured()) {
119  12 reset();
120    }
121  60 values.add(value);
122  60 break;
123    // ///CLOVER:OFF
124    default:
125    throw new IllegalArgumentException("Unknown capture type: " + type);
126    // ///CLOVER:ON
127    }
128    }
129   
 
130  12 toggle @Override
131    public String toString() {
132  12 if (values.isEmpty()) {
133  4 return "Nothing captured yet";
134    }
135  8 if (values.size() == 1) {
136  6 return String.valueOf(values.get(0));
137    }
138  2 return values.toString();
139    }
140    }