Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c37d6f8c9bfd3178b5b9daee803839038ae66597 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/*******************************************************************************
 *
 * Copyright (c) 2011 Oracle Corporation.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *
 *    Nikita Levyankov
 *
 *******************************************************************************/

package org.eclipse.hudson.api.model.project.property;

import antlr.ANTLRException;
import hudson.FilePath;
import hudson.Launcher;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.model.FreeStyleProjectMock;
import hudson.model.TaskListener;
import hudson.scm.ChangeLogParser;
import hudson.scm.NullSCM;
import hudson.scm.PollingResult;
import hudson.scm.SCM;
import hudson.scm.SCMRevisionState;
import hudson.tasks.JavadocArchiver;
import hudson.tasks.LogRotator;
import hudson.tasks.Shell;
import hudson.triggers.TimerTrigger;
import hudson.triggers.Trigger;
import hudson.util.DescribableList;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import org.junit.Before;
import org.junit.Test;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static junit.framework.Assert.fail;

/**
 * Contains test-cases for IProjectProperty and its' implementations.
 * <p/>
 * Date: 9/27/11
 *
 * @author Nikita Levyankov
 */
@SuppressWarnings("unchecked")
public class ProjectPropertyTest {
    private FreeStyleProjectMock project;
    private FreeStyleProjectMock parent;
    private final String propertyKey = "propertyKey";

    @Before
    public void setUp() {
        project = new FreeStyleProjectMock("project");
        parent = new FreeStyleProjectMock("parent");
    }

    /**
     * Verify all constructors for ProjectProperties hierarchy.
     */
    @Test
    public void testBaseProjectConstructor() {
        try {
            new BaseProjectProperty(null);
            fail("Null should be handled by BaseProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
        BaseProjectProperty property = new BaseProjectProperty(project);
        assertNotNull(property.getJob());
        assertEquals(project, property.getJob());
    }

    @Test
    public void testStringProjectConstructor() {
        try {
            new StringProjectProperty(null);
            fail("Null should be handled by StringProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    @Test
    public void testIntegerProjectConstructor() {
        try {
            new IntegerProjectProperty(null);
            fail("Null should be handled by IntegerProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    @Test
    public void testBooleanProjectPropertyConstructor() {
        try {
            new BooleanProjectProperty(null);
            fail("Null should be handled by BooleanProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    @Test
    public void testLogRotatorProjectPropertyConstructor() {
        try {
            new LogRotatorProjectProperty(null);
            fail("Null should be handled by LogRotatorProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    @Test
    public void testDescribableListProjectPropertyConstructor() {
        try {
            new DescribableListProjectProperty(null);
            fail("Null should be handled by DescribableListProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    @Test
    public void testAxisListProjectPropertyConstructor() {
        try {
            new AxisListProjectProperty(null);
            fail("Null should be handled by AxisListProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    @Test
    public void testSCMProjectPropertyConstructor() {
        try {
            new SCMProjectProperty(null);
            fail("Null should be handled by SCMProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    @Test
    public void testExternalProjectPropertyConstructor() {
        try {
            new ExternalProjectProperty(null);
            fail("Null should be handled by ExternalProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    @Test
    public void testTriggerProjectPropertyConstructor() {
        try {
            new TriggerProjectProperty(null);
            fail("Null should be handled by TriggerProjectProperty constructor.");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_JOB_EXCEPTION, e.getMessage());
        }
    }

    /**
     * Checks prepareValue method.
     */
    @Test
    public void testBaseProjectPropertyPrepareValue() {
        //BaseProject property doesn't perform any changes with value.
        BaseProjectProperty property = new BaseProjectProperty(project);
        assertNull(property.prepareValue(null));
        Object value = new Object();
        assertEquals(value, property.prepareValue(value));
    }

    @Test
    public void testBooleanProjectPropertyPrepareValue() {
        //Boolean property acts as BaseProperty
        BaseProjectProperty property = new BooleanProjectProperty(project);
        assertNull(property.prepareValue(null));
        assertFalse((Boolean) property.prepareValue(false));
        assertTrue((Boolean) property.prepareValue(true));
    }

    @Test
    public void testLogRotatorProjectPropertyPrepareValue() {
        //Boolean property acts as BaseProperty
        BaseProjectProperty property = new LogRotatorProjectProperty(project);
        assertNull(property.prepareValue(null));
        LogRotator value = new LogRotator(1, 1, 1, 1);
        assertEquals(value, property.prepareValue(value));
    }

    @Test
    public void testIntegerProjectPropertyPrepareValue() {
        //Integer property acts as BaseProperty
        BaseProjectProperty property = new IntegerProjectProperty(project);
        assertNull(property.prepareValue(null));
        int intValue = 10;
        assertEquals(intValue, property.prepareValue(intValue));
    }

    @Test
    public void testStringProjectPropertyPrepareValue() {
        //String project property trims string values to null and uses StringUtils.trimToNull logic.
        BaseProjectProperty property = new StringProjectProperty(project);
        assertNull(property.prepareValue(null));
        assertNull(property.prepareValue(""));
        assertNull(property.prepareValue("     "));
        assertEquals("abc", property.prepareValue("abc"));
        assertEquals("abc", property.prepareValue("    abc    "));
    }

    @Test
    public void testAxisListProjectPropertyPrepareValue() {
        //Boolean property acts as BaseProperty
        BaseProjectProperty property = new AxisListProjectProperty(project);
        assertNull(property.prepareValue(null));
        AxisList value = new AxisList();
        assertEquals(value, property.prepareValue(value));
    }

    @Test
    public void testSCMProjectPropertyPrepareValue() {
        //Boolean property acts as BaseProperty
        BaseProjectProperty property = new SCMProjectProperty(project);
        assertNull(property.prepareValue(null));
        SCM value = new NullSCM();
        assertEquals(value, property.prepareValue(value));
    }

    /**
     * Verify getDefaultValue method.
     */
    @Test
    public void testBaseProjectPropertyGetDefaultValue() {
        BaseProjectProperty property = new BaseProjectProperty(project);
        assertNull(property.getDefaultValue());
    }

    @Test
    public void testStringProjectPropertyGetDefaultValue() {
        BaseProjectProperty property = new StringProjectProperty(project);
        assertNull(property.getDefaultValue());
    }

    @Test
    public void testIntegerProjectPropertyGetDefaultValue() {
        BaseProjectProperty property = new IntegerProjectProperty(project);
        assertEquals(0, property.getDefaultValue());
    }

    @Test
    public void testBooleanProjectPropertyGetDefaultValue() {
        BaseProjectProperty property = new BooleanProjectProperty(project);
        assertFalse((Boolean) property.getDefaultValue());
    }

    @Test
    public void testLogRotatorProjectPropertyGetDefaultValue() {
        BaseProjectProperty property = new LogRotatorProjectProperty(project);
        assertNull(property.getDefaultValue());
    }

    @Test
    public void testAxisListProjectPropertyGetDefaultValue() {
        AxisListProjectProperty property = new AxisListProjectProperty(project);
        assertEquals(property.getDefaultValue().size(), 0);
    }

    @Test
    public void testDescribableListProjectPropertyGetDefaultValue() {
        BaseProjectProperty property = new DescribableListProjectProperty(project);
        assertNotNull(property.getDefaultValue());
    }


    @Test
    public void testScmtProjectPropertyGetDefaultValue() {
        BaseProjectProperty property = new SCMProjectProperty(project);
        assertEquals(new NullSCM(), property.getDefaultValue());
    }

    /**
     * Verify allowOverrideValue method.
     */
    @Test
    public void testBaseProjectPropertyAllowOverrideValue() {
        BaseProjectProperty property = new BaseProjectProperty(project);
        assertFalse(property.allowOverrideValue(null, null));
        assertTrue(property.allowOverrideValue(new Object(), null));
        assertTrue(property.allowOverrideValue(null, new Object()));
        //Test properties that don't have correct equals methods
        assertFalse(property.allowOverrideValue(new JavadocArchiver("", false), new JavadocArchiver("", false)));
        assertTrue(property.allowOverrideValue(new JavadocArchiver("", true), new JavadocArchiver("", false)));
    }

    @Test
    public void testBooleanProjectPropertyAllowOverrideValue() {
        BaseProjectProperty property = new BooleanProjectProperty(project);
        assertFalse(property.allowOverrideValue(null, null));
        assertFalse(property.allowOverrideValue(false, false));
        assertFalse(property.allowOverrideValue(true, true));
        assertTrue(property.allowOverrideValue(true, false));
        assertTrue(property.allowOverrideValue(false, true));
    }

    @Test
    public void testIntegerProjectPropertyAllowOverrideValue() {
        BaseProjectProperty property = new IntegerProjectProperty(project);
        assertFalse(property.allowOverrideValue(null, null));
        assertFalse(property.allowOverrideValue(1, 1));
        assertTrue(property.allowOverrideValue(1, 0));
        assertTrue(property.allowOverrideValue(0, 1));
    }

    @Test
    public void testStringProjectPropertyAllowOverrideValue() {
        BaseProjectProperty property = new StringProjectProperty(project);
        assertFalse(property.allowOverrideValue(null, null));
        assertFalse(property.allowOverrideValue("", ""));
        assertFalse(property.allowOverrideValue("abc", "abc"));
        assertFalse(property.allowOverrideValue("abc", "ABC"));
        assertTrue(property.allowOverrideValue(null, "abc"));
        assertTrue(property.allowOverrideValue("abc", null));
        assertTrue(property.allowOverrideValue("abc", "abcd"));
    }

    @Test
    public void testLogRotatorProjectPropertyAllowOverrideValue() {
        BaseProjectProperty property = new LogRotatorProjectProperty(project);
        assertFalse(property.allowOverrideValue(null, null));
        assertTrue(property.allowOverrideValue(new LogRotator(1, 1, 1, 1), null));
        assertTrue(property.allowOverrideValue(null, new LogRotator(1, 1, 1, 1)));
        assertTrue(property.allowOverrideValue(new LogRotator(1, 1, 1, 2), new LogRotator(1, 1, 1, 1)));
    }

    @Test
    public void testDescribableListProjectPropertyAllowOverrideValue() throws IOException {
        BaseProjectProperty property = new DescribableListProjectProperty(project);
        //Don't need to override null values
        assertFalse(property.allowOverrideValue(null, null));
        //Allow override if cascading or candidate are null
        assertTrue(property.allowOverrideValue(new DescribableList(project), null));
        assertTrue(property.allowOverrideValue(null, new DescribableList(project)));
        //Don't need to override Describable lists which has same Describable#data values, even if owners are not equal.
        assertFalse(property.allowOverrideValue(new DescribableList(project), new DescribableList(project)));
        assertFalse(property.allowOverrideValue(new DescribableList(project), new DescribableList(parent)));
        DescribableList describableList1 = new DescribableList(project);
        DescribableList describableList2 = new DescribableList(project);
        describableList1.add(new Shell("echo 'test3'"));
        describableList1.add(new Shell("echo 'test2'"));
        describableList2.add(new Shell("echo 'test2'"));
        describableList2.add(new Shell("echo 'test3'"));
        assertFalse(property.allowOverrideValue(describableList1, describableList2));

        DescribableList describableList3 = new DescribableList(parent);
        describableList3.replaceBy(describableList2.toList());
        assertFalse(property.allowOverrideValue(describableList1, describableList3));

        describableList2.add(new Shell("echo 'test1'"));
        assertTrue(property.allowOverrideValue(describableList1, describableList2));
    }

    @Test
    public void testAxisListProjectPropertyAllowOverrideValue() {
        BaseProjectProperty property = new AxisListProjectProperty(project);
        assertFalse(property.allowOverrideValue(null, null));
        assertTrue(property.allowOverrideValue(new AxisList(), null));
        assertTrue(property.allowOverrideValue(null, new AxisList()));
        assertTrue(property.allowOverrideValue(new AxisList().add(new Axis("DB", "mysql")), new AxisList()));
    }

    @Test
    public void testSCMProjectPropertyAllowOverrideValue() {
        BaseProjectProperty property = new SCMProjectProperty(project);
        assertFalse(property.allowOverrideValue(null, null));
        assertTrue(property.allowOverrideValue(new NullSCM(), null));
        assertTrue(property.allowOverrideValue(null, new NullSCM()));
        assertTrue(property.allowOverrideValue(new NullSCM(), new FakeSCM()));
    }


    /**
     * Verify getCascadingValue method.
     */
    @Test
    public void testBaseProjectPropertyGetCascadingValue() {
        String parentValue = "parentValue";

        BaseProjectProperty property = new BaseProjectProperty(project);
        property.setKey(propertyKey);
        //If project doesn't have cascading project - default value is used as cascading value.
        assertEquals(property.getDefaultValue(), property.getCascadingValue());

        project.setCascadingProject(parent);
        property = new BaseProjectProperty(project);
        property.setKey(propertyKey);
        //If project has cascading project and cascading value is not set - default value is used.
        assertEquals(property.getDefaultValue(), property.getCascadingValue());

        BaseProjectProperty parentProperty = new BaseProjectProperty(parent);
        parentProperty.setKey(propertyKey);
        parentProperty.setValue(parentValue);
        parent.putProjectProperty(propertyKey, parentProperty);
        project.setCascadingProject(parent);
        property = new BaseProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(parentValue);
        //If project has cascading project and cascading value is set - property value will be used.
        assertEquals(parentProperty.getOriginalValue(), property.getCascadingValue());
    }

    /**
     * Verify getOriginalValue method.
     */
    @Test
    public void testBaseProjectPropertyGetOriginalValue() {
        BaseProjectProperty property = new BaseProjectProperty(project);
        assertNull(property.getOriginalValue());
        Object value = new Object();
        property.setKey(propertyKey);
        property.setValue(value);
        assertEquals(value, property.getOriginalValue());
        property.setValue(null);
        assertNull(property.getOriginalValue());
    }

    @Test
    public void testIntegerProjectPropertyGetOriginalValue() {
        int value = 10;
        BaseProjectProperty property = new IntegerProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(value);
        assertEquals(value, property.getOriginalValue());
    }

    @Test
    public void testStringProjectPropertyGetOriginalValue() {
        String value = "abs";
        BaseProjectProperty property = new StringProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(value);
        assertEquals(value, property.getOriginalValue());
    }

    @Test
    public void testBooleanProjectPropertyGetOriginalValue() {
        boolean value = Boolean.TRUE;
        BaseProjectProperty property = new BooleanProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(value);
        assertEquals(value, property.getOriginalValue());
        property.setValue(null);
        assertFalse((Boolean) property.getOriginalValue());
    }

    @Test
    public void testLogRotatorProjectPropertyGetOriginalValue() {
        LogRotator value = new LogRotator(1, 1, 1, 1);
        BaseProjectProperty property = new LogRotatorProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(value);
        assertEquals(value, property.getOriginalValue());
    }

    @Test
    public void testAxisListProjectPropertyGetOriginalValue() {
        AxisList value = new AxisList();
        value.add(new Axis("DB", "mysql"));
        BaseProjectProperty property = new AxisListProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(value);
        assertEquals(value, property.getOriginalValue());
    }

    @Test
    public void testScmProjectPropertyGetOriginalValue() {
        SCM value = new NullSCM();
        BaseProjectProperty property = new SCMProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(value);
        assertEquals(value, property.getOriginalValue());
    }

    /**
     * Verify setValue method.
     */
    @Test
    public void testSetValue() {
        BaseProjectProperty property = new BaseProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(null);
        //If project doesn't have cascading - default boolean value is used for propertyOverridden flag
        assertFalse(property.isOverridden());
        assertNull(property.getOriginalValue());

        Object value = 12345;
        property.setValue(value);
        //If project doesn't have cascading - default boolean value is used for propertyOverridden flag
        assertFalse(property.isOverridden());
        assertEquals(value, property.getOriginalValue());

        String parentValue = "equalValue";
        BaseProjectProperty parentProperty = new BaseProjectProperty(parent);
        parentProperty.setKey(propertyKey);
        parentProperty.setValue(parentValue);
        parent.putProjectProperty(propertyKey, parentProperty);
        project.setCascadingProject(parent);

        //If value set to null, need to check whether default value is equals to cascading
        property.setValue(null);
        assertTrue(property.isOverridden());
        String overriddenValue = "newValue";
        property.setValue(overriddenValue);
        assertTrue(property.isOverridden());

        //Check whether current value is not null, after setting equal-to-cascading value current will be null
        assertNotNull(property.getOriginalValue());
        assertTrue(property.isOverridden());
        property.setValue(parentValue);
        //Reset current property to null
        assertNull(property.getOriginalValue());
        //Cascading value is equal to current - reset flag to false.
        assertFalse(property.isOverridden());
    }

    @Test
    public void testGetValue() {
        Integer propertyValue = 10;
        IntegerProjectProperty property = new IntegerProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(propertyValue);
        //if value is not null - return it
        assertEquals(propertyValue, property.getValue());
        property.setValue(null);
        assertNull(property.getOriginalValue());

        //if current value is set to null and property is not overridden - defaultValue (Zero) will be taken
        Integer value = property.getValue();
        assertNotNull(value);
        assertFalse(property.isOverridden());
        assertEquals(property.getDefaultValue(), value);

        property.setOverridden(true);
        //If property is overridden - return its actual value.
        assertNull(property.getValue());

        IntegerProjectProperty parentProperty = new IntegerProjectProperty(parent);
        parentProperty.setKey(propertyKey);
        parentProperty.setValue(propertyValue);
        parent.putProjectProperty(propertyKey, parentProperty);

        project.setCascadingProject(parent);
        property = new IntegerProjectProperty(project);
        property.setKey(propertyKey);
        //if current value is null and is not overridden value, take from cascading
        assertNull(property.getOriginalValue());
        assertEquals(propertyValue, property.getValue());

        property.setOverridden(true);
        //Property is overridden - return current value even if it is null.
        assertNull(property.getOriginalValue());
        assertNull(property.getValue());
    }

    /**
     * Property should have not null property key.
     */
    @Test
    public void testNullPropertyKey() {
        BaseProjectProperty property = new BaseProjectProperty(project);
        try {
            property.setValue("value");
            fail("PropertyKey shouldn't be null while calling setValue");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_PROPERTY_KEY_EXCEPTION, e.getMessage());
        }
        try {
            property.getCascadingValue();
            fail("PropertyKey shouldn't be null while calling getCascadingValue()");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_PROPERTY_KEY_EXCEPTION, e.getMessage());
        }
        try {
            property.getValue();
            fail("PropertyKey shouldn't be null while calling getValue()");
        } catch (Exception e) {
            assertEquals(BaseProjectProperty.INVALID_PROPERTY_KEY_EXCEPTION, e.getMessage());
        }

        property.setKey("key");
        try {
            property.setValue("value");
            property.getCascadingValue();
            property.getValue();
        } catch (Exception e) {
            fail("PropertyKey is valid");
        }
    }

    /**
     * Resets project property value,
     */
    @Test
    public void testResetValue() {
        BaseProjectProperty property = new BaseProjectProperty(project);
        property.setKey(propertyKey);
        property.setValue(new Object());
        property.setOverridden(true);
        assertNotNull(property.getOriginalValue());
        assertTrue(property.isOverridden());
        property.resetValue();
        assertNull(property.getOriginalValue());
        assertFalse(property.isOverridden());
    }

    /**
     * Test setOverridden method.
     */
    @Test
    public void testSetPropertyOverridden() {
        BaseProjectProperty property = new BaseProjectProperty(project);
        //By default property isOverridden flag is set to false.
        assertFalse(property.isOverridden());
        //Test if flag is configured as expected. Set true/false values and check whether they are set correctly.
        property.setOverridden(true);
        assertTrue(property.isOverridden());
        property.setOverridden(false);
        assertFalse(property.isOverridden());
    }

    /**
     * Test returnOriginalValue method for BaseProjectProperty.
     */
    @Test
    public void testBaseProjectPropertyReturnOriginalValue() {
        BaseProjectProperty property = new BaseProjectProperty(project);
        //False - If isOverridden flag is false and original value is null
        assertFalse(property.returnOriginalValue());
        property.setOverridden(true);
        //True - If isOverridden flag is true or original value is not null
        assertTrue(property.returnOriginalValue());
        property.setOriginalValue(new Object(), false);
        assertTrue(property.returnOriginalValue());
    }

    /**
     * Test returnOriginalValue method for DescribableListProjectProperty.
     *
     * @throws IOException if any.
     */
    @Test
    public void testDescribableListProjectPropertyReturnOriginalValue() throws IOException {
        DescribableListProjectProperty property = new DescribableListProjectProperty(project);
        //False - If isOverridden flag is false and original value is null
        assertFalse(property.returnOriginalValue());

        //False - If isOverridden flag is false and Describable list is null or empty
        property.setOriginalValue(null, false);
        assertFalse(property.returnOriginalValue());
        property.setOriginalValue(new DescribableList(project), false);
        assertFalse(property.returnOriginalValue());

        //True - If isOverridden flag is true or original value is not null
        property.setOverridden(true);
        assertTrue(property.returnOriginalValue());

        DescribableList originalValue = new DescribableList(project, Arrays.asList(new Object()));
        property.setOriginalValue(originalValue, false);
        assertTrue(property.returnOriginalValue());
    }

    /**
     * Test updateOriginalValue method for ExternalProjectProperty.
     */
    @Test
    public void testExternalProjectPropertyUpdateOriginalValue() {
        ExternalProjectProperty property = new ExternalProjectProperty(project);
        //If property is not modified, than it shouldn't be updated.
        assertFalse(property.isModified());
        assertFalse(property.updateOriginalValue(new Object(), new Object()));

        //If property is modified, than BaseProjectProperty#updateOriginalValue method will be called.
        //Result will be true, because in this case property value will be updated.
        property.setModified(true);
        assertTrue(property.isModified());
        assertTrue(property.updateOriginalValue(new Object(), new Object()));

    }

    /**
     * Test 1updateOriginalValue method for TriggerProjectProperty.
     *
     * @throws ANTLRException if any
     */
    @Test
    public void testTriggerProjectPropertyUpdateOriginalValue() throws ANTLRException {
        TriggerProjectProperty property = new TriggerProjectProperty(project);
        Trigger originalTrigger = new TimerTrigger("* * * * *");
        Trigger cascadingTrigger = new TimerTrigger("* * * * *");
        property.setModified(true);
        property.updateOriginalValue(originalTrigger, cascadingTrigger);
        //Property isn't overridden because of values equal.
        assertFalse(property.isOverridden());
        //If trigger property value equals to cascading be sure that sets original value instead of cascading.
        assertEquals(property.getOriginalValue(), originalTrigger);

        cascadingTrigger= new TimerTrigger("*/2 * * * *");
        property.updateOriginalValue(originalTrigger, cascadingTrigger);
        assertTrue(property.isOverridden());
        assertEquals(property.getOriginalValue(), originalTrigger);
    }

    private class FakeSCM extends SCM {
        @Override
        public SCMRevisionState calcRevisionsFromBuild(AbstractBuild<?, ?> build, Launcher launcher,
                                                       TaskListener listener)
            throws IOException, InterruptedException {
            return null;
        }

        @Override
        protected PollingResult compareRemoteRevisionWith(AbstractProject<?, ?> project, Launcher launcher,
                                                          FilePath workspace, TaskListener listener,
                                                          SCMRevisionState baseline)
            throws IOException, InterruptedException {
            return null;
        }

        @Override
        public boolean checkout(AbstractBuild<?, ?> build, Launcher launcher, FilePath workspace,
                                BuildListener listener,
                                File changelogFile) throws IOException, InterruptedException {
            return false;
        }

        @Override
        public ChangeLogParser createChangeLogParser() {
            return null;
        }
    }

}

Back to the top