20
20
21
21
import org .apache .struts2 .ActionContext ;
22
22
import org .apache .struts2 .ActionInvocation ;
23
- import org .apache .struts2 .ActionProxy ;
24
- import org .apache .struts2 .util .ValueStack ;
25
23
import org .apache .struts2 .ServletActionContext ;
26
24
import org .apache .struts2 .junit .StrutsTestCase ;
27
- import org .easymock . EasyMock ;
25
+ import org .apache . struts2 . util . ValueStack ;
28
26
import org .jfree .chart .ChartFactory ;
29
27
import org .jfree .chart .JFreeChart ;
30
28
import org .jfree .data .general .DefaultPieDataset ;
34
32
import jakarta .servlet .http .HttpServletResponse ;
35
33
import java .io .IOException ;
36
34
35
+ import static org .junit .Assert .assertThrows ;
36
+ import static org .mockito .Mockito .mock ;
37
+ import static org .mockito .Mockito .verify ;
38
+ import static org .mockito .Mockito .when ;
37
39
38
- /**
39
- *
40
- */
41
40
public class ChartResultTest extends StrutsTestCase {
42
41
42
+ private ChartResult result ;
43
+
43
44
private ActionInvocation actionInvocation ;
44
45
private JFreeChart mockChart ;
45
46
private MockServletOutputStream os ;
46
- private ValueStack stack ;
47
- private ActionProxy mockActionProxy ;
48
47
private HttpServletResponse responseMock ;
49
48
49
+ @ Override
50
+ protected void setUp () throws Exception {
51
+ super .setUp ();
50
52
51
- public void testChart () throws Exception {
52
- EasyMock .expect (responseMock .getOutputStream ()).andReturn (os );
53
- EasyMock .replay (responseMock , mockActionProxy , actionInvocation );
53
+ result = new ChartResult ();
54
+
55
+ var data = new DefaultPieDataset <String >();
56
+ data .setValue ("Java" , Double .valueOf (43.2 ));
57
+ data .setValue ("Visual Basic" , Double .valueOf (0.0 ));
58
+ data .setValue ("C/C++" , Double .valueOf (17.5 ));
59
+ mockChart = ChartFactory .createPieChart ("Pie Chart" , data , true , true , false );
60
+
61
+ actionInvocation = mock (ActionInvocation .class );
62
+ when (actionInvocation .getStack ()).thenReturn (ActionContext .getContext ().getValueStack ());
54
63
55
- ChartResult result = new ChartResult ();
64
+ os = new MockServletOutputStream ();
65
+ responseMock = mock (HttpServletResponse .class );
66
+ when (responseMock .getOutputStream ()).thenReturn (os );
67
+
68
+ ServletActionContext .setResponse (responseMock );
69
+ }
56
70
71
+ public void testChart () throws Exception {
57
72
result .setChart (mockChart );
58
73
59
74
result .setHeight ("10" );
60
75
result .setWidth ("10" );
61
76
result .execute (actionInvocation );
62
77
63
- EasyMock .verify (responseMock );
64
78
assertTrue (os .isWritten ());
65
79
}
66
80
67
81
public void testContentTypePng () throws Exception {
68
- EasyMock .expect (responseMock .getOutputStream ()).andReturn (os );
69
- responseMock .setContentType ("image/png" );
70
- EasyMock .replay (responseMock , mockActionProxy , actionInvocation );
71
- ChartResult result = new ChartResult ();
72
-
73
82
result .setChart (mockChart );
74
83
75
84
result .setHeight ("10" );
76
85
result .setWidth ("10" );
77
86
result .setType ("png" );
78
87
result .execute (actionInvocation );
79
88
80
- EasyMock . verify (responseMock );
89
+ verify (responseMock ). setContentType ( "image/png" );
81
90
assertTrue (os .isWritten ());
82
91
}
83
92
84
93
public void testContentTypeJpg () throws Exception {
85
- EasyMock .expect (responseMock .getOutputStream ()).andReturn (os );
86
- responseMock .setContentType ("image/jpg" );
87
- EasyMock .replay (responseMock , mockActionProxy , actionInvocation );
88
- ChartResult result = new ChartResult ();
89
-
90
94
result .setChart (mockChart );
91
95
92
96
result .setHeight ("10" );
93
97
result .setWidth ("10" );
94
98
result .setType ("jpg" );
95
99
result .execute (actionInvocation );
96
100
97
- EasyMock . verify (responseMock );
101
+ verify (responseMock ). setContentType ( "image/jpg" );
98
102
assertTrue (os .isWritten ());
99
103
}
100
104
101
-
102
105
public void testChartNotSet () {
103
- ChartResult result = new ChartResult ();
104
- EasyMock .replay (responseMock , mockActionProxy , actionInvocation );
105
-
106
- // expect exception if chart not set.
107
106
result .setChart (null );
108
107
109
- try {
110
- result .execute (actionInvocation );
111
- fail ();
112
- } catch (Exception e ) {
113
- }
108
+ assertThrows (NullPointerException .class , () -> result .execute (actionInvocation ));
114
109
115
- EasyMock .verify (responseMock );
116
110
assertFalse (os .isWritten ());
117
111
}
118
112
119
-
120
113
public void testChartWithOGNLProperties () throws Exception {
121
- EasyMock .expect (responseMock .getOutputStream ()).andReturn (os );
122
- EasyMock .replay (responseMock , mockActionProxy , actionInvocation );
123
-
124
-
125
- ChartResult result = new ChartResult ();
126
-
127
114
result .setChart (mockChart );
128
115
129
116
result .setHeight ("${myHeight}" );
@@ -135,50 +122,14 @@ public void testChartWithOGNLProperties() throws Exception {
135
122
136
123
result .execute (actionInvocation );
137
124
138
- EasyMock .verify (responseMock );
139
125
assertEquals (result .getHeight (), stack .findValue ("myHeight" ).toString ());
140
126
assertEquals (result .getWidth (), stack .findValue ("myWidth" ).toString ());
141
- assertEquals ("250" , result .getHeight (). toString () );
142
- assertEquals ("150" , result .getWidth (). toString () );
127
+ assertEquals ("250" , result .getHeight ());
128
+ assertEquals ("150" , result .getWidth ());
143
129
assertTrue (os .isWritten ());
144
130
}
145
131
146
- protected void setUp () throws Exception {
147
- super .setUp ();
148
-
149
- DefaultPieDataset data = new DefaultPieDataset ();
150
- data .setValue ("Java" , new Double (43.2 ));
151
- data .setValue ("Visual Basic" , new Double (0.0 ));
152
- data .setValue ("C/C++" , new Double (17.5 ));
153
- mockChart = ChartFactory .createPieChart ("Pie Chart" , data , true , true , false );
154
-
155
-
156
- stack = ActionContext .getContext ().getValueStack ();
157
-
158
- mockActionProxy = EasyMock .createNiceMock (ActionProxy .class );
159
- EasyMock .expect (mockActionProxy .getNamespace ()).andReturn ("/html" );
160
-
161
- actionInvocation = EasyMock .createMock (ActionInvocation .class );
162
-
163
- EasyMock .expect (actionInvocation .getStack ()).andReturn (stack ).anyTimes ();
164
-
165
-
166
- os = new MockServletOutputStream ();
167
- responseMock = EasyMock .createNiceMock (HttpServletResponse .class );
168
-
169
- ServletActionContext .setResponse ((HttpServletResponse ) responseMock );
170
- }
171
-
172
- protected void tearDown () throws Exception {
173
- actionInvocation = null ;
174
- os = null ;
175
- responseMock = null ;
176
- stack = null ;
177
- mockActionProxy = null ;
178
- }
179
-
180
-
181
- private class MockServletOutputStream extends ServletOutputStream {
132
+ private static class MockServletOutputStream extends ServletOutputStream {
182
133
// very simple check that outputStream was written to.
183
134
private boolean written = false ;
184
135
@@ -189,6 +140,7 @@ public boolean isWritten() {
189
140
return written ;
190
141
}
191
142
143
+ @ Override
192
144
public void write (int arg0 ) throws IOException {
193
145
written = true ;
194
146
}
0 commit comments