From 8fbaaf1feab1bf70be64cf1b7d043f41ce9e1a0d Mon Sep 17 00:00:00 2001 From: James Blackburn Date: Sun, 26 Apr 2009 10:56:23 +0000 Subject: [PATCH] Bug 271264 : Error Parser tests intermittently fail - Turn tests into fixtures - ResourceHelper removes resources it creates via #cleanUp() - createCDTProject joins() FAMILY_AUTO_REFRESH job. Related to bug 273676: The platform doesn't fire resource change events if more than one WorkspaceJob is prepared. As createCDTProject opens the project with BACKGROUND_REFRESH, a refresh job is scheduled and run once during project creation. This effects the ResourceLookup's sync state wrt the tests. --- .../tests/ErrorParserEfsFileMatchingTest.java | 2 +- .../tests/ErrorParserFileMatchingTest.java | 4 +- .../errorparsers/tests/ResourceHelper.java | 111 ++++++++++++++++-- 3 files changed, 105 insertions(+), 12 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ErrorParserEfsFileMatchingTest.java b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ErrorParserEfsFileMatchingTest.java index 0a3db7374cf..bb1bc42098a 100644 --- a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ErrorParserEfsFileMatchingTest.java +++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ErrorParserEfsFileMatchingTest.java @@ -1 +1 @@ -/******************************************************************************* * Copyright (c) 2009 Andrew Gvozdev 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: * Andrew Gvozdev - Initial API and implementation *******************************************************************************/ package org.eclipse.cdt.core.internal.errorparsers.tests; import java.io.ByteArrayInputStream; import java.util.ArrayList; import junit.framework.Assert; import junit.framework.TestCase; import junit.framework.TestSuite; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.ErrorParserManager; import org.eclipse.cdt.core.IErrorParser; import org.eclipse.cdt.core.IMarkerGenerator; import org.eclipse.cdt.core.ProblemMarkerInfo; import org.eclipse.cdt.core.errorparsers.AbstractErrorParser; import org.eclipse.cdt.core.errorparsers.ErrorPattern; import org.eclipse.cdt.core.testplugin.CTestPlugin; import org.eclipse.core.internal.registry.ExtensionRegistry; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.ContributorFactoryOSGi; import org.eclipse.core.runtime.IContributor; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; /** * The test case includes a few tests checking that {@link AbstractErrorParser}/{@link ErrorPattern} * properly locate and resolve filenames found in build output in case of EFS files/folders. */ public class ErrorParserEfsFileMatchingTest extends TestCase { private final static String testName = "FindMatchingFilesEfsTest"; // Default project gets created once then used by all test cases. private IProject fProject = null; private String mockErrorParserId = null; private ArrayList errorList; private final IMarkerGenerator markerGenerator = new IMarkerGenerator() { // deprecated public void addMarker(IResource file, int lineNumber, String errorDesc, int severity, String errorVar) {} public void addMarker(ProblemMarkerInfo problemMarkerInfo) { errorList.add(problemMarkerInfo); } }; /** * Simple error parser parsing line like "file:line:description" */ public static class MockErrorParser extends AbstractErrorParser { /** * Constructor to set the error pattern. */ public MockErrorParser() { super(new ErrorPattern[] { new ErrorPattern("(.*):(.*):(.*)", 1, 2, 3, 0, IMarkerGenerator.SEVERITY_ERROR_RESOURCE) }); } } /** * Constructor. * @param name - name of the test. */ public ErrorParserEfsFileMatchingTest(String name) { super(name); } @Override protected void setUp() throws Exception { if (fProject==null) { fProject = ResourceHelper.createCDTProject(testName); Assert.assertNotNull(fProject); mockErrorParserId = addErrorParserExtension("MockErrorParser", MockErrorParser.class); } errorList = new ArrayList(); } @Override protected void tearDown() { } /** * @return - new TestSuite. */ public static TestSuite suite() { return new TestSuite(ErrorParserEfsFileMatchingTest.class); } /** * main function of the class. * * @param args - arguments */ public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } /** * Adds Error Parser extension to the global repository. * Note that this function will "pollute" the working environment and * the error parser will be seen by other test cases as well. * * @param shortId - last portion of ID with which error parser will be added. * @param cl - Error Parser class * @return - full ID of the error parser (generated by the method). */ private static String addErrorParserExtension(String shortId, Class cl) { String ext = "" + "" + ""; IContributor contributor = ContributorFactoryOSGi.createContributor(CTestPlugin.getDefault().getBundle()); boolean added = Platform.getExtensionRegistry().addContribution(new ByteArrayInputStream(ext.getBytes()), contributor, false, shortId, null, ((ExtensionRegistry) Platform.getExtensionRegistry()).getTemporaryUserToken()); assertTrue("failed to add extension", added); String fullId = "org.eclipse.cdt.core.tests." + shortId; IErrorParser[] errorParser = CCorePlugin.getDefault().getErrorParser(fullId); assertTrue(errorParser.length > 0); return fullId; } /** * Convenience method to let {@link ErrorParserManager} parse one line of output. * This method goes through the whole working cycle every time creating * new {@link ErrorParserManager}. * * @param project - for which project to parse output. * @param buildDir - location of build for {@link ErrorParserManager}. * @param line - one line of output. * @throws Exception */ private void parseOutput(IProject project, IPath buildDir, String line) throws Exception { ErrorParserManager epManager = new ErrorParserManager(project, buildDir, markerGenerator, new String[] { mockErrorParserId }); line = line + '\n'; epManager.write(line.getBytes(), 0, line.length()); epManager.close(); epManager.reportProblems(); } /** * Convenience method to parse one line of output. */ private void parseOutput(IProject project, String buildDir, String line) throws Exception { parseOutput(project, new Path(buildDir), line); } // /** // * Convenience method to parse one line of output. // * Search is done in project location. // */ // private void parseOutput(IProject project, String line) throws Exception { // parseOutput(project, project.getLocation(), line); // } /** * Convenience method to parse one line of output. * Search is done for current project in default location. */ private void parseOutput(String line) throws Exception { parseOutput(fProject, fProject.getLocation(), line); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testSingle() throws Exception { ResourceHelper.createEfsFile(fProject, "testSingle.c", "null:/efsTestSingle.c"); parseOutput("efsTestSingle.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/testSingle.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testEfsVsRegular() throws Exception { ResourceHelper.createFile(fProject, "testEfsVsRegular.c"); ResourceHelper.createEfsFile(fProject, "efsTestEfsVsRegular.c", "null:/testEfsVsRegular.c"); parseOutput("testEfsVsRegular.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/testEfsVsRegular.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testFullPath() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testFullPath.c", "null:/EfsFolder/efsTestFullPath.c"); parseOutput("EfsFolder/efsTestFullPath.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Folder/testFullPath.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testInNonEfsFolder() throws Exception { ResourceHelper.createFolder(fProject, "NonEfsFolder"); ResourceHelper.createEfsFile(fProject, "NonEfsFolder/testInNonEfsFolder.c", "null:/EfsFolder/efsTestInNonEfsFolder.c"); parseOutput("efsTestInNonEfsFolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/NonEfsFolder/testInNonEfsFolder.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testInFolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testInFolder.c", "null:/EfsFolder/efsTestInFolder.c"); parseOutput("efsTestInFolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Folder/testInFolder.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testDuplicateInRoot() throws Exception { ResourceHelper.createEfsFile(fProject, "testDuplicateInRoot.c", "null:/testDuplicateInRoot.c"); ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testDuplicateInRoot.c", "null:/Folder/testDuplicateInRoot.c"); // Resolved to the file in root folder parseOutput("testDuplicateInRoot.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("L/FindMatchingFilesEfsTest/testDuplicateInRoot.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathFromProjectRoot() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testRelativePathFromProjectRoot.c", "null:/EfsFolder/testRelativePathFromProjectRoot.c"); parseOutput("EfsFolder/testRelativePathFromProjectRoot.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Folder/testRelativePathFromProjectRoot.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathFromSubfolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Subfolder", "null:/Subfolder"); ResourceHelper.createEfsFolder(fProject, "Subfolder/Folder", "null:/Subfolder/Folder"); ResourceHelper.createEfsFile(fProject, "Subfolder/Folder/testRelativePathFromSubfolder.c", "null:/Subfolder/Folder/testRelativePathFromSubfolder.c"); parseOutput("Folder/testRelativePathFromSubfolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Subfolder/Folder/testRelativePathFromSubfolder.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathNotMatchingFolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testRelativePathNotMatchingFolder.c", "null:/Folder/testRelativePathNotMatchingFolder.c"); parseOutput("NotMatchingFolder/testRelativePathNotMatchingFolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); // No match assertEquals("P/FindMatchingFilesEfsTest",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); assertEquals(new Path("NotMatchingFolder/testRelativePathNotMatchingFolder.c"),problemMarkerInfo.externalPath); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathDuplicate() throws Exception { ResourceHelper.createEfsFolder(fProject, "SubfolderA", "null:/SubfolderA"); ResourceHelper.createEfsFolder(fProject, "SubfolderA/Folder", "null:/SubfolderA/Folder"); ResourceHelper.createEfsFile(fProject, "SubfolderA/Folder/testRelativePathDuplicate.c", "null:/SubfolderA/Folder/testRelativePathDuplicate.c"); ResourceHelper.createEfsFolder(fProject, "SubfolderB", "null:/SubfolderB"); ResourceHelper.createEfsFolder(fProject, "SubfolderB/Folder", "null:/SubfolderB/Folder"); ResourceHelper.createEfsFile(fProject, "SubfolderB/Folder/testRelativePathDuplicate.c", "null:/SubfolderBS/Folder/testRelativePathDuplicate.c"); parseOutput("Folder/testRelativePathDuplicate.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); // No match found assertEquals("P/FindMatchingFilesEfsTest",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); assertEquals(new Path("Folder/testRelativePathDuplicate.c"),problemMarkerInfo.externalPath); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathUpSubfolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testRelativePathUpSubfolder.c", "null:/Folder/testRelativePathUpSubfolder.c"); parseOutput("../Folder/testRelativePathUpSubfolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Folder/testRelativePathUpSubfolder.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathDotFromSubfolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Subfolder", "null:/Subfolder"); ResourceHelper.createEfsFolder(fProject, "Subfolder/Folder", "null:/Subfolder/Folder"); ResourceHelper.createEfsFile(fProject, "Subfolder/Folder/testRelativePathDotFromSubfolder.c", "null:/Subfolder/Folder/testRelativePathDotFromSubfolder.c"); parseOutput("./Folder/testRelativePathDotFromSubfolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Subfolder/Folder/testRelativePathDotFromSubfolder.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testBuildDir() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testBuildDir.c", "null:/Folder/testBuildDir.c"); ResourceHelper.createEfsFolder(fProject, "BuildDir", "null:/BuildDir"); ResourceHelper.createEfsFile(fProject, "BuildDir/testBuildDir.c", "null:/BuildDir/testBuildDir.c"); String buildDir = fProject.getLocation().append("BuildDir").toOSString(); parseOutput(fProject, buildDir, "testBuildDir.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/BuildDir/testBuildDir.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } } \ No newline at end of file +/******************************************************************************* * Copyright (c) 2009 Andrew Gvozdev 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: * Andrew Gvozdev - Initial API and implementation *******************************************************************************/ package org.eclipse.cdt.core.internal.errorparsers.tests; import java.io.ByteArrayInputStream; import java.util.ArrayList; import junit.framework.Assert; import junit.framework.TestCase; import junit.framework.TestSuite; import org.eclipse.cdt.core.CCorePlugin; import org.eclipse.cdt.core.ErrorParserManager; import org.eclipse.cdt.core.IErrorParser; import org.eclipse.cdt.core.IMarkerGenerator; import org.eclipse.cdt.core.ProblemMarkerInfo; import org.eclipse.cdt.core.errorparsers.AbstractErrorParser; import org.eclipse.cdt.core.errorparsers.ErrorPattern; import org.eclipse.cdt.core.testplugin.CTestPlugin; import org.eclipse.core.internal.registry.ExtensionRegistry; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.ContributorFactoryOSGi; import org.eclipse.core.runtime.IContributor; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; /** * The test case includes a few tests checking that {@link AbstractErrorParser}/{@link ErrorPattern} * properly locate and resolve filenames found in build output in case of EFS files/folders. */ public class ErrorParserEfsFileMatchingTest extends TestCase { private final static String testName = "FindMatchingFilesEfsTest"; // Default project gets created once then used by all test cases. private IProject fProject = null; private String mockErrorParserId = null; private ArrayList errorList; private final IMarkerGenerator markerGenerator = new IMarkerGenerator() { // deprecated public void addMarker(IResource file, int lineNumber, String errorDesc, int severity, String errorVar) {} public void addMarker(ProblemMarkerInfo problemMarkerInfo) { errorList.add(problemMarkerInfo); } }; /** * Simple error parser parsing line like "file:line:description" */ public static class MockErrorParser extends AbstractErrorParser { /** * Constructor to set the error pattern. */ public MockErrorParser() { super(new ErrorPattern[] { new ErrorPattern("(.*):(.*):(.*)", 1, 2, 3, 0, IMarkerGenerator.SEVERITY_ERROR_RESOURCE) }); } } /** * Constructor. * @param name - name of the test. */ public ErrorParserEfsFileMatchingTest(String name) { super(name); } @Override protected void setUp() throws Exception { if (fProject==null) { fProject = ResourceHelper.createCDTProject(testName); Assert.assertNotNull(fProject); mockErrorParserId = addErrorParserExtension("MockErrorParser", MockErrorParser.class); } errorList = new ArrayList(); } @Override protected void tearDown() throws Exception { ResourceHelper.cleanUp(); fProject = null; } /** * @return - new TestSuite. */ public static TestSuite suite() { return new TestSuite(ErrorParserEfsFileMatchingTest.class); } /** * main function of the class. * * @param args - arguments */ public static void main(String[] args) { junit.textui.TestRunner.run(suite()); } /** * Adds Error Parser extension to the global repository. * Note that this function will "pollute" the working environment and * the error parser will be seen by other test cases as well. * * @param shortId - last portion of ID with which error parser will be added. * @param cl - Error Parser class * @return - full ID of the error parser (generated by the method). */ private static String addErrorParserExtension(String shortId, Class cl) { String ext = "" + "" + ""; IContributor contributor = ContributorFactoryOSGi.createContributor(CTestPlugin.getDefault().getBundle()); boolean added = Platform.getExtensionRegistry().addContribution(new ByteArrayInputStream(ext.getBytes()), contributor, false, shortId, null, ((ExtensionRegistry) Platform.getExtensionRegistry()).getTemporaryUserToken()); assertTrue("failed to add extension", added); String fullId = "org.eclipse.cdt.core.tests." + shortId; IErrorParser[] errorParser = CCorePlugin.getDefault().getErrorParser(fullId); assertTrue(errorParser.length > 0); return fullId; } /** * Convenience method to let {@link ErrorParserManager} parse one line of output. * This method goes through the whole working cycle every time creating * new {@link ErrorParserManager}. * * @param project - for which project to parse output. * @param buildDir - location of build for {@link ErrorParserManager}. * @param line - one line of output. * @throws Exception */ private void parseOutput(IProject project, IPath buildDir, String line) throws Exception { ErrorParserManager epManager = new ErrorParserManager(project, buildDir, markerGenerator, new String[] { mockErrorParserId }); line = line + '\n'; epManager.write(line.getBytes(), 0, line.length()); epManager.close(); epManager.reportProblems(); } /** * Convenience method to parse one line of output. */ private void parseOutput(IProject project, String buildDir, String line) throws Exception { parseOutput(project, new Path(buildDir), line); } // /** // * Convenience method to parse one line of output. // * Search is done in project location. // */ // private void parseOutput(IProject project, String line) throws Exception { // parseOutput(project, project.getLocation(), line); // } /** * Convenience method to parse one line of output. * Search is done for current project in default location. */ private void parseOutput(String line) throws Exception { parseOutput(fProject, fProject.getLocation(), line); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testSingle() throws Exception { ResourceHelper.createEfsFile(fProject, "testSingle.c", "null:/efsTestSingle.c"); parseOutput("efsTestSingle.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/testSingle.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testEfsVsRegular() throws Exception { ResourceHelper.createFile(fProject, "testEfsVsRegular.c"); ResourceHelper.createEfsFile(fProject, "efsTestEfsVsRegular.c", "null:/testEfsVsRegular.c"); parseOutput("testEfsVsRegular.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/testEfsVsRegular.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testFullPath() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testFullPath.c", "null:/EfsFolder/efsTestFullPath.c"); parseOutput("EfsFolder/efsTestFullPath.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Folder/testFullPath.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testInNonEfsFolder() throws Exception { ResourceHelper.createFolder(fProject, "NonEfsFolder"); ResourceHelper.createEfsFile(fProject, "NonEfsFolder/testInNonEfsFolder.c", "null:/EfsFolder/efsTestInNonEfsFolder.c"); parseOutput("efsTestInNonEfsFolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/NonEfsFolder/testInNonEfsFolder.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testInFolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testInFolder.c", "null:/EfsFolder/efsTestInFolder.c"); parseOutput("efsTestInFolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Folder/testInFolder.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testDuplicateInRoot() throws Exception { ResourceHelper.createEfsFile(fProject, "testDuplicateInRoot.c", "null:/testDuplicateInRoot.c"); ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testDuplicateInRoot.c", "null:/Folder/testDuplicateInRoot.c"); // Resolved to the file in root folder parseOutput("testDuplicateInRoot.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("L/FindMatchingFilesEfsTest/testDuplicateInRoot.c",problemMarkerInfo.file.toString()); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathFromProjectRoot() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testRelativePathFromProjectRoot.c", "null:/EfsFolder/testRelativePathFromProjectRoot.c"); parseOutput("EfsFolder/testRelativePathFromProjectRoot.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Folder/testRelativePathFromProjectRoot.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathFromSubfolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Subfolder", "null:/Subfolder"); ResourceHelper.createEfsFolder(fProject, "Subfolder/Folder", "null:/Subfolder/Folder"); ResourceHelper.createEfsFile(fProject, "Subfolder/Folder/testRelativePathFromSubfolder.c", "null:/Subfolder/Folder/testRelativePathFromSubfolder.c"); parseOutput("Folder/testRelativePathFromSubfolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Subfolder/Folder/testRelativePathFromSubfolder.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathNotMatchingFolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testRelativePathNotMatchingFolder.c", "null:/Folder/testRelativePathNotMatchingFolder.c"); parseOutput("NotMatchingFolder/testRelativePathNotMatchingFolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); // No match assertEquals("P/FindMatchingFilesEfsTest",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); assertEquals(new Path("NotMatchingFolder/testRelativePathNotMatchingFolder.c"),problemMarkerInfo.externalPath); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathDuplicate() throws Exception { ResourceHelper.createEfsFolder(fProject, "SubfolderA", "null:/SubfolderA"); ResourceHelper.createEfsFolder(fProject, "SubfolderA/Folder", "null:/SubfolderA/Folder"); ResourceHelper.createEfsFile(fProject, "SubfolderA/Folder/testRelativePathDuplicate.c", "null:/SubfolderA/Folder/testRelativePathDuplicate.c"); ResourceHelper.createEfsFolder(fProject, "SubfolderB", "null:/SubfolderB"); ResourceHelper.createEfsFolder(fProject, "SubfolderB/Folder", "null:/SubfolderB/Folder"); ResourceHelper.createEfsFile(fProject, "SubfolderB/Folder/testRelativePathDuplicate.c", "null:/SubfolderBS/Folder/testRelativePathDuplicate.c"); parseOutput("Folder/testRelativePathDuplicate.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); // No match found assertEquals("P/FindMatchingFilesEfsTest",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); assertEquals(new Path("Folder/testRelativePathDuplicate.c"),problemMarkerInfo.externalPath); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathUpSubfolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testRelativePathUpSubfolder.c", "null:/Folder/testRelativePathUpSubfolder.c"); parseOutput("../Folder/testRelativePathUpSubfolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Folder/testRelativePathUpSubfolder.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testRelativePathDotFromSubfolder() throws Exception { ResourceHelper.createEfsFolder(fProject, "Subfolder", "null:/Subfolder"); ResourceHelper.createEfsFolder(fProject, "Subfolder/Folder", "null:/Subfolder/Folder"); ResourceHelper.createEfsFile(fProject, "Subfolder/Folder/testRelativePathDotFromSubfolder.c", "null:/Subfolder/Folder/testRelativePathDotFromSubfolder.c"); parseOutput("./Folder/testRelativePathDotFromSubfolder.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/Subfolder/Folder/testRelativePathDotFromSubfolder.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } /** * Checks if a file from error output can be found. * @throws Exception... */ public void testBuildDir() throws Exception { ResourceHelper.createEfsFolder(fProject, "Folder", "null:/Folder"); ResourceHelper.createEfsFile(fProject, "Folder/testBuildDir.c", "null:/Folder/testBuildDir.c"); ResourceHelper.createEfsFolder(fProject, "BuildDir", "null:/BuildDir"); ResourceHelper.createEfsFile(fProject, "BuildDir/testBuildDir.c", "null:/BuildDir/testBuildDir.c"); String buildDir = fProject.getLocation().append("BuildDir").toOSString(); parseOutput(fProject, buildDir, "testBuildDir.c:1:error"); assertEquals(1, errorList.size()); ProblemMarkerInfo problemMarkerInfo = errorList.get(0); assertEquals("L/FindMatchingFilesEfsTest/BuildDir/testBuildDir.c",problemMarkerInfo.file.toString()); assertEquals(1,problemMarkerInfo.lineNumber); assertEquals("error",problemMarkerInfo.description); } } \ No newline at end of file diff --git a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ErrorParserFileMatchingTest.java b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ErrorParserFileMatchingTest.java index 5d7f1e6cb42..355f43f364f 100644 --- a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ErrorParserFileMatchingTest.java +++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ErrorParserFileMatchingTest.java @@ -96,7 +96,9 @@ public class ErrorParserFileMatchingTest extends TestCase { } @Override - protected void tearDown() { + protected void tearDown() throws Exception { + ResourceHelper.cleanUp(); + fProject = null; } /** diff --git a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ResourceHelper.java b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ResourceHelper.java index 922f7969486..34540381712 100644 --- a/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ResourceHelper.java +++ b/core/org.eclipse.cdt.core.tests/misc/org/eclipse/cdt/core/internal/errorparsers/tests/ResourceHelper.java @@ -13,15 +13,19 @@ package org.eclipse.cdt.core.internal.errorparsers.tests; import java.io.BufferedReader; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.URI; import java.net.URISyntaxException; +import java.util.HashSet; +import java.util.Set; import junit.framework.Assert; import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.core.resources.IContainer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.resources.IProject; @@ -37,6 +41,7 @@ import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.core.runtime.OperationCanceledException; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.jobs.Job; /** * This class contains utility methods for creating resources @@ -53,6 +58,9 @@ import org.eclipse.core.runtime.Platform; public class ResourceHelper { private final static IProgressMonitor NULL_MONITOR = new NullProgressMonitor(); + private final static Set externalFilesCreated = new HashSet(); + private final static Set resourcesCreated = new HashSet(); + /** * Creates CDT project in a specific location and opens it. * @@ -66,6 +74,7 @@ public class ResourceHelper { IWorkspace workspace = ResourcesPlugin.getWorkspace(); IWorkspaceRoot root = workspace.getRoot(); IProject project = root.getProject(projectName); + resourcesCreated.add(project); IProjectDescription description = workspace.newProjectDescription(projectName); if(locationInWorkspace != null) { IPath absoluteLocation = root.getLocation().append(locationInWorkspace); @@ -73,8 +82,17 @@ public class ResourceHelper { } project = CCorePlugin.getDefault().createCDTProject(description, project, NULL_MONITOR); Assert.assertNotNull(project); - project.open(null); + + try { + // CDT opens the Project with BACKGROUND_REFRESH enabled which causes the + // refresh manager to refresh the project 200ms later. This Job interferes + // with the resource change handler firing see: bug 271264 + Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, null); + } catch (Exception e) { + // Ignore + } + Assert.assertTrue(project.isOpen()); return project; @@ -97,8 +115,17 @@ public class ResourceHelper { description.setLocationURI(locationURI); project = CCorePlugin.getDefault().createCDTProject(description, project, NULL_MONITOR); Assert.assertNotNull(project); - project.open(null); + + try { + // CDT opens the Project with BACKGROUND_REFRESH enabled which causes the + // refresh manager to refresh the project 200ms later. This Job interferes + // with the resource change handler firing see: bug 271264 + Job.getJobManager().join(ResourcesPlugin.FAMILY_AUTO_REFRESH, null); + } catch (Exception e) { + // Ignore + } + Assert.assertTrue(project.isOpen()); return project; @@ -131,6 +158,7 @@ public class ResourceHelper { InputStream inputStream = new ByteArrayInputStream(contents.getBytes()); file.create(inputStream, true, NULL_MONITOR); + resourcesCreated.add(file); return file; } @@ -145,6 +173,8 @@ public class ResourceHelper { * @throws CoreException if something goes wrong. */ public static IFile createFile(IProject project, String name) throws CoreException { + if (new Path(name).segmentCount() > 1) + createFolder(project, new Path(name).removeLastSegments(1).toString()); return createFile(project.getFile(name), null); } @@ -168,6 +198,8 @@ public class ResourceHelper { } Assert.assertTrue(file.exists()); + externalFilesCreated.add(fullPath.toOSString()); + workspaceRoot.refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor()); return fullPath; } @@ -182,11 +214,15 @@ public class ResourceHelper { * @throws CoreException if something goes wrong. */ public static IFolder createFolder(IProject project, String name) throws CoreException { - IFolder folder= project.getFolder(name); - if (!folder.exists()) { - folder.create(true, true, NULL_MONITOR); + final IPath p = new Path(name); + IContainer folder = project; + for (String seg : p.segments()) { + folder = folder.getFolder(new Path(seg)); + if (!folder.exists()) + ((IFolder)folder).create(true, true, NULL_MONITOR); } - return folder; + resourcesCreated.add(folder); + return (IFolder)folder; } /** @@ -198,7 +234,7 @@ public class ResourceHelper { * @return full folder path. * @throws IOException if something goes wrong. */ - public static IPath createWorkspaceFolder(String name) throws IOException { + public static IPath createWorkspaceFolder(String name) throws CoreException, IOException { IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot(); IPath fullPath = workspaceRoot.getLocation().append(name); java.io.File folder = new java.io.File(fullPath.toOSString()); @@ -208,6 +244,8 @@ public class ResourceHelper { } Assert.assertTrue(folder.exists()); + externalFilesCreated.add(fullPath.toOSString()); + workspaceRoot.refreshLocal(IResource.DEPTH_INFINITE, NULL_MONITOR); return fullPath; } @@ -226,7 +264,7 @@ public class ResourceHelper { IFile file = project.getFile(fileLink); file.createLink(realFile, IResource.REPLACE, null); Assert.assertTrue(file.exists()); - + resourcesCreated.add(file); return file; } @@ -256,7 +294,8 @@ public class ResourceHelper { */ public static IFile createEfsFile(IProject project, String fileLink, URI realFile) throws CoreException { IFile file= project.getFile(fileLink); - file.createLink(realFile, IResource.ALLOW_MISSING_LOCAL, new NullProgressMonitor()); + file.createLink(realFile, IResource.ALLOW_MISSING_LOCAL, NULL_MONITOR); + resourcesCreated.add(file); return file; } @@ -289,7 +328,7 @@ public class ResourceHelper { IFolder folder = project.getFolder(folderLink); folder.createLink(realFolder, IResource.REPLACE, null); Assert.assertTrue(folder.exists()); - + resourcesCreated.add(folder); return folder; } @@ -326,6 +365,7 @@ public class ResourceHelper { } folder.createLink(realFolder, IResource.ALLOW_MISSING_LOCAL, new NullProgressMonitor()); + resourcesCreated.add(folder); return folder; } @@ -381,6 +421,8 @@ public class ResourceHelper { } Assert.assertTrue(resource.exists()); + externalFilesCreated.add(linkedPath.toOSString()); + ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, NULL_MONITOR); return resource; } @@ -452,4 +494,53 @@ public class ResourceHelper { return windowsPath.trim(); } + /** + * Clean-up any files created as part of a unit test. + * This method removes *all* Workspace IResources and any external + * files / folders created with the #createWorkspaceFile #createWorkspaceFolder + * methods in this class + */ + public static void cleanUp() throws CoreException, IOException { + IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); + root.refreshLocal(IResource.DEPTH_INFINITE, NULL_MONITOR); + + // Delete all external files & folders created using ResourceHelper + for (String loc : externalFilesCreated) { + File f = new File(loc); + if (f.exists()) + deleteRecursive(f); + } + externalFilesCreated.clear(); + + // Remove IResources created by this helper + for (IResource r : resourcesCreated) { + if (r.exists()) + try { + r.delete(true, NULL_MONITOR); + } catch (CoreException e) { + // Ignore + } + } + resourcesCreated.clear(); + } + + /** + * Recursively delete a directory / file + * + * For safety this method only deletes files created under the workspace + * + * @param file + */ + private static final void deleteRecursive(File f) throws IllegalArgumentException { + // Ensure that the file being deleted is a child of the workspace + // root to prevent anything nasty happening + if (! f.getAbsolutePath().startsWith( + ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().getAbsolutePath())) + throw new IllegalArgumentException("File must exist within the workspace!"); + + if (f.isDirectory()) + for (File f1 : f.listFiles()) + deleteRecursive(f1); + f.delete(); + } }