PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/netbeans-7.3/php.editor/test/unit/src/org/netbeans/modules/php/editor/actions/ImportDataCreatorTest.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 232 lines | 159 code | 26 blank | 47 comment | 14 complexity | 432f83e6fa1ec97d4c84e96b0b177ea5 MD5 | raw file
  1. /*
  2. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  3. *
  4. * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
  5. *
  6. * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
  7. * Other names may be trademarks of their respective owners.
  8. *
  9. * The contents of this file are subject to the terms of either the GNU
  10. * General Public License Version 2 only ("GPL") or the Common
  11. * Development and Distribution License("CDDL") (collectively, the
  12. * "License"). You may not use this file except in compliance with the
  13. * License. You can obtain a copy of the License at
  14. * http://www.netbeans.org/cddl-gplv2.html
  15. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  16. * specific language governing permissions and limitations under the
  17. * License. When distributing the software, include this License Header
  18. * Notice in each file and include the License file at
  19. * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
  20. * particular file as subject to the "Classpath" exception as provided
  21. * by Oracle in the GPL Version 2 section of the License file that
  22. * accompanied this code. If applicable, add the following below the
  23. * License Header, with the fields enclosed by brackets [] replaced by
  24. * your own identifying information:
  25. * "Portions Copyrighted [year] [name of copyright owner]"
  26. *
  27. * If you wish your version of this file to be governed by only the CDDL
  28. * or only the GPL Version 2, indicate your decision by adding
  29. * "[Contributor] elects to include this software in this distribution
  30. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  31. * single choice of license, a recipient has the option to distribute
  32. * your version of this file under either the CDDL, the GPL Version 2 or
  33. * to extend the choice of license to its licensees as provided above.
  34. * However, if you add GPL Version 2 code and therefore, elected the GPL
  35. * Version 2 license, then the option applies only if the new code is
  36. * made subject to such option by the copyright holder.
  37. *
  38. * Contributor(s):
  39. *
  40. * Portions Copyrighted 2012 Sun Microsystems, Inc.
  41. */
  42. package org.netbeans.modules.php.editor.actions;
  43. import java.io.File;
  44. import java.util.Arrays;
  45. import java.util.Collections;
  46. import java.util.List;
  47. import java.util.Map;
  48. import javax.swing.text.Document;
  49. import org.netbeans.api.java.classpath.ClassPath;
  50. import org.netbeans.modules.csl.spi.ParserResult;
  51. import org.netbeans.modules.parsing.api.ParserManager;
  52. import org.netbeans.modules.parsing.api.ResultIterator;
  53. import org.netbeans.modules.parsing.api.Source;
  54. import org.netbeans.modules.parsing.api.UserTask;
  55. import org.netbeans.modules.parsing.spi.Parser;
  56. import org.netbeans.modules.parsing.spi.indexing.support.QuerySupport;
  57. import org.netbeans.modules.php.editor.PHPCodeCompletionTestBase;
  58. import org.netbeans.modules.php.editor.actions.FixUsesAction.Options;
  59. import org.netbeans.modules.php.editor.actions.ImportData.ItemVariant;
  60. import org.netbeans.modules.php.editor.api.ElementQuery.Index;
  61. import org.netbeans.modules.php.editor.api.ElementQueryFactory;
  62. import org.netbeans.modules.php.editor.api.QuerySupportFactory;
  63. import org.netbeans.modules.php.editor.indent.CodeStyle;
  64. import org.netbeans.modules.php.editor.model.ModelUtils;
  65. import org.netbeans.modules.php.editor.model.NamespaceScope;
  66. import org.netbeans.modules.php.editor.parser.PHPParseResult;
  67. import org.netbeans.modules.php.project.api.PhpSourcePath;
  68. import org.netbeans.spi.java.classpath.support.ClassPathSupport;
  69. import org.openide.filesystems.FileObject;
  70. import org.openide.filesystems.FileUtil;
  71. /**
  72. *
  73. * @author Ondrej Brejla <obrejla@netbeans.org>
  74. */
  75. public class ImportDataCreatorTest extends PHPCodeCompletionTestBase {
  76. private Index index;
  77. public ImportDataCreatorTest(String testName) {
  78. super(testName);
  79. }
  80. @Override
  81. public void setUp() throws Exception {
  82. super.setUp();
  83. QuerySupport querySupport = QuerySupportFactory.get(Arrays.asList(createSourceClassPathsForTest()));
  84. index = ElementQueryFactory.createIndexQuery(querySupport);
  85. }
  86. public void testImportData_01() throws Exception {
  87. performTest("Homepage^Presenter");
  88. }
  89. public void testImportData_02() throws Exception {
  90. performTest("Homepage^Presenter");
  91. }
  92. public void testImportData_03() throws Exception {
  93. performTest("Homepage^Presenter");
  94. }
  95. public void testImportData_04() throws Exception {
  96. // see issue #219548
  97. performTest("Homepage^Presenter");
  98. }
  99. public void testImportData_05() throws Exception {
  100. performTest("Homepage^Presenter", new Options(true, false, false, false));
  101. }
  102. public void testImportData_06() throws Exception {
  103. performTest("Homepage^Presenter", new Options(false, false, false, false));
  104. }
  105. public void testImportData_07() throws Exception {
  106. performTest("class ^Blah");
  107. }
  108. public void testImportData_08() throws Exception {
  109. // see issue #219548
  110. performTest("class ^Blah");
  111. }
  112. public void testImportData_09_issue209408() throws Exception {
  113. performTest("class ^ClassName");
  114. }
  115. private void performTest(String caretLine) throws Exception {
  116. performTest(caretLine, null);
  117. }
  118. private void performTest(String caretLine, Options options) throws Exception {
  119. String exactFileName = getTestPath();
  120. ImportData testResult = getTestResult(exactFileName, caretLine, options);
  121. String target = createResultString(testResult);
  122. assertDescriptionMatches(exactFileName, target, false, ".importData");
  123. }
  124. private ImportData getTestResult(String fileName, String caretLine, final Options options) throws Exception {
  125. FileObject testFile = getTestFile(fileName);
  126. Source testSource = getTestSource(testFile);
  127. final int caretOffset;
  128. if (caretLine != null) {
  129. caretOffset = getCaretOffset(testSource.createSnapshot().getText().toString(), caretLine);
  130. enforceCaretOffset(testSource, caretOffset);
  131. } else {
  132. caretOffset = -1;
  133. }
  134. final ImportData[] result = new ImportData[1];
  135. ParserManager.parse(Collections.singleton(testSource), new UserTask() {
  136. @Override
  137. public void run(ResultIterator resultIterator) throws Exception {
  138. Parser.Result r = caretOffset == -1 ? resultIterator.getParserResult() : resultIterator.getParserResult(caretOffset);
  139. if (r != null) {
  140. assertTrue(r instanceof ParserResult);
  141. PHPParseResult phpResult = (PHPParseResult)r;
  142. Map<String, List<UsedNamespaceName>> usedNames = new UsedNamesComputer(phpResult, caretOffset).computeNames();
  143. NamespaceScope namespaceScope = ModelUtils.getNamespaceScope(phpResult.getModel().getFileScope(), caretOffset);
  144. Options currentOptions = options;
  145. if (currentOptions == null) {
  146. Document document = phpResult.getSnapshot().getSource().getDocument(false);
  147. CodeStyle codeStyle = CodeStyle.get(document);
  148. currentOptions = new Options(codeStyle);
  149. }
  150. ImportData importData = new ImportDataCreator(usedNames, index, namespaceScope.getNamespaceName(), currentOptions).create();
  151. importData.caretPosition = caretOffset;
  152. result[0] = importData;
  153. }
  154. }
  155. });
  156. return result[0];
  157. }
  158. private String createResultString(ImportData testResult) {
  159. StringBuilder sb = new StringBuilder();
  160. sb.append("Caret position: ").append(testResult.caretPosition);
  161. sb.append("\nShould show uses panel: ").append(testResult.shouldShowUsesPanel ? "true" : "false");
  162. sb.append("\nDefaults:\n");
  163. for (ImportData.DataItem dataItem : testResult.getItems()) {
  164. sb.append(" ").append(dataItem.getDefaultVariant().getName()).append("\n");
  165. }
  166. sb.append("\nNames:\n");
  167. for (ImportData.DataItem dataItem : testResult.getItems()) {
  168. sb.append(" ").append(dataItem.getTypeName()).append("\n");
  169. }
  170. sb.append("\nVariants:\n");
  171. for (ImportData.DataItem dataItem : testResult.getItems()) {
  172. for (ItemVariant itemVariant : dataItem.getVariants()) {
  173. sb.append(" ").append(itemVariant.getName()).append("\n");
  174. }
  175. sb.append("\n");
  176. }
  177. return sb.toString();
  178. }
  179. protected FileObject[] createSourceClassPathsForTest() {
  180. final File folder = new File(getDataDir(), getTestFolderPath());
  181. return new FileObject[]{FileUtil.toFileObject(folder)};
  182. }
  183. @Override
  184. protected Map<String, ClassPath> createClassPathsForTest() {
  185. return Collections.singletonMap(
  186. PhpSourcePath.SOURCE_CP,
  187. ClassPathSupport.createClassPath(new FileObject[] {
  188. FileUtil.toFileObject(new File(getDataDir(), getTestFolderPath()))
  189. })
  190. );
  191. }
  192. private String getTestFolderPath() {
  193. return "testfiles/actions/" + getTestName();//NOI18N
  194. }
  195. private String getTestPath() {
  196. return getTestFolderPath() + "/" + getName() + ".php";//NOI18N
  197. }
  198. private String getTestName() {
  199. String name = getName();
  200. int indexOf = name.indexOf("_");
  201. if (indexOf != -1) {
  202. name = name.substring(0, indexOf);
  203. }
  204. return name;
  205. }
  206. }