1 package com.sslexplorer.properties; 2 3 21 22 23 import java.util.Properties ; 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 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 name) { 73 super(name, MEMORY); 74 } 75 } 76 77 class MemoryPropertyClassImpl extends AbstractPropertyClass { 78 79 private Properties properties = new Properties (); 80 81 public MemoryPropertyClassImpl() { 82 super(MEMORY, false); 83 } 84 85 public String retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException { 86 if(!properties.containsKey(key.getName())) { 87 return getDefinition(key.getName()).getDefaultValue(); 88 } 89 return properties.getProperty(key.getName()); 90 } 91 92 public String storePropertyImpl(AbstractPropertyKey key, String value) throws IllegalArgumentException { 93 return properties.setProperty(key.getName(), value).toString(); 94 } 95 96 } 97 } | Popular Tags |