diff --git a/php.codeception/src/org/netbeans/modules/php/codeception/ImportantFilesImpl.java b/php.codeception/src/org/netbeans/modules/php/codeception/ImportantFilesImpl.java new file mode 100644 --- /dev/null +++ b/php.codeception/src/org/netbeans/modules/php/codeception/ImportantFilesImpl.java @@ -0,0 +1,128 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2015 Oracle and/or its affiliates. All rights reserved. + * + * Oracle and Java are registered trademarks of Oracle and/or its affiliates. + * Other names may be trademarks of their respective owners. + * + * The contents of this file are subject to the terms of either the GNU + * General Public License Version 2 only ("GPL") or the Common + * Development and Distribution License("CDDL") (collectively, the + * "License"). You may not use this file except in compliance with the + * License. You can obtain a copy of the License at + * http://www.netbeans.org/cddl-gplv2.html + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the + * specific language governing permissions and limitations under the + * License. When distributing the software, include this License Header + * Notice in each file and include the License file at + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the GPL Version 2 section of the License file that + * accompanied this code. If applicable, add the following below the + * License Header, with the fields enclosed by brackets [] replaced by + * your own identifying information: + * "Portions Copyrighted [year] [name of copyright owner]" + * + * If you wish your version of this file to be governed by only the CDDL + * or only the GPL Version 2, indicate your decision by adding + * "[Contributor] elects to include this software in this distribution + * under the [CDDL or GPL Version 2] license." If you do not indicate a + * single choice of license, a recipient has the option to distribute + * your version of this file under either the CDDL, the GPL Version 2 or + * to extend the choice of license to its licensees as provided above. + * However, if you add GPL Version 2 code and therefore, elected the GPL + * Version 2 license, then the option applies only if the new code is + * made subject to such option by the copyright holder. + * + * Contributor(s): + * + * Portions Copyrighted 2015 Sun Microsystems, Inc. + */ +package org.netbeans.modules.php.codeception; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.prefs.PreferenceChangeEvent; +import java.util.prefs.PreferenceChangeListener; +import javax.swing.event.ChangeListener; +import org.netbeans.api.project.Project; +import org.netbeans.modules.php.api.phpmodule.PhpModule; +import org.netbeans.modules.php.codeception.commands.Codecept; +import org.netbeans.modules.php.codeception.preferences.CodeceptionPreferences; +import org.netbeans.modules.php.spi.phpmodule.ImportantFilesImplementation; +import org.netbeans.modules.php.spi.phpmodule.ImportantFilesSupport; +import org.netbeans.spi.project.ProjectServiceProvider; +import org.openide.filesystems.FileObject; +import org.openide.util.ChangeSupport; +import org.openide.util.WeakListeners; + +public final class ImportantFilesImpl implements ImportantFilesImplementation, PreferenceChangeListener { + + private final Project project; + private final ChangeSupport changeSupport = new ChangeSupport(this); + private final ImportantFilesSupport defaultConfigSupport; + private boolean initialized = false; + + private ImportantFilesImpl(Project project) { + assert project != null; + this.project = project; + defaultConfigSupport = ImportantFilesSupport.create(project.getProjectDirectory(), Codecept.CODECEPTION_CONFIG_FILE_NAME); + } + + @ProjectServiceProvider(service = ImportantFilesImplementation.class, projectType = "org-netbeans-modules-php-project") // NOI18N + public static ImportantFilesImplementation createImpotantFiles(Project project) { + ImportantFilesImpl importantFiles = new ImportantFilesImpl(project); + return importantFiles; + } + + @Override + public Collection getFiles() { + PhpModule phpModule = PhpModule.Factory.lookupPhpModule(project); + if (phpModule == null) { + return Collections.emptyList(); + } + if (!initialized) { + initialized = true; + CodeceptionPreferences.addPreferenceChangeListener(phpModule, WeakListeners.create(PreferenceChangeListener.class, this, CodeceptionPreferences.class)); + } + + // global configuration + List files = new ArrayList<>(); + files.addAll(defaultConfigSupport.getFiles(null)); + if (CodeceptionPreferences.isCustomCodeceptionYmlEnabled(phpModule)) { + FileObject codeceptionYml = Codecept.getCodeceptionYml(phpModule); + if (codeceptionYml != null) { + files.add(new FileInfo(codeceptionYml)); + } + } + return files; + } + + @Override + public void addChangeListener(ChangeListener listener) { + changeSupport.addChangeListener(listener); + defaultConfigSupport.addChangeListener(listener); + } + + @Override + public void removeChangeListener(ChangeListener listener) { + changeSupport.removeChangeListener(listener); + defaultConfigSupport.removeChangeListener(listener); + } + + @Override + public void preferenceChange(PreferenceChangeEvent evt) { + if (CodeceptionPreferences.CUSTOM_CODECEPTION_YML_PATH.equals(evt.getKey()) + || CodeceptionPreferences.CUSTOM_CODECEPTION_YML_ENABLED.equals(evt.getKey())) { + fireChange(); + } + } + + void fireChange() { + changeSupport.fireChange(); + } + +} diff --git a/php.codeception/src/org/netbeans/modules/php/codeception/commands/Codecept.java b/php.codeception/src/org/netbeans/modules/php/codeception/commands/Codecept.java --- a/php.codeception/src/org/netbeans/modules/php/codeception/commands/Codecept.java +++ b/php.codeception/src/org/netbeans/modules/php/codeception/commands/Codecept.java @@ -134,6 +134,7 @@ private static final String CEST_FILE_SUFFIX = CEST_CLASS_SUFFIX + ".php"; // NOI18N public static final String CEPT_CLASS_SUFFIX = "Cept"; // NOI18N private static final String CEPT_FILE_SUFFIX = CEPT_CLASS_SUFFIX + ".php"; // NOI18N + private static final String SUITE_CONFIG_SUFFIX = ".suite.yml"; // NOI18N // test method prefix public static final String TEST_METHOD_PREFIX = "test"; // NOI18N @@ -506,12 +507,11 @@ } @CheckForNull - public FileObject getCodeceptionYml(PhpModule phpModule) { + public static FileObject getCodeceptionYml(PhpModule phpModule) { if (phpModule == null) { return null; } // custom - // XXX allow only the source directory? // A PHP Framework may have a codeception.yml in an inner directory. // e.g. In case of Yii2 framework, source/tests/codeception.yml if (CodeceptionPreferences.isCustomCodeceptionYmlEnabled(phpModule)) { @@ -557,7 +557,7 @@ continue; } String name = child.getNameExt(); - int lastIndexOfSuiteSuffix = name.lastIndexOf(".suite.yml"); // NOI18N + int lastIndexOfSuiteSuffix = name.lastIndexOf(SUITE_CONFIG_SUFFIX); if (lastIndexOfSuiteSuffix != -1) { suites.add(name.substring(0, lastIndexOfSuiteSuffix)); } diff --git a/php.codeception/src/org/netbeans/modules/php/codeception/preferences/CodeceptionPreferences.java b/php.codeception/src/org/netbeans/modules/php/codeception/preferences/CodeceptionPreferences.java --- a/php.codeception/src/org/netbeans/modules/php/codeception/preferences/CodeceptionPreferences.java +++ b/php.codeception/src/org/netbeans/modules/php/codeception/preferences/CodeceptionPreferences.java @@ -42,6 +42,9 @@ package org.netbeans.modules.php.codeception.preferences; import java.io.File; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; +import java.util.prefs.PreferenceChangeListener; import java.util.prefs.Preferences; import org.netbeans.api.annotations.common.CheckForNull; import org.netbeans.modules.php.api.phpmodule.PhpModule; @@ -54,10 +57,11 @@ private static final String CUSTOM_CODECEPT_ENABLED = "custom.codecept.enabled"; // NOI18N private static final String CUSTOM_CODECEPT_PATH = "custom.codecept.path"; // NOI18N - private static final String CUSTOM_CODECEPTION_YML_ENABLED = "custom.codeception.yml.enabled"; // NOI18N - private static final String CUSTOM_CODECEPTION_YML_PATH = "custom.codeception.yml.path"; // NOI18N + public static final String CUSTOM_CODECEPTION_YML_ENABLED = "custom.codeception.yml.enabled"; // NOI18N + public static final String CUSTOM_CODECEPTION_YML_PATH = "custom.codeception.yml.path"; // NOI18N private static final String ASK_FOR_ADDITIONAL_PARAMS = "additional.params.ask"; // NOI18N + private static final ConcurrentMap CACHE = new ConcurrentHashMap<>(); private CodeceptionPreferences() { } @@ -104,8 +108,25 @@ getPreference(phpModule).putBoolean(ASK_FOR_ADDITIONAL_PARAMS, ask); } + public static void addPreferenceChangeListener(PhpModule phpModule, PreferenceChangeListener listener) { + getPreference(phpModule).addPreferenceChangeListener(listener); + } + + public static void removePreferenceChangeListener(PhpModule phpModule, PreferenceChangeListener listener) { + getPreference(phpModule).removePreferenceChangeListener(listener); + } + private static Preferences getPreference(PhpModule phpModule) { - return phpModule.getPreferences(CodeceptionTestingProvider.class, true); + Preferences preferences = CACHE.get(phpModule); + if (preferences == null) { + preferences = phpModule.getPreferences(CodeceptionTestingProvider.class, true); + Preferences currentPreferences = CACHE.putIfAbsent(phpModule, preferences); + if (currentPreferences != null) { + preferences = currentPreferences; + } + } + assert preferences != null; + return preferences; } private static String relativizePath(PhpModule phpModule, String filePath) {