### Eclipse Workspace Patch 1.0 #P org.eclipse.pde.ui.tests Index: src/org/eclipse/pde/ui/tests/build/properties/AllValidatorTests.java =================================================================== RCS file: src/org/eclipse/pde/ui/tests/build/properties/AllValidatorTests.java diff -N src/org/eclipse/pde/ui/tests/build/properties/AllValidatorTests.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/pde/ui/tests/build/properties/AllValidatorTests.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,27 @@ +/******************************************************************************* + * Copyright (c) 2010 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.pde.ui.tests.build.properties; + +import junit.framework.Test; +import junit.framework.TestSuite; + +/** + * @since 3.6 + */ +public class AllValidatorTests { + + public static Test suite() { + TestSuite suite = new TestSuite("Test Suite for testing build.properties validations"); //$NON-NLS-1$ + suite.addTest(BuildPropertiesValidator.suite()); + return suite; + } + +} Index: src/org/eclipse/pde/ui/tests/build/properties/BaseValidator.java =================================================================== RCS file: src/org/eclipse/pde/ui/tests/build/properties/BaseValidator.java diff -N src/org/eclipse/pde/ui/tests/build/properties/BaseValidator.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/pde/ui/tests/build/properties/BaseValidator.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,303 @@ +/******************************************************************************* + * Copyright (c) 2010 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.pde.ui.tests.build.properties; + +import org.eclipse.core.runtime.CoreException; + +import org.eclipse.core.resources.IMarker; + +import java.io.*; +import java.net.URL; +import java.util.Enumeration; +import java.util.PropertyResourceBundle; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; +import junit.framework.*; +import org.eclipse.core.resources.*; +import org.eclipse.core.runtime.*; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.core.runtime.preferences.IEclipsePreferences; +import org.eclipse.pde.core.plugin.IPluginModelBase; +import org.eclipse.pde.core.plugin.PluginRegistry; +import org.eclipse.pde.internal.core.builders.CompilerFlags; +import org.eclipse.pde.internal.core.builders.PDEMarkerFactory; +import org.eclipse.pde.internal.core.natures.PDE; +import org.eclipse.pde.internal.ui.PDEPlugin; +import org.eclipse.pde.internal.ui.correction.ResolutionGenerator; +import org.eclipse.pde.internal.ui.tests.macro.MacroPlugin; +import org.eclipse.pde.ui.tests.target.LocalTargetDefinitionTests; +import org.eclipse.ui.IMarkerResolution; +import org.osgi.service.prefs.BackingStoreException; + +public abstract class BaseValidator extends TestCase { + + private static final String MARKER = "marker"; + private int fBuildPropIndex; + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + protected void setUp() throws Exception { + IPath projectsLocation = new Path(MacroPlugin.getDefault().getBundle().getLocation() + "/tests/build.properties"); + projectsLocation = projectsLocation.setDevice(null).removeFirstSegments(1); + File[] zipFiles = projectsLocation.toFile().listFiles(); + for (int i = 0; i < zipFiles.length; i++) { + IProject project = findProject(zipFiles[i].getName().substring(0, zipFiles[i].getName().lastIndexOf('.'))); + if (project.exists()) { + project.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); + project.delete(true, new NullProgressMonitor()); + } + project.create(new NullProgressMonitor()); + doUnZip(MacroPlugin.getDefault().getStateLocation().removeLastSegments(2), "/tests/build.properties/" + zipFiles[i].getName()); + } + } + + /** + * Runs the quick fix and verifies that the marker is now gone. + * + * @param buildProperty build.properties file (on which markers will looked for) + * @param expectedValues properties file from which expected values will be read + * @throws CoreException + */ + protected void verifyQuickFixes(IResource buildProperty, PropertyResourceBundle expectedValues) throws CoreException { + IMarker[] markers = buildProperty.findMarkers(PDEMarkerFactory.MARKER_ID, true, IResource.DEPTH_INFINITE); + ResolutionGenerator resGen = new ResolutionGenerator(); + for (int i = 0; i < markers.length; i++) { + if (resGen.hasResolutions(markers[i])) { + String markerEntry = (String) markers[i].getAttribute(PDEMarkerFactory.BK_BUILD_ENTRY); + IMarkerResolution[] resolutions = resGen.getResolutions(markers[i]); + String quickFixindex = getProperty(expectedValues, markerEntry , "quickfix"); + resolutions[new Integer(quickFixindex.trim()).intValue()].run(markers[i]); + buildProject(markers[i].getResource().getProject(), 0); + assertFalse("Quick fix verification failed for build.properties" + fBuildPropIndex, markers[i].exists()); + } + } + } + + /** + * Verify the problem markers on the build.properties + * @param buildProperty build.properties file (on which markers will looked for) + * @param expectedValues properties file from which expected values will be read + * @param severity expected severity of the problem markers + * @throws CoreException + */ + protected void verifyBuildPropertiesMarkers(IResource buildProperty, PropertyResourceBundle expectedValues, int severity) throws CoreException { + IMarker[] markers = buildProperty.findMarkers(PDEMarkerFactory.MARKER_ID, true, IResource.DEPTH_INFINITE); + + String message; + String markercount = getProperty(expectedValues, "count"); + + message = "Marker count for build.properties" + fBuildPropIndex; + assertEquals(message, markercount, String.valueOf(markers.length)); + + int markerSeverity; + switch (severity) { + case CompilerFlags.ERROR : + markerSeverity = IMarker.SEVERITY_ERROR; + break; + case CompilerFlags.WARNING : + markerSeverity = IMarker.SEVERITY_WARNING; + break; + default : + markerSeverity = IMarker.SEVERITY_INFO; + } + + for (int i = 0; i < markers.length; i++) { + message = "Marker severity for build.properties" + fBuildPropIndex; + String markerEntry = (String) markers[i].getAttribute(PDEMarkerFactory.BK_BUILD_ENTRY); + assertEquals(message, markerSeverity, getIntAttribute(markers[i],IMarker.SEVERITY)); + + message = "Marker type for build.properties" + fBuildPropIndex; + String markerType = getProperty(expectedValues, markerEntry, PDEMarkerFactory.CAT_ID); + assertEquals(message, markerType, getStringAttribute(markers[i],PDEMarkerFactory.CAT_ID)); + + message = "Marker line number for build.properties" + fBuildPropIndex; + int lineNumber; + try { + lineNumber = new Integer(getProperty(expectedValues, markerEntry, IMarker.LINE_NUMBER)).intValue(); + } catch (Exception e) { + message = "Could not read expected line number for build.properties" + fBuildPropIndex; + lineNumber = 0; + } + assertEquals(message, lineNumber, getIntAttribute(markers[i],IMarker.LINE_NUMBER)); + + message = "Marker build entry token value for build.properties" + fBuildPropIndex; + String tokenValue = getProperty(expectedValues, markerEntry, PDEMarkerFactory.BK_BUILD_TOKEN); + assertEquals(message, tokenValue, getStringAttribute(markers[i],PDEMarkerFactory.BK_BUILD_TOKEN)); + } + + } + + private int getIntAttribute(IMarker marker, String property) { + Integer value; + try { + value = (Integer) marker.getAttribute(property); + } catch (CoreException e) { + return 0; + } + + if (value == null ) + return 0; + return value.intValue(); + } + + private String getStringAttribute(IMarker marker, String property) { + String value; + try { + value = (String) marker.getAttribute(property); + } catch (CoreException e) { + value = ""; + } + + if (value == null || value.equalsIgnoreCase("\"\"")) + value = ""; + return value.trim(); + } + + private String getProperty(PropertyResourceBundle propertyBundle, String property) { + String value; + try { + value = propertyBundle.getString(MARKER + '.' + property); + } catch (Exception e) { + value = ""; + } + if (value == null || value.equalsIgnoreCase("\"\"")) + value = ""; + return value.trim(); + } + + private String getProperty(PropertyResourceBundle propertyBundle, String entry, String property) { + return getProperty(propertyBundle, entry + '.' + property); + } + + /** + * Unzips the given archive to the specified location. + * + * @param location path in the local file system + * @param archivePath path to archive relative to the test plug-in + * @throws IOException + */ + protected IPath doUnZip(IPath location, String archivePath) throws IOException { + URL zipURL = MacroPlugin.getBundleContext().getBundle().getEntry(archivePath); + Path zipPath = new Path(new File(FileLocator.toFileURL(zipURL).getFile()).getAbsolutePath()); + ZipFile zipFile = new ZipFile(zipPath.toFile()); + Enumeration entries = zipFile.entries(); + IPath parent = location.removeLastSegments(1); + while (entries.hasMoreElements()) { + ZipEntry entry = (ZipEntry) entries.nextElement(); + if (!entry.isDirectory()) { + IPath entryPath = parent.append(entry.getName()); + File dir = entryPath.removeLastSegments(1).toFile(); + dir.mkdirs(); + File file = entryPath.toFile(); + file.createNewFile(); + InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(entry)); + byte[] bytes = LocalTargetDefinitionTests.getInputStreamAsByteArray(inputStream, -1); + inputStream.close(); + BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file)); + outputStream.write(bytes); + outputStream.close(); + } + } + zipFile.close(); + return parent; + } + + /** + * Build the given project with the specified build.properties file and wait till the build the complete + * @param project project to be build + * @param index suffix to the build.properties that shall be used to build the project + * @return true if the project got build successfully. false otherwise. + * @throws CoreException + */ + protected boolean buildProject(IProject project, int index) throws CoreException { + fBuildPropIndex = index; + IResource buildProp = project.findMember("build.properties"); + if (index > 0) { + if (buildProp != null && buildProp.exists()) + buildProp.delete(true, new NullProgressMonitor()); + buildProp = project.findMember("build.properties" + index); + if (buildProp == null) { + fail("build.properties" + index + "is missing. Can not build the project '" + project.getName() + "'"); + return false; + } + buildProp.copy(buildProp.getProjectRelativePath().removeFileExtension().addFileExtension("properties"), true, new NullProgressMonitor()); + } + project.build(IncrementalProjectBuilder.FULL_BUILD, new NullProgressMonitor()); + boolean wasInterrupted = false; + do { + try { + Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_BUILD, null); + Job.getJobManager().join(ResourcesPlugin.FAMILY_MANUAL_BUILD, null); + wasInterrupted = false; + } catch (OperationCanceledException e) { + return false; + } catch (InterruptedException e) { + wasInterrupted = true; + } + } while (wasInterrupted); + return true; + } + + /** + * Set the project specific preferences on build.properties + * + * @param project project for which the preferences are to be set + * @param severity severity level + * @throws BackingStoreException + */ + protected void setPreferences(IProject project, int severity) throws BackingStoreException { + ProjectScope scope = new ProjectScope(project); + IEclipsePreferences projectPrefs = scope.getNode(PDE.PLUGIN_ID); + projectPrefs.putInt(CompilerFlags.P_BUILD, severity); + projectPrefs.putInt(CompilerFlags.P_BUILD_MISSING_OUTPUT, severity); + projectPrefs.putInt(CompilerFlags.P_BUILD_SOURCE_LIBRARY, severity); + projectPrefs.putInt(CompilerFlags.P_BUILD_OUTPUT_LIBRARY, severity); + projectPrefs.putInt(CompilerFlags.P_BUILD_SRC_INCLUDES, severity); + projectPrefs.putInt(CompilerFlags.P_BUILD_BIN_INCLUDES, severity); + projectPrefs.putInt(CompilerFlags.P_BUILD_JAVA_COMPLIANCE, severity); + projectPrefs.flush(); + projectPrefs.sync(); + } + + /** + * Sets the given project specific preferences + * + * @param project project for which the preference are to be set + * @param pref the preference + * @param value the value + * @throws BackingStoreException + */ + protected void setPreference(IProject project, String node, String pref, String value) throws BackingStoreException { + ProjectScope scope = new ProjectScope(project); + IEclipsePreferences projectPrefs = scope.getNode(node); + projectPrefs.put(pref, value); + projectPrefs.flush(); + projectPrefs.sync(); + } + + /** + * Find the project in workspace with the given id + * @param id project id + * @return project + */ + protected IProject findProject(String id) { + IPluginModelBase model = PluginRegistry.findModel(id); + if (model != null) { + IResource resource = model.getUnderlyingResource(); + if (resource != null && resource.exists()) { + return resource.getProject(); + } + } + return PDEPlugin.getWorkspace().getRoot().getProject(id); + } + +} Index: src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidator.java =================================================================== RCS file: src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidator.java diff -N src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidator.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/org/eclipse/pde/ui/tests/build/properties/BuildPropertiesValidator.java 1 Jan 1970 00:00:00 -0000 @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2010 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.pde.ui.tests.build.properties; + +import org.eclipse.jdt.core.JavaCore; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import java.io.FileInputStream; +import java.util.PropertyResourceBundle; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.pde.internal.core.builders.CompilerFlags; + +public class BuildPropertiesValidator extends BaseValidator { + + + public static Test suite() { + return new TestSuite(BuildPropertiesValidator.class); + } + + private static boolean fOneTimeSetupComplete = false; + + protected void setUp() throws Exception { + if (fOneTimeSetupComplete) + return; + super.setUp(); + fOneTimeSetupComplete = true; + } + + public void testBuildPropertiesOne() { + try { + IProject project = findProject("org.eclipse.pde.tests.build.properties.one"); + project.open(new NullProgressMonitor()); + setPreferences(project, CompilerFlags.ERROR); + for (int i = 1; i <= 4; i++) { + if (buildProject(project, i)) { + IResource buildProperty = project.findMember("build.properties"); + PropertyResourceBundle expectedValues = new PropertyResourceBundle(new FileInputStream(buildProperty.getLocation().toFile())); + + verifyBuildPropertiesMarkers(buildProperty, expectedValues, CompilerFlags.ERROR); + verifyQuickFixes(buildProperty, expectedValues); + } else { + fail("Could not build the project '" + project.getName() + "'"); + } + } + } catch (Exception e) { + fail(e.getMessage()); + } + } + + public void testBuildPropertiesTwo() { + try { + IProject project = findProject("org.eclipse.pde.tests.build.properties.two"); + project.open(new NullProgressMonitor()); + setPreferences(project, CompilerFlags.WARNING); + setPreference(project, JavaCore.PLUGIN_ID, JavaCore.COMPILER_SOURCE, "1.3"); + setPreference(project, JavaCore.PLUGIN_ID, JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.3"); + setPreference(project, JavaCore.PLUGIN_ID, JavaCore.COMPILER_COMPLIANCE, "1.5"); + + if (buildProject(project, 1)) { + IResource buildProperty = project.findMember("build.properties"); + PropertyResourceBundle expectedValues = new PropertyResourceBundle(new FileInputStream(buildProperty.getLocation().toFile())); + + verifyBuildPropertiesMarkers(buildProperty, expectedValues, CompilerFlags.WARNING); + verifyQuickFixes(buildProperty, expectedValues); + } else { + fail("Could not build the project '" + project.getName() + "'"); + } + + } catch (Exception e) { + fail(e.getMessage()); + } + } + + public void testBuildPropertiesTwo_JreCompliance() { + try { + IProject project = findProject("org.eclipse.pde.tests.build.properties.two"); + project.open(new NullProgressMonitor()); + setPreferences(project, CompilerFlags.ERROR); + setPreference(project, JavaCore.PLUGIN_ID, JavaCore.COMPILER_SOURCE, "1.3"); + setPreference(project, JavaCore.PLUGIN_ID, JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, "1.2"); + setPreference(project, JavaCore.PLUGIN_ID, JavaCore.COMPILER_COMPLIANCE, "1.5"); + + if (buildProject(project, 2)) { + IResource buildProperty = project.findMember("build.properties"); + PropertyResourceBundle expectedValues = new PropertyResourceBundle(new FileInputStream(buildProperty.getLocation().toFile())); + + verifyBuildPropertiesMarkers(buildProperty, expectedValues, CompilerFlags.ERROR); + verifyQuickFixes(buildProperty, expectedValues); + } else { + fail("Could not build the project '" + project.getName() + "'"); + } + } catch (Exception e) { + fail(e.getMessage()); + } + } +}