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.test;
043:
044: import java.io.File;
045: import java.io.PrintWriter;
046: import java.util.Collections;
047: import java.util.List;
048: import org.netbeans.editor.BaseDocument;
049: import org.netbeans.editor.Settings;
050: import org.netbeans.modules.cnd.api.model.CsmFile;
051: import org.netbeans.modules.cnd.api.model.CsmModelAccessor;
052: import org.netbeans.modules.cnd.editor.cplusplus.CCKit;
053: import org.netbeans.modules.cnd.editor.cplusplus.CCSettingsInitializer;
054: import org.netbeans.modules.cnd.editor.cplusplus.CKit;
055: import org.netbeans.modules.cnd.modelimpl.trace.TestModelHelper;
056: import org.netbeans.modules.cnd.test.BaseTestCase;
057: import org.netbeans.modules.cnd.test.CndCoreTestUtils;
058: import org.openide.filesystems.FileObject;
059: import org.openide.filesystems.FileUtil;
060: import org.openide.loaders.DataObject;
061:
062: /**
063: * IMPORTANT NOTE:
064: * If This class is not compiled with the notification about not resolved
065: * BaseTestCase class => cnd/core tests are not compiled
066: *
067: * To solve this problem compile or run tests for cnd/core
068: */
069:
070: /**
071: * test case for working with projects
072: * test has possibility to copy project data into working test dir to prevent changes
073: * in source folders when test makes any changes in content of files
074: *
075: * @author Vladimir Voskresensky
076: */
077: public abstract class ProjectBasedTestCase extends BaseTestCase {
078:
079: private TestModelHelper projectHelper = null;
080: private List<String> sysIncludes = Collections.<String> emptyList();
081: private List<String> usrIncludes = Collections.<String> emptyList();
082:
083: protected PrintWriter outputWriter = null;
084:
085: protected PrintWriter logWriter = null;
086:
087: private final boolean performInWorkDir;
088: private File workDirBasedProject = null;
089:
090: /**
091: * Creates a new instance of CompletionBaseTestCase
092: */
093: public ProjectBasedTestCase(String testName) {
094: this (testName, false);
095: }
096:
097: /**
098: * if test performs any modifications in data files or create new files
099: * => pass performInWorkDir as 'true' to create local copy of project in work dir
100: */
101: public ProjectBasedTestCase(String testName,
102: boolean performInWorkDir) {
103: super (testName);
104: this .performInWorkDir = performInWorkDir;
105: }
106:
107: protected final List<String> getSysIncludes() {
108: return sysIncludes;
109: }
110:
111: protected void setSysIncludes(List<String> sysIncludes) {
112: this .sysIncludes = sysIncludes;
113: }
114:
115: protected final List<String> getUsrIncludes() {
116: return usrIncludes;
117: }
118:
119: protected void setUsrIncludes(List<String> usrIncludes) {
120: this .usrIncludes = usrIncludes;
121: }
122:
123: protected final void initDocumentSettings() {
124: Settings.addInitializer(new CCSettingsInitializer(CCKit.class));
125: Settings.addInitializer(new CCSettingsInitializer(CKit.class));
126: }
127:
128: @Override
129: protected void setUp() throws Exception {
130: super .setUp();
131: System.setProperty("cnd.modelimpl.persistent", "false");
132: initDocumentSettings();
133: super .clearWorkDir();
134:
135: outputWriter = new PrintWriter(getRef());
136: logWriter = new PrintWriter(getLog());
137:
138: log("setUp preparing project.");
139: projectHelper = new TestModelHelper();
140: File projectDir;
141: if (performInWorkDir) {
142: workDirBasedProject = new File(getWorkDir(), "project"); // NOI18N
143: // copy data dir
144: CndCoreTestUtils.copyDirToWorkDir(getTestCaseDataDir(),
145: workDirBasedProject);
146: projectDir = workDirBasedProject;
147: } else {
148: projectDir = getTestCaseDataDir();
149: }
150: projectDir = changeDefProjectDirBeforeParsingProjectIfNeeded(projectDir);
151: projectHelper.initParsedProject(projectDir.getAbsolutePath(),
152: getSysIncludes(), getUsrIncludes());
153: log("setUp finished preparing project.");
154: log("Test " + getName() + "started");
155: }
156:
157: /**
158: * change the folder if needed from test folder to subfolder
159: * i.e. if test folder has several folders: for project and libs =>
160: * change dir to subfolder corresponding to project dir
161: * @param projectDir current project dir
162: * @return folder that should be used as project dir
163: */
164: protected File changeDefProjectDirBeforeParsingProjectIfNeeded(
165: File projectDir) {
166: return projectDir;
167: }
168:
169: @Override
170: protected void tearDown() throws Exception {
171: super .tearDown();
172: projectHelper.shutdown();
173: outputWriter.flush();
174: logWriter.flush();
175: outputWriter.close();
176: logWriter.close();
177: sysIncludes = Collections.<String> emptyList();
178: usrIncludes = Collections.<String> emptyList();
179: }
180:
181: @Override
182: protected File getDataFile(String filename) {
183: if (performInWorkDir) {
184: return new File(workDirBasedProject, filename);
185: } else {
186: return super .getDataFile(filename);
187: }
188: }
189:
190: protected BaseDocument getBaseDocument(File testSourceFile)
191: throws Exception {
192: FileObject testFileObject = FileUtil
193: .toFileObject(testSourceFile);
194: assertNotNull("Unresolved test file " + testSourceFile,
195: testFileObject);//NOI18N
196: DataObject testDataObject = DataObject.find(testFileObject);
197: assertNotNull("Unresolved data object for file "
198: + testFileObject, testDataObject);//NOI18N
199: BaseDocument doc = CndCoreTestUtils
200: .getBaseDocument(testDataObject);
201: assertNotNull("Unresolved document for data object "
202: + testDataObject, testDataObject);//NOI18N
203: return doc;
204: }
205:
206: protected CsmFile getCsmFile(File testSourceFile) throws Exception {
207: CsmFile csmFile = CsmModelAccessor.getModel().findFile(
208: testSourceFile.getAbsolutePath());
209: assertNotNull("Unresolved CsmFile for test file "
210: + testSourceFile, csmFile);//NOI18N
211: return csmFile;
212: }
213:
214: }
|