1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.grinder.console.model;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verifyNoMoreInteractions;
31
32 import java.awt.Rectangle;
33 import java.beans.PropertyChangeEvent;
34 import java.beans.PropertyChangeListener;
35 import java.io.File;
36 import java.io.FileInputStream;
37 import java.io.FileOutputStream;
38 import java.util.Properties;
39 import java.util.Random;
40 import java.util.regex.Pattern;
41 import java.util.regex.PatternSyntaxException;
42
43 import net.grinder.common.GrinderProperties;
44 import net.grinder.communication.CommunicationDefaults;
45 import net.grinder.console.common.ConsoleException;
46 import net.grinder.console.common.DisplayMessageConsoleException;
47 import net.grinder.console.common.Resources;
48 import net.grinder.console.common.ResourcesImplementation;
49 import net.grinder.testutility.AbstractJUnit4FileTestCase;
50 import static net.grinder.testutility.AssertUtilities.*;
51 import net.grinder.testutility.FileUtilities;
52 import net.grinder.util.Directory;
53
54 import org.junit.Before;
55 import org.junit.Test;
56
57
58
59
60
61
62
63 public class TestConsoleProperties extends AbstractJUnit4FileTestCase {
64
65 private static final Resources s_resources = new ResourcesImplementation(
66 "net.grinder.console.common.resources.Console");
67
68 private File m_file;
69
70 private Random m_random = new Random();
71
72 @Before public void setup() throws Exception {
73 m_file = new File(getDirectory(), "properties");
74 }
75
76 @Test public void testCollectSamples() throws Exception {
77
78 new TestIntTemplate(ConsoleProperties.COLLECT_SAMPLES_PROPERTY, 0,
79 Integer.MAX_VALUE) {
80
81 protected int get(ConsoleProperties properties) {
82 return properties.getCollectSampleCount();
83 }
84
85 protected void set(ConsoleProperties properties, int i)
86 throws ConsoleException {
87 properties.setCollectSampleCount(i);
88 }
89 }.doTest();
90 }
91
92 @Test public void testIgnoreSamples() throws Exception {
93
94 new TestIntTemplate(ConsoleProperties.IGNORE_SAMPLES_PROPERTY, 0,
95 Integer.MAX_VALUE) {
96
97 protected int get(ConsoleProperties properties) {
98 return properties.getIgnoreSampleCount();
99 }
100
101 protected void set(ConsoleProperties properties, int i)
102 throws ConsoleException {
103 properties.setIgnoreSampleCount(i);
104 }
105 }.doTest();
106 }
107
108 @Test public void testSampleInterval() throws Exception {
109
110 new TestIntTemplate(ConsoleProperties.SAMPLE_INTERVAL_PROPERTY, 1,
111 Integer.MAX_VALUE) {
112
113 protected int get(ConsoleProperties properties) {
114 return properties.getSampleInterval();
115 }
116
117 protected void set(ConsoleProperties properties, int i)
118 throws ConsoleException {
119 properties.setSampleInterval(i);
120 }
121 }.doTest();
122 }
123
124 @Test public void testSignificantFigures() throws Exception {
125
126 new TestIntTemplate(ConsoleProperties.SIG_FIG_PROPERTY, 0,
127 Integer.MAX_VALUE) {
128
129 protected int get(ConsoleProperties properties) {
130 return properties.getSignificantFigures();
131 }
132
133 protected void set(ConsoleProperties properties, int i)
134 throws ConsoleException {
135 properties.setSignificantFigures(i);
136 }
137 }.doTest();
138 }
139
140 @Test public void testConsoleHost() throws Exception {
141
142 final String propertyName = ConsoleProperties.CONSOLE_HOST_PROPERTY;
143
144 final String s1 = "123.1.2.3";
145
146 writePropertyToFile(propertyName, s1);
147
148 final ConsoleProperties properties = new ConsoleProperties(s_resources,
149 m_file);
150
151 assertEquals(s1, properties.getConsoleHost());
152
153 final String s2 = "123.99.33.11";
154
155 properties.setConsoleHost(s2);
156 assertEquals(s2, properties.getConsoleHost());
157
158 properties.save();
159
160 final ConsoleProperties properties2 = new ConsoleProperties(s_resources,
161 m_file);
162
163 assertEquals(s2, properties2.getConsoleHost());
164
165 final String s3 = "1.46.68.80";
166
167 final PropertyChangeEvent expected = new PropertyChangeEvent(properties2,
168 propertyName, s2, s3);
169
170 final ChangeListener listener = new ChangeListener(expected);
171 final ChangeListener listener2 = new ChangeListener(expected);
172
173 properties2.addPropertyChangeListener(listener);
174 properties2.addPropertyChangeListener(propertyName, listener2);
175
176 properties2.setConsoleHost(s3);
177
178 listener.assertCalledOnce();
179 listener2.assertCalledOnce();
180
181 try {
182 properties.setConsoleHost("234.12.23.2");
183 fail("Expected a DisplayMessageConsoleException for multicast address");
184 }
185 catch (DisplayMessageConsoleException e) {
186 }
187
188 try {
189 properties.setConsoleHost("not a host");
190 fail("Expected a DisplayMessageConsoleException for unknown host");
191 }
192 catch (DisplayMessageConsoleException e) {
193 }
194
195 properties.setConsoleHost("");
196 assertEquals("", properties.getConsoleHost());
197 }
198
199 @Test public void testConsolePort() throws Exception {
200
201 new TestIntTemplate(ConsoleProperties.CONSOLE_PORT_PROPERTY,
202 CommunicationDefaults.MIN_PORT, CommunicationDefaults.MAX_PORT) {
203
204 protected int get(ConsoleProperties properties) {
205 return properties.getConsolePort();
206 }
207
208 protected void set(ConsoleProperties properties, int i)
209 throws ConsoleException {
210 properties.setConsolePort(i);
211 }
212 }.doTest();
213 }
214
215 @Test public void testHttpHost() throws Exception {
216
217 final String propertyName = ConsoleProperties.HTTP_HOST_PROPERTY;
218
219 final String s1 = "123.1.2.3";
220
221 writePropertyToFile(propertyName, s1);
222
223 final ConsoleProperties properties = new ConsoleProperties(s_resources,
224 m_file);
225
226 assertEquals(s1, properties.getHttpHost());
227
228 final String s2 = "123.99.33.11";
229
230 properties.setHttpHost(s2);
231 assertEquals(s2, properties.getHttpHost());
232
233 properties.save();
234
235 final ConsoleProperties properties2 = new ConsoleProperties(s_resources,
236 m_file);
237
238 assertEquals(s2, properties2.getHttpHost());
239
240 final String s3 = "1.46.68.80";
241
242 final PropertyChangeEvent expected = new PropertyChangeEvent(properties2,
243 propertyName, s2, s3);
244
245 final ChangeListener listener = new ChangeListener(expected);
246 final ChangeListener listener2 = new ChangeListener(expected);
247
248 properties2.addPropertyChangeListener(listener);
249 properties2.addPropertyChangeListener(propertyName, listener2);
250
251 properties2.setHttpHost(s3);
252
253 listener.assertCalledOnce();
254 listener2.assertCalledOnce();
255
256 try {
257 properties.setHttpHost("234.12.23.2");
258 fail("Expected a DisplayMessageConsoleException for multicast address");
259 }
260 catch (DisplayMessageConsoleException e) {
261 }
262
263 try {
264 properties.setHttpHost("not a host");
265 fail("Expected a DisplayMessageConsoleException for unknown host");
266 }
267 catch (DisplayMessageConsoleException e) {
268 }
269
270 properties.setHttpHost("");
271 assertEquals("", properties.getHttpHost());
272 }
273
274 @Test public void testHttpPort() throws Exception {
275
276 new TestIntTemplate(ConsoleProperties.HTTP_PORT_PROPERTY,
277 CommunicationDefaults.MIN_PORT, CommunicationDefaults.MAX_PORT) {
278
279 protected int get(ConsoleProperties properties) {
280 return properties.getHttpPort();
281 }
282
283 protected void set(ConsoleProperties properties, int i)
284 throws ConsoleException {
285 properties.setHttpPort(i);
286 }
287 }.doTest();
288 }
289
290 @Test public void testResetConsoleWithProcesses() throws Exception {
291 new TestBooleanTemplate(
292 ConsoleProperties.RESET_CONSOLE_WITH_PROCESSES_PROPERTY) {
293
294 protected boolean get(ConsoleProperties properties) {
295 return properties.getResetConsoleWithProcesses();
296 }
297
298 protected void set(ConsoleProperties properties, boolean b) {
299 properties.setResetConsoleWithProcesses(b);
300 }
301
302 }.doTest();
303 }
304
305 @Test public void testResetConsoleWithProcessesAsk() throws Exception {
306
307 new TestBooleanTemplate(
308 ConsoleProperties.RESET_CONSOLE_WITH_PROCESSES_ASK_PROPERTY) {
309
310 protected boolean get(ConsoleProperties properties) {
311 return properties.getResetConsoleWithProcessesAsk();
312 }
313
314 protected void set(ConsoleProperties properties, boolean b)
315 throws Exception {
316 properties.setResetConsoleWithProcessesAsk(b);
317 }
318 }.doTest();
319 }
320
321 @Test public void testSetScriptNotSetAsk() throws Exception {
322
323 new TestBooleanTemplate(ConsoleProperties.PROPERTIES_NOT_SET_ASK_PROPERTY) {
324
325 protected boolean get(ConsoleProperties properties) {
326 return properties.getPropertiesNotSetAsk();
327 }
328
329 protected void set(ConsoleProperties properties, boolean b)
330 throws Exception {
331 properties.setPropertiesNotSetAsk(b);
332 }
333 }.doTest();
334 }
335
336 @Test public void testStartWithUnsavedBuffersAsk() throws Exception {
337
338 new TestBooleanTemplate(
339 ConsoleProperties.START_WITH_UNSAVED_BUFFERS_ASK_PROPERTY) {
340
341 protected boolean get(ConsoleProperties properties) {
342 return properties.getStartWithUnsavedBuffersAsk();
343 }
344
345 protected void set(ConsoleProperties properties, boolean b)
346 throws Exception {
347 properties.setStartWithUnsavedBuffersAsk(b);
348 }
349 }.doTest();
350 }
351
352 @Test public void testStopProcessesAsk() throws Exception {
353
354 new TestBooleanTemplate(ConsoleProperties.STOP_PROCESSES_ASK_PROPERTY) {
355
356 protected boolean get(ConsoleProperties properties) {
357 return properties.getStopProcessesAsk();
358 }
359
360 protected void set(ConsoleProperties properties, boolean b)
361 throws Exception {
362 properties.setStopProcessesAsk(b);
363 }
364 }.doTest();
365 }
366
367 @Test public void testDistributeOnStartAsk() throws Exception {
368
369 new TestBooleanTemplate(
370 ConsoleProperties.DISTRIBUTE_ON_START_ASK_PROPERTY) {
371
372 protected boolean get(ConsoleProperties properties) {
373 return properties.getDistributeOnStartAsk();
374 }
375
376 protected void set(ConsoleProperties properties, boolean b)
377 throws Exception {
378 properties.setDistributeOnStartAsk(b);
379 }
380 }.doTest();
381 }
382
383 @Test public void testPropertiesFile() throws Exception {
384
385 new TestFileTemplate(ConsoleProperties.PROPERTIES_FILE_PROPERTY) {
386
387 protected File get(ConsoleProperties properties) {
388 return properties.getPropertiesFile();
389 }
390
391 protected void set(ConsoleProperties properties, File file)
392 throws ConsoleException {
393 properties.setAndSavePropertiesFile(file);
394 }
395 }.doTest();
396
397 final File file = new File(getDirectory(), "testing");
398
399 final ConsoleProperties properties =
400 new ConsoleProperties(s_resources, file);
401
402 assertNull(properties.getPropertiesFile());
403
404 final File propertiesFile = new File(getDirectory(), "foo");
405 properties.setAndSavePropertiesFile(propertiesFile);
406 assertEquals(propertiesFile, properties.getPropertiesFile());
407
408 final ConsoleProperties properties2 =
409 new ConsoleProperties(s_resources, file);
410 assertEquals(propertiesFile, properties2.getPropertiesFile());
411
412 properties.setAndSavePropertiesFile(null);
413 assertNull(properties.getPropertiesFile());
414
415 final ConsoleProperties properties3 =
416 new ConsoleProperties(s_resources, file);
417 assertNull(properties3.getPropertiesFile());
418 }
419
420 @Test public void testDistributionDirectory() throws Exception {
421
422 new TestDirectoryTemplate(
423 ConsoleProperties.DISTRIBUTION_DIRECTORY_PROPERTY) {
424
425 protected Directory getDirectory(ConsoleProperties properties) {
426 return properties.getDistributionDirectory();
427 }
428
429 protected void setDirectory(ConsoleProperties properties,
430 Directory directory) throws Exception {
431 properties.setAndSaveDistributionDirectory(directory);
432 }
433 }.doTest();
434
435
436 final File file = new File(getDirectory(), "testing");
437
438 final ConsoleProperties properties =
439 new ConsoleProperties(s_resources, file);
440
441 assertNotNull(properties.getDistributionDirectory());
442 final Directory defaultDirectory = properties.getDistributionDirectory();
443
444 final Directory directory = new Directory(new File("getDirectory()", "d"));
445 properties.setAndSaveDistributionDirectory(directory);
446
447 final Properties rawProperties = new Properties();
448 final FileInputStream in = new FileInputStream(file);
449 rawProperties.load(in);
450 in.close();
451 assertEquals(1, rawProperties.size());
452 assertEquals(rawProperties
453 .getProperty(ConsoleProperties.DISTRIBUTION_DIRECTORY_PROPERTY),
454 directory.getFile().getPath());
455
456
457 final File file2 = new File(getDirectory(), "testing2");
458 properties.setAndSaveDistributionDirectory(new Directory(file2));
459 assertTrue(file2.createNewFile());
460
461 final ConsoleProperties p2 =
462 new ConsoleProperties(s_resources, file);
463 assertEquals(defaultDirectory, p2.getDistributionDirectory());
464
465 FileUtilities.setCanAccess(file, false);
466 try {
467 properties.setAndSaveDistributionDirectory(directory);
468 fail("Expected DisplayMessageConsoleException");
469 }
470 catch (DisplayMessageConsoleException e) {
471 }
472 }
473
474 @Test public void testDistributionFileFilterPattern() throws Exception {
475
476 new TestPatternTemplate(
477 ConsoleProperties.DISTRIBUTION_FILE_FILTER_EXPRESSION_PROPERTY) {
478
479 @Override protected Pattern get(ConsoleProperties properties) {
480 return properties.getDistributionFileFilterPattern();
481 }
482
483 @Override protected String getExpression(ConsoleProperties properties) {
484 return properties.getDistributionFileFilterExpression();
485 }
486
487 @Override protected void set(ConsoleProperties properties, String expression)
488 throws ConsoleException {
489 properties.setDistributionFileFilterExpression(expression);
490 }
491 }.doTest();
492 }
493
494 @Test public void testScanDistributionFilesPeriod() throws Exception {
495
496 new TestIntTemplate(
497 ConsoleProperties.SCAN_DISTRIBUTION_FILES_PERIOD_PROPERTY, 0,
498 Integer.MAX_VALUE) {
499
500 protected int get(ConsoleProperties properties) {
501 return properties.getScanDistributionFilesPeriod();
502 }
503
504 protected void set(ConsoleProperties properties, int i)
505 throws ConsoleException {
506 properties.setScanDistributionFilesPeriod(i);
507 }
508 }.doTest();
509 }
510
511 @Test public void testLookAndFeel() throws Exception {
512
513 new TestStringTemplate(ConsoleProperties.LOOK_AND_FEEL_PROPERTY, true) {
514
515 protected String get(ConsoleProperties properties) {
516 return properties.getLookAndFeel();
517 }
518
519 protected void set(ConsoleProperties properties, String name) {
520 properties.setLookAndFeel(name);
521 }
522 }.doTest();
523 }
524
525
526 @Test public void testNullLookAndFeel() throws Exception {
527
528 final ConsoleProperties properties =
529 new ConsoleProperties(s_resources, m_file);
530
531 assertNull(properties.getLookAndFeel());
532
533 final PropertyChangeListener listener = mock(PropertyChangeListener.class);
534 properties.addPropertyChangeListener(listener);
535
536 properties.setLookAndFeel(null);
537 assertNull(properties.getLookAndFeel());
538 verifyNoMoreInteractions(listener);
539 }
540
541 @Test public void testExternalEditorCommand() throws Exception {
542
543 new TestFileTemplate(
544 ConsoleProperties.EXTERNAL_EDITOR_COMMAND_PROPERTY) {
545
546 protected File get(ConsoleProperties properties) {
547 return properties.getExternalEditorCommand();
548 }
549
550 protected void set(ConsoleProperties properties, File file) {
551 properties.setExternalEditorCommand(file);
552 }
553 }.doTest();
554 }
555
556 @Test public void testExternalEditorArguments() throws Exception {
557
558 new TestStringTemplate(
559 ConsoleProperties.EXTERNAL_EDITOR_ARGUMENTS_PROPERTY, true) {
560
561 protected String get(ConsoleProperties properties) {
562 return properties.getExternalEditorArguments();
563 }
564
565 protected void set(ConsoleProperties properties, String name) {
566 properties.setExternalEditorArguments(name);
567 }
568 }.doTest();
569 }
570
571 @Test public void testFrameBounds() throws Exception {
572
573 final ConsoleProperties properties =
574 new ConsoleProperties(s_resources, m_file);
575
576 assertNull(properties.getFrameBounds());
577
578 final Rectangle rectangle = new Rectangle(12, 42, 311, 1322);
579
580 properties.setAndSaveFrameBounds(rectangle);
581 assertEquals(rectangle, properties.getFrameBounds());
582
583 final ConsoleProperties properties2 =
584 new ConsoleProperties(s_resources, m_file);
585
586 properties.setAndSaveFrameBounds(null);
587 assertNull(properties.getFrameBounds());
588
589 assertEquals(rectangle, properties2.getFrameBounds());
590
591 FileUtilities.setCanAccess(m_file, false);
592
593 try {
594 properties.setAndSaveFrameBounds(rectangle);
595 fail("Expected DisplayMessageConsoleException");
596 }
597 catch (DisplayMessageConsoleException e) {
598 }
599
600 FileUtilities.setCanAccess(m_file, true);
601
602 final GrinderProperties writeProperties =
603 new GrinderProperties(m_file);
604 writeProperties.setProperty(
605 ConsoleProperties.FRAME_BOUNDS_PROPERTY, "1,2,3");
606 writeProperties.save();
607
608 assertNull(new ConsoleProperties(s_resources, m_file).getFrameBounds());
609
610 writeProperties.setProperty(
611 ConsoleProperties.FRAME_BOUNDS_PROPERTY, "A,2,3");
612 writeProperties.save();
613
614 assertNull(new ConsoleProperties(s_resources, m_file).getFrameBounds());
615
616 writeProperties.setProperty(
617 ConsoleProperties.FRAME_BOUNDS_PROPERTY, "1,2,3,4");
618 writeProperties.save();
619
620 assertEquals(new Rectangle(1, 2, 3, 4),
621 new ConsoleProperties(s_resources, m_file).getFrameBounds());
622 }
623
624 @Test public void testSaveTotalsWithResults() throws Exception {
625 new TestBooleanTemplate(
626 ConsoleProperties.SAVE_TOTALS_WITH_RESULTS_PROPERTY) {
627
628 protected boolean get(ConsoleProperties properties) {
629 return properties.getSaveTotalsWithResults();
630 }
631
632 protected void set(ConsoleProperties properties, boolean b)
633 throws ConsoleException {
634
635 properties.setSaveTotalsWithResults(b);
636 }
637
638 }.doTest();
639 }
640
641 @Test public void testCopyConstructor() throws Exception {
642 final ConsoleProperties p1 = new ConsoleProperties(s_resources, m_file);
643 final ConsoleProperties p2 = new ConsoleProperties(p1);
644
645 assertEquals(p1.getCollectSampleCount(), p2.getCollectSampleCount());
646 assertEquals(p1.getIgnoreSampleCount(), p2.getIgnoreSampleCount());
647 assertEquals(p1.getSampleInterval(), p2.getSampleInterval());
648 assertEquals(p1.getSignificantFigures(), p2.getSignificantFigures());
649 assertEquals(p1.getConsoleHost(), p2.getConsoleHost());
650 assertEquals(p1.getConsolePort(), p2.getConsolePort());
651 assertEquals(p1.getResetConsoleWithProcesses(),
652 p2.getResetConsoleWithProcesses());
653 assertEquals(p1.getResetConsoleWithProcessesAsk(),
654 p2.getResetConsoleWithProcessesAsk());
655 assertEquals(p1.getPropertiesNotSetAsk(), p2.getPropertiesNotSetAsk());
656 assertEquals(p1.getStartWithUnsavedBuffersAsk(),
657 p2.getStartWithUnsavedBuffersAsk());
658 assertEquals(p1.getStopProcessesAsk(), p2.getStopProcessesAsk());
659 assertEquals(p1.getDistributeOnStartAsk(), p2.getDistributeOnStartAsk());
660 assertEquals(p1.getPropertiesFile(), p2.getPropertiesFile());
661 assertEquals(p1.getDistributionDirectory(), p2.getDistributionDirectory());
662 assertEquals(p1.getDistributionFileFilterPattern().pattern(),
663 p2.getDistributionFileFilterPattern().pattern());
664 assertEquals(p1.getScanDistributionFilesPeriod(),
665 p2.getScanDistributionFilesPeriod());
666 assertEquals(p1.getLookAndFeel(), p2.getLookAndFeel());
667 assertEquals(p1.getSaveTotalsWithResults(), p2.getSaveTotalsWithResults());
668
669 p1.setCollectSampleCount(99);
670 assertNotEquals(p1.getCollectSampleCount(), p2.getCollectSampleCount());
671 }
672
673 @Test public void testAssignment() throws Exception {
674 final ConsoleProperties p1 = new ConsoleProperties(s_resources, m_file);
675 final ConsoleProperties p2 = new ConsoleProperties(s_resources, m_file);
676 p2.setCollectSampleCount(99);
677 p2.setIgnoreSampleCount(99);
678 p2.setSampleInterval(99);
679 p2.setSignificantFigures(99);
680 p2.setConsoleHost("99.99.99.99");
681 p2.setConsolePort(99);
682 p2.setResetConsoleWithProcesses(true);
683 p2.setResetConsoleWithProcessesAsk(false);
684 p2.setPropertiesNotSetAsk(false);
685 p2.setStartWithUnsavedBuffersAsk(false);
686 p2.setStopProcessesAsk(false);
687 p2.setDistributeOnStartAsk(false);
688 p2.setAndSavePropertiesFile(new File("foo"));
689 p2.setAndSaveDistributionDirectory(new Directory(new File("bah")));
690 p2.setDistributionFileFilterExpression(".*");
691 p2.setScanDistributionFilesPeriod(100);
692 p2.setLookAndFeel("something");
693 p2.setExternalEditorCommand(new File("bah"));
694 p2.setExternalEditorArguments("foo");
695 p2.setSaveTotalsWithResults(true);
696
697 assertTrue(p1.getCollectSampleCount() != p2.getCollectSampleCount());
698 assertTrue(p1.getIgnoreSampleCount() != p2.getIgnoreSampleCount());
699 assertTrue(p1.getSampleInterval() != p2.getSampleInterval());
700 assertTrue(p1.getSignificantFigures() != p2.getSignificantFigures());
701 assertTrue(!p1.getConsoleHost().equals(p2.getConsoleHost()));
702 assertTrue(p1.getConsolePort() != p2.getConsolePort());
703 assertTrue(p1.getResetConsoleWithProcesses() !=
704 p2.getResetConsoleWithProcesses());
705 assertTrue(p1.getResetConsoleWithProcessesAsk() !=
706 p2.getResetConsoleWithProcessesAsk());
707 assertTrue(p1.getPropertiesNotSetAsk() != p2.getPropertiesNotSetAsk());
708 assertTrue(p1.getStartWithUnsavedBuffersAsk() !=
709 p2.getStartWithUnsavedBuffersAsk());
710 assertTrue(p1.getStopProcessesAsk() != p2.getStopProcessesAsk());
711 assertTrue(p1.getDistributeOnStartAsk() != p2.getDistributeOnStartAsk());
712 assertNotEquals(p1.getPropertiesFile(), p2.getPropertiesFile());
713 assertNotEquals(p1.getDistributionDirectory(),
714 p2.getDistributionDirectory());
715 assertNotEquals(p1.getDistributionFileFilterPattern(),
716 p2.getDistributionFileFilterPattern());
717 assertTrue(p1.getScanDistributionFilesPeriod() !=
718 p2.getScanDistributionFilesPeriod());
719 assertNotEquals(p1.getLookAndFeel(), p2.getLookAndFeel());
720 assertNotEquals(p1.getExternalEditorCommand(),
721 p2.getExternalEditorCommand());
722 assertNotEquals(p1.getExternalEditorArguments(),
723 p2.getExternalEditorArguments());
724 assertTrue(p1.getSaveTotalsWithResults() != p2.getSaveTotalsWithResults());
725
726 p2.set(p1);
727
728 assertEquals(p1.getCollectSampleCount(), p2.getCollectSampleCount());
729 assertEquals(p1.getIgnoreSampleCount(), p2.getIgnoreSampleCount());
730 assertEquals(p1.getSampleInterval(), p2.getSampleInterval());
731 assertEquals(p1.getSignificantFigures(), p2.getSignificantFigures());
732 assertEquals(p1.getConsoleHost(), p2.getConsoleHost());
733 assertEquals(p1.getConsolePort(), p2.getConsolePort());
734 assertTrue(p1.getResetConsoleWithProcesses() ==
735 p2.getResetConsoleWithProcesses());
736 assertTrue(p1.getResetConsoleWithProcessesAsk() ==
737 p2.getResetConsoleWithProcessesAsk());
738 assertTrue(p1.getPropertiesNotSetAsk() == p2.getPropertiesNotSetAsk());
739 assertTrue(p1.getStartWithUnsavedBuffersAsk() ==
740 p2.getStartWithUnsavedBuffersAsk());
741 assertTrue(p1.getStopProcessesAsk() == p2.getStopProcessesAsk());
742 assertTrue(p1.getDistributeOnStartAsk() == p2.getDistributeOnStartAsk());
743 assertEquals(p1.getPropertiesFile(), p2.getPropertiesFile());
744 assertEquals(p1.getDistributionDirectory(), p2.getDistributionDirectory());
745 assertEquals(p1.getDistributionFileFilterPattern().pattern(),
746 p2.getDistributionFileFilterPattern().pattern());
747 assertEquals(p1.getScanDistributionFilesPeriod(),
748 p2.getScanDistributionFilesPeriod());
749 assertEquals(p1.getLookAndFeel(), p2.getLookAndFeel());
750 assertEquals(p1.getExternalEditorCommand(), p2.getExternalEditorCommand());
751 assertEquals(p1.getExternalEditorArguments(),
752 p2.getExternalEditorArguments());
753 assertTrue(p1.getSaveTotalsWithResults() == p2.getSaveTotalsWithResults());
754 }
755
756 @Test public void testWithBadFile() throws Exception {
757
758 final File badFile = new File(getDirectory(), "bad");
759 assertTrue(badFile.createNewFile());
760 FileUtilities.setCanAccess(badFile, false);
761
762 try {
763 new ConsoleProperties(s_resources, badFile);
764 fail("Expected DisplayMessageConsoleException");
765 }
766 catch (DisplayMessageConsoleException e) {
767 }
768
769 FileUtilities.setCanAccess(badFile, true);
770 final ConsoleProperties p = new ConsoleProperties(s_resources, badFile);
771 FileUtilities.setCanAccess(badFile, false);
772
773 try {
774 p.save();
775 fail("Expected DisplayMessageConsoleException");
776 }
777 catch (DisplayMessageConsoleException e) {
778 }
779
780 try {
781 p.setResetConsoleWithProcessesAsk(false);
782 fail("Expected DisplayMessageConsoleException");
783 }
784 catch (DisplayMessageConsoleException e) {
785 }
786 }
787
788 private abstract class TestIntTemplate {
789 private final String m_propertyName;
790
791 private final int m_minimum;
792
793 private final int m_maximum;
794
795 public TestIntTemplate(String propertyName, int minimum, int maximum) {
796 if (maximum <= minimum) {
797 throw new IllegalArgumentException("Minimum not less than maximum");
798 }
799
800 m_propertyName = propertyName;
801 m_minimum = minimum;
802 m_maximum = maximum;
803 }
804
805 private int getRandomInt() {
806 return getRandomInt(m_minimum, m_maximum);
807 }
808
809 private int getRandomInt(int minimum, int maximum) {
810
811
812
813 long range = (long) maximum + 1 - minimum;
814
815 return (int) (minimum + Math.abs(m_random.nextLong()) % range);
816 }
817
818 public void doTest() throws Exception {
819 final int i1 = getRandomInt();
820
821 writePropertyToFile(m_propertyName, Integer.toString(i1));
822
823 final ConsoleProperties properties =
824 new ConsoleProperties(s_resources, m_file);
825
826 assertEquals(i1, get(properties));
827
828 final int i2 = getRandomInt();
829
830 set(properties, i2);
831 assertEquals(i2, get(properties));
832
833 properties.save();
834
835 final ConsoleProperties properties2 =
836 new ConsoleProperties(s_resources, m_file);
837
838 assertEquals(i2, get(properties2));
839
840 int i3;
841
842 do {
843 i3 = getRandomInt();
844 }
845 while (i3 == i2);
846
847 final PropertyChangeEvent expected = new PropertyChangeEvent(properties2,
848 m_propertyName, new Integer(i2), new Integer(i3));
849
850 final ChangeListener listener = new ChangeListener(expected);
851 final ChangeListener listener2 = new ChangeListener(expected);
852
853 properties2.addPropertyChangeListener(listener);
854 properties2.addPropertyChangeListener(m_propertyName, listener2);
855
856 set(properties2, i3);
857
858 if (m_minimum > Integer.MIN_VALUE) {
859 try {
860 set(properties, m_minimum - 1);
861 fail("Should not reach");
862 }
863 catch (DisplayMessageConsoleException e) {
864 }
865
866 try {
867 set(properties, Integer.MIN_VALUE);
868 fail("Should not reach");
869 }
870 catch (DisplayMessageConsoleException e) {
871 }
872
873 try {
874 set(properties, getRandomInt(Integer.MIN_VALUE, m_minimum - 1));
875 fail("Should not reach");
876 }
877 catch (DisplayMessageConsoleException e) {
878 }
879 }
880
881 if (m_maximum < Integer.MAX_VALUE) {
882 try {
883 set(properties, m_maximum + 1);
884 fail("Should not reach");
885 }
886 catch (DisplayMessageConsoleException e) {
887 }
888
889 try {
890 set(properties, Integer.MAX_VALUE);
891 fail("Should not reach");
892 }
893 catch (DisplayMessageConsoleException e) {
894 }
895
896 try {
897 set(properties, getRandomInt(m_maximum + 1, Integer.MAX_VALUE));
898 fail("Should not reach");
899 }
900 catch (DisplayMessageConsoleException e) {
901 }
902 }
903
904 listener.assertCalled();
905 listener2.assertCalled();
906 }
907
908 protected abstract int get(ConsoleProperties properties);
909
910 protected abstract void set(ConsoleProperties properties, int i)
911 throws ConsoleException;
912 }
913
914 private abstract class TestBooleanTemplate {
915 private final String m_propertyName;
916
917 public TestBooleanTemplate(String propertyName) {
918 m_propertyName = propertyName;
919 }
920
921 public void doTest() throws Exception {
922
923 writePropertyToFile(m_propertyName, "false");
924
925 final ConsoleProperties properties =
926 new ConsoleProperties(s_resources, m_file);
927
928 assertTrue(!get(properties));
929
930 set(properties, true);
931 assertTrue(get(properties));
932
933 properties.save();
934
935 final ConsoleProperties properties2 =
936 new ConsoleProperties(s_resources, m_file);
937
938 assertTrue(get(properties2));
939
940 final PropertyChangeEvent expected = new PropertyChangeEvent(properties2,
941 m_propertyName, Boolean.TRUE, Boolean.FALSE);
942
943 final ChangeListener listener = new ChangeListener(expected);
944 final ChangeListener listener2 = new ChangeListener(expected);
945
946 properties2.addPropertyChangeListener(listener);
947 properties2.addPropertyChangeListener(m_propertyName, listener2);
948
949 set(properties2, false);
950
951 listener.assertCalledOnce();
952 listener2.assertCalledOnce();
953 }
954
955 protected abstract boolean get(ConsoleProperties properties)
956 throws Exception;
957
958 protected abstract void set(ConsoleProperties properties, boolean b)
959 throws Exception;
960 }
961
962 private String getRandomString() {
963 final int length = m_random.nextInt(200);
964 final char[] characters = new char[length];
965
966 for (int i = 0; i < characters.length; ++i) {
967 characters[i] = (char) (0x20 + m_random.nextInt(0x60));
968 }
969
970 return new String(characters);
971 }
972
973 private abstract class TestStringTemplate {
974 private final String m_propertyName;
975
976 private final boolean m_testNulls;
977
978 public TestStringTemplate(String propertyName, boolean testNulls) {
979 m_propertyName = propertyName;
980 m_testNulls = testNulls;
981 }
982
983 public void doTest() throws Exception {
984 if (m_testNulls) {
985 final ConsoleProperties properties =
986 new ConsoleProperties(s_resources, m_file);
987
988 assertNull(get(properties));
989
990 final String s = getRandomString();
991 set(properties, s);
992 assertNotNull(get(properties));
993
994 set(properties, null);
995 assertNull(get(properties));
996
997 properties.save();
998
999 final ConsoleProperties properties2 =
1000 new ConsoleProperties(s_resources, m_file);
1001
1002 assertNull(get(properties2));
1003 }
1004
1005 final String s1 = getRandomString();
1006
1007 writePropertyToFile(m_propertyName, s1);
1008
1009 final ConsoleProperties properties =
1010 new ConsoleProperties(s_resources, m_file);
1011
1012 assertEquals(s1, get(properties));
1013
1014 final String s2 = getRandomString();
1015
1016 set(properties, s2);
1017 assertEquals(s2, get(properties));
1018
1019 properties.save();
1020
1021 final ConsoleProperties properties2 =
1022 new ConsoleProperties(s_resources, m_file);
1023
1024 assertEquals(s2, get(properties2));
1025
1026 String s3 = getRandomString();
1027
1028 do {
1029 s3 = getRandomString();
1030 }
1031 while (s3.equals(s2));
1032
1033 final PropertyChangeEvent expected =
1034 new PropertyChangeEvent(properties2, m_propertyName, s2, s3);
1035
1036 final ChangeListener listener = new ChangeListener(expected);
1037 final ChangeListener listener2 = new ChangeListener(expected);
1038
1039 properties2.addPropertyChangeListener(listener);
1040 properties2.addPropertyChangeListener(m_propertyName, listener2);
1041
1042 set(properties2, s3);
1043
1044 listener.assertCalledOnce();
1045 listener2.assertCalledOnce();
1046 }
1047
1048 protected abstract String get(ConsoleProperties properties);
1049
1050 protected abstract void set(ConsoleProperties properties, String i)
1051 throws ConsoleException;
1052 }
1053
1054 private abstract class TestFileTemplate {
1055
1056 private String m_propertyName;
1057
1058 public TestFileTemplate(String propertyName) {
1059 m_propertyName = propertyName;
1060 }
1061
1062 private File getRandomFile() {
1063 return new File(getRandomString());
1064 }
1065
1066 public void doTest() throws Exception {
1067
1068 final File f1 = getRandomFile();
1069
1070 writePropertyToFile(m_propertyName, f1.getPath());
1071
1072 final ConsoleProperties properties =
1073 new ConsoleProperties(s_resources, m_file);
1074
1075 assertEquals(f1, get(properties));
1076
1077 final File f2 = getRandomFile();
1078
1079 set(properties, f2);
1080 assertEquals(f2, get(properties));
1081
1082 properties.save();
1083
1084 final ConsoleProperties properties2 =
1085 new ConsoleProperties(s_resources, m_file);
1086
1087 assertEquals(f2, get(properties2));
1088
1089 File f3 = getRandomFile();
1090
1091 do {
1092 f3 = getRandomFile();
1093 }
1094 while (f3.equals(f2));
1095
1096 final PropertyChangeEvent expected =
1097 createPropertyChangeEvent(properties2, f2, f3);
1098
1099 final ChangeListener listener = new ChangeListener(expected);
1100 final ChangeListener listener2 = new ChangeListener(expected);
1101
1102 properties2.addPropertyChangeListener(listener);
1103 properties2.addPropertyChangeListener(m_propertyName, listener2);
1104
1105 set(properties2, f3);
1106
1107 listener.assertCalledOnce();
1108 listener2.assertCalledOnce();
1109 }
1110
1111 protected PropertyChangeEvent createPropertyChangeEvent(
1112 ConsoleProperties properties2, File f2, File f3) throws Exception {
1113 return new PropertyChangeEvent(properties2, getPropertyName(), f2, f3);
1114 }
1115
1116 protected final String getPropertyName() {
1117 return m_propertyName;
1118 }
1119
1120 protected abstract File get(ConsoleProperties properties);
1121
1122 protected abstract void set(ConsoleProperties properties, File i)
1123 throws Exception;
1124 }
1125
1126 private abstract class TestDirectoryTemplate extends TestFileTemplate {
1127
1128 public TestDirectoryTemplate(String propertyName) {
1129 super(propertyName);
1130 }
1131
1132 protected File get(ConsoleProperties properties) {
1133 return getDirectory(properties).getFile();
1134 }
1135
1136 protected void set(ConsoleProperties properties, File i) throws Exception {
1137 setDirectory(properties, new Directory(i));
1138 }
1139
1140 protected PropertyChangeEvent createPropertyChangeEvent(
1141 ConsoleProperties properties, File f2, File f3) throws Exception {
1142 return new PropertyChangeEvent(properties, getPropertyName(),
1143 new Directory(f2), new Directory(f3));
1144 }
1145
1146 protected abstract Directory getDirectory(ConsoleProperties properties);
1147
1148 protected abstract void setDirectory(ConsoleProperties properties,
1149 Directory i) throws Exception;
1150 }
1151
1152 private abstract class TestPatternTemplate {
1153 private final String m_propertyName;
1154
1155 public TestPatternTemplate(String propertyName) {
1156 m_propertyName = propertyName;
1157 }
1158
1159 public void doTest() throws Exception {
1160
1161 final String s1 = "[a-z]*";
1162
1163 writePropertyToFile(m_propertyName, s1);
1164
1165 final ConsoleProperties properties =
1166 new ConsoleProperties(s_resources, m_file);
1167
1168 assertEquals(s1, get(properties).pattern());
1169 assertEquals(s1, getExpression(properties));
1170
1171 final String s2 = "(some|a)\\w*pattern";
1172
1173 set(properties, s2);
1174 assertEquals(s2, get(properties).pattern());
1175 assertEquals(s2, getExpression(properties));
1176
1177 properties.save();
1178
1179 final ConsoleProperties properties2 =
1180 new ConsoleProperties(s_resources, m_file);
1181
1182 assertEquals(s2, get(properties2).pattern());
1183 assertEquals(s2, getExpression(properties2));
1184
1185 final String s3 = "^abc$";
1186
1187 final PropertyChangeEvent expected = new PropertyChangeEvent(properties2,
1188 m_propertyName, s2, s3);
1189
1190 final ChangeListener listener = new PatternChangeListener(expected);
1191 final ChangeListener listener2 = new PatternChangeListener(expected);
1192
1193 properties2.addPropertyChangeListener(listener);
1194 properties2.addPropertyChangeListener(m_propertyName, listener2);
1195
1196 set(properties2, s3);
1197
1198 listener.assertCalledOnce();
1199 listener2.assertCalledOnce();
1200
1201 set(properties, null);
1202 assertEquals(
1203 ConsoleProperties.DEFAULT_DISTRIBUTION_FILE_FILTER_EXPRESSION,
1204 get(properties).pattern());
1205
1206 assertEquals(
1207 ConsoleProperties.DEFAULT_DISTRIBUTION_FILE_FILTER_EXPRESSION,
1208 getExpression(properties));
1209
1210 try {
1211 set(properties, "malformed(((");
1212 fail("Malformed expression, expected DisplayMessageConsoleException");
1213 }
1214 catch (DisplayMessageConsoleException e) {
1215 assertTrue("Nested exception is a PatternSyntaxException",
1216 e.getCause() instanceof PatternSyntaxException);
1217 }
1218
1219 assertEquals(
1220 ConsoleProperties.DEFAULT_DISTRIBUTION_FILE_FILTER_EXPRESSION,
1221 get(properties).pattern());
1222 }
1223
1224 protected abstract Pattern get(ConsoleProperties properties);
1225
1226 protected abstract void set(ConsoleProperties properties, String i)
1227 throws ConsoleException;
1228
1229 protected String getExpression(ConsoleProperties properties) {
1230 return get(properties).pattern();
1231 }
1232 }
1233
1234 private static class ChangeListener implements PropertyChangeListener {
1235 private final PropertyChangeEvent m_expected;
1236
1237 private int m_callCount;
1238
1239 ChangeListener(PropertyChangeEvent expected) {
1240 m_expected = expected;
1241 }
1242
1243 public void propertyChange(PropertyChangeEvent event) {
1244 ++m_callCount;
1245 assertAreEqual(m_expected.getOldValue(), event.getOldValue());
1246 assertEquals(m_expected.getPropertyName(), event.getPropertyName());
1247 }
1248
1249 public void assertCalledOnce() {
1250 assertEquals(1, m_callCount);
1251 }
1252
1253 public void assertCalled() {
1254 assertTrue(m_callCount > 0);
1255 }
1256
1257 public void assertAreEqual(Object expected, Object result) {
1258 assertEquals(expected, result);
1259 }
1260 }
1261
1262 private static class PatternChangeListener extends ChangeListener {
1263
1264
1265
1266
1267
1268 PatternChangeListener(PropertyChangeEvent expectedExpressions) {
1269 super(expectedExpressions);
1270 }
1271
1272 public void assertAreEqual(Object expected, Object result) {
1273 if (expected == null) {
1274 assertNull(result);
1275 }
1276 else {
1277 assertNotNull(result);
1278 assertEquals(expected, ((Pattern) result).pattern());
1279 }
1280 }
1281 }
1282
1283
1284
1285
1286
1287 private final void writePropertyToFile(String name, String value)
1288 throws Exception {
1289
1290 final FileOutputStream outputStream = new FileOutputStream(m_file);
1291
1292 final Properties properties = new Properties();
1293 properties.setProperty(name, value);
1294 properties.store(outputStream, "");
1295 outputStream.close();
1296 }
1297 }
1298