summaryrefslogtreecommitdiffstats
path: root/appc-common/src/test/java/org/onap/appc/util/UnmodifiablePropertiesTest.java
blob: d238dc3528c418c28f545857d95ca6c8a571757e (plain)
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
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.util;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Properties;

public class UnmodifiablePropertiesTest {

    private static final String propKey1 = "testKey1";
    private static final String propKey2 = "testKey2";
    private static final String propValue1 = "testValue1";
    private static final String propValue2 = "testValue2";
    private static final String noKey = "unusedKey";
    private static final String noValue = "unusedValue";
    private Properties properties = new Properties();

    private UnmodifiableProperties unmodifiableProperties = new UnmodifiableProperties(properties);
    private String desiredMessage = "Property cannot be modified!";

    @Before
    public void setUp() throws Exception {
        properties.setProperty(propKey1, propValue1);
        properties.setProperty(propKey2, propValue2);
    }

    @Test(expected = UnsupportedOperationException.class)
    public void testClear() {
        try {
            unmodifiableProperties.clear();
        } catch (UnsupportedOperationException exceptionMessage) {
            Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
            throw exceptionMessage;
        }
    }

    @Test(expected = UnsupportedOperationException.class)
    public void testClone() {
        try {
            unmodifiableProperties.clone();
        } catch (UnsupportedOperationException exceptionMessage) {
            Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
            throw exceptionMessage;
        }
    }

    @Test
    public final void testContainsObject() {
        Assert.assertTrue(unmodifiableProperties.contains(propValue2));
        Assert.assertFalse(unmodifiableProperties.contains(noValue));
    }

    @Test
    public final void testContainsKeyObject() {
        Assert.assertTrue(unmodifiableProperties.containsKey(propKey1));
        Assert.assertFalse(unmodifiableProperties.containsKey(noKey));
    }

    @Test
    public final void testContainsValueObject() {
        Assert.assertTrue(unmodifiableProperties.containsValue(propValue1));
        Assert.assertFalse(unmodifiableProperties.containsValue(noValue));
    }

    @Test
    public final void testEntrySet() {
        // Should match my properties K/V entries in setUp.
        // Expect entrySet=[testKey2=testValue2, testKey1=testValue1].
        Assert.assertEquals(properties.entrySet(), unmodifiableProperties.entrySet());
    }

    @Test
    public final void testEqualsObject() {
        Assert.assertTrue(unmodifiableProperties.equals(properties));
    }

    @Test
    public final void testGetObject() {
        Assert.assertEquals(propValue2, unmodifiableProperties.get(propKey2));
    }

    @Test
    public final void testGetPropertyString() {
        Assert.assertEquals(propValue1, unmodifiableProperties.getProperty("testKey1"));
    }

    @Test
    public final void testGetPropertyStringString() {
        Assert.assertEquals(propValue2, unmodifiableProperties.getProperty(propKey2, noValue));
        Assert.assertEquals(propValue2, unmodifiableProperties.getProperty(noKey, propValue2));
    }

    @Test
    public final void testIsEmpty() {
        Assert.assertFalse(unmodifiableProperties.isEmpty());
    }

    @Test(expected = UnsupportedOperationException.class)
    public final void testPutObjectObject() {
        try {
            unmodifiableProperties.put(propKey2, propValue1);
        } catch (UnsupportedOperationException exceptionMessage) {
            Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
            throw exceptionMessage;
        }
    }

    @Test(expected = UnsupportedOperationException.class)
    public final void testRehash() {
        try {
            unmodifiableProperties.rehash();
        } catch (UnsupportedOperationException exceptionMessage) {
            Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
            throw exceptionMessage;
        }
    }

    @Test(expected = UnsupportedOperationException.class)
    public final void testRemoveObject() {
        try {
            unmodifiableProperties.remove(propKey1);
        } catch (UnsupportedOperationException exceptionMessage) {
            Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
            throw exceptionMessage;
        }
    }

    @Test(expected = UnsupportedOperationException.class)
    public final void testSetPropertyStringString() {
        try {
            unmodifiableProperties.setProperty(propKey1, propValue2);
        } catch (UnsupportedOperationException exceptionMessage) {
            Assert.assertEquals(desiredMessage, exceptionMessage.getMessage());
            throw exceptionMessage;
        }
    }

    @Test
    public final void testSize() {
        Assert.assertEquals(2, unmodifiableProperties.size());
    }

    @Test
    public final void testStringPropertyNames() {
        Assert.assertEquals(properties.stringPropertyNames(),unmodifiableProperties.stringPropertyNames());
    }

    @Test
    public final void testToString() {
        // toString=[{testKey2=testValue2, testKey1=testValue1}]
        Assert.assertEquals(properties.toString(),unmodifiableProperties.toString());
    }
}