001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: /*
043: * CodeGenerationTestCase.java
044: *
045: * Created on February 2, 2007, 6:04 PM
046: *
047: * To change this template, choose Tools | Template Manager
048: * and open the template in the editor.
049: */
050:
051: package org.netbeans.test.uml.codegen;
052:
053: import org.netbeans.jellytools.EditorOperator;
054: import org.netbeans.test.umllib.project.JavaProject;
055: import org.netbeans.test.umllib.project.Project;
056: import org.netbeans.test.umllib.project.elem.IJavaElem;
057: import org.netbeans.test.umllib.project.elem.impl.PredefinedType;
058: import org.netbeans.test.umllib.testcases.UMLTestCase;
059: import org.netbeans.test.umllib.util.Debug;
060: import org.netbeans.test.umllib.util.PopupConstants;
061: import org.netbeans.test.umllib.util.Utils;
062: import org.netbeans.test.umllib.util.Utils;
063: import org.netbeans.jellytools.nodes.Node;
064: import org.netbeans.test.umllib.project.ProjectType;
065: import org.netbeans.test.umllib.project.UMLProject;
066: import org.netbeans.test.umllib.project.elem.IPackageElem;
067: import org.netbeans.test.umllib.project.elem.impl.PackageElem;
068: import org.netbeans.test.umllib.project.verifier.TestVerifier;
069:
070: /**
071: *
072: * @author Alexandr Scherbatiy
073: */
074:
075: public class CodeGenerationTestCase extends UMLTestCase {
076:
077: protected static final String UML_PROJECT_NAME = "CodeGenerationProject-Model";
078: protected static final String JAVA_PROJECT_NAME = "CodeGenerationProject-Java";
079:
080: private static int id = 0;
081:
082: protected IPackageElem java = new PackageElem("java");
083: protected IPackageElem javaLang = new PackageElem("lang", java);
084: protected IPackageElem javaIO = new PackageElem("io", java);
085:
086: protected IJavaElem OBJECT = PredefinedType
087: .getClassElem(PredefinedType.JAVA_LANG_OBJECT);
088: protected IJavaElem STRING = PredefinedType
089: .getClassElem(PredefinedType.JAVA_LANG_STRING);
090: protected IJavaElem CLASS = PredefinedType
091: .getClassElem(PredefinedType.JAVA_LANG_CLASS);
092:
093: protected IJavaElem INTEGER = PredefinedType
094: .getClassElem(PredefinedType.JAVA_LANG_INTEGER);
095:
096: private static boolean openProjectFlag = false;
097:
098: /** Creates a new instance of CodeGenerationTestCase */
099: public CodeGenerationTestCase(String name) {
100: super (name);
101: }
102:
103: protected void setUp() throws Exception {
104: super .setUp();
105: Debug.showLog(this );
106: Project.openProject(XTEST_PROJECT_DIR
107: + "/Projects-CodeGeneration/" + UML_PROJECT_NAME);
108: }
109:
110: protected void tearDown() throws Exception {
111: super .tearDown();
112: Utils.tearDown();
113: }
114:
115: protected void compileJavaProject(String projectName) {
116:
117: compileJavaProject(projectName, 0);
118: }
119:
120: protected void compileJavaProject(String projectName, int bugNumber) {
121:
122: Debug.showLog(this );
123:
124: System.out.println("Test Compilation!!!");
125: JavaProject javaProject = new JavaProject(projectName);
126: javaProject.build();
127: //sleep(6000);
128:
129: int maxTime = 150;
130: //System.out.println("start: " + System.currentTimeMillis());
131: while (true) {
132: JavaProject.Output output = javaProject.getOutput();
133: String outputText = output.getText();
134:
135: assertNotNull("Output is Null!!!", output);
136: //System.out.println("Output: \n" + output.getText());
137:
138: if (outputText.contains(JavaProject.BUILD_SUCCESSFUL)) {
139: break;
140: }
141:
142: if (outputText.contains(JavaProject.BUILD_FAILED)
143: || (maxTime < 0)) {
144: //System.out.println("end : " + System.currentTimeMillis());
145: if (bugNumber == 0) {
146: fail("Build Fail: " + outputText);
147: } else {
148: failByBug(bugNumber, "Build Fail: " + outputText);
149:
150: }
151: }
152: sleep(100);
153: maxTime--;
154: }
155:
156: }
157:
158: public static String getJavaProjectName() {
159: return JAVA_PROJECT_NAME + id;
160: }
161:
162: public static String getNextJavaProjectName() {
163: id++;
164: return getJavaProjectName();
165: }
166:
167: protected static void openProject(String projectPath) {
168:
169: System.out.println("WORK DIR: \"" + Utils.WORK_DIR + "\"");
170: System.out.println("path: \"" + projectPath + "\"");
171:
172: String fullPath = Utils.WORK_DIR + projectPath;
173: System.out.println("full path: \"" + fullPath + "\"");
174:
175: if (!openProjectFlag) {
176: Project.openProject(fullPath);
177: openProjectFlag = true;
178: }
179:
180: }
181:
182: protected EditorOperator getEditorOperator(IJavaElem javaElem) {
183:
184: JavaProject javaProject = new JavaProject(getJavaProjectName());
185:
186: String path = "";
187:
188: /*
189: StringTokenizer tokenizer = new StringTokenizer(javaElem.getFullName(),".");
190:
191: path += tokenizer.nextToken();
192:
193: while(tokenizer.hasMoreTokens()){
194: path += "|" + tokenizer.nextToken();
195: }
196: */
197:
198: path += javaElem.getPackage().getFullName();
199: path += "|" + javaElem.getName();
200:
201: path += ".java";
202:
203: System.out.println("path = \"" + path + "\"");
204: Node javaProjectNode = new Node(javaProject.getSrcNode(), path);
205:
206: javaProjectNode.performPopupActionNoBlock(PopupConstants.OPEN);
207: sleep(2000);
208: EditorOperator editorOperator = new EditorOperator(javaElem
209: .getName()
210: + ".java");
211:
212: return editorOperator;
213: }
214:
215: public void generateProject(String umlNodePath) {
216:
217: JavaProject javaProject = JavaProject.createProject(
218: getNextJavaProjectName(), ProjectType.JAVA_APPLICATION,
219: true, false);
220: UMLProject umlProject = new UMLProject(UML_PROJECT_NAME,
221: ProjectType.UML_JAVA_PLATFORM_MODEL);
222:
223: Node umlNode = new Node(umlProject.getProjectNode(), "Model|"
224: + umlNodePath);
225: umlProject.generateCode(umlNode, javaProject);
226:
227: compileJavaProject(getJavaProjectName());
228:
229: }
230:
231: public void assertVerification(TestVerifier testVerifier) {
232: testVerifier.verify();
233: assertTrue(testVerifier.getMessage(), testVerifier.getResult());
234: }
235:
236: protected void closeJavaProject() {
237: new JavaProject(getJavaProjectName()).close();
238: }
239:
240: protected void sleep(int time) {
241: try {
242: Thread.sleep(time);
243: } catch (InterruptedException ex) {
244: ex.printStackTrace();
245: }
246: }
247:
248: }
|