1 19 20 package org.netbeans.jmi.javamodel.codegen; 21 22 import java.lang.reflect.Modifier ; 23 import java.util.Collections ; 24 import java.util.List ; 25 import java.io.IOException ; 26 import org.openide.filesystems.FileStateInvalidException; 27 import junit.textui.TestRunner; 28 import org.netbeans.junit.NbTestCase; 29 import org.netbeans.junit.NbTestSuite; 30 import org.netbeans.jmi.javamodel.JavaModelPackage; 31 import org.netbeans.jmi.javamodel.Method; 32 import org.netbeans.jmi.javamodel.Type; 33 import org.netbeans.jmi.javamodel.Import; 34 import org.netbeans.jmi.javamodel.Parameter; 35 import org.netbeans.jmi.javamodel.Resource; 36 import org.netbeans.jmi.javamodel.JavaClass; 37 import org.netbeans.jmi.javamodel.MultipartId; 38 import org.netbeans.jmi.javamodel.TypeReference; 39 40 44 public class ParametersTest extends NbTestCase { 45 46 47 public ParametersTest() { 48 super("ParametersTest"); 49 } 50 51 public static NbTestSuite suite() { 52 NbTestSuite suite = new NbTestSuite(ParametersTest.class); 53 return suite; 54 } 55 56 private JavaModelPackage pkg; 57 private JavaClass clazz; 58 59 protected void setUp() { 60 clazz = Utility.findClass("org.netbeans.test.codegen.ParametersTestClass"); 61 pkg = (JavaModelPackage) clazz.refImmediatePackage(); 62 } 63 64 67 public void testCreation() throws IOException , FileStateInvalidException { 68 boolean fail = true; 69 Utility.beginTrans(true); 70 try { 71 MultipartId typeName = pkg.getMultipartId().createMultipartId("boolean", null, null); 75 MultipartId parType = pkg.getMultipartId().createMultipartId("long", null, null); 76 Parameter par = pkg.getParameter().createParameter( 77 "varString", null, false, parType, 0, true ); 84 Method newMethod = pkg.getMethod().createMethod( 85 "tretiMethod", null, Modifier.PRIVATE, null, null, null, "return true;", null, 93 Collections.singletonList(par), Collections.EMPTY_LIST, typeName, 0 ); 98 Type type = pkg.getType().resolve("String"); 99 par.setType(type); 100 clazz.getFeatures().add(newMethod); 101 fail = false; 102 } finally { 103 Utility.endTrans(fail); 104 } 105 assertFile("File is not correctly generated.", 106 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ParametersTestClass.java"), 107 getGoldenFile("testCreation_ParametersTest.pass"), 108 getWorkDir() 109 ); 110 } 111 112 public void testSwapTypeChng() throws IOException , FileStateInvalidException { 113 boolean fail = true; 114 Utility.beginTrans(true); 115 try { 116 Method method = (Method) clazz.getContents().get(2); 117 List pars = method.getParameters(); 118 Parameter par0 = (Parameter) pars.remove(0); 119 Type type = pkg.getType().resolve("long"); 120 MultipartId typeName = pkg.getMultipartId().createMultipartId("short", null, null); 121 par0.setType(type); 122 par0.setTypeName(typeName); 123 pars.add(par0); 124 fail = false; 125 } finally { 126 Utility.endTrans(fail); 127 } 128 assertFile("File is not correctly generated.", 129 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ParametersTestClass.java"), 130 getGoldenFile("testSwapTypeChng_ParametersTest.pass"), 131 getWorkDir() 132 ); 133 } 134 135 public void testAddChng() throws IOException , FileStateInvalidException { 136 boolean fail = true; 137 Utility.beginTrans(true); 138 try { 139 Method method = (Method) clazz.getContents().get(1); 140 List pars = method.getParameters(); 141 MultipartId typeName = pkg.getMultipartId().createMultipartId("boolean", null, null); 142 MultipartId parType = pkg.getMultipartId().createMultipartId("long", null, null); 143 Parameter par = (Parameter) pkg.getParameter().createParameter( 144 "novyParameter", null, true, parType, 1, false ); 151 Type type = pkg.getType().resolve("long[][]"); 152 par.setTypeName(typeName); 153 par.setType(type); 154 pars.add(par); 155 fail = false; 156 } finally { 157 Utility.endTrans(fail); 158 } 159 assertFile("File is not correctly generated.", 160 Utility.getFile(getDataDir(), "org/netbeans/test/codegen/ParametersTestClass.java"), 161 getGoldenFile("testAddChng_ParametersTest.pass"), 162 getWorkDir() 163 ); 164 } 165 166 169 public static void main(String [] args) { 170 TestRunner.run(suite()); 171 } 172 } 173 | Popular Tags |