KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > rollback > RollbackTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.jmi.javamodel.rollback;
20
21 import java.io.IOException JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23 import java.util.Collections JavaDoc;
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 /**
43  * @author Daniel Prusa
44  */

45 public class RollbackTest extends NbTestCase {
46     
47     private static final String JavaDoc CLASS_1 = "ClassOne";
48     private static final String JavaDoc CLASS_2 = "ClassTwo";
49     private static final String JavaDoc CLASS_3 = "ClassThree";
50     
51     private static final String JavaDoc[] resultFiles = {
52         CLASS_1,
53         CLASS_2,
54         CLASS_3
55     };
56     
57     private String JavaDoc JAVA_EXT = "java";
58     private String JavaDoc PASS_EXT = "pass";
59     private String JavaDoc PATH_PREFIX = "org/netbeans/test/rollback/";
60     private String JavaDoc PACKAGE_PREFIX = "org.netbeans.test.rollback.";
61
62     private TypeClass typeProxy;
63     
64     /** Need to be defined because of JUnit */
65     public RollbackTest(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc fileName = PATH_PREFIX + resultFiles[x] + '.' + JAVA_EXT;
100                 int index = resultFiles[x].lastIndexOf('/') + 1;
101                 String JavaDoc 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 JavaDoc 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             // set body containing a syntax error
128
method.setBodyText("char x;\nSystem.out.println(\"***\");\nx = 'c;System.out.println(x);");
129             failed = false;
130         } catch (Exception JavaDoc e) {
131             fail(e.getMessage());
132         } finally {
133             try {
134                 repository.endTrans(failed);
135             } catch (ConstraintViolationException e) {
136                 // expected syntax error
137
}
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             // set incorrect multipart id
157
jc_two.getField("name", false).getTypeName().setName("$><...$");
158             failed = false;
159         } catch (Exception JavaDoc e) {
160             fail(e.getMessage());
161         } finally {
162             try {
163                 repository.endTrans(failed);
164             } catch (ConstraintViolationException e) {
165                 // expected syntax error
166
}
167         }
168         compareFiles();
169     }
170     
171     /**
172      * @param args the command line arguments
173      */

174     public static void main(String JavaDoc[] args) {
175         TestRunner.run(suite());
176     }
177 }
178
Popular Tags