1 19 package org.netbeans.jmi.javamodel.codegen.MethodTest1; 20 21 import java.lang.reflect.Modifier ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import org.netbeans.jmi.javamodel.JavaClass; 26 import org.netbeans.jmi.javamodel.JavaModelPackage; 27 import org.netbeans.jmi.javamodel.Method; 28 import org.netbeans.jmi.javamodel.MultipartId; 29 import org.netbeans.jmi.javamodel.TypeParameter; 30 import org.netbeans.jmi.javamodel.codegen.Utility; 31 import org.netbeans.junit.NbTestSuite; 32 import junit.textui.TestRunner; 33 import org.netbeans.jmi.javamodel.Parameter; 34 import org.netbeans.jmi.javamodel.Type; 35 import org.netbeans.jmi.javamodel.codegen.CodegenTestCase; 36 import org.netbeans.jmi.javamodel.codegen.Utility; 37 38 43 public class MethodTest1 extends CodegenTestCase { 44 45 46 public MethodTest1(String name) { 47 super(name, "MethodTest1"); 48 } 49 50 public static NbTestSuite suite() { 51 NbTestSuite suite = new NbTestSuite(); 52 suite.addTest(new MethodTest1("testMethodModifiers")); 53 suite.addTest(new MethodTest1("testMethodName")); 54 suite.addTest(new MethodTest1("testMethodParameters")); 55 suite.addTest(new MethodTest1("testMethodParameterChange")); 56 suite.addTest(new MethodTest1("testMethodThrows")); 57 suite.addTest(new MethodTest1("testMethodReturnType")); 58 suite.addTest(new MethodTest1("testMethodBody")); 59 suite.addTest(new MethodTest1("testParameterizedMethod")); 60 suite.addTest(new MethodTest1("testAddRemoveInOneTrans")); 61 return suite; 62 } 63 64 JavaClass clazz; 65 JavaModelPackage pkg; 66 Method[] method = new Method[10]; 67 68 protected void setUp() { 69 clazz = Utility.findClass("org.netbeans.test.codegen.MethodTest1"); 70 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 71 int i = 0; 73 for (Iterator mIt = clazz.getFeatures().iterator(); mIt.hasNext(); i++) { 74 Object temp = mIt.next(); 75 if (temp instanceof JavaClass) { 76 method[i] = (Method) ((JavaClass) temp).getFeatures().get(0); 77 } else { 78 method[i] = (Method) temp; 79 } 80 } 81 } 82 83 87 public void testMethodModifiers() { 88 boolean fail = true; 89 Utility.beginTrans(true); 90 try { 91 int modifiers = method[0].getModifiers(); 92 modifiers &= ~Modifier.PUBLIC; 93 modifiers |= Modifier.STATIC; 94 modifiers |= Modifier.PRIVATE; 95 method[0].setModifiers(modifiers); 96 fail = false; 97 } 98 finally { 99 Utility.endTrans(fail); 100 } 101 makeDiff("testMethodModifiers"); 102 } 103 104 107 public void testMethodName() { 108 boolean fail = true; 109 Utility.beginTrans(true); 110 try { 111 method[1].setName("druhaMetoda"); 112 fail = false; 113 } 114 finally { 115 Utility.endTrans(fail); 116 } 117 makeDiff("testMethodName"); 118 } 119 120 123 public void testMethodParameters() { 124 boolean fail = true; 125 Utility.beginTrans(true); 126 try { 127 List parameters = method[2].getParameters(); 128 Parameter par = (Parameter) parameters.remove(0); 129 parameters.add(par); 130 fail = false; 131 } 132 finally { 133 Utility.endTrans(fail); 134 } 135 makeDiff("testMethodParameters"); 136 } 137 138 141 public void testMethodParameterChange() { 142 boolean fail = true; 143 Utility.beginTrans(true); 144 try { 145 List parameters = method[3].getParameters(); 146 Parameter par = (Parameter) parameters.get(0); 147 par.setName("aParNewName"); 148 fail = false; 149 } 150 finally { 151 Utility.endTrans(fail); 152 } 153 makeDiff("testMethodParameterChange"); 154 } 155 156 159 public void testMethodThrows() { 160 boolean fail = true; 161 Utility.beginTrans(true); 162 try { 163 List exceptions = method[4].getExceptionNames(); 164 exceptions.remove(0); 165 exceptions.add(pkg.getMultipartId().createMultipartId("java.lang.IllegalMonitorStateException", null, null)); 166 fail = false; 167 } 168 finally { 169 Utility.endTrans(fail); 170 } 171 makeDiff("testMethodThrows"); 172 } 173 174 177 public void testMethodReturnType() { 178 boolean fail = true; 179 Utility.beginTrans(true); 180 try { 181 method[5].setTypeName(pkg.getMultipartId().createMultipartId("String", null, null)); 182 fail = false; 183 } 184 finally { 185 Utility.endTrans(fail); 186 } 187 makeDiff("testMethodReturnType"); 188 } 189 190 193 public void testMethodBody() { 194 boolean fail = true; 195 Utility.beginTrans(true); 196 try { 197 method[6].setModifiers(Modifier.PUBLIC); 198 method[6].setBody(pkg.getStatementBlock().createStatementBlock()); 199 method[7].setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT); 200 method[7].setBody(null); 201 method[8].getBody(); 202 method[8].getExceptionNames().add(pkg.getMultipartId().createMultipartId("java.io.IOException", null, null)); 203 fail = false; 204 } 205 finally { 206 Utility.endTrans(fail); 207 } 208 makeDiff("testMethodBody"); 209 } 210 211 public void testParameterizedMethod() { 212 boolean fail = true; 213 Utility.beginTrans(true); 214 try { 215 TypeParameter typePar = pkg.getTypeParameter().createTypeParameter(); 216 typePar.setName("T"); 217 Parameter param = pkg.getParameter().createParameter(); 219 param.setName("cl"); 220 param.setType(typePar); 221 223 Method method = pkg.getMethod().createMethod( 224 "getIt", 225 Collections.EMPTY_LIST, 226 Modifier.PUBLIC, 227 "new method", 228 null, null, "return null;", Collections.singletonList(typePar), Collections.singletonList(param), null, null, 0); 236 method.setType(typePar); 237 fail = false; 238 clazz.getContents().add(method); 239 } 240 finally { 241 Utility.endTrans(fail); 242 } 243 makeDiff("testParameterizedMethod"); 244 } 245 246 public void testAddRemoveInOneTrans() { 247 boolean fail = true; 248 Utility.beginTrans(true); 249 try { 250 TypeParameter typePar = pkg.getTypeParameter().createTypeParameter(); 251 typePar.setName("T2"); 252 Parameter param = pkg.getParameter().createParameter(); 254 param.setName("newPar"); 255 param.setType(typePar); 256 258 Method method = pkg.getMethod().createMethod( 259 "setIt", 260 Collections.EMPTY_LIST, 261 Modifier.PUBLIC, 262 "new javadoc", 263 null, null, "return null;", Collections.singletonList(typePar), Collections.singletonList(param), null, null, 0); 271 method.setType(typePar); 272 fail = false; 273 List features = clazz.getContents(); 274 features.remove(features.size()-1); 275 features.add(method); 276 } 277 finally { 278 Utility.endTrans(fail); 279 } 280 makeDiff("testAddRemoveInOneTrans"); 281 } 282 283 286 public static void main(String [] args) { 287 TestRunner.run(suite()); 288 } 289 } 290 | Popular Tags |