1 23 24 package com.sun.enterprise.cli.framework; 25 26 32 import java.util.ArrayList ; 33 import java.util.Arrays ; 34 import java.util.HashMap ; 35 import java.util.List ; 36 import java.util.Properties ; 37 import java.util.Vector ; 38 import junit.framework.*; 39 import junit.textui.TestRunner; 40 41 46 47 52 53 public class CommandTest extends TestCase { 54 public void testGetDelimeterIndex(){ 55 assertEquals(3, testCommand.getDelimeterIndex("0123", "3", 0)); 56 } 57 58 public void testLocalizedString(){ 59 assertEquals("Key not found", testCommand.getLocalizedString("a", (Object []) null)); 60 assertEquals("Key not found", testCommand.getLocalizedString("InvalidCommand")); 61 } 62 63 public void testGetSetProperty2(){ 64 testCommand.setProperties(new Properties ()); 65 testCommand.setProperty("prop", "value"); 66 assertEquals("value", testCommand.getProperty("prop")); 67 } 68 69 public void testGetSetProperty1(){ 70 testCommand.setProperties(new Properties ()); 71 testCommand.setProperty("prop", "value"); 72 assertEquals("value", testCommand.getProperty("prop")); 73 } 74 75 76 public void testGetSetProperty(){ 77 testCommand.setProperties(new Properties ()); 78 assertEquals(null, testCommand.getProperty("prop")); 79 } 80 81 82 public void testSetGetIntegerOptions(){ 83 testCommand.setOption("number 1", "1"); 84 assertEquals(1, testCommand.getIntegerOption("number 1")); 85 } 86 87 public void testSetBooleanOptionsList(){ 88 final List l = new ArrayList (); 89 testCommand.setBooleanOptions(l); 90 l.add("bool"); 91 testCommand.setOption("bool", "fargle"); 92 assertEquals("sampleCommand --bool=fargle", testCommand.toString()); 93 } 94 95 96 public void testBooleanOptions(){ 97 assertTrue(!testCommand.getBooleanOption("bool")); 98 testCommand.setOption("bool", "true"); 99 assertEquals("true", testCommand.getOption("bool")); 100 } 101 102 103 public void testGetSetOptions(){ 104 testCommand.setOption("key", "value"); 105 assertEquals("value", testCommand.getOption("key")); 106 assertNull(testCommand.getOption("foo")); 107 108 } 109 110 public void testConstructorAndAccessors(){ 111 assertEquals("sampleCommand", testCommand.getName()); 112 assertTrue(testCommand.getOperands().isEmpty()); 113 assertTrue(testCommand.getOptions().isEmpty()); 114 assertEquals(null, testCommand.getUsageText()); 115 assertNull(testCommand.getProperties("")); 116 assertEquals("Key not found", testCommand.getLocalizedString("fargle")); 117 assertEquals("sampleCommand", testCommand.toString()); 118 119 } 120 121 public void testReplacePatternOperands() throws Exception { 123 final String [] operands = new String [] {"abc=xyz", "$123", "#123", "", "//////", "abc:xyz=123:456$888=\"99:99\"", "foo-bar"}; 124 testCommand.setOperands(new Vector (Arrays.asList(operands))); 125 assertEquals("first operand should be abc=xyz", "abc=xyz", testCommand.replacePattern("{#1}")); 126 assertEquals("second operand should be $123", "$123", testCommand.replacePattern("{#2}")); 127 assertEquals("third operand should be #123", "#123", testCommand.replacePattern("{#3}")); 128 assertEquals("fourth operand should be empty", null, testCommand.replacePattern("{#4}")); 129 assertEquals("fifth operand should be //////", "//////", testCommand.replacePattern("{#5}")); 130 assertEquals("sixth operand should be abc:xyz=123:456$888=\"99:99\"", 131 "abc:xyz=123:456$888=\"99:99\"", testCommand.replacePattern("{#6}")); 132 assertEquals("seventh operand should be foo-bar", "foo-bar", testCommand.replacePattern("{#7}")); 133 assertEquals("all operands should be", 134 "7=foo-bar," + 135 "6=abc:xyz=123:456$888=\"99:99\"," + 136 "5=//////,"+ 137 "4=,"+ 138 "3=#123,"+ 139 "2=$123,"+ 140 "1=abc=xyz", testCommand.replacePattern("7={#7},"+ 141 "6={#6},"+ 142 "5={#5},"+ 143 "4={#4},"+ 144 "3={#3},"+ 145 "2={#2},"+ 146 "1={#1}")); 147 } 148 149 150 public void testReplacePatternOptions() throws Exception { 152 HashMap options = new HashMap (); 153 options.put("user", "admin"); 154 options.put("password", "adminadmin"); 155 options.put("host", "fuyako"); 156 options.put("port", "4848"); 157 options.put("property", "--D:${abc.xyz}=123:\"456\":88:99$01"); 158 options.put("empty", ""); 159 options.put("foo-bar", "foobar"); 160 testCommand.setOptions(options); 161 162 assertEquals("first option should be admin", "admin", testCommand.replacePattern("{$user}")); 163 assertEquals("second option should be adminadmin", "adminadmin", testCommand.replacePattern("{$password}")); 164 assertEquals("third option should be fuyako", "fuyako", testCommand.replacePattern("{$host}")); 165 assertEquals("fourth option should be 4848", "4848", testCommand.replacePattern("{$port}")); 166 assertEquals("fifth option should be --D:${abc.xyz}=123:\"456\":88:99$01", 167 "--D:${abc.xyz}=123:\"456\":88:99$01", testCommand.replacePattern("{$property}")); 168 assertEquals("sixth option should be empty", null, testCommand.replacePattern("{$empty}")); 169 assertEquals("seventh option should be foobar", "foobar", testCommand.replacePattern("{$foo-bar}")); 170 assertEquals("all options", "user=admin,password=adminadmin,host=fuyako,port=4848,"+ 171 "property=--D:${abc.xyz}=123:\"456\":88:99$01,empty=,foo-bar=foobar", 172 testCommand.replacePattern("user={$user},password={$password},host={$host},"+ 173 "port={$port},property={$property},empty={$empty},foo-bar={$foo-bar}")); 174 175 } 176 177 178 public CommandTest(String name){ 179 super(name); 180 } 181 182 Command testCommand = null; 183 184 protected void setUp() { 185 testCommand = new Command() { 186 public void runCommand() 187 throws CommandException, CommandValidationException 188 { 189 } 190 public boolean validateOptions() throws CommandValidationException 191 { 192 return true; 193 } 194 }; 195 testCommand.setName("sampleCommand"); 196 } 197 198 199 200 protected void tearDown() { 201 } 202 203 private void nyi(){ 204 fail("Not Yet Implemented"); 205 } 206 207 public static Test suite(){ 208 TestSuite suite = new TestSuite(CommandTest.class); 209 return suite; 210 } 211 212 public static void main(String args[]) throws Exception { 213 final TestRunner runner= new TestRunner(); 214 final TestResult result = runner.doRun(CommandTest.suite(), false); 215 System.exit(result.errorCount() + result.failureCount()); 216 } 217 } 218 219 | Popular Tags |