1 46 47 package org.codehaus.groovy.classgen; 48 49 import groovy.lang.Closure; 50 import groovy.lang.MetaClass; 51 52 import java.util.Iterator ; 53 import java.util.List ; 54 import java.util.Map ; 55 56 import org.codehaus.groovy.runtime.InvokerHelper; 57 58 65 public class DumpClass { 66 67 private String bar; 68 private String result; 69 private Object x; 70 71 public String getResult() { 72 return result; 73 } 74 75 public String getBar() { 76 return bar; 77 } 78 79 public void setBar(String value) { 80 this.bar = value; 81 } 82 83 public void iterateOverList() { 84 List list = InvokerHelper.createList(new Object [] { "a", "b", "c" }); 89 for (Iterator iter = InvokerHelper.asIterator(list); iter.hasNext();) { 90 Object i = iter.next(); 91 InvokerHelper.invokeMethod(System.out, "println", i); 92 } 93 } 94 95 public void iterateOverMap() { 96 Map map = InvokerHelper.createMap(new Object [] { "a", "x", "b", "y", "c", "z" }); 97 for (Iterator iter = InvokerHelper.asIterator(map); iter.hasNext();) { 98 Object i = iter.next(); 99 InvokerHelper.invokeMethod(System.out, "println", i); 100 } 101 } 102 103 public void printValues(Object collection) { 104 for (Iterator iter = InvokerHelper.asIterator(collection); iter.hasNext();) { 105 Object foo = iter.next(); 106 InvokerHelper.invokeMethod(System.out, "println", foo); 107 } 108 } 109 110 public Object emptyMethod() { 111 return null; 112 } 113 114 public void emptyVoidMethod() { 115 } 116 117 122 public void testGroovyAssertion2() { 123 if (InvokerHelper.compareEqual(bar, "foo")) { 124 } 125 else { 126 InvokerHelper.assertFailed("expression", "message"); 127 } 128 129 bar = "abc"; 130 131 if (InvokerHelper.compareNotEqual(bar, "foo")) { 132 } 133 else { 134 InvokerHelper.assertFailed("expression", "not null"); 135 } 136 137 if (InvokerHelper.compareEqual(bar, "abc")) { 138 } 139 else { 140 InvokerHelper.assertFailed("expression", "not null"); 141 } 142 } 143 144 public void testFieldSet() { 145 if (InvokerHelper.compareNotEqual(x, "foo")) { 146 } 147 else { 148 InvokerHelper.assertFailed("expression", "message"); 149 } 150 151 x = "foo"; 152 153 if (InvokerHelper.compareEqual(x, "foo")) { 154 } 155 else { 156 InvokerHelper.assertFailed("expression", "message"); 157 } 158 if (InvokerHelper.compareNotEqual(x, "foo")) { 159 } 160 else { 161 InvokerHelper.assertFailed("expression", "message"); 162 } 163 } 164 public void assertFailed() { 165 StringBuffer buffer = new StringBuffer ("Exception: "); 166 buffer.append("x = "); 167 buffer.append(x); 168 InvokerHelper.assertFailed(buffer, "message"); 169 } 170 171 public void setLocalVar() { 172 Object x = null; 173 Object i = null; 174 for (Iterator iter = InvokerHelper.asIterator(InvokerHelper.createRange(new Integer (0), new Integer (10), true)); 175 iter.hasNext(); 176 ) { 177 i = iter.next(); 178 x = i; 179 } 180 } 181 182 public void testGroovyAssertion() { 183 x = "abc"; 184 if (InvokerHelper.compareEqual(x, "foo")) { 185 } 186 else { 187 InvokerHelper.assertFailed("expression", "message"); 188 } 189 } 190 191 public void doPlus() { 192 Object z = "abcd"; 193 x = InvokerHelper.invokeMethod(z, "length", null); 194 } 195 196 public void setBoolean() { 197 x = Boolean.TRUE; 198 } 199 200 public void tryCatch() { 201 try { 202 InvokerHelper.invokeMethod(this, "testGroovyAssertion", null); 203 } 204 catch (AssertionError e) { 205 InvokerHelper.invokeMethod(this, "onException", e); 206 } 207 finally { 208 InvokerHelper.invokeMethod(this, "finallyBlock", null); 209 } 210 InvokerHelper.invokeMethod(this, "afterTryCatch", null); 211 } 212 213 public void doPrintln() { 214 Object value = InvokerHelper.getProperty(System .class, "out"); 215 InvokerHelper.invokeMethod(value, "println", "Hello"); 216 } 217 218 public void doClosure() { 219 x = new Closure(this) { 220 public Object call(Object arguments) { 221 System.out.println(); 222 return null; 223 } 224 225 public MetaClass getMetaClass() { 226 return null; 227 } 228 229 public void setMetaClass(MetaClass metaClass) { 230 } 231 }; 232 } 233 234 public Object ifDemo() { 235 if (InvokerHelper.compareEqual(bar, "abc")) { 236 return Boolean.TRUE; 237 } 238 else { 239 return Boolean.FALSE; 240 } 241 } 242 243 public void testWhile() { 244 while (InvokerHelper.compareEqual(bar, "abc")) { 245 System.out.println("Hello"); 246 } 247 } 248 249 public void testDoWhile() { 250 do { 251 System.out.println("Hello"); 252 } 253 while (InvokerHelper.compareEqual(bar, "abc")); 254 } 255 } 256 | Popular Tags |