Skip to main content
summaryrefslogtreecommitdiffstats
blob: bd137f8c9976a3a84407322c51789aa7a8df02a3 (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.jdk.core.test.type;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.osee.framework.jdk.core.test.mock.MockPropertyStore;
import org.eclipse.osee.framework.jdk.core.test.mock.PropertyStoreTestUtil;
import org.eclipse.osee.framework.jdk.core.type.PropertyStore;
import org.junit.Assert;

/**
 * Test Case For {@link PropertyStore}
 * 
 * @author Roberto E. Escobar
 */
public class PropertyStoreTest {

   @org.junit.Test
   public void testCreateWithProperties() {
      Properties properties = (Properties) System.getProperties().clone();
      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore(properties);
      PropertyStoreTestUtil.checkPropertiesEqual(properties, store1.getItems());
      Assert.assertTrue(!properties.equals(store1.getArrays()));
   }

   @org.junit.Test
   public void testCreateWithId() {
      String id = "123456";
      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore(id);
      Assert.assertEquals(id, store1.getId());
   }

   @org.junit.Test
   public void testEqualsAndHashCode() {
      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore("ID");
      store1.put("key1", 12.3f);
      store1.put("key2", 543L);
      store1.put("key3", new String[] {"entry1", "entry2", "entry3"});

      Assert.assertFalse(store1.equals("Test"));

      MockPropertyStore store2 = PropertyStoreTestUtil.createPropertyStore("ID");
      store2.put("key1", 12.3f);
      store2.put("key2", 543L);
      store2.put("key3", new String[] {"entry1", "entry2", "entry3"});

      Assert.assertTrue(store1.equals(store2));

      int hash = store1.hashCode();
      Assert.assertEquals(hash, store2.hashCode());
      // Check that it didn't change
      Assert.assertEquals(hash, store2.hashCode());

      store2.put("key2", 542L);
      Assert.assertTrue(!store1.equals(store2));
      Assert.assertTrue(hash != store2.hashCode());

      store2.put("key2", 543L);
      Assert.assertTrue(store1.equals(store2));

      store2.put("key3", new String[] {"entry1", "entry2"});
      Assert.assertTrue(!store1.equals(store2));

      store2.put("key3", new String[] {"entry1", "entry2", "entry4"});
      Assert.assertTrue(!store1.equals(store2));
   }

   @org.junit.Test
   public void testToString() {
      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore("ID");
      store1.put("key1", 12.3f);
      store1.put("key2", 543L);
      store1.put("key3", new String[] {"entry1", "entry2", "entry3"});
      store1.put("key4", new String[] {"entry4", "entry5"});

      Assert.assertEquals(
         "Id:[ID] Data:{key2=543, key1=12.3} Arrays:{key3=[entry1, entry2, entry3], key4=[entry4, entry5]}",
         store1.toString());
   }

   @org.junit.Test
   public void testNumberException() {
      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore();

      try {
         store1.getDouble("key1");
      } catch (NumberFormatException ex) {
         Assert.assertEquals("No setting found for key: [key1]", ex.getLocalizedMessage());
      }

      try {
         store1.getFloat("key1");
      } catch (NumberFormatException ex) {
         Assert.assertEquals("No setting found for key: [key1]", ex.getLocalizedMessage());
      }
      try {
         store1.getInt("key1");
      } catch (NumberFormatException ex) {
         Assert.assertEquals("No setting found for key: [key1]", ex.getLocalizedMessage());
      }
      try {
         store1.getLong("key1");
      } catch (NumberFormatException ex) {
         Assert.assertEquals("No setting found for key: [key1]", ex.getLocalizedMessage());
      }
      store1.put("key1", "hello");
      try {
         store1.getDouble("key1");
      } catch (NumberFormatException ex) {
         Assert.assertEquals("For input string: \"hello\"", ex.getLocalizedMessage());
      }
      try {
         store1.getFloat("key1");
      } catch (NumberFormatException ex) {
         Assert.assertEquals("For input string: \"hello\"", ex.getLocalizedMessage());
      }
      try {
         store1.getInt("key1");
      } catch (NumberFormatException ex) {
         Assert.assertEquals("For input string: \"hello\"", ex.getLocalizedMessage());
      }
      try {
         store1.getLong("key1");
      } catch (NumberFormatException ex) {
         Assert.assertEquals("For input string: \"hello\"", ex.getLocalizedMessage());
      }
      Assert.assertEquals("hello", store1.get("key1"));
   }

   @org.junit.Test
   public void testSetsAndGets() throws Exception {
      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore();
      store1.setId("myId");
      store1.put("key1", true);
      store1.put("key2", "aKey");
      store1.put("key3", 0.1112);
      store1.put("key4", 12);
      store1.put("key5", 12.3f);
      store1.put("key6", 543L);
      store1.put("key6.5", "");
      store1.put("key7", new String[] {"entry1", "entry2", "entry3"});
      store1.put("key8", new String[] {"entry4", "entry5", "entry6"});
      MockPropertyStore nested = PropertyStoreTestUtil.createPropertyStore(System.getProperties());
      store1.put("key9", nested);

      Set<String> set1 = new TreeSet<String>(store1.keySet());
      PropertyStoreTestUtil.checkArrays(new String[] {"key1", "key2", "key3", "key4", "key5", "key6", "key6.5"},
         set1.toArray(new String[set1.size()]));
      Set<String> set2 = new TreeSet<String>(store1.arrayKeySet());
      PropertyStoreTestUtil.checkArrays(new String[] {"key7", "key8"}, set2.toArray(new String[set2.size()]));

      Assert.assertEquals("myId", store1.getId());
      Assert.assertEquals(true, store1.getBoolean("key1"));
      Assert.assertEquals("aKey", store1.get("key2"));
      Assert.assertEquals(0.1112, store1.getDouble("key3"), 0);
      Assert.assertEquals(12, store1.getInt("key4"));
      Assert.assertEquals(12.3f, store1.getFloat("key5"), 0);
      Assert.assertEquals(543L, store1.getLong("key6"));
      Assert.assertEquals("", store1.get("key6.5"));

      PropertyStoreTestUtil.checkArrays(new String[] {"entry1", "entry2", "entry3"}, store1.getArray("key7"));
      PropertyStoreTestUtil.checkArrays(new String[] {"entry4", "entry5", "entry6"}, store1.getArray("key8"));

      PropertyStoreTestUtil.checkEquals(nested, (MockPropertyStore) store1.getPropertyStore("key9"));
   }

   @org.junit.Test
   public void testNullSetsAndGets() {
      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore("HelloId");
      Assert.assertEquals("HelloId", store1.getId());
      store1.setId(null);

      Assert.assertEquals("", store1.getId());

      store1.put("key1", (String) null);
      Assert.assertEquals("", store1.get("key1"));

      store1.put("key2", (String[]) null);
      Assert.assertTrue(store1.getArray("key2").length == 0);

      store1.put("key3", (PropertyStore) null);
      Assert.assertTrue(store1.getPropertyStores().isEmpty());
      Assert.assertNull(store1.getPropertyStore("key3"));
   }

   @org.junit.Test
   public void testSaveAndLoad() throws Exception {
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore();
      store1.put("key1", true);
      store1.put("key2", "aKey");
      store1.put("key3", 0.1112);
      store1.put("key4", 12);
      store1.put("key5", 12.3f);
      store1.put("key6", 543L);
      store1.put("key7", new String[] {"entry1", "entry2", "entry3"});
      store1.put("key8", new String[] {"entry4", "entry5", "entry6"});
      store1.put("key9", PropertyStoreTestUtil.createPropertyStore(System.getProperties()));

      store1.save(outputStream);

      MockPropertyStore store2 = PropertyStoreTestUtil.createPropertyStore();
      store2.load(new ByteArrayInputStream(outputStream.toByteArray()));

      PropertyStoreTestUtil.checkEquals(store1, store2);
   }

   @org.junit.Test
   public void testSaveAndLoadRW() throws Exception {
      StringWriter writer = new StringWriter();

      MockPropertyStore store1 = PropertyStoreTestUtil.createPropertyStore();
      store1.put("key1", true);
      store1.put("key2", "aKey");
      store1.put("key3", 0.1112);
      store1.put("key4", 12);
      store1.put("key5", 12.3f);
      store1.put("key6", 543L);
      store1.put("key7", new String[] {"entry1", "entry2", "entry3"});
      store1.put("key8", new String[] {"entry4", "entry5", "entry6"});
      store1.put("key9", PropertyStoreTestUtil.createPropertyStore(System.getProperties()));

      store1.save(writer);

      MockPropertyStore store2 = PropertyStoreTestUtil.createPropertyStore();
      store2.load(new InputStreamReader(new ByteArrayInputStream(writer.toString().getBytes("utf-8")), "utf-8"));

      PropertyStoreTestUtil.checkEquals(store1, store2);
   }

}

Back to the top