KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > properties > PropertyClassTest


1 package com.sslexplorer.properties;
2
3 /*
4  * SSL-Explorer
5  *
6  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public
18  * License along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */

21             
22
23 import java.util.Properties JavaDoc;
24
25 import junit.framework.Assert;
26
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30
31 import com.sslexplorer.boot.AbstractPropertyClass;
32 import com.sslexplorer.boot.AbstractPropertyKey;
33 import com.sslexplorer.boot.DefaultPropertyDefinition;
34 import com.sslexplorer.boot.PropertyClass;
35 import com.sslexplorer.boot.PropertyClassManager;
36
37
38 public class PropertyClassTest {
39     
40     final static String JavaDoc MEMORY = "Memory";
41
42     @Before
43     public void register() {
44         PropertyClassManager.getInstance().registerPropertyClass(new MemoryPropertyClassImpl());
45     }
46     
47     @After
48     public void deregister() {
49         PropertyClassManager.getInstance().deregisterPropertyClass(MEMORY);
50     }
51     
52     @Test
53     public void registerPropertyDefinitions() {
54         Assert.assertNotNull(PropertyClassManager.getInstance().getPropertyClass(MEMORY));
55         PropertyClass propertyClass = PropertyClassManager.getInstance().getPropertyClass(MEMORY);
56         Assert.assertTrue(!propertyClass.isDefinitionExists("con1"));
57         propertyClass.registerPropertyDefinition(new DefaultPropertyDefinition(
58             DefaultPropertyDefinition.TYPE_BOOLEAN,
59             "con1", "on,off", 10, "true", 50, true));
60         Assert.assertTrue(propertyClass.isDefinitionExists("con1"));
61     }
62     
63     @Test
64     public void deregisterPropertyDefinitions() {
65         Assert.assertNotNull(PropertyClassManager.getInstance().getPropertyClass(MEMORY));
66         PropertyClass propertyClass = PropertyClassManager.getInstance().getPropertyClass(MEMORY);
67         propertyClass.deregisterPropertyDefinition("con1");
68         Assert.assertTrue(!propertyClass.isDefinitionExists("con1"));
69     }
70     
71     class MemoryKey extends AbstractPropertyKey {
72         public MemoryKey(String JavaDoc name) {
73             super(name, MEMORY);
74         }
75     }
76     
77     class MemoryPropertyClassImpl extends AbstractPropertyClass {
78         
79         private Properties JavaDoc properties = new Properties JavaDoc();
80
81         public MemoryPropertyClassImpl() {
82             super(MEMORY, false);
83         }
84
85         public String JavaDoc retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
86             if(!properties.containsKey(key.getName())) {
87                 return getDefinition(key.getName()).getDefaultValue();
88             }
89             return properties.getProperty(key.getName());
90         }
91
92         public String JavaDoc storePropertyImpl(AbstractPropertyKey key, String JavaDoc value) throws IllegalArgumentException JavaDoc {
93             return properties.setProperty(key.getName(), value).toString();
94         }
95         
96     }
97 }
Popular Tags