sandbox/old-modules/bridges/refactoring/test/unit/src/org/netbeans/modules/jackpot30/refactoring/RefactoringTestBase.java
1.1 --- a/sandbox/old-modules/bridges/refactoring/test/unit/src/org/netbeans/modules/jackpot30/refactoring/RefactoringTestBase.java Mon Dec 19 11:37:36 2016 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,251 +0,0 @@
1.4 -/*
1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
1.6 - *
1.7 - * Copyright 2009-2010 Sun Microsystems, Inc. All rights reserved.
1.8 - *
1.9 - * The contents of this file are subject to the terms of either the GNU
1.10 - * General Public License Version 2 only ("GPL") or the Common
1.11 - * Development and Distribution License("CDDL") (collectively, the
1.12 - * "License"). You may not use this file except in compliance with the
1.13 - * License. You can obtain a copy of the License at
1.14 - * http://www.netbeans.org/cddl-gplv2.html
1.15 - * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
1.16 - * specific language governing permissions and limitations under the
1.17 - * License. When distributing the software, include this License Header
1.18 - * Notice in each file and include the License file at
1.19 - * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
1.20 - * particular file as subject to the "Classpath" exception as provided
1.21 - * by Sun in the GPL Version 2 section of the License file that
1.22 - * accompanied this code. If applicable, add the following below the
1.23 - * License Header, with the fields enclosed by brackets [] replaced by
1.24 - * your own identifying information:
1.25 - * "Portions Copyrighted [year] [name of copyright owner]"
1.26 - *
1.27 - * If you wish your version of this file to be governed by only the CDDL
1.28 - * or only the GPL Version 2, indicate your decision by adding
1.29 - * "[Contributor] elects to include this software in this distribution
1.30 - * under the [CDDL or GPL Version 2] license." If you do not indicate a
1.31 - * single choice of license, a recipient has the option to distribute
1.32 - * your version of this file under either the CDDL, the GPL Version 2 or
1.33 - * to extend the choice of license to its licensees as provided above.
1.34 - * However, if you add GPL Version 2 code and therefore, elected the GPL
1.35 - * Version 2 license, then the option applies only if the new code is
1.36 - * made subject to such option by the copyright holder.
1.37 - *
1.38 - * Contributor(s):
1.39 - *
1.40 - * Portions Copyrighted 2009-2010 Sun Microsystems, Inc.
1.41 - */
1.42 -package org.netbeans.modules.jackpot30.refactoring;
1.43 -
1.44 -import java.io.IOException;
1.45 -import java.util.Arrays;
1.46 -import java.util.Collections;
1.47 -import java.util.HashMap;
1.48 -import java.util.HashSet;
1.49 -import java.util.LinkedList;
1.50 -import java.util.List;
1.51 -import java.util.Map;
1.52 -import java.util.Set;
1.53 -import org.netbeans.api.editor.mimelookup.MimePath;
1.54 -import org.netbeans.api.java.classpath.ClassPath;
1.55 -import org.netbeans.api.java.classpath.GlobalPathRegistry;
1.56 -import org.netbeans.api.java.source.SourceUtils;
1.57 -import org.netbeans.api.java.source.SourceUtilsTestUtil;
1.58 -import org.netbeans.api.java.source.TestUtilities;
1.59 -import org.netbeans.api.project.Project;
1.60 -import org.netbeans.api.project.ProjectManager;
1.61 -import org.netbeans.core.startup.Main;
1.62 -import org.netbeans.junit.NbTestCase;
1.63 -import org.netbeans.modules.java.source.indexing.JavaCustomIndexer;
1.64 -import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
1.65 -import org.netbeans.modules.parsing.impl.indexing.MimeTypes;
1.66 -import org.netbeans.modules.parsing.impl.indexing.RepositoryUpdater;
1.67 -import org.netbeans.modules.refactoring.api.AbstractRefactoring;
1.68 -import org.netbeans.modules.refactoring.api.RefactoringSession;
1.69 -import org.netbeans.spi.editor.mimelookup.MimeDataProvider;
1.70 -import org.netbeans.spi.java.classpath.ClassPathProvider;
1.71 -import org.netbeans.spi.java.classpath.support.ClassPathSupport;
1.72 -import org.netbeans.spi.project.ProjectFactory;
1.73 -import org.netbeans.spi.project.ProjectState;
1.74 -import org.openide.filesystems.FileObject;
1.75 -import org.openide.filesystems.FileUtil;
1.76 -import org.openide.util.Lookup;
1.77 -import org.openide.util.lookup.Lookups;
1.78 -import org.openide.util.lookup.ServiceProvider;
1.79 -
1.80 -public class RefactoringTestBase extends NbTestCase {
1.81 -
1.82 - public RefactoringTestBase(String name) {
1.83 - super(name);
1.84 - }
1.85 -
1.86 - protected static void writeFilesAndWaitForScan(FileObject sourceRoot, File... files) throws Exception {
1.87 - for (FileObject c : sourceRoot.getChildren()) {
1.88 - c.delete();
1.89 - }
1.90 -
1.91 - for (File f : files) {
1.92 - FileObject fo = FileUtil.createData(sourceRoot, f.filename);
1.93 - TestUtilities.copyStringToFile(fo, f.content);
1.94 - }
1.95 -
1.96 - SourceUtils.waitScanFinished();
1.97 - }
1.98 -
1.99 - protected static final class File {
1.100 - public final String filename;
1.101 - public final String content;
1.102 -
1.103 - public File(String filename, String content) {
1.104 - this.filename = filename;
1.105 - this.content = content;
1.106 - }
1.107 - }
1.108 -
1.109 - protected FileObject src;
1.110 - protected Project prj;
1.111 -
1.112 - @Override
1.113 - protected void setUp() throws Exception {
1.114 - SourceUtilsTestUtil.prepareTest(new String[0], new Object[] {
1.115 - new ClassPathProvider() {
1.116 - public ClassPath findClassPath(FileObject file, String type) {
1.117 - if (src != null && (file == src || FileUtil.isParentOf(src, file))) {
1.118 - if (ClassPath.BOOT.equals(type)) {
1.119 - return ClassPathSupport.createClassPath(System.getProperty("sun.boot.class.path"));
1.120 - }
1.121 - if (ClassPath.COMPILE.equals(type)) {
1.122 - return ClassPathSupport.createClassPath(new FileObject[0]);
1.123 - }
1.124 - if (ClassPath.SOURCE.equals(type)) {
1.125 - return ClassPathSupport.createClassPath(src);
1.126 - }
1.127 - }
1.128 -
1.129 - return null;
1.130 - }
1.131 - },
1.132 - new ProjectFactory() {
1.133 - public boolean isProject(FileObject projectDirectory) {
1.134 - return src == projectDirectory;
1.135 - }
1.136 - public Project loadProject(final FileObject projectDirectory, ProjectState state) throws IOException {
1.137 - if (!isProject(projectDirectory)) return null;
1.138 - return new Project() {
1.139 - public FileObject getProjectDirectory() {
1.140 - return projectDirectory;
1.141 - }
1.142 - public Lookup getLookup() {
1.143 - return Lookup.EMPTY;
1.144 - }
1.145 - };
1.146 - }
1.147 - public void saveProject(Project project) throws IOException, ClassCastException {}
1.148 - }
1.149 - });
1.150 - Main.initializeURLFactory();
1.151 - org.netbeans.api.project.ui.OpenProjects.getDefault().getOpenProjects();
1.152 - prepareTest();
1.153 - org.netbeans.api.project.ui.OpenProjects.getDefault().open(new Project[] {prj = ProjectManager.getDefault().findProject(src)}, false);
1.154 - MimeTypes.setAllMimeTypes(Collections.singleton("text/x-java"));
1.155 - GlobalPathRegistry.getDefault().register(ClassPath.SOURCE, new ClassPath[] {ClassPathSupport.createClassPath(src)});
1.156 - RepositoryUpdater.getDefault().start(true);
1.157 - super.setUp();
1.158 - }
1.159 -
1.160 - @Override
1.161 - protected void tearDown() throws Exception {
1.162 - super.tearDown();
1.163 - org.netbeans.api.project.ui.OpenProjects.getDefault().open(new Project[] {prj}, false);
1.164 - prj = null;
1.165 - }
1.166 -
1.167 - private void prepareTest() throws Exception {
1.168 - FileObject workdir = SourceUtilsTestUtil.makeScratchDir(this);
1.169 -
1.170 - src = FileUtil.createFolder(workdir, "src");
1.171 -
1.172 - FileObject cache = FileUtil.createFolder(workdir, "cache");
1.173 -
1.174 - CacheFolder.setCacheFolder(cache);
1.175 - }
1.176 -
1.177 - protected void performRefactoring(AbstractRefactoring r, File... verify) throws IOException {
1.178 - Map<FileObject, String> originalContent = new HashMap<FileObject, String>();
1.179 -
1.180 - for (FileObject file : recursive(src)) {
1.181 - originalContent.put(file, readFile(file));
1.182 - }
1.183 -
1.184 - RefactoringSession rs = RefactoringSession.create("Session");
1.185 -
1.186 - r.prepare(rs);
1.187 -
1.188 - assertNull(rs.doRefactoring(true));
1.189 -
1.190 -// IndexingManager.getDefault().refreshIndex(src.getURL(), null);
1.191 -// SourceUtils.waitScanFinished();
1.192 -// assertEquals(false, TaskCache.getDefault().isInError(src, true));
1.193 -
1.194 - for (File v : verify) {
1.195 - FileObject file = src.getFileObject(v.filename);
1.196 -
1.197 - assertNotNull(v.filename, file);
1.198 -
1.199 - String content = readFile(file);
1.200 -
1.201 - assertEquals(v.content.replaceAll("[ \n\t]+", " "), content.replaceAll("[ \n\t]+", " "));
1.202 - }
1.203 -
1.204 - rs.undoRefactoring(true);
1.205 -
1.206 - for (FileObject file : recursive(src)) {
1.207 - String content = originalContent.remove(file);
1.208 -
1.209 - assertNotNull(FileUtil.getFileDisplayName(file), content);
1.210 - assertEquals(content, readFile(file));
1.211 - }
1.212 -
1.213 - assertTrue(originalContent.toString(), originalContent.isEmpty());
1.214 - }
1.215 -
1.216 - private String readFile(FileObject file) throws IOException {
1.217 - return TestUtilities.copyFileToString(FileUtil.toFile(file));
1.218 - }
1.219 -
1.220 - private static Iterable<? extends FileObject> recursive(FileObject r) {
1.221 - Set<FileObject> result = new HashSet<FileObject>();
1.222 - List<FileObject> queue = new LinkedList<FileObject>();
1.223 -
1.224 - queue.add(r);
1.225 -
1.226 - while (!queue.isEmpty()) {
1.227 - FileObject c = queue.remove(0);
1.228 -
1.229 - if (c.isData()) {
1.230 - result.add(c);
1.231 - } else {
1.232 - queue.addAll(Arrays.asList(c.getChildren()));
1.233 - }
1.234 - }
1.235 -
1.236 - return result;
1.237 - }
1.238 -
1.239 - @ServiceProvider(service=MimeDataProvider.class)
1.240 - public static final class MimeDataProviderImpl implements MimeDataProvider {
1.241 -
1.242 - private static final Lookup L = Lookups.singleton(new JavaCustomIndexer.Factory());
1.243 -
1.244 - public Lookup getLookup(MimePath mimePath) {
1.245 - if ("text/x-java".equals(mimePath.getPath())) {
1.246 - return L;
1.247 - }
1.248 -
1.249 - return null;
1.250 - }
1.251 -
1.252 - }
1.253 -
1.254 -}