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: package org.netbeans.modules.uml.core.reverseengineering.reintegration.umlparsingintegratorTC;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.net.URL;
047: import java.util.StringTokenizer;
048:
049: import org.dom4j.Document;
050:
051: import org.netbeans.modules.uml.core.AbstractUMLTestCase;
052: import org.netbeans.modules.uml.core.metamodel.structure.IProject;
053: import org.netbeans.modules.uml.core.metamodel.structure.Project;
054: import org.netbeans.modules.uml.core.reverseengineering.reframework.ClassEvent;
055: import org.netbeans.modules.uml.core.reverseengineering.reframework.DependencyEvent;
056: import org.netbeans.modules.uml.core.reverseengineering.reframework.PackageEvent;
057: import org.netbeans.modules.uml.core.reverseengineering.reintegration.ReverseEngineerTask;
058: import org.netbeans.modules.uml.core.reverseengineering.reintegration.UMLParsingIntegrator;
059: import org.netbeans.modules.uml.core.support.umlsupport.XMLManip;
060: import org.openide.util.RequestProcessor;
061:
062: public abstract class AbstractUMLParsingIntegrationTestCase extends
063: AbstractUMLTestCase {
064: /* Begin:Initialize Product */
065: // private static ICoreProduct prod;
066: // static {
067: // CoreProductManager.instance().setCoreProduct(new ADProduct());
068: // prod = CoreProductManager.instance().getCoreProduct();
069: // prod.initialize();
070: // }
071: /* End: Initialize Product */
072: static String testDataPath = null;
073:
074: final static String possibleClassRoot = "Class|Enumeration|Interface";
075:
076: protected void setUp() throws Exception {
077: super .setUp();
078: }
079:
080: public void execute(String className) {
081:
082: final IProject project = getProject();
083:
084: // conover - RE is a n NB task now, so can't call it directly anymore
085: // final UMLParsingIntegrator integrator = new UMLParsingIntegrator();
086: // integrator.reverseEngineer(project, false, false, true, true);
087:
088: ReverseEngineerTask reTask = new ReverseEngineerTask(project,
089: null, false, false, false, true, null);
090:
091: final UMLParsingIntegrator integrator = (UMLParsingIntegrator) reTask
092: .getParsingIntegrator();
093:
094: reTask.run();
095:
096: // RequestProcessor processor =
097: // new RequestProcessor("uml/ReverseEngineer"); // NOI18N
098: // processor.post(reTask);
099:
100: String fileNameInput = getDataDir() + "InputFiles"
101: + File.separator + className + "InputFile.txt";
102:
103: assertTrue("Input File Not Found", new File(fileNameInput)
104: .exists());
105:
106: String xmiDatas = readFile(fileNameInput);
107: assertNotSame("Input File contains Empty String:", xmiDatas, "");
108:
109: integrator.onBeginParseFile(className + "File.java", null);
110:
111: StringTokenizer strTokens = new StringTokenizer(xmiDatas,
112: "\r\n");
113: while (strTokens.hasMoreTokens()) {
114: String xmiData = (String) strTokens.nextElement();
115: if (xmiData.trim().equals(""))
116: continue;
117: buildModel(integrator, xmiData);
118: }
119:
120: integrator.integrate(project);
121:
122: String etdStr = project.getDocument().asXML();
123: String newetdStr = getXMIRemovedStr(etdStr);
124: // compareModel(newetdStr, className);
125: }
126:
127: private void buildModel(UMLParsingIntegrator integrator,
128: String xmiData) {
129: int spaceIndex = xmiData.indexOf(" ");
130:
131: // Remove "<UML:" and find root Name
132: String rootName = xmiData.substring(5, spaceIndex);
133:
134: Document doc = XMLManip.loadXML(xmiData);
135:
136: if (possibleClassRoot.indexOf(rootName) != -1) {
137: onClassFound(integrator, doc);
138: } else if (rootName.equals("Package")) {
139: onPackageFound(integrator, doc);
140: } else if (rootName.equals("Dependency")) {
141: onDependencyFound(integrator, doc);
142: }
143:
144: }
145:
146: private void onClassFound(UMLParsingIntegrator integrator,
147: Document doc) {
148: ClassEvent classEvent = new ClassEvent();
149: classEvent.setEventData(doc);
150: integrator.onClassFound(classEvent, null);
151: }
152:
153: private void onPackageFound(UMLParsingIntegrator integrator,
154: Document doc) {
155: PackageEvent packageEvent = new PackageEvent();
156: packageEvent.setEventData(doc);
157: integrator.onPackageFound(packageEvent, null);
158: }
159:
160: private void onDependencyFound(UMLParsingIntegrator integrator,
161: Document doc) {
162: DependencyEvent dependencyEvent = new DependencyEvent();
163: dependencyEvent.setEventData(doc);
164: integrator.onDependencyFound(dependencyEvent, null);
165: }
166:
167: private void compareModel(String modelData, String className) {
168: String resultFile = getDataDir() + "ResultFiles"
169: + File.separator + className + "ResultFile.txt";
170: assertTrue("Result File Not Found", new File(resultFile)
171: .exists());
172:
173: String expectedModelData = readFile(resultFile);
174: assertNotSame("Result File contains Empty String:",
175: expectedModelData, "");
176:
177: expectedModelData = expectedModelData.replace("\n", "");
178: expectedModelData = expectedModelData.replace("\r", "");
179: if (!expectedModelData.equals(modelData))
180: System.out.println("\nClass Name: " + className + "\n\n"
181: + modelData);
182: assertEquals("Model Data are Different ", expectedModelData,
183: modelData);
184: }
185:
186: private IProject getProject() {
187: IProject project = new Project();
188: Document doc = XMLManip.getDOMDocument();
189: project.setDocument(doc);
190: project.prepareNode();
191: project.setMode("Implementation");
192: project.setName("TestReIntegration");
193: return project;
194: }
195:
196: private static String readFile(String fileName) {
197: String str = "";
198: try {
199: FileInputStream p = new FileInputStream(fileName);
200: int ch = -1;
201: while ((ch = p.read()) != -1)
202: str += (char) ch;
203: } catch (Exception ewe) {
204: ewe.printStackTrace();
205: }
206: return str;
207: }
208:
209: private String getXMIRemovedStr(String etdStr) {
210: String retVal = etdStr;
211: int index = -1;
212: int newxmiID = 0;
213: while ((index = retVal.indexOf("xmi.id=")) != -1) {
214:
215: String partI = retVal.substring(0, index);
216: String partII = "xmi1.id=\"";
217: String partIII = retVal.substring(index + 8);
218:
219: int idEndPos = partIII.indexOf("\"");
220: String xmiId = partIII.substring(0, idEndPos);
221: retVal = partI + partII + partIII;
222:
223: retVal = retVal.replace(xmiId, "" + ++newxmiID);
224: }
225:
226: retVal = retVal.replace("xmi1.id=", "xmi.id=");
227: retVal = retVal.replace(" neverSavedBefore=\"true\"", "");
228: retVal = retVal.replace("\n", "");
229: retVal = retVal.replace("\r", "");
230: return retVal;
231: }
232:
233: /**
234: * Get the path of test Data Folder
235: */
236: private String getDataDir() {
237: if (testDataPath != null)
238: return testDataPath;
239:
240: testDataPath = System.getProperty("xtest.data");
241: if (testDataPath != null)
242: return (testDataPath += File.separator
243: + "ReIntegrationTestFiles" + File.separator);
244:
245: String s1 = getClass().getName();
246: URL url = getClass().getResource(
247: s1.substring(s1.lastIndexOf('.') + 1) + ".class");
248: File file = (new File(url.getFile())).getParentFile();
249: for (int i = 0; (i = s1.indexOf('.', i) + 1) > 0;)
250: file = file.getParentFile();
251: testDataPath = file.getParent() + File.separator + "data"
252: + File.separator + "ReIntegrationTestFiles"
253: + File.separator;
254: return testDataPath;
255: }
256:
257: /*
258: * (non-Javadoc)
259: *
260: * @see junit.framework.TestCase#tearDown()
261: */
262: protected void tearDown() throws Exception {
263: super.tearDown();
264: }
265:
266: }
|