1 package org.incava.jagol; 2 3 import java.io.*; 4 import java.util.*; 5 import junit.framework.TestCase; 6 7 8 public class TestFloatOption extends TestCase 9 { 10 FloatOption opt = new FloatOption("fltopt", "this is the description of fltopt"); 11 TestFloatOption(String name)12 public TestFloatOption(String name) 13 { 14 super(name); 15 } 16 testDefaultNull()17 public void testDefaultNull() 18 { 19 assertEquals("fltopt", opt.getLongName()); 20 assertEquals("this is the description of fltopt", opt.getDescription()); 21 22 assertNull("default value", opt.getValue()); 23 } 24 testDefaultValue()25 public void testDefaultValue() 26 { 27 FloatOption opt = new FloatOption("fltopt", "this is the description of fltopt", new Float(10.12F)); 28 assertEquals("default value", new Float(10.12F), opt.getValue()); 29 } 30 testShortName()31 public void testShortName() 32 { 33 opt.setShortName('d'); 34 assertEquals('d', opt.getShortName()); 35 } 36 testSetFloatValue()37 public void testSetFloatValue() 38 { 39 opt.setValue(new Float(1.4F)); 40 assertEquals("option value", new Float(1.4F), opt.getValue()); 41 } 42 testSetInvalidValueString()43 public void testSetInvalidValueString() 44 { 45 try { 46 opt.setValue("fred"); 47 fail("exception expected"); 48 } 49 catch (InvalidTypeException ite) { 50 } 51 } 52 testSetInvalidValue()53 public void testSetInvalidValue() 54 { 55 try { 56 opt.setValue("1.4.8"); 57 fail("exception expected"); 58 } 59 catch (InvalidTypeException ite) { 60 } 61 } 62 testSetValidValueNegative()63 public void testSetValidValueNegative() 64 { 65 try { 66 opt.setValue("-9.87"); 67 assertEquals("option value", new Float(-9.87F), opt.getValue()); 68 } 69 catch (InvalidTypeException ite) { 70 fail("exception not expected"); 71 } 72 } 73 testSetValidValueNoLeadingZero()74 public void testSetValidValueNoLeadingZero() 75 { 76 try { 77 opt.setValue(".87"); 78 assertEquals("option value", new Float(0.87F), opt.getValue()); 79 } 80 catch (InvalidTypeException ite) { 81 fail("exception not expected"); 82 } 83 } 84 testSetFromArgsListEqual()85 public void testSetFromArgsListEqual() 86 { 87 List args = new ArrayList(); 88 try { 89 boolean processed = opt.set("--fltopt=4.44", args); 90 assertEquals("option processed", true, processed); 91 assertEquals("option value", new Float(4.44F), opt.getValue()); 92 assertEquals("argument removed from list", 0, args.size()); 93 } 94 catch (OptionException ite) { 95 fail("failure is not an option"); 96 } 97 } 98 testSetFromArgsListSeparateString()99 public void testSetFromArgsListSeparateString() 100 { 101 List args = new ArrayList(); 102 args.add("41.82"); 103 try { 104 boolean processed = opt.set("--fltopt", args); 105 assertEquals("option processed", true, processed); 106 assertEquals("option value", new Float(41.82F), opt.getValue()); 107 assertEquals("argument removed from list", 0, args.size()); 108 } 109 catch (OptionException ite) { 110 fail("failure is not an option"); 111 } 112 } 113 testSetFromLongerArgsListEqual()114 public void testSetFromLongerArgsListEqual() 115 { 116 List args = new ArrayList(); 117 args.add("--anotheropt"); 118 try { 119 boolean processed = opt.set("--fltopt=3.1415", args); 120 assertEquals("option processed", true, processed); 121 assertEquals("option value", new Float(3.1415F), opt.getValue()); 122 assertEquals("argument not removed from list", 1, args.size()); 123 } 124 catch (OptionException ite) { 125 fail("failure is not an option"); 126 } 127 } 128 testSetFromLongerArgsListSeparateString()129 public void testSetFromLongerArgsListSeparateString() 130 { 131 List args = new ArrayList(); 132 args.add("1234.567890"); 133 args.add("--anotheropt"); 134 try { 135 boolean processed = opt.set("--fltopt", args); 136 assertEquals("option processed", true, processed); 137 assertEquals("option value", new Float(1234.567890F), opt.getValue()); 138 assertEquals("argument removed from list", 1, args.size()); 139 } 140 catch (OptionException ite) { 141 fail("failure is not an option"); 142 } 143 } 144 testSetInvalidValueDanglingEquals()145 public void testSetInvalidValueDanglingEquals() 146 { 147 List args = new ArrayList(); 148 args.add("--anotheropt"); 149 try { 150 boolean processed = opt.set("--fltopt=", args); 151 fail("exception expected"); 152 } 153 catch (OptionException ite) { 154 } 155 } 156 157 } 158