001 /*
002 * Copyright 2002-2004 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package org.springframework.richclient.application.config;
017
018 import java.awt.Color;
019 import java.awt.Image;
020 import java.awt.image.BufferedImage;
021 import java.util.Locale;
022
023 import javax.swing.Icon;
024 import javax.swing.ImageIcon;
025
026 import junit.framework.Assert;
027 import junit.framework.TestCase;
028
029 import org.easymock.EasyMock;
030 import org.springframework.context.MessageSource;
031 import org.springframework.richclient.command.config.CommandButtonIconInfo;
032 import org.springframework.richclient.command.config.CommandButtonLabelInfo;
033 import org.springframework.richclient.command.config.CommandIconConfigurable;
034 import org.springframework.richclient.command.config.CommandLabelConfigurable;
035 import org.springframework.richclient.core.DescriptionConfigurable;
036 import org.springframework.richclient.core.LabelConfigurable;
037 import org.springframework.richclient.core.LabelInfo;
038 import org.springframework.richclient.core.SecurityControllable;
039 import org.springframework.richclient.core.TitleConfigurable;
040 import org.springframework.richclient.image.IconSource;
041 import org.springframework.richclient.image.ImageSource;
042 import org.springframework.richclient.image.config.IconConfigurable;
043 import org.springframework.richclient.image.config.ImageConfigurable;
044 import org.springframework.richclient.security.SecurityController;
045 import org.springframework.richclient.security.SecurityControllerManager;
046 import org.springframework.richclient.test.TestIcon;
047
048 /**
049 * Provides a suite of unit tests for the
050 * {@link DefaultApplicationObjectConfigurer} class.
051 *
052 * @author Kevin Stembridge
053 * @since 0.3
054 *
055 */
056 public class DefaultApplicationObjectConfigurerTests extends TestCase {
057
058 /**
059 * Creates a new {@code DefaultApplicationObjectConfigurerTests}.
060 */
061 public DefaultApplicationObjectConfigurerTests() {
062 super();
063 }
064
065 /**
066 * Confirms that an IllegalArgumentException is thrown if the object to be
067 * configured is not null but the object name is null.
068 */
069 public void testForNullInputToConfigureMethod() {
070
071 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer();
072
073 // should succeed
074 configurer.configure(null, "bogus");
075
076 try {
077 configurer.configure("bogus", null);
078 Assert.fail("Should have thrown an IllegalArgumentException for null object name");
079 }
080 catch (IllegalArgumentException e) {
081 // do nothing, test succeeded
082 }
083
084 }
085
086 /**
087 * Confirms that a {@link TitleConfigurable} object will have its title set
088 * correctly, as retrieved from a MessageSource using the key
089 * 'beanName.title'.
090 */
091 public void testConfigureTitleConfigurable() {
092 MessageSource messageSource = (MessageSource) EasyMock.createMock(MessageSource.class);
093
094 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(messageSource);
095
096 String objectName = "bogusTitleable";
097 String messageCode = objectName + ".title";
098 String message = "bogusTitle";
099
100 EasyMock.expect(messageSource.getMessage(messageCode, null, Locale.getDefault())).andReturn(message);
101
102 TitleConfigurable configurable = (TitleConfigurable) EasyMock.createMock(TitleConfigurable.class);
103 configurable.setTitle(message);
104
105 EasyMock.replay(messageSource);
106 EasyMock.replay(configurable);
107
108 configurer.configure(configurable, objectName);
109
110 EasyMock.verify(messageSource);
111 EasyMock.verify(configurable);
112 }
113
114 public void testConfigureTitleConfigurableWithNoTitleFound() {
115 MessageSource messageSource = (MessageSource) EasyMock.createMock(MessageSource.class);
116
117 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(messageSource);
118
119 String objectName = "bogusTitleable";
120 String messageCode = objectName + ".title";
121
122 EasyMock.expect(messageSource.getMessage(messageCode, null, Locale.getDefault())).andReturn(null);
123
124 TitleConfigurable configurable = (TitleConfigurable) EasyMock.createMock(TitleConfigurable.class);
125
126 EasyMock.replay(messageSource);
127 EasyMock.replay(configurable);
128
129 configurer.configure(configurable, objectName);
130
131 EasyMock.verify(messageSource);
132 EasyMock.verify(configurable);
133 }
134
135 /**
136 * Confirms that a {@link DescriptionConfigurable} object will have its
137 * description and caption set correctly, as retrieved from a MessageSource
138 * using the key 'beanName.description' and 'beanName.caption' respectively.
139 */
140 public void testDescriptionConfigurable() {
141 MessageSource messageSource = (MessageSource) EasyMock.createMock(MessageSource.class);
142
143 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(messageSource);
144
145 String objectName = "bogusDescriptionConfigurable";
146 String descriptionCode = objectName + ".description";
147 String captionCode = objectName + ".caption";
148 String description = "bogusDescription";
149 String caption = "bogusCaption";
150
151 EasyMock.expect(messageSource.getMessage(descriptionCode, null, Locale.getDefault())).andReturn(description);
152 EasyMock.expect(messageSource.getMessage(captionCode, null, Locale.getDefault())).andReturn(caption);
153
154 DescriptionConfigurable configurable = (DescriptionConfigurable) EasyMock
155 .createMock(DescriptionConfigurable.class);
156 configurable.setDescription(description);
157 configurable.setCaption(caption);
158
159 EasyMock.replay(messageSource);
160 EasyMock.replay(configurable);
161
162 configurer.configure(configurable, objectName);
163
164 EasyMock.verify(messageSource);
165 EasyMock.verify(configurable);
166 }
167
168 public void testDescriptionConfigurableWithNoDescriptionFound() {
169 MessageSource messageSource = (MessageSource) EasyMock.createMock(MessageSource.class);
170
171 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(messageSource);
172
173 String objectName = "bogusDescriptionConfigurable";
174 String descriptionCode = objectName + ".description";
175 String captionCode = objectName + ".caption";
176
177 EasyMock.expect(messageSource.getMessage(descriptionCode, null, Locale.getDefault())).andReturn(null);
178 EasyMock.expect(messageSource.getMessage(captionCode, null, Locale.getDefault())).andReturn(null);
179
180 DescriptionConfigurable configurable = (DescriptionConfigurable) EasyMock
181 .createMock(DescriptionConfigurable.class);
182
183 EasyMock.replay(messageSource);
184 EasyMock.replay(configurable);
185
186 configurer.configure(configurable, objectName);
187
188 EasyMock.verify(messageSource);
189 EasyMock.verify(configurable);
190 }
191
192 /**
193 * Confirms that a {@link LabelConfigurable} object will have its label set
194 * correctly, as retrieved from a MessageSource using the key
195 * 'beanName.label'.
196 */
197 public void testLabelConfigurable() {
198 MessageSource messageSource = (MessageSource) EasyMock.createMock(MessageSource.class);
199
200 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(messageSource);
201
202 String objectName = "bogusLabelable";
203 String messageCode = objectName + ".label";
204 String message = "bogusLabelInfo";
205 LabelInfo expectedLabelInfo = LabelInfo.valueOf(message);
206
207 EasyMock.expect(messageSource.getMessage(messageCode, null, Locale.getDefault())).andReturn(message);
208
209 LabelConfigurable configurable = (LabelConfigurable) EasyMock.createMock(LabelConfigurable.class);
210 configurable.setLabelInfo(expectedLabelInfo);
211
212 EasyMock.replay(messageSource);
213 EasyMock.replay(configurable);
214
215 configurer.configure(configurable, objectName);
216
217 EasyMock.verify(messageSource);
218 EasyMock.verify(configurable);
219
220 }
221
222 public void testLabelConfigurableWithNoLabelFound() {
223 MessageSource messageSource = (MessageSource) EasyMock.createMock(MessageSource.class);
224
225 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(messageSource);
226
227 String objectName = "bogusLabelable";
228 String messageCode = objectName + ".label";
229
230 EasyMock.expect(messageSource.getMessage(messageCode, null, Locale.getDefault())).andReturn(null);
231
232 LabelConfigurable configurable = (LabelConfigurable) EasyMock.createMock(LabelConfigurable.class);
233
234 EasyMock.replay(messageSource);
235 EasyMock.replay(configurable);
236
237 configurer.configure(configurable, objectName);
238
239 EasyMock.verify(messageSource);
240 EasyMock.verify(configurable);
241 }
242
243 /**
244 * Confirms that a {@link CommandLabelConfigurable} object will have its
245 * label set correctly, as retrieved from a MessageSource using the key
246 * 'beanName.label'.
247 */
248 public void testCommandLabelConfigurable() {
249 MessageSource messageSource = (MessageSource) EasyMock.createMock(MessageSource.class);
250
251 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(messageSource);
252
253 String objectName = "bogusLabelable";
254 String messageCode = objectName + ".label";
255 String message = "bogusLabelInfo";
256 CommandButtonLabelInfo expectedLabelInfo = CommandButtonLabelInfo.valueOf(message);
257
258 EasyMock.expect(messageSource.getMessage(messageCode, null, Locale.getDefault())).andReturn(message);
259
260 CommandLabelConfigurable configurable = (CommandLabelConfigurable) EasyMock
261 .createMock(CommandLabelConfigurable.class);
262 configurable.setLabelInfo(expectedLabelInfo);
263
264 EasyMock.replay(messageSource);
265 EasyMock.replay(configurable);
266
267 configurer.configure(configurable, objectName);
268
269 EasyMock.verify(messageSource);
270 EasyMock.verify(configurable);
271
272 }
273
274 public void testCommandLabelConfigurableWithNoLabelFound() {
275 MessageSource messageSource = (MessageSource) EasyMock.createMock(MessageSource.class);
276
277 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(messageSource);
278
279 String objectName = "bogusLabelable";
280 String messageCode = objectName + ".label";
281
282 EasyMock.expect(messageSource.getMessage(messageCode, null, Locale.getDefault())).andReturn(null);
283
284 CommandLabelConfigurable configurable = (CommandLabelConfigurable) EasyMock
285 .createMock(CommandLabelConfigurable.class);
286
287 EasyMock.replay(messageSource);
288 EasyMock.replay(configurable);
289
290 configurer.configure(configurable, objectName);
291
292 EasyMock.verify(messageSource);
293 EasyMock.verify(configurable);
294
295 }
296
297 /**
298 * Confirms that a {@link IconConfigurable} object will have its icon set
299 * correctly, if the icon source can find it using the key
300 * 'objectName.icon'.
301 */
302 public void testConfigureIconConfigurable() {
303 String objectName = "bogusIconConfigurable";
304 String iconKey = objectName + ".icon";
305 Icon expectedIcon = new TestIcon(Color.GREEN);
306
307 // Create the required mock objects
308 IconSource iconSource = (IconSource) EasyMock.createMock(IconSource.class);
309 IconConfigurable configurable = (IconConfigurable) EasyMock.createMock(IconConfigurable.class);
310
311 // Create the configurer with the mock icon source
312 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(null, null, iconSource,
313 null);
314
315 // Set the expectations for the mock objects. For the first run, we
316 // don't want the icon
317 // source to be able to find the icon, so setIcon should be called with
318 // a null value.
319 EasyMock.expect(iconSource.getIcon(iconKey)).andReturn(expectedIcon);
320 configurable.setIcon(expectedIcon);
321
322 EasyMock.replay(iconSource);
323 EasyMock.replay(configurable);
324
325 configurer.configure(configurable, objectName);
326
327 EasyMock.verify(iconSource);
328 EasyMock.verify(configurable);
329 }
330
331 public void testConfigureIconConfigurableWithNoIconFound() {
332 String objectName = "undefinedIconConfigurable";
333 String iconKey = objectName + ".icon";
334
335 // Create the required mock objects
336 IconSource iconSource = (IconSource) EasyMock.createMock(IconSource.class);
337 IconConfigurable configurable = (IconConfigurable) EasyMock.createMock(IconConfigurable.class);
338
339 // Create the configurer with the mock icon source
340 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(null, null, iconSource,
341 null);
342
343 // Set the expectations for the mock objects. For the first run, we
344 // don't want the icon
345 // source to be able to find the icon, so setIcon should be called with
346 // a null value.
347 EasyMock.expect(iconSource.getIcon(iconKey)).andReturn(null);
348
349 EasyMock.replay(iconSource);
350 EasyMock.replay(configurable);
351
352 configurer.configure(configurable, objectName);
353
354 EasyMock.verify(iconSource);
355 EasyMock.verify(configurable);
356 }
357
358 /**
359 * Confirms that a {@link CommandIconConfigurable} object will have its
360 * icons set correctly.
361 */
362 public void testCommandIconConfigurable() {
363
364 String objectName = "bogusCommandIconConfigurable";
365 String iconKey = objectName + ".icon";
366 String disabledIconKey = objectName + ".disabledIcon";
367 String selectedIconKey = objectName + ".selectedIcon";
368 String rolloverIconKey = objectName + ".rolloverIcon";
369 String pressedIconKey = objectName + ".pressedIcon";
370 String largeIconKey = objectName + ".large.icon";
371 String disabledLargeIconKey = objectName + ".large.disabledIcon";
372 String selectedLargeIconKey = objectName + ".large.selectedIcon";
373 String rolloverLargeIconKey = objectName + ".large.rolloverIcon";
374 String pressedLargeIconKey = objectName + ".large.pressedIcon";
375
376 Icon expectedIcon = new ImageIcon();
377 Icon expectedSelectedIcon = new ImageIcon();
378 Icon expectedRolloverIcon = new ImageIcon();
379 Icon expectedDisabledIcon = new ImageIcon();
380 Icon expectedPressedIcon = new ImageIcon();
381
382 Icon expectedLargeIcon = new ImageIcon();
383 Icon expectedSelectedLargeIcon = new ImageIcon();
384 Icon expectedRolloverLargeIcon = new ImageIcon();
385 Icon expectedDisabledLargeIcon = new ImageIcon();
386 Icon expectedPressedLargeIcon = new ImageIcon();
387
388 CommandButtonIconInfo expectedIconInfo = new CommandButtonIconInfo(expectedIcon, expectedSelectedIcon,
389 expectedRolloverIcon, expectedDisabledIcon, expectedPressedIcon);
390
391 CommandButtonIconInfo expectedLargeIconInfo = new CommandButtonIconInfo(expectedLargeIcon,
392 expectedSelectedLargeIcon, expectedRolloverLargeIcon, expectedDisabledLargeIcon,
393 expectedPressedLargeIcon);
394
395 // Create the required mock objects
396 IconSource iconSource = (IconSource) EasyMock.createMock(IconSource.class);
397 CommandIconConfigurable configurable = (CommandIconConfigurable) EasyMock
398 .createMock(CommandIconConfigurable.class);
399
400 // Create the configurer with the mock icon source
401 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(null, null, iconSource,
402 null);
403
404 EasyMock.expect(iconSource.getIcon(iconKey)).andReturn(expectedIcon);
405 EasyMock.expect(iconSource.getIcon(disabledIconKey)).andReturn(expectedDisabledIcon);
406 EasyMock.expect(iconSource.getIcon(selectedIconKey)).andReturn(expectedSelectedIcon);
407 EasyMock.expect(iconSource.getIcon(rolloverIconKey)).andReturn(expectedRolloverIcon);
408 EasyMock.expect(iconSource.getIcon(pressedIconKey)).andReturn(expectedPressedIcon);
409 EasyMock.expect(iconSource.getIcon(largeIconKey)).andReturn(expectedLargeIcon);
410 EasyMock.expect(iconSource.getIcon(disabledLargeIconKey)).andReturn(expectedDisabledLargeIcon);
411 EasyMock.expect(iconSource.getIcon(selectedLargeIconKey)).andReturn(expectedSelectedLargeIcon);
412 EasyMock.expect(iconSource.getIcon(rolloverLargeIconKey)).andReturn(expectedRolloverLargeIcon);
413 EasyMock.expect(iconSource.getIcon(pressedLargeIconKey)).andReturn(expectedPressedLargeIcon);
414 configurable.setIconInfo(expectedIconInfo);
415 configurable.setLargeIconInfo(expectedLargeIconInfo);
416
417 EasyMock.replay(iconSource);
418 EasyMock.replay(configurable);
419
420 configurer.configure(configurable, objectName);
421
422 EasyMock.verify(iconSource);
423 EasyMock.verify(configurable);
424
425 // Reset the mock objects for the next test
426 EasyMock.reset(iconSource);
427 EasyMock.reset(configurable);
428
429 // Set the expectations. This time the loadOptionalIcons will be set to
430 // false.
431 configurer.setLoadOptionalIcons(false);
432 expectedIconInfo = new CommandButtonIconInfo(expectedIcon);
433 expectedLargeIconInfo = new CommandButtonIconInfo(expectedLargeIcon);
434 EasyMock.expect(iconSource.getIcon(iconKey)).andReturn(expectedIcon);
435 EasyMock.expect(iconSource.getIcon(largeIconKey)).andReturn(expectedLargeIcon);
436 configurable.setIconInfo(expectedIconInfo);
437 configurable.setLargeIconInfo(expectedLargeIconInfo);
438
439 EasyMock.replay(iconSource);
440 EasyMock.replay(configurable);
441
442 configurer.configure(configurable, objectName);
443
444 EasyMock.verify(iconSource);
445 EasyMock.verify(configurable);
446 }
447
448 public void testCommandIconConfigurableWithNoIconFound() {
449
450 String objectName = "bogusCommandIconConfigurable";
451 String iconKey = objectName + ".icon";
452 String largeIconKey = objectName + ".large.icon";
453
454 // Create the required mock objects
455 IconSource iconSource = (IconSource) EasyMock.createMock(IconSource.class);
456 CommandIconConfigurable configurable = (CommandIconConfigurable) EasyMock
457 .createMock(CommandIconConfigurable.class);
458
459 // Create the configurer with the mock icon source
460 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(null, null, iconSource,
461 null);
462
463 EasyMock.expect(iconSource.getIcon(iconKey)).andReturn(null);
464 EasyMock.expect(iconSource.getIcon(largeIconKey)).andReturn(null);
465
466 EasyMock.replay(iconSource);
467 EasyMock.replay(configurable);
468
469 configurer.configure(configurable, objectName);
470
471 EasyMock.verify(iconSource);
472 EasyMock.verify(configurable);
473 }
474
475 /**
476 * Confirms that a {@link ImageConfigurable} object will have its image set
477 * correctly, if the image source can find it using the key
478 * 'objectName.image'.
479 */
480 public void testImageConfigurable() {
481
482 String objectName = "bogusImageConfigurable";
483 String iconKey = objectName + ".image";
484 Image expectedImage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
485
486 // Create the required mock objects
487 ImageSource imageSource = (ImageSource) EasyMock.createMock(ImageSource.class);
488 ImageConfigurable configurable = (ImageConfigurable) EasyMock.createMock(ImageConfigurable.class);
489
490 // Create the configurer with the mock image source
491 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(null, imageSource, null,
492 null);
493
494 EasyMock.expect(imageSource.getImage(iconKey)).andReturn(expectedImage);
495 configurable.setImage(expectedImage);
496
497 EasyMock.replay(imageSource);
498 EasyMock.replay(configurable);
499
500 configurer.configure(configurable, objectName);
501
502 EasyMock.verify(imageSource);
503 EasyMock.verify(configurable);
504
505 }
506
507 public void testImageConfigurableWithNoImageFound() {
508 String objectName = "bogusImageConfigurable";
509 String iconKey = objectName + ".image";
510
511 // Create the required mock objects
512 ImageSource imageSource = (ImageSource) EasyMock.createMock(ImageSource.class);
513 ImageConfigurable configurable = (ImageConfigurable) EasyMock.createMock(ImageConfigurable.class);
514
515 // Create the configurer with the mock image source
516 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(null, imageSource, null,
517 null);
518
519 EasyMock.expect(imageSource.getImage(iconKey)).andReturn(null);
520
521 EasyMock.replay(imageSource);
522 EasyMock.replay(configurable);
523
524 configurer.configure(configurable, objectName);
525
526 EasyMock.verify(imageSource);
527 EasyMock.verify(configurable);
528
529 }
530
531 /**
532 * Confirms that a {@link SecurityControllable} object will have its
533 * security controller set correctly, as retrieved from a MessageSource
534 * using the key 'beanName.label'.
535 */
536 public void testSecurityControllable() {
537
538 String objectName = "bogusSecurityControllable";
539 String securityControllerId = "bogusSecurityControllerId";
540
541 // create the required mock objects
542 SecurityControllerManager controllerManager = (SecurityControllerManager) EasyMock
543 .createMock(SecurityControllerManager.class);
544 SecurityControllable controllable = (SecurityControllable) EasyMock.createMock(SecurityControllable.class);
545 SecurityController controller = (SecurityController) EasyMock.createMock(SecurityController.class);
546
547 // Create the configurer with the mock security controller manager
548 DefaultApplicationObjectConfigurer configurer = new DefaultApplicationObjectConfigurer(null, null, null,
549 controllerManager);
550
551 // Set the expectations for this run. The security controller manager
552 // has not been provided
553 // with the controller for the given controller id yet so we don't
554 // expect the controllable
555 // to be added to the controllerManager as a controlled object
556 EasyMock.expect(controllable.getSecurityControllerId()).andReturn(securityControllerId);
557 EasyMock.expect(controllerManager.getSecurityController(securityControllerId)).andReturn(null);
558
559 // switch to replay mode...
560 EasyMock.replay(controllable);
561 EasyMock.replay(controllerManager);
562 EasyMock.replay(controller);
563
564 // run the test...
565 configurer.configure(controllable, objectName);
566
567 // and verify the results...
568 EasyMock.verify(controllable);
569 EasyMock.verify(controllerManager);
570 EasyMock.verify(controller);
571
572 // reset the mocks for the next part of the test
573 EasyMock.reset(controllable);
574 EasyMock.reset(controllerManager);
575 EasyMock.reset(controller);
576
577 // Set the expectations for the next test. This time we want the
578 // controller manager to find
579 // the controller for the given id and we expect the controllable to be
580 // added to the
581 // controller manager as a controlled object
582 EasyMock.expect(controllable.getSecurityControllerId()).andReturn(securityControllerId);
583 EasyMock.expect(controllerManager.getSecurityController(securityControllerId)).andReturn(controller);
584 controller.addControlledObject(controllable);
585
586 // switch to replay mode...
587 EasyMock.replay(controllable);
588 EasyMock.replay(controllerManager);
589 EasyMock.replay(controller);
590
591 // run the test...
592 configurer.configure(controllable, objectName);
593
594 // and verify the results
595 EasyMock.verify(controllable);
596 EasyMock.verify(controllerManager);
597 EasyMock.verify(controller);
598
599 }
600
601 }