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.parser.java;
043:
044: import java.util.ArrayList;
045:
046: import junit.framework.TestCase;
047: import java.io.File;
048: import java.net.URL;
049: import org.netbeans.modules.uml.core.reverseengineering.parsers.javaparser.REJavaParser;
050:
051: /**
052: *
053: */
054: public abstract class AbstractParserTestCase extends TestCase {
055:
056: // Retrieved Tokens are stored
057: protected ArrayList<String[]> tokens = new ArrayList<String[]>();
058:
059: // Retrieved States are stored
060: protected ArrayList<String> states = new ArrayList<String>();
061:
062: //Istansiate Parser for parse the Java Files
063: private static REJavaParser parser = new REJavaParser();
064:
065: //Store testDataPath which contains URL of Data folder
066: private static String testDataPath = null;
067:
068: @Override
069: protected void setUp() throws Exception {
070: super .setUp();
071: }
072:
073: protected void clearAll() {
074: clearTokens();
075: clearStates();
076: }
077:
078: protected void clearTokens() {
079: tokens = new ArrayList<String[]>();
080: }
081:
082: protected void clearStates() {
083: states = new ArrayList<String>();
084: }
085:
086: protected void addToken(String tokenName, String tokenType) {
087: String ast[] = { tokenName, tokenType };
088: tokens.add(ast);
089: }
090:
091: protected void addState(String o) {
092: states.add(o);
093: }
094:
095: /**
096: * Compares expected States with Retrieved States
097: */
098: protected boolean compareStatesWith(String expectedStates[]) {
099: if (states.size() != expectedStates.length) {
100: System.out.println("\nNumber Of States\n \tResult:"
101: + states.size() + "Expected:"
102: + expectedStates.length);
103: showStates();
104: return false;
105: }
106:
107: for (int i = 0; i < states.size(); i++)
108: if (!states.get(i).equals(expectedStates[i])) {
109: System.out.println("\nState Obtained:" + states.get(i)
110: + " Expected State:" + expectedStates[i]);
111: showStates();
112: return false;
113: }
114:
115: return true;
116: }
117:
118: /**
119: * Compares expected Tokens with Retrieved Tokens
120: */
121: protected boolean compareTokensWith(String expectedTokens[][]) {
122:
123: if (tokens.size() != expectedTokens.length) {
124: System.out.println("\nNumber Of Tokens\n \tResult:"
125: + tokens.size() + " Expected:"
126: + expectedTokens.length);
127: showTokens();
128: return false;
129: }
130:
131: for (int i = 0; i < tokens.size(); i++) {
132: String ast[] = (String[]) tokens.get(i);
133: for (int j = 0; j < 2; j++) {
134: if (!ast[j].equals(expectedTokens[i][j])) {
135: System.out
136: .println("\nToken Obtained:" + ast[j]
137: + " Expected Token:"
138: + expectedTokens[i][j]);
139: showTokens();
140: return false;
141: }
142: }
143: }
144:
145: return true;
146:
147: }
148:
149: /**
150: * Parse and test the tokens and states are coming properly.
151: */
152: protected void execute(String fileName, String expectedStates[],
153: String expectedTokens[][]) {
154: // fileName =getDataDir() + fileName;
155: // clearAll();
156: //
157: // if (parser != null) {
158: // ParserTestListener listener = new ParserTestListener(this);
159: // parser.setStateListener(listener);
160: // parser.setTokenProcessor(listener);
161: //
162: // parser.parseFile(fileName);
163: //
164: // assertTrue("States Mismatch",compareStatesWith(expectedStates));
165: // assertTrue("Tokens Mismatch",compareTokensWith(expectedTokens));
166: // } else
167: // fail("REJavaParser is Not instanciated");
168: }
169:
170: /**
171: * Shows the states
172: */
173: private void showStates() {
174: System.out.println("\n");
175: for (String state : states) {
176: System.out.print("\"" + state + "\",");
177: }
178:
179: System.out.println("");
180: }
181:
182: /**
183: * Shows the tokens
184: */
185: private void showTokens() {
186:
187: System.out.println("");
188:
189: System.out.print("{ ");
190: for (String[] pToken : tokens) {
191: System.out.print("{\"" + pToken[0] + "\", \"" + pToken[1]
192: + "\"} ,");
193: }
194: System.out.println(" }");
195:
196: }
197:
198: /**
199: * Get the path of test Data Folder
200: */
201: public String getDataDir() {
202: if (testDataPath != null)
203: return testDataPath;
204: String s = System.getProperty("xtest.data");
205: if (s != null)
206: return s;
207: String s1 = getClass().getName();
208: URL url = getClass().getResource(
209: s1.substring(s1.lastIndexOf('.') + 1) + ".class");
210: File file = (new File(url.getFile())).getParentFile();
211: for (int i = 0; (i = s1.indexOf('.', i) + 1) > 0;)
212: file = file.getParentFile();
213: testDataPath = file.getParent()
214: + "\\data\\Java5ParserTestJavaFiles\\";
215: return testDataPath;
216: }
217: }
|