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.cnd.modelimpl.trace;
043:
044: import java.io.File;
045: import java.io.PrintStream;
046: import org.netbeans.modules.cnd.api.model.CsmFile;
047: import org.netbeans.modules.cnd.api.model.CsmModel;
048: import org.netbeans.modules.cnd.api.model.CsmProject;
049: import org.netbeans.modules.cnd.modelimpl.csm.core.FileImpl;
050: import org.netbeans.modules.cnd.modelimpl.csm.core.ProjectBase;
051: import org.netbeans.modules.cnd.modelimpl.test.ModelImplBaseTestCase;
052: import org.netbeans.modules.cnd.test.CndCoreTestUtils;
053:
054: /**
055: *
056: * @author Vladimir Voskresensky
057: */
058: public class TraceModelTestBase extends ModelImplBaseTestCase {
059:
060: private TestModelHelper helper;
061:
062: public TraceModelTestBase(String testName) {
063: super (testName);
064: }
065:
066: protected TraceModel getTraceModel() {
067: assert helper != null;
068: return helper.getTraceModel();
069: }
070:
071: protected void performTest(String source) throws Exception {
072: performTest(source, source + ".dat", source + ".err"); // NOI18N
073: }
074:
075: protected final ProjectBase getProject() {
076: return helper.getProject();
077: }
078:
079: protected final CsmProject getCsmProject() {
080: return helper.getProject();
081: }
082:
083: protected final void resetProject() {
084: helper.resetProject();
085: }
086:
087: protected final CsmModel getModel() {
088: return helper.getModel();
089: }
090:
091: protected void preSetUp() throws Exception {
092: // init flags needed for file model tests before creating TraceModel
093: }
094:
095: protected void postSetUp() throws Exception {
096: // init flags needed for file model tests
097: }
098:
099: protected final void initParsedProject() throws Exception {
100: File projectDir = getTestCaseDataDir();
101: helper.initParsedProject(projectDir.getAbsolutePath());
102: }
103:
104: protected final FileImpl getFileImpl(File file) {
105: return helper.getProject().getFile(file);
106: }
107:
108: protected final void reparseFile(CsmFile file) {
109: if (file instanceof FileImpl) {
110: ((FileImpl) file).stateChanged(true);
111: try {
112: ((FileImpl) file).scheduleParsing(true);
113: } catch (InterruptedException ex) {
114: ex.printStackTrace();
115: }
116: }
117: }
118:
119: @Override
120: protected void setUp() throws Exception {
121: preSetUp();
122: super .setUp();
123: super .clearWorkDir();
124: helper = new TestModelHelper();
125: assertNotNull("Model must be valid", getTraceModel().getModel()); // NOI18N
126: postSetUp();
127: }
128:
129: @Override
130: protected void tearDown() throws Exception {
131: super .tearDown();
132: helper.shutdown();
133: }
134:
135: protected final void performModelTest(File testFile,
136: PrintStream streamOut, PrintStream streamErr)
137: throws Exception {
138: performModelTest(new String[] { testFile.getAbsolutePath() },
139: streamOut, streamErr);
140: }
141:
142: protected final void performModelTest(String[] args,
143: PrintStream streamOut, PrintStream streamErr)
144: throws Exception {
145: getTraceModel().test(args, streamOut, streamErr);
146: }
147:
148: protected void doTest(String[] args, PrintStream streamOut,
149: PrintStream streamErr, Object... params) throws Exception {
150: PrintStream oldOut = System.out;
151: PrintStream oldErr = System.err;
152: try {
153: // redirect output and err
154: System.setOut(streamOut);
155: System.setErr(streamErr);
156: performModelTest(args, streamOut, streamErr);
157: postTest(args, params);
158: } finally {
159: // restore err and out
160: System.setOut(oldOut);
161: System.setErr(oldErr);
162: }
163: }
164:
165: protected void postTest(String[] args, Object... params) {
166:
167: }
168:
169: protected void performTest(String source,
170: String goldenDataFileName, String goldenErrFileName,
171: Object... params) throws Exception {
172: File testFile = getDataFile(source);
173: performTest(new String[] { testFile.getAbsolutePath() },
174: goldenDataFileName, goldenErrFileName, params);
175: }
176:
177: protected void performTest(String[] args,
178: String goldenDataFileName, String goldenErrFileName,
179: Object... params) throws Exception {
180: File workDir = getWorkDir();
181:
182: File output = new File(workDir, goldenDataFileName);
183: PrintStream streamOut = new PrintStream(output);
184: File error = goldenErrFileName == null ? null : new File(
185: workDir, goldenErrFileName);
186: PrintStream streamErr = goldenErrFileName == null ? null
187: : new PrintStream(error);
188: try {
189: doTest(args, streamOut, streamErr, params);
190: } finally {
191: // restore err and out
192: streamOut.close();
193: if (streamErr != null) {
194: streamErr.close();
195: }
196: }
197: //System.out.println("finished testing " + testFile);
198: boolean errTheSame = true;
199: File goldenErrFile = null;
200: File goldenErrFileCopy = null;
201: // first of all check err, because if not failed (often) => dat diff will be created
202: if (goldenErrFileName != null) {
203: goldenErrFile = getGoldenFile(goldenErrFileName);
204: if (CndCoreTestUtils.diff(error, goldenErrFile, null)) {
205: errTheSame = false;
206: // copy golden
207: goldenErrFileCopy = new File(workDir, goldenErrFileName
208: + ".golden");
209: CndCoreTestUtils.copyToWorkDir(goldenErrFile,
210: goldenErrFileCopy); // NOI18N
211: }
212: }
213:
214: boolean outTheSame = true;
215: File goldenDataFile = getGoldenFile(goldenDataFileName);
216: File goldenDataFileCopy = null;
217: if (CndCoreTestUtils.diff(output, goldenDataFile, null)) {
218: outTheSame = false;
219: // copy golden
220: goldenDataFileCopy = new File(workDir, goldenDataFileName
221: + ".golden");
222: CndCoreTestUtils.copyToWorkDir(goldenDataFile,
223: goldenDataFileCopy); // NOI18N
224: }
225: if (outTheSame) {
226: assertTrue("ERR Difference - check: diff " + error + " "
227: + goldenErrFileCopy, errTheSame); // NOI18N
228: } else if (errTheSame) {
229: assertTrue("OUTPUT Difference - check: diff " + output
230: + " " + goldenDataFileCopy, outTheSame); // NOI18N
231: } else {
232: assertTrue(
233: "OUTPUT and ERR are different, see content of folder "
234: + workDir, false); // NOI18N
235: }
236: }
237: }
|