diff --git a/java.api.common/apichanges.xml b/java.api.common/apichanges.xml --- a/java.api.common/apichanges.xml +++ b/java.api.common/apichanges.xml @@ -105,6 +105,22 @@ + + + Added PlatformUiSupport.storePlatform with updatePreferredPlatform parameter + + + + + +

+ Added PlatformUiSupport.storePlatform with updatePreferredPlatform parameter allowing + the API clients to specify if the PreferredProjectPlatform should be updated. +

+
+ + +
Added methods to track ant invocation diff --git a/java.api.common/manifest.mf b/java.api.common/manifest.mf --- a/java.api.common/manifest.mf +++ b/java.api.common/manifest.mf @@ -1,4 +1,4 @@ Manifest-Version: 1.0 OpenIDE-Module: org.netbeans.modules.java.api.common/0 OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/api/common/resources/Bundle.properties -OpenIDE-Module-Specification-Version: 1.36 +OpenIDE-Module-Specification-Version: 1.37 diff --git a/java.api.common/nbproject/project.xml b/java.api.common/nbproject/project.xml --- a/java.api.common/nbproject/project.xml +++ b/java.api.common/nbproject/project.xml @@ -82,7 +82,7 @@ 1 - 1.13 + 1.46 diff --git a/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java b/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java --- a/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java +++ b/java.api.common/src/org/netbeans/modules/java/api/common/ui/PlatformUiSupport.java @@ -56,10 +56,13 @@ import javax.swing.*; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.api.annotations.common.NullAllowed; import org.netbeans.api.java.platform.JavaPlatform; import org.netbeans.api.java.platform.JavaPlatformManager; import org.netbeans.api.java.platform.Specification; import org.netbeans.modules.java.api.common.ant.UpdateHelper; +import org.netbeans.spi.java.project.support.PreferredProjectPlatform; import org.netbeans.spi.project.support.ant.EditableProperties; import org.openide.DialogDisplayer; import org.openide.NotifyDescriptor; @@ -129,6 +132,24 @@ } storePlatform(props, helper, projectConfigurationNamespace, platformKey, new SourceLevelKey(sourceLevel)); } + + + /** + * Stores active platform, javac.source and javac.target into the project's metadata. + * @param props project's shared properties + * @param helper {@link UpdateHelper} that is capable to upgrade project metadata if needed. + * @param projectConfigurationNamespace project configuration namespace. + * @param platformKey the {@link PlatformKey} got from the platform model. + * @param sourceLevelKey {@link SourceLevelKey} representing source level; can be null. + */ + public static void storePlatform( + @NonNull final EditableProperties props, + @NonNull final UpdateHelper helper, + @NonNull final String projectConfigurationNamespace, + @NonNull final Object platformKey, + @NullAllowed final Object sourceLevelKey) { + storePlatform(props, helper, projectConfigurationNamespace, platformKey, sourceLevelKey, true); + } /** * Stores active platform, javac.source and javac.target into the project's metadata. @@ -137,9 +158,16 @@ * @param projectConfigurationNamespace project configuration namespace. * @param platformKey the {@link PlatformKey} got from the platform model. * @param sourceLevelKey {@link SourceLevelKey} representing source level; can be null. + * @param updatePreferredPlatform if true the {@link PreferredProjectPlatform} will be updated + * @since 1.37 */ - public static void storePlatform(EditableProperties props, UpdateHelper helper, - String projectConfigurationNamespace, Object platformKey, Object sourceLevelKey) { + public static void storePlatform( + @NonNull final EditableProperties props, + @NonNull final UpdateHelper helper, + @NonNull final String projectConfigurationNamespace, + @NonNull final Object platformKey, + @NullAllowed final Object sourceLevelKey, + final boolean updatePreferredPlatform) { Parameters.notNull("props", props); //NOI18N Parameters.notNull("helper", helper); //NOI18N Parameters.notNull("projectConfigurationNamespace", projectConfigurationNamespace); //NOI18N @@ -157,7 +185,9 @@ if (platform == null) { return; } - + if (updatePreferredPlatform) { + PreferredProjectPlatform.setPreferredPlatform(platform); + } SpecificationVersion jdk13 = new SpecificationVersion("1.3"); //NOI18N String platformAntName = platform.getProperties().get("platform.ant.name"); //NOI18N assert platformAntName != null; diff --git a/java.j2seproject/nbproject/project.properties b/java.j2seproject/nbproject/project.properties --- a/java.j2seproject/nbproject/project.properties +++ b/java.j2seproject/nbproject/project.properties @@ -42,7 +42,7 @@ javac.compilerargs=-Xlint -Xlint:-serial javac.source=1.6 -spec.version.base=1.52.0 +spec.version.base=1.53.0 javadoc.arch=${basedir}/arch.xml javadoc.apichanges=${basedir}/apichanges.xml diff --git a/java.j2seproject/nbproject/project.xml b/java.j2seproject/nbproject/project.xml --- a/java.j2seproject/nbproject/project.xml +++ b/java.j2seproject/nbproject/project.xml @@ -142,7 +142,7 @@ 0-1 - 1.35 + 1.37 @@ -159,7 +159,7 @@ 1 - 1.41 + 1.46 diff --git a/java.j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProjectGenerator.java b/java.j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProjectGenerator.java --- a/java.j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProjectGenerator.java +++ b/java.j2seproject/src/org/netbeans/modules/java/j2seproject/J2SEProjectGenerator.java @@ -46,7 +46,10 @@ import java.io.File; import java.io.IOException; +import org.netbeans.api.java.platform.JavaPlatform; +import org.netbeans.api.java.platform.JavaPlatformManager; import org.netbeans.modules.java.j2seproject.api.J2SEProjectBuilder; +import org.netbeans.spi.java.project.support.PreferredProjectPlatform; import org.netbeans.spi.project.support.ant.AntProjectHelper; import org.openide.modules.SpecificationVersion; import org.openide.util.Parameters; @@ -77,6 +80,7 @@ setManifest(manifestFile). setLibrariesDefinitionFile(librariesDefinition). setSourceLevel(defaultSourceLevel). + setJavaPlatform(getPlatform()). build(); } @@ -96,8 +100,14 @@ setLibrariesDefinitionFile(librariesDefinition). setBuildXmlName(buildXmlName). setSourceLevel(defaultSourceLevel). + setJavaPlatform(getPlatform()). build(); } + + private static JavaPlatform getPlatform() { + return PreferredProjectPlatform.getPreferredPlatform( + JavaPlatformManager.getDefault().getDefaultPlatform().getSpecification().getName()); + } //------------ Used by unit tests ------------------- private static SpecificationVersion defaultSourceLevel; diff --git a/java.j2seproject/src/org/netbeans/modules/java/j2seproject/api/J2SEProjectBuilder.java b/java.j2seproject/src/org/netbeans/modules/java/j2seproject/api/J2SEProjectBuilder.java --- a/java.j2seproject/src/org/netbeans/modules/java/j2seproject/api/J2SEProjectBuilder.java +++ b/java.j2seproject/src/org/netbeans/modules/java/j2seproject/api/J2SEProjectBuilder.java @@ -99,7 +99,10 @@ * @author Tomas Zezula * @since 1.42 */ -public class J2SEProjectBuilder { +public class J2SEProjectBuilder { + + private static final String PLATFORM_ANT_NAME = "platform.ant.name"; //NOI18N + private static final String DEFAULT_PLATFORM_ID = "default_platform"; //NOI18N private final File projectDirectory; private final String name; @@ -118,6 +121,7 @@ private String buildXmlName; private String distFolder; private String mainClassTemplate; + private JavaPlatform platform; /** * Creates a new instance of {@link J2SEProjectBuilder} @@ -136,6 +140,7 @@ this.jvmArgs = new StringBuilder(); this.compileLibraries = new ArrayList(); this.runtimeLibraries = new ArrayList(); + this.platform = JavaPlatformManager.getDefault().getDefaultPlatform(); } /** @@ -295,6 +300,21 @@ } /** + * Sets a platform to be used for a new project + * @param platform to be used + * @return the builder + * @since 1.53 + */ + public J2SEProjectBuilder setJavaPlatform (@NonNull final JavaPlatform platform) { + Parameters.notNull("platform", platform); + if (platform.getProperties().get(PLATFORM_ANT_NAME) == null) { + throw new IllegalArgumentException("Invalid platform, the platform has no platform.ant.name"); //NOI18N + } + this.platform = platform; + return this; + } + + /** * Creates the J2SEProject * @return the {@link AntProjectHelper} of the created project * @throws IOException when creation fails @@ -305,7 +325,7 @@ dirFO.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() { @Override public void run() throws IOException { - final SpecificationVersion sourceLevel = getDefaultSourceLevel(); + final SpecificationVersion sourceLevel = getSourceLevel(); h[0] = createProject( dirFO, name, @@ -321,7 +341,8 @@ librariesDefinition, jvmArgs.toString(), toClassPathElements(compileLibraries), - toClassPathElements(runtimeLibraries, "${javac.classpath}:", "${build.classes.dir}")); //NOI18N + toClassPathElements(runtimeLibraries, "${javac.classpath}:", "${build.classes.dir}"), + platform.getProperties().get(PLATFORM_ANT_NAME)); //NOI18N final J2SEProject p = (J2SEProject) ProjectManager.getDefault().findProject(dirFO); ProjectManager.getDefault().saveProject(p); final ReferenceHelper refHelper = p.getReferenceHelper(); @@ -377,7 +398,9 @@ String librariesDefinition, String jvmArgs, String[] compileClassPath, - String[] runtimeClassPath) throws IOException { + String[] runtimeClassPath, + @NonNull final String platformId + ) throws IOException { AntProjectHelper h = ProjectGenerator.createProject(dirFO, J2SEProject.TYPE, librariesDefinition); Element data = h.getPrimaryConfigurationData(true); @@ -386,6 +409,13 @@ nameEl.appendChild(doc.createTextNode(name)); data.appendChild(nameEl); EditableProperties ep = h.getProperties(AntProjectHelper.PROJECT_PROPERTIES_PATH); + if (!DEFAULT_PLATFORM_ID.equals(platformId)) { + final Element platformEl = doc.createElementNS(J2SEProject.PROJECT_CONFIGURATION_NAMESPACE, "explicit-platform"); //NOI18N + final SpecificationVersion jdk13 = new SpecificationVersion("1.3"); //NOI18N + final boolean supportsExplicitSource = jdk13.compareTo(sourceLevel) < 0; + platformEl.setAttribute("explicit-source-supported", Boolean.toString(supportsExplicitSource)); //NOI18N + data.appendChild(platformEl); + } Element sourceRoots = doc.createElementNS(J2SEProject.PROJECT_CONFIGURATION_NAMESPACE,"source-roots"); //NOI18N if (srcRoot != null) { Element root = doc.createElementNS (J2SEProject.PROJECT_CONFIGURATION_NAMESPACE,"root"); //NOI18N @@ -467,7 +497,7 @@ ep.setProperty("build.test.results.dir", "${build.dir}/test/results"); // NOI18N ep.setProperty("build.classes.excludes", "**/*.java,**/*.form"); // NOI18N ep.setProperty("dist.javadoc.dir", "${dist.dir}/javadoc"); // NOI18N - ep.setProperty("platform.active", "default_platform"); // NOI18N + ep.setProperty("platform.active", platformId); // NOI18N ep.setProperty(ProjectProperties.RUN_JVM_ARGS, jvmArgs); // NOI18N ep.setComment(ProjectProperties.RUN_JVM_ARGS, new String[] { @@ -559,12 +589,11 @@ Logger.getLogger(loggerName).log(logRecord); } - private SpecificationVersion getDefaultSourceLevel () { + private SpecificationVersion getSourceLevel () { if (defaultSourceLevel != null) { return defaultSourceLevel; } else { - final JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform(); - final SpecificationVersion v = defaultPlatform.getSpecification().getVersion(); + final SpecificationVersion v = platform.getSpecification().getVersion(); return v; } } diff --git a/java.j2seproject/src/org/netbeans/modules/java/j2seproject/ui/customizer/J2SEProjectProperties.java b/java.j2seproject/src/org/netbeans/modules/java/j2seproject/ui/customizer/J2SEProjectProperties.java --- a/java.j2seproject/src/org/netbeans/modules/java/j2seproject/ui/customizer/J2SEProjectProperties.java +++ b/java.j2seproject/src/org/netbeans/modules/java/j2seproject/ui/customizer/J2SEProjectProperties.java @@ -540,7 +540,8 @@ projectGroup.store( projectProperties ); privateGroup.store( privateProperties ); - if(!isFXProject()) { + final boolean isFXProject = isFXProject(); + if(!isFXProject) { storeRunConfigs(RUN_CONFIGS, projectProperties, privateProperties); EditableProperties ep = updateHelper.getProperties("nbproject/private/config.properties"); if (activeConfig == null) { @@ -571,7 +572,13 @@ projectProperties.setProperty( ProjectProperties.ENDORSED_CLASSPATH, endorsed_cp ); //Handle platform selection and javac.source javac.target properties - PlatformUiSupport.storePlatform (projectProperties, updateHelper, J2SEProject.PROJECT_CONFIGURATION_NAMESPACE, PLATFORM_MODEL.getSelectedItem(), JAVAC_SOURCE_MODEL.getSelectedItem()); + PlatformUiSupport.storePlatform ( + projectProperties, + updateHelper, + J2SEProject.PROJECT_CONFIGURATION_NAMESPACE, + PLATFORM_MODEL.getSelectedItem(), + JAVAC_SOURCE_MODEL.getSelectedItem(), + !isFXProject); // Handle other special cases if ( NO_DEPENDENCIES_MODEL.isSelected() ) { // NOI18N diff --git a/java.project/apichanges.xml b/java.project/apichanges.xml --- a/java.project/apichanges.xml +++ b/java.project/apichanges.xml @@ -109,6 +109,22 @@ + + + Added PreferredProjectPlatform + + + + + +

+ Added PreferredProjectPlatform to provide a + JavaPlatform which should be used for a new project. +

+
+ + +
Added diff --git a/java.project/manifest.mf b/java.project/manifest.mf --- a/java.project/manifest.mf +++ b/java.project/manifest.mf @@ -3,7 +3,7 @@ OpenIDE-Module-Layer: org/netbeans/modules/java/project/layer.xml OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/java/project/Bundle.properties OpenIDE-Module-Needs: javax.script.ScriptEngine.freemarker -OpenIDE-Module-Specification-Version: 1.45 +OpenIDE-Module-Specification-Version: 1.46 OpenIDE-Module-Recommends: org.netbeans.spi.java.project.runner.JavaRunnerImplementation AutoUpdate-Show-In-Client: false diff --git a/java.project/src/org/netbeans/spi/java/project/support/PreferredProjectPlatform.java b/java.project/src/org/netbeans/spi/java/project/support/PreferredProjectPlatform.java new file mode 100644 --- /dev/null +++ b/java.project/src/org/netbeans/spi/java/project/support/PreferredProjectPlatform.java @@ -0,0 +1,108 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 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 2012 Sun Microsystems, Inc. + */ +package org.netbeans.spi.java.project.support; + +import java.text.MessageFormat; +import org.netbeans.api.annotations.common.CheckForNull; +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.api.java.platform.JavaPlatform; +import org.netbeans.api.java.platform.JavaPlatformManager; +import org.netbeans.api.java.platform.Specification; +import org.openide.util.NbPreferences; +import org.openide.util.Parameters; + +/** + * Returns a preferred {@link JavaPlatform} for a new project. + * @author Tomas Zezula + * @since 1.46 + */ +public final class PreferredProjectPlatform { + + private static final String PREFERRED_PLATFORM = "preferred.platform.{0}"; //NOI18N + private static final String PLATFORM_ANT_NAME = "platform.ant.name"; //NOI18N + + private PreferredProjectPlatform() { + throw new AssertionError(); + } + + /** + * Returns a preferred {@link JavaPlatform} for a new project. + * @param platformType the platform type as specified by {@link Specification#getName()} + * @return the preferred {@link JavaPlatform} + */ + @CheckForNull + public static JavaPlatform getPreferredPlatform(@NonNull final String platformType) { + Parameters.notNull("platformType", platformType); //NOI18N + final String platformId = NbPreferences.forModule(PreferredProjectPlatform.class).get( + MessageFormat.format(PREFERRED_PLATFORM, platformType), + null); + final JavaPlatformManager jpm = JavaPlatformManager.getDefault(); + if (platformId != null) { + for (JavaPlatform jp : jpm.getInstalledPlatforms()) { + if (platformId.equals(jp.getProperties().get(PLATFORM_ANT_NAME)) && + platformType.equals(jp.getSpecification().getName())) { + return jp; + } + } + } + final JavaPlatform defaultPlatform = jpm.getDefaultPlatform(); + return platformType.equals(defaultPlatform.getSpecification().getName())? + defaultPlatform: + null; + } + + /** + * Sets a preferred {@link JavaPlatform} for a new project. + * @param platform the preferred {@link JavaPlatform} + */ + public static void setPreferredPlatform(@NonNull final JavaPlatform platform) { + Parameters.notNull("platform", platform); //NOI18N + final String platformId = platform.getProperties().get(PLATFORM_ANT_NAME); + if (platformId == null) { + throw new IllegalArgumentException("Invalid platform, the platform has no platform.ant.name"); //NOI18N + } + final String platformType = platform.getSpecification().getName(); + NbPreferences.forModule(PreferredProjectPlatform.class).put( + MessageFormat.format(PREFERRED_PLATFORM, platformType), + platformId); + } +} diff --git a/java.project/test/unit/src/org/netbeans/spi/java/project/support/PreferredProjectPlatformTest.java b/java.project/test/unit/src/org/netbeans/spi/java/project/support/PreferredProjectPlatformTest.java new file mode 100644 --- /dev/null +++ b/java.project/test/unit/src/org/netbeans/spi/java/project/support/PreferredProjectPlatformTest.java @@ -0,0 +1,214 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2012 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 2012 Sun Microsystems, Inc. + */ +package org.netbeans.spi.java.project.support; + +import java.beans.PropertyChangeListener; +import java.net.URL; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.netbeans.api.annotations.common.NonNull; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.platform.JavaPlatform; +import org.netbeans.api.java.platform.JavaPlatformManager; +import org.netbeans.api.java.platform.Specification; +import org.netbeans.junit.MockServices; +import org.netbeans.junit.NbTestCase; +import org.netbeans.modules.java.platform.JavaPlatformProvider; +import org.openide.filesystems.FileObject; +import org.openide.modules.SpecificationVersion; + + +/** + * + * @author Tomas Zezula + */ +public class PreferredProjectPlatformTest extends NbTestCase { + + private static final String J2SE = "j2se"; //NOI18N + private static final String J2ME = "j2me"; //NOI18N + + public PreferredProjectPlatformTest(final String name) { + super(name); + } + + @Override + protected void setUp() throws Exception { + super.setUp(); + MockServices.setServices(MockJavaPlatformProvider.class); + } + + + public void testPreferredPlatforms() { + final JavaPlatform defaultPlatform = JavaPlatformManager.getDefault().getDefaultPlatform(); + final JavaPlatform[] javaPlatforms = JavaPlatformManager.getDefault().getInstalledPlatforms(); + final JavaPlatform[] nonDefaultJ2SEPlatforms = new JavaPlatform[javaPlatforms.length - 2]; + JavaPlatform j2mePlatform = null; + for (int i = 0, j = 0; i < javaPlatforms.length; i++ ) { + if (!javaPlatforms[i].equals(defaultPlatform) && + J2SE.equals(javaPlatforms[i].getSpecification().getName())) { + nonDefaultJ2SEPlatforms[j++] = javaPlatforms[i]; + } + if (J2ME.equals(javaPlatforms[i].getSpecification().getName())) { + j2mePlatform = javaPlatforms[i]; + } + } + assertEquals(defaultPlatform, PreferredProjectPlatform.getPreferredPlatform(J2SE)); + PreferredProjectPlatform.setPreferredPlatform(nonDefaultJ2SEPlatforms[0]); + assertEquals(nonDefaultJ2SEPlatforms[0], PreferredProjectPlatform.getPreferredPlatform(J2SE)); + PreferredProjectPlatform.setPreferredPlatform(nonDefaultJ2SEPlatforms[1]); + assertEquals(nonDefaultJ2SEPlatforms[1], PreferredProjectPlatform.getPreferredPlatform(J2SE)); + PreferredProjectPlatform.setPreferredPlatform(defaultPlatform); + assertEquals(defaultPlatform, PreferredProjectPlatform.getPreferredPlatform(J2SE)); + assertNull(PreferredProjectPlatform.getPreferredPlatform(J2ME)); + PreferredProjectPlatform.setPreferredPlatform(j2mePlatform); + assertEquals(j2mePlatform, PreferredProjectPlatform.getPreferredPlatform(J2ME)); + } + + + public static final class MockJavaPlatformProvider implements JavaPlatformProvider { + + private final JavaPlatform[] platforms; + + public MockJavaPlatformProvider() { + this.platforms = new JavaPlatform[4]; + this.platforms[0] = new MockJavaPlatform("default_platform", J2SE); //NOI18N + this.platforms[1] = new MockJavaPlatform("nondefault_platform_1", J2SE); //NOI18N + this.platforms[2] = new MockJavaPlatform("nondefault_platform_2", J2SE); //NOI18N + this.platforms[3] = new MockJavaPlatform("j2me_platform", J2ME); //NOI18N + } + + @Override + public JavaPlatform[] getInstalledPlatforms() { + return platforms; + } + + @Override + public JavaPlatform getDefaultPlatform() { + return platforms[0]; + } + + @Override + public void addPropertyChangeListener(PropertyChangeListener listener) { + } + + @Override + public void removePropertyChangeListener(PropertyChangeListener listener) { + } + + } + + private static final class MockJavaPlatform extends JavaPlatform { + + private final String displayName; + private final String platformType; + private final Map props; + + MockJavaPlatform( + @NonNull final String name, + @NonNull final String platformType) { + super(); + this.displayName = name; + this.props = new HashMap(); + this.props.put("platform.ant.name",name); //NOI18N + this.platformType = platformType; + } + + @Override + public String getDisplayName() { + return displayName; + } + + @Override + public Map getProperties() { + return props; + } + + @Override + public ClassPath getBootstrapLibraries() { + return ClassPath.EMPTY; + } + + @Override + public ClassPath getStandardLibraries() { + return ClassPath.EMPTY; + } + + @Override + public String getVendor() { + return "me"; //NOI18N + } + + @Override + public Specification getSpecification() { + return new Specification(platformType, new SpecificationVersion("1.6")); //NOI18N + } + + @Override + public Collection getInstallFolders() { + return Collections.emptySet(); + } + + @Override + public FileObject findTool(String toolName) { + return null; + } + + @Override + public ClassPath getSourceFolders() { + return ClassPath.EMPTY; + } + + @Override + public List getJavadocFolders() { + return Collections.emptyList(); + } + + @Override + public String toString() { + return displayName; + } + + } +}