1 19 package org.netbeans.jmi.javamodel.rollback; 20 21 import java.io.IOException ; 22 import java.lang.reflect.Modifier ; 23 import java.util.Collections ; 24 import javax.jmi.reflect.ConstraintViolationException; 25 import junit.textui.TestRunner; 26 import org.netbeans.api.mdr.MDRepository; 27 import org.netbeans.jmi.javamodel.*; 28 import org.netbeans.jmi.javamodel.codegen.Utility; 29 import org.netbeans.junit.NbTestCase; 30 import org.netbeans.junit.NbTestSuite; 31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 32 import org.openide.cookies.SaveCookie; 33 import org.openide.filesystems.FileObject; 34 import org.openide.filesystems.FileStateInvalidException; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.loaders.DataObject; 37 import org.openide.src.ClassElement; 38 import org.openide.src.FieldElement; 39 import org.openide.src.Identifier; 40 41 42 45 public class RollbackTest extends NbTestCase { 46 47 private static final String CLASS_1 = "ClassOne"; 48 private static final String CLASS_2 = "ClassTwo"; 49 private static final String CLASS_3 = "ClassThree"; 50 51 private static final String [] resultFiles = { 52 CLASS_1, 53 CLASS_2, 54 CLASS_3 55 }; 56 57 private String JAVA_EXT = "java"; 58 private String PASS_EXT = "pass"; 59 private String PATH_PREFIX = "org/netbeans/test/rollback/"; 60 private String PACKAGE_PREFIX = "org.netbeans.test.rollback."; 61 62 private TypeClass typeProxy; 63 64 65 public RollbackTest(String name) { 66 super(name); 67 } 68 69 public static NbTestSuite suite() { 70 NbTestSuite suite = new NbTestSuite(); 71 suite.addTest(new RollbackTest("testRollback")); 72 return suite; 73 } 74 75 protected void setUp() { 76 JavaClass temp = Utility.findClass(PACKAGE_PREFIX + CLASS_1); 77 typeProxy = ((JavaModelPackage) temp.refImmediatePackage()).getType(); 78 } 79 80 private void saveFiles() { 81 for (int x = 0; x < resultFiles.length; x++) { 82 String fileName = PATH_PREFIX + resultFiles[x] + '.' + JAVA_EXT; 83 try { 84 FileObject fileObj = FileUtil.toFileObject(Utility.getFile(getDataDir(), fileName)); 85 DataObject dobj = DataObject.find(fileObj); 86 SaveCookie sc = (SaveCookie)dobj.getCookie(SaveCookie.class); 87 if (sc != null) 88 sc.save(); 89 } catch (Exception e) { 90 fail(e.getMessage()); 91 } 92 } 93 } 94 95 private void compareFiles() { 96 saveFiles(); 97 try { 98 for (int x = 0; x < resultFiles.length; x++) { 99 String fileName = PATH_PREFIX + resultFiles[x] + '.' + JAVA_EXT; 100 int index = resultFiles[x].lastIndexOf('/') + 1; 101 String passName = resultFiles[x].substring(index) + '.' + PASS_EXT; 102 assertFile(Utility.getFile(getDataDir(), fileName), getGoldenFile(passName), getWorkDir()); 103 } 104 } catch (FileStateInvalidException e) { 105 fail(e.getMessage()); 106 } catch (IOException e) { 107 fail(e.getMessage()); 108 } 109 } 110 111 public void testRollback() { 112 boolean failed; 113 Method method; 114 Field field; 115 JavaClass jc_one = (JavaClass)typeProxy.resolve(PACKAGE_PREFIX + CLASS_1); 116 JavaClass jc_two = (JavaClass)typeProxy.resolve(PACKAGE_PREFIX + CLASS_2); 117 JavaClass jc_three = (JavaClass)typeProxy.resolve(PACKAGE_PREFIX + CLASS_3); 118 119 MDRepository repository = JavaMetamodel.getManager().getDefaultRepository(); 120 121 method = jc_one.getMethod("go", Collections.EMPTY_LIST, false); 122 field = jc_two.getField("colour", false); 123 failed = true; 124 repository.beginTrans(true); 125 try { 126 field.setName("color"); 127 method.setBodyText("char x;\nSystem.out.println(\"***\");\nx = 'c;System.out.println(x);"); 129 failed = false; 130 } catch (Exception e) { 131 fail(e.getMessage()); 132 } finally { 133 try { 134 repository.endTrans(failed); 135 } catch (ConstraintViolationException e) { 136 } 138 } 139 compareFiles(); 140 141 failed = true; 142 repository.beginTrans(true); 143 try { 144 FileObject file1 = FileUtil.toFileObject(Utility.getFile(getDataDir(), PATH_PREFIX + resultFiles[2] + '.' + JAVA_EXT)); 145 ClassElement clsElem = ClassElement.forName(PACKAGE_PREFIX + CLASS_3, file1); 146 FieldElement fieldElem = new FieldElement(); 147 fieldElem.setName(Identifier.create("newField")); 148 fieldElem.setType(clsElem.getField(Identifier.create("numero")).getType()); 149 clsElem.addField(fieldElem); 150 151 FileObject file2 = FileUtil.toFileObject(Utility.getFile(getDataDir(), PATH_PREFIX + resultFiles[0] + '.' + JAVA_EXT)); 152 clsElem = ClassElement.forName(PACKAGE_PREFIX + CLASS_1, file2); 153 fieldElem = clsElem.getField(Identifier.create("defaultInstance")); 154 fieldElem.setModifiers(Modifier.PUBLIC); 155 156 jc_two.getField("name", false).getTypeName().setName("$><...$"); 158 failed = false; 159 } catch (Exception e) { 160 fail(e.getMessage()); 161 } finally { 162 try { 163 repository.endTrans(failed); 164 } catch (ConstraintViolationException e) { 165 } 167 } 168 compareFiles(); 169 } 170 171 174 public static void main(String [] args) { 175 TestRunner.run(suite()); 176 } 177 } 178 | Popular Tags |