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.parsers.umlparser;
043:
044: import java.io.File;
045: import java.io.FileInputStream;
046: import java.net.URL;
047: import java.util.ArrayList;
048: import java.util.StringTokenizer;
049:
050: import antlr.CommonASTWithLocationsAndHidden;
051: import antlr.CommonHiddenStreamToken;
052:
053: import org.netbeans.modules.uml.core.AbstractUMLTestCase;
054: import org.netbeans.modules.uml.core.reverseengineering.parsers.javaparser.JavaTokenTypes;
055: import org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.CommentGather;
056: import org.netbeans.modules.uml.core.reverseengineering.reframework.parsingframework.ParserEventController;
057:
058: public abstract class AbstractUmlParserTestCase extends
059: AbstractUMLTestCase {
060:
061: // /* Begin:Initialize Product */
062: // private static ICoreProduct prod;
063: // static {
064: // CoreProductManager.instance().setCoreProduct(new ADProduct());
065: // prod = CoreProductManager.instance().getCoreProduct();
066: // prod.initialize();
067: // }
068: //
069: // /* End: Initialize Product */
070:
071: /** Store testDataPath which contains URL of Data folder */
072: private static String testDataPath = null;
073:
074: /** Instanciate EventController */
075: private ParserEventController m_EventController = null;
076:
077: /** Instanciate Listeners-Token JavaUMLParserProcessor */
078: private UMLParserTestListener javaUMLParserProcessor = null;
079:
080: protected void setUp() throws Exception {
081: super .setUp();
082:
083: if (m_EventController == null) {
084: m_EventController = new ParserEventController(
085: new CommentGather(JavaTokenTypes.SL_COMMENT,
086: JavaTokenTypes.ML_COMMENT), "Java");
087:
088: javaUMLParserProcessor = new UMLParserTestListener();
089:
090: m_EventController.setTokenProcessor(javaUMLParserProcessor);
091: m_EventController.setStateListener(javaUMLParserProcessor);
092: m_EventController.setTokenFilter(javaUMLParserProcessor);
093: m_EventController.setStateFilter(javaUMLParserProcessor);
094: }
095:
096: }
097:
098: /**
099: * This method is the main entrance to all the UMLParser TestCases.
100: *
101: * @param String
102: * ClassName TestCase Name is passed
103: */
104: public void execute(String className) {
105: javaUMLParserProcessor.clearList();
106: String fileNameInput = getDataDir() + "InputFiles"
107: + File.separator + className + "InputFile.txt";
108: assertTrue("Input File Not Found", new File(fileNameInput)
109: .exists());
110:
111: // Read States and Tokens from Input file
112: String strInput = readFile(fileNameInput);
113: assertNotSame("Input File contains Empty String:", strInput, "");
114:
115: int level = -1;
116: StringTokenizer strTokens = new StringTokenizer(strInput,
117: "\r\n");
118: while (strTokens.hasMoreTokens()) {
119: String strToken = (String) strTokens.nextElement();
120: if (strToken.trim().equals(""))
121: continue;
122:
123: // Initial State Finding Algorithm
124: if (!strToken.startsWith(" ")) {
125: if (strToken.startsWith("{"))
126: throw new RuntimeException(
127: "Instead of State, Token Found");
128:
129: // If Same class has more than one root tag then we need close
130: // all states of previous root node
131: while (level > -1) {
132: m_EventController.stateEnd();
133: level--;
134: }
135: m_EventController.stateBegin(strToken);
136: level++;
137: continue;
138: }
139:
140: // Calculate The Level
141: int tmpLevel = 0;
142: while (strToken.startsWith(" ")) {
143: strToken = strToken.substring(1);
144: tmpLevel++;
145: }
146:
147: // Close The States Which are unwanted
148: while (tmpLevel <= level) {
149: m_EventController.stateEnd();
150: level--;
151: }
152:
153: // check strToken contains state or token
154: if (!strToken.startsWith("{")) {
155: level = tmpLevel;
156: m_EventController.stateBegin(strToken);
157: } else {
158: m_EventController.tokenFound(
159: makeASTNode(getTokenName(strToken)),
160: getTokenType(strToken));
161: }
162:
163: }
164:
165: // Close All States
166: while (level > -1) {
167: m_EventController.stateEnd();
168: level--;
169: }
170:
171: compareXMI(className);
172:
173: }
174:
175: /**
176: * Compare Expected and retrived XMIData
177: */
178: private void compareXMI(String className) {
179:
180: String fileNameResult = getDataDir() + "ResultFiles"
181: + File.separator + className + "ResultFile.txt";
182: assertTrue("Result File Not Found", new File(fileNameResult)
183: .exists());
184:
185: // Retrieve XMI Data
186: ArrayList<String> xmiTags = javaUMLParserProcessor.getXMIData();
187: String retrivedResult = "";
188: for (String xmiTag : xmiTags) {
189: retrivedResult += xmiTag;
190: }
191:
192: // read Expected Result from ResultFile
193: String expectedResult = readFile(fileNameResult);
194: assertNotSame("Result File contains Empty String:",
195: expectedResult, "");
196: if (!retrivedResult.equals(expectedResult))
197: System.out.println("\nTestCase " + className
198: + ":\nRetrived XMIData:\n" + retrivedResult);
199:
200: assertEquals("Expected and Retrived XMI Data are Different",
201: expectedResult, retrivedResult);
202: }
203:
204: private String getTokenName(String strToken) {
205: int index = strToken.lastIndexOf(",");
206: String name = strToken.substring(1, index).trim();
207: return name;
208: }
209:
210: private String getTokenType(String strToken) {
211: int index = strToken.lastIndexOf(",");
212: String type = strToken.substring(index + 1,
213: strToken.length() - 2).trim();
214: return type;
215: }
216:
217: /** Creates AST Node using the Token Name */
218: private CommonASTWithLocationsAndHidden makeASTNode(String name) {
219: CommonASTWithLocationsAndHidden astNode = new CommonASTWithLocationsAndHidden();
220: astNode.initialize(new CommonHiddenStreamToken(name));
221: return astNode;
222: }
223:
224: /**
225: * Get the path of test Data Folder
226: */
227: private String getDataDir() {
228: if (testDataPath != null)
229: return testDataPath;
230: testDataPath = System.getProperty("xtest.data");
231: if (testDataPath != null)
232: return (testDataPath += File.separator
233: + "UMLParserTestFiles" + File.separator);
234:
235: String s1 = getClass().getName();
236: URL url = getClass().getResource(
237: s1.substring(s1.lastIndexOf('.') + 1) + ".class");
238: File file = (new File(url.getFile())).getParentFile();
239: for (int i = 0; (i = s1.indexOf('.', i) + 1) > 0;)
240: file = file.getParentFile();
241: testDataPath = file.getParent() + File.separator + "data"
242: + File.separator + "UMLParserTestFiles"
243: + File.separator;
244: return testDataPath;
245: }
246:
247: /**
248: * Reads the File and forms as String Data.
249: *
250: * @param String
251: * FileName to be read
252: * @return String
253: */
254: public static String readFile(String fileName) {
255: String str = "";
256: try {
257: FileInputStream p = new FileInputStream(fileName);
258: int ch = -1;
259: while ((ch = p.read()) != -1)
260: str += (char) ch;
261: } catch (Exception ewe) {
262: ewe.printStackTrace();
263: }
264: return str;
265: }
266:
267: }
|