1 51 package org.apache.avalon.framework.parameters.test; 52 53 import java.io.ByteArrayInputStream ; 54 import java.util.Properties ; 55 56 import junit.framework.TestCase; 57 58 import org.apache.avalon.framework.configuration.Configuration; 59 import org.apache.avalon.framework.configuration.ConfigurationException; 60 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 61 import org.apache.avalon.framework.parameters.ParameterException; 62 import org.apache.avalon.framework.parameters.Parameters; 63 64 71 public class ParameterTestCase 72 extends TestCase 73 { 74 private static final String EOL = "\n"; 75 76 public ParameterTestCase( final String name ) 77 { 78 super( name ); 79 } 80 81 public void testRemoveParameter() 82 { 83 final Parameters parameters = new Parameters(); 84 parameters.setParameter( "key1", "value1" ); 85 assertEquals( 1, parameters.getNames().length ); 86 parameters.setParameter( "key1", null ); 87 assertTrue( ! parameters.isParameter( "key1" ) ); 88 assertEquals( 0, parameters.getNames().length ); 89 } 90 91 public void testIsParameter() 92 { 93 final Parameters parameters = new Parameters(); 94 parameters.setParameter( "key1", "value1" ); 95 assertTrue( parameters.isParameter( "key1" ) ); 96 assertTrue( ! parameters.isParameter( "key2" ) ); 97 } 98 99 public void testGetParameter() 100 { 101 final Parameters parameters = new Parameters(); 102 parameters.setParameter( "key1", "value1" ); 103 104 try 105 { 106 assertEquals( "value1", parameters.getParameter( "key1" ) ); 107 } 108 catch ( final ParameterException pe ) 109 { 110 fail( pe.getMessage() ); 111 } 112 113 try 114 { 115 parameters.getParameter( "key2" ); 116 fail( "Not inserted parameter 'key2' exists" ); 117 } 118 catch( final ParameterException pe ) 119 { 120 } 122 123 assertEquals( "value1", parameters.getParameter( "key1", "value1-1" ) ); 124 125 assertEquals( "value2", parameters.getParameter( "key2", "value2" ) ); 126 } 127 128 public void testFromConfiguration() 129 { 130 final ByteArrayInputStream confInput = new ByteArrayInputStream ( ( 131 "<?xml version=\"1.0\"?>" + EOL + 132 "<test>" + EOL + 133 "<parameter name=\"key1\" value=\"value1\"/>" + EOL + 134 "<parameter name=\"key2\" value=\"value2\"/>" + EOL + 135 "<parameter name=\"key3\" value=\"value3\"/>" + EOL + 136 "</test>" ).getBytes() ); 137 138 try 139 { 140 final DefaultConfigurationBuilder builder = 141 new DefaultConfigurationBuilder(); 142 final Configuration configuration = builder.build( confInput ); 143 144 final Parameters parameters = 145 Parameters.fromConfiguration( configuration ); 146 147 assertEquals( "value1", parameters.getParameter( "key1" ) ); 148 assertEquals( "value2", parameters.getParameter( "key2" ) ); 149 assertEquals( "value3", parameters.getParameter( "key3" ) ); 150 } 151 catch ( final ConfigurationException ce ) 152 { 153 fail( "Converting failed: " + ce.getMessage() ); 154 } 155 catch ( final Exception e ) 156 { 157 fail( e.getMessage() ); 158 } 159 } 160 161 public void testFromProperties() 162 { 163 final Properties properties = new Properties (); 164 properties.put( "key1", "value1" ); 165 properties.put( "key2", "value2" ); 166 properties.put( "key3", "value3" ); 167 168 final Parameters parameters = Parameters.fromProperties( properties ); 169 170 try 171 { 172 assertEquals( "value1", parameters.getParameter( "key1" ) ); 173 assertEquals( "value2", parameters.getParameter( "key2" ) ); 174 assertEquals( "value3", parameters.getParameter( "key3" ) ); 175 } 176 catch ( final ParameterException pe ) 177 { 178 fail( pe.getMessage() ); 179 } 180 } 181 } 182 | Popular Tags |