1/*
2 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.hotspot.igv.data;
27
28import com.sun.hotspot.igv.data.Properties.InvertPropertyMatcher;
29import com.sun.hotspot.igv.data.Properties.PropertyMatcher;
30import com.sun.hotspot.igv.data.Properties.PropertySelector;
31import com.sun.hotspot.igv.data.Properties.RegexpPropertyMatcher;
32import com.sun.hotspot.igv.data.Properties.StringPropertyMatcher;
33import java.util.ArrayList;
34import java.util.Collection;
35import java.util.Iterator;
36import junit.framework.TestCase;
37
38/**
39 *
40 * @author Thomas Wuerthinger
41 */
42public class PropertiesTest extends TestCase {
43
44
45
46    public PropertiesTest(String testName) {
47        super(testName);
48    }
49
50    @Override
51    protected void setUp() throws Exception {
52        super.setUp();
53    }
54
55    @Override
56    protected void tearDown() throws Exception {
57        super.tearDown();
58    }
59
60    /**
61     * Test of equals method, of class Properties.
62     */
63    public void testEquals() {
64        Properties a = new Properties();
65        assertFalse(a.equals(null));
66        assertTrue(a.equals(a));
67
68        Properties b = new Properties();
69        assertTrue(a.equals(b));
70        assertTrue(a.hashCode() == b.hashCode());
71
72        a.setProperty("p1", "1");
73        assertFalse(a.equals(b));
74        assertFalse(b.equals(a));
75        assertFalse(a.hashCode() == b.hashCode());
76
77        b.setProperty("p1", "1");
78        assertTrue(a.equals(b));
79        assertTrue(a.hashCode() == b.hashCode());
80
81        Properties c = new Properties(a);
82        assertTrue(c.equals(a));
83        assertTrue(c.equals(b));
84
85        c.setProperty("p1", "2");
86        assertFalse(c.equals(b));
87        assertFalse(c.hashCode() == b.hashCode());
88        assertFalse(c.equals(a));
89        assertFalse(c.hashCode() == a.hashCode());
90
91        a.setProperty("p2", "2");
92        Properties d = new Properties();
93        d.setProperty("p2", "2");
94        d.setProperty("p1", "1");
95        assertTrue(d.equals(a));
96    }
97
98    /**
99     * Test of selectSingle method, of class Properties.
100     */
101    public void testSelectSingle() {
102
103        final boolean[] called = new boolean[1];
104        final String v = "2";
105        final String n = "p2";
106
107        PropertyMatcher matcher = new PropertyMatcher() {
108
109            @Override
110            public String getName() {
111                assertFalse(called[0]);
112                called[0] = true;
113                return n;
114            }
115
116            @Override
117            public boolean match(String value) {
118                assertTrue(v.equals(value));
119                return true;
120            }
121        };
122
123        Properties instance = new Properties();
124        instance.setProperty("p1", "1");
125        instance.setProperty(n, v);
126        instance.setProperty("p3", "3");
127        Property result = instance.selectSingle(matcher);
128        assertEquals(result, new Property(n, v));
129
130
131        called[0] = false;
132        PropertyMatcher matcher2 = new PropertyMatcher() {
133
134            @Override
135            public String getName() {
136                assertFalse(called[0]);
137                called[0] = true;
138                return n;
139            }
140
141            @Override
142            public boolean match(String value) {
143                return false;
144            }
145        };
146
147
148        Property result2 = instance.selectSingle(matcher2);
149        assertTrue(result2 == null);
150    }
151
152    /**
153     * Test of get method, of class Properties.
154     */
155    public void testGet() {
156        Properties instance = new Properties();
157        instance.setProperty("p1", "1");
158        assertEquals("1", instance.get("p1"));
159        assertEquals(null, instance.get("p2"));
160    }
161
162    /**
163     * Test of getProperties method, of class Properties.
164     */
165    public void testIterator() {
166        Properties instance = new Properties();
167        instance.setProperty("p1", "1");
168        instance.setProperty("p2", "2");
169        Iterator<Property> result = instance.iterator();
170        assertTrue(result.hasNext());
171        assertEquals(new Property("p1", "1"), result.next());
172        assertTrue(result.hasNext());
173        assertEquals(new Property("p2", "2"), result.next());
174        assertFalse(result.hasNext());
175        assertTrue(result.next() == null);
176
177        try {
178            result.remove();
179            fail();
180        } catch(UnsupportedOperationException e) {}
181    }
182
183    /**
184     * Test of add method, of class Properties.
185     */
186    public void testAdd() {
187        Properties a = new Properties();
188        a.setProperty("p1", "1");
189        a.setProperty("p2", "2");
190
191        Properties b = new Properties();
192        b.setProperty("p1", "1");
193
194        Properties c = new Properties();
195        c.setProperty("p2", "2");
196
197        assertFalse(a.equals(b));
198        b.add(c);
199
200        assertTrue(a.equals(b));
201
202        b.setProperty("p3", null);
203        assertTrue(a.equals(b));
204
205        Properties empty = new Properties();
206        b.add(empty);
207        assertTrue(a.equals(b));
208
209        empty.add(b);
210        assertTrue(a.equals(empty));
211    }
212
213
214    /**
215     * Test the multiple argument constructors.
216     */
217    public void testConstructors() {
218        Properties a = new Properties("p1", "1", "p2", "2", "p3", "3");
219        Properties b = new Properties("p1", "1", "p2", "2");
220        Properties c = new Properties("p1", "1");
221
222        assertTrue(a.get("p3").equals("3"));
223        assertTrue(b.get("p2").equals("2"));
224        assertTrue(b.get("p1").equals("1"));
225
226        b.setProperty("p3", "3");
227        c.setProperty("p2", "2");
228        c.setProperty("p3", "3");
229
230        assertTrue(a.equals(b));
231        assertTrue(a.equals(c));
232    }
233
234    /**
235     * Test Entity class
236     */
237    public void testEntity() {
238
239        Properties p = new Properties();
240
241        Properties.Entity entity = new Properties.Entity();
242        assertEquals(entity.getProperties(), p);
243
244        entity.getProperties().setProperty("p1", "1");
245        Properties.Entity entity2 = new Properties.Entity(entity);
246        assertEquals(entity.getProperties(), entity2.getProperties());
247    }
248
249    /**
250     * Test property selector
251     */
252    public void testPropertySelector() {
253        final Collection<Properties.Entity> c = new ArrayList<>();
254
255        final Properties.Entity e1 = new Properties.Entity();
256        e1.getProperties().setProperty("p1", "1");
257        e1.getProperties().setProperty("p2", "2");
258        c.add(e1);
259
260        final Properties.Entity e2 = new Properties.Entity();
261        e2.getProperties().setProperty("p2", "2");
262        e2.getProperties().setProperty("p1", "1");
263        e2.getProperties().setProperty("p3", "3");
264        c.add(e2);
265
266        final Properties.Entity e3 = new Properties.Entity();
267        e3.getProperties().setProperty("p3", "3");
268        e3.getProperties().setProperty("p4", "4");
269        c.add(e3);
270
271        final PropertySelector<Properties.Entity> sel = new PropertySelector<>(c);
272
273        final StringPropertyMatcher matcher1 = new StringPropertyMatcher("p2", "2");
274        assertTrue(sel.selectMultiple(matcher1).size() == 2);
275        assertTrue(sel.selectMultiple(matcher1).contains(e1));
276        assertTrue(sel.selectMultiple(matcher1).contains(e2));
277        assertTrue(sel.selectSingle(matcher1).equals(e1) || sel.selectSingle(matcher1).equals(e2));
278
279        final StringPropertyMatcher matcher2 = new StringPropertyMatcher("p3", "3");
280        assertTrue(sel.selectMultiple(matcher2).size() == 2);
281        assertTrue(sel.selectMultiple(matcher2).contains(e2));
282        assertTrue(sel.selectMultiple(matcher2).contains(e3));
283        assertTrue(sel.selectSingle(matcher2).equals(e2) || sel.selectSingle(matcher2).equals(e3));
284
285        final StringPropertyMatcher matcher3 = new StringPropertyMatcher("p4", "4");
286        assertTrue(sel.selectMultiple(matcher3).size() == 1);
287        assertTrue(sel.selectMultiple(matcher3).contains(e3));
288        assertTrue(sel.selectSingle(matcher3).equals(e3));
289
290        final StringPropertyMatcher matcher4 = new StringPropertyMatcher("p5", "5");
291        assertTrue(sel.selectMultiple(matcher4).size() == 0);
292        assertTrue(sel.selectSingle(matcher4) == null);
293    }
294
295    public void testRemoveProperty() {
296        final Properties p = new Properties();
297        p.setProperty("p1", "1");
298        p.setProperty("p2", "2");
299
300        assertTrue(p.get("p1").equals("1"));
301        assertTrue(p.get("p2").equals("2"));
302
303        p.setProperty("p1", null);
304        assertTrue(p.get("p1") == null);
305        assertTrue(p.get("p2").equals("2"));
306
307        p.setProperty("p2", null);
308        assertTrue(p.get("p1") == null);
309        assertTrue(p.get("p2") == null);
310
311        p.setProperty("p3", "3");
312        assertTrue(p.get("p1") == null);
313        assertTrue(p.get("p2") == null);
314        assertTrue(p.get("p3").equals("3"));
315    }
316
317    /**
318     * Test property matchers
319     */
320    public void testPropertyMatchers() {
321        final StringPropertyMatcher matcher = new StringPropertyMatcher("p1", "1");
322        assertTrue(matcher.getName().equals("p1"));
323        assertTrue(matcher.match("1"));
324        assertFalse(matcher.match("2"));
325        try {
326            matcher.match(null);
327            fail();
328        } catch(IllegalArgumentException e) {}
329
330        try {
331            new StringPropertyMatcher(null, "**");
332            fail();
333        } catch(IllegalArgumentException e) {}
334
335        try {
336            new StringPropertyMatcher("p1", null);
337            fail();
338        } catch(IllegalArgumentException e) {}
339
340        final RegexpPropertyMatcher matcher2 = new RegexpPropertyMatcher("p1", "C.*");
341        assertTrue(matcher2.getName().equals("p1"));
342        assertTrue(matcher2.match("C"));
343        assertTrue(matcher2.match("Casdf"));
344        assertFalse(matcher2.match(" C"));
345        assertFalse(matcher2.match("c"));
346        assertFalse(matcher2.match("asdfC"));
347
348        try {
349            matcher2.match(null);
350            fail();
351        } catch(IllegalArgumentException e) {}
352
353        try {
354            new RegexpPropertyMatcher("p1", "**");
355            fail();
356        } catch(IllegalArgumentException e) {}
357
358        try {
359            new RegexpPropertyMatcher(null, "1");
360            fail();
361        } catch(IllegalArgumentException e) {}
362
363        try {
364            new RegexpPropertyMatcher("p1", null);
365            fail();
366        } catch(IllegalArgumentException e) {}
367
368        final InvertPropertyMatcher matcher3 = new InvertPropertyMatcher(matcher);
369        assertTrue(matcher3.getName().equals("p1"));
370        assertFalse(matcher3.match("1"));
371        assertTrue(matcher3.match("2"));
372        assertFalse(matcher3.match(null));
373    }
374
375    public void testToString() {
376        Properties p = new Properties();
377        assertEquals(p.toString(), "[]");
378
379        p.setProperty("p1", "1");
380        assertEquals(p.toString(), "[p1=1]");
381
382        Properties p2 = new Properties();
383        p2.setProperty("p1", "1");
384        p2.setProperty("p2", "2");
385        assertEquals(p2.toString(), "[p1=1, p2=2]");
386
387        Properties p3 = new Properties();
388        p3.setProperty("p2", "2");
389        p3.setProperty("p1", "1");
390        assertEquals(p3.toString(), "[p1=1, p2=2]");
391
392        p3.setProperty("p0", "0");
393        assertEquals(p3.toString(), "[p0=0, p1=1, p2=2]");
394
395        p2.setProperty("p1", null);
396        assertEquals(p2.toString(), "[p2=2]");
397
398        p2.setProperty("p2", null);
399        assertEquals(p2.toString(), "[]");
400    }
401}
402