Skip to content

Commit

Permalink
Move resource test methods for random contents to utility class eclip…
Browse files Browse the repository at this point in the history
…se-platform#903

This moves utility methods for creating random strings and stream from
the ResourceTest class hierarchy to the dedicated ResourceTestUtil
utility class. This prepares for removing the ResourceTest inheritance
hierarchy to migrate to JUnit 4.

Contributes to
eclipse-platform#903
  • Loading branch information
HeikoKlare committed Dec 6, 2023
1 parent f05973c commit a306b1a
Show file tree
Hide file tree
Showing 76 changed files with 700 additions and 570 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor;
import static org.eclipse.core.tests.resources.ResourceTestUtil.buildResources;
import static org.eclipse.core.tests.resources.ResourceTestUtil.waitForBuild;
import static org.eclipse.core.tests.resources.ResourceTestUtil.*;

/**
* This class needs to be used with SaveManager2Test. Basically this
Expand Down Expand Up @@ -177,7 +178,7 @@ public void testBuilder() throws CoreException {
DeltaVerifierBuilder verifier = DeltaVerifierBuilder.getInstance();
verifier.reset();
verifier.addExpectedChange(added, project, IResourceDelta.ADDED, 0);
added.create(getRandomContents(), true, null);
added.create(createRandomContentsStream(), true, null);
waitForBuild();
assertTrue("3.2", verifier.wasAutoBuild());
assertTrue("3.3", verifier.isDeltaValid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import static org.eclipse.core.tests.resources.ResourceTestUtil.waitForBuild;
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertDoesNotExistInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInFileSystem;
import static org.eclipse.core.tests.resources.ResourceTestUtil.*;

/**
* @see SaveManager1Test
Expand Down Expand Up @@ -73,7 +74,7 @@ public void testBuilder() throws CoreException {
DeltaVerifierBuilder verifier = DeltaVerifierBuilder.getInstance();
verifier.reset();
verifier.addExpectedChange(added, project, IResourceDelta.ADDED, 0);
added.create(getRandomContents(), true, null);
added.create(createRandomContentsStream(), true, null);
waitForBuild();
assertTrue("3.2", verifier.wasAutoBuild());
assertTrue("3.3", verifier.isDeltaValid());
Expand Down Expand Up @@ -131,7 +132,7 @@ public void testVerifyProject2() throws CoreException {

// add a file to test save participant delta
IFile file = project.getFile("addedFile");
file.create(getRandomContents(), true, null);
file.create(createRandomContentsStream(), true, null);
}

public void testVerifyRestoredWorkspace() throws CoreException {
Expand All @@ -146,6 +147,6 @@ public void testVerifyRestoredWorkspace() throws CoreException {

// add a file to test save participant delta
IFile file = project.getFile("addedFile");
file.create(getRandomContents(), true, null);
file.create(createRandomContentsStream(), true, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.core.tests.resources.saveparticipant3.SaveParticipant3Plugin;
import org.osgi.framework.Bundle;
import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.*;

/**
* @see SaveManager1Test
Expand Down Expand Up @@ -67,7 +68,7 @@ public void testBuilder() throws CoreException {

IFile added = project.getFile("added file");
verifier.addExpectedChange(added, project, IResourceDelta.ADDED, 0);
added.create(getRandomContents(), true, null);
added.create(createRandomContentsStream(), true, null);
waitForBuild();
assertTrue("3.2", verifier.wasAutoBuild());
assertTrue("3.3", verifier.isDeltaValid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.compareContent;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createInputStream;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createRandomString;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor;
import static org.eclipse.core.tests.resources.ResourceTestUtil.isAttributeSupported;
import static org.eclipse.core.tests.resources.ResourceTestUtil.isReadOnlySupported;
Expand Down Expand Up @@ -201,7 +203,7 @@ public void testCopyDirectoryParentMissing() throws Throwable {
IFileStore parent = getTempStore();
IFileStore child = parent.getChild("child");
IFileStore existing = getTempStore();
createFile(existing, getRandomString());
createFile(existing, createRandomString());
// try to copy when parent of destination does not exist
assertThrows(CoreException.class, () -> existing.copy(child, EFS.NONE, createTestMonitor()));
// destination should not exist
Expand All @@ -221,15 +223,15 @@ public void testCaseInsensitive() throws Throwable {
createFile(fileWithSmallName, content);
System.out.println(fileWithSmallName.fetchInfo().getName());
assertTrue("1.3", fileWithSmallName.fetchInfo().exists());
assertTrue("1.4", compareContent(getContents(content), fileWithSmallName.openInputStream(EFS.NONE, null)));
assertTrue("1.4", compareContent(createInputStream(content), fileWithSmallName.openInputStream(EFS.NONE, null)));

IFileStore fileWithOtherName = temp.getChild("FILENAME");
System.out.println(fileWithOtherName.fetchInfo().getName());
// file content is already the same for both Cases:
assertTrue("2.0", compareContent(getContents(content), fileWithOtherName.openInputStream(EFS.NONE, null)));
assertTrue("2.0", compareContent(createInputStream(content), fileWithOtherName.openInputStream(EFS.NONE, null)));
fileWithSmallName.copy(fileWithOtherName, IResource.DEPTH_INFINITE, null); // a NOP Operation
// file content is still the same for both Cases:
assertTrue("2.1", compareContent(getContents(content), fileWithOtherName.openInputStream(EFS.NONE, null)));
assertTrue("2.1", compareContent(createInputStream(content), fileWithOtherName.openInputStream(EFS.NONE, null)));
assertTrue("3.0", fileWithOtherName.fetchInfo().exists());
assertTrue("3.1", fileWithSmallName.fetchInfo().exists());
fileWithOtherName.delete(EFS.NONE, null);
Expand All @@ -251,12 +253,12 @@ public void testCopyFile() throws Throwable {
target.delete(EFS.NONE, null);
createFile(target, content);
assertTrue("1.3", target.fetchInfo().exists());
assertTrue("1.4", compareContent(getContents(content), target.openInputStream(EFS.NONE, null)));
assertTrue("1.4", compareContent(createInputStream(content), target.openInputStream(EFS.NONE, null)));

/* temp\target -> temp\copy of target */
IFileStore copyOfTarget = temp.getChild("copy of target");
target.copy(copyOfTarget, IResource.DEPTH_INFINITE, null);
assertTrue("2.1", compareContent(getContents(content), copyOfTarget.openInputStream(EFS.NONE, null)));
assertTrue("2.1", compareContent(createInputStream(content), copyOfTarget.openInputStream(EFS.NONE, null)));
copyOfTarget.delete(EFS.NONE, null);

// We need to know whether or not we can unset the read-only flag
Expand All @@ -267,7 +269,7 @@ public void testCopyFile() throws Throwable {
setReadOnly(target, true);

target.copy(copyOfTarget, IResource.DEPTH_INFINITE, null);
assertTrue("3.1", compareContent(getContents(content), copyOfTarget.openInputStream(EFS.NONE, null)));
assertTrue("3.1", compareContent(createInputStream(content), copyOfTarget.openInputStream(EFS.NONE, null)));
// reset read only flag for cleanup
setReadOnly(copyOfTarget, false);
copyOfTarget.delete(EFS.NONE, null);
Expand All @@ -284,12 +286,12 @@ public void testCopyFile() throws Throwable {
IFileStore bigFile = temp.getChild("bigFile");
createFile(bigFile, sb.toString());
assertTrue("7.1", bigFile.fetchInfo().exists());
assertTrue("7.2", compareContent(getContents(sb.toString()), bigFile.openInputStream(EFS.NONE, null)));
assertTrue("7.2", compareContent(createInputStream(sb.toString()), bigFile.openInputStream(EFS.NONE, null)));
IFileStore destination = temp.getChild("copy of bigFile");
// IProgressMonitor monitor = new LoggingProgressMonitor(System.out);
IProgressMonitor monitor = createTestMonitor();
bigFile.copy(destination, EFS.NONE, monitor);
assertTrue("7.3", compareContent(getContents(sb.toString()), destination.openInputStream(EFS.NONE, null)));
assertTrue("7.3", compareContent(createInputStream(sb.toString()), destination.openInputStream(EFS.NONE, null)));
destination.delete(EFS.NONE, null);
}

Expand Down Expand Up @@ -318,21 +320,21 @@ public void testCopyFileAcrossVolumes() throws Throwable {
createFile(target, content);
deleteOnTearDown(target);
assertTrue("1.3", target.fetchInfo().exists());
assertTrue("1.4", compareContent(getContents(content), target.openInputStream(EFS.NONE, null)));
assertTrue("1.4", compareContent(createInputStream(content), target.openInputStream(EFS.NONE, null)));

/* c:\temp\target -> d:\temp\target */
IFileStore destination = tempDest.getChild(subfolderName);
deleteOnTearDown(destination);
target.copy(destination, IResource.DEPTH_INFINITE, null);
assertTrue("3.1", compareContent(getContents(content), destination.openInputStream(EFS.NONE, null)));
assertTrue("3.1", compareContent(createInputStream(content), destination.openInputStream(EFS.NONE, null)));
destination.delete(EFS.NONE, null);

/* c:\temp\target -> d:\temp\copy of target */
String copyOfSubfoldername = "copy of " + subfolderName;
destination = tempDest.getChild(copyOfSubfoldername);
deleteOnTearDown(destination);
target.copy(destination, IResource.DEPTH_INFINITE, null);
assertTrue("4.1", compareContent(getContents(content), destination.openInputStream(EFS.NONE, null)));
assertTrue("4.1", compareContent(createInputStream(content), destination.openInputStream(EFS.NONE, null)));
destination.delete(EFS.NONE, null);

/* c:\temp\target -> d:\temp\target (but the destination is already a file */
Expand All @@ -342,7 +344,7 @@ public void testCopyFileAcrossVolumes() throws Throwable {
createFile(destination, anotherContent);
assertTrue("5.1", !destination.fetchInfo().isDirectory());
target.copy(destination, IResource.DEPTH_INFINITE, null);
assertTrue("5.2", compareContent(getContents(content), destination.openInputStream(EFS.NONE, null)));
assertTrue("5.2", compareContent(createInputStream(content), destination.openInputStream(EFS.NONE, null)));
destination.delete(EFS.NONE, null);

/* c:\temp\target -> d:\temp\target (but the destination is already a folder */
Expand Down Expand Up @@ -502,7 +504,7 @@ public void testMoveDirectoryParentMissing() throws Throwable {
IFileStore parent = getTempStore();
IFileStore child = parent.getChild("child");
IFileStore existing = getTempStore();
createFile(existing, getRandomString());
createFile(existing, createRandomString());
// try to move when parent of destination does not exist
assertThrows(CoreException.class, () -> existing.move(child, EFS.NONE, createTestMonitor()));
// destination should not exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertDoesNotExistInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertExistsInWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.buildResources;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createRandomContentsStream;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createRandomString;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createUniqueString;
import static org.eclipse.core.tests.resources.ResourceTestUtil.findAvailableDevices;
Expand Down Expand Up @@ -209,7 +211,7 @@ public void testBug32785() throws CoreException {
location.toFile().mkdirs();
deleteOnTearDown(location);
link.createLink(location, IResource.NONE, createTestMonitor());
createInWorkspace(child, getRandomString());
createInWorkspace(child, createRandomString());
// move the link (rename)
IFolder movedLink = project.getFolder("MovedLink");
link.move(movedLink.getFullPath(), IResource.SHALLOW, createTestMonitor());
Expand Down Expand Up @@ -251,7 +253,7 @@ public void testBug156082() throws CoreException {
sub2.open(createTestMonitor());
IFile sub2File = sub2.getFile("file.txt");
IFile topFile = top.getFolder(sub2.getName()).getFile(sub2File.getName());
createInWorkspace(sub2File, getRandomString());
createInWorkspace(sub2File, createRandomString());
assertTrue("1.0", topFile.exists());
}

Expand Down Expand Up @@ -471,7 +473,7 @@ public void testCloseOpenProject() throws CoreException {
// close the project and make sure aliases in that project are no longer updated
pOverlap.close(createTestMonitor());
IFile linkFile = fLinked.getFile("ChildFile.txt");
linkFile.create(getRandomContents(), IResource.NONE, createTestMonitor());
linkFile.create(createRandomContentsStream(), IResource.NONE, createTestMonitor());
IFile closedFile = fOverlap.getFile(linkFile.getName());
assertFalse("1.0", closedFile.exists());
pOverlap.open(IResource.NONE, createTestMonitor());
Expand Down Expand Up @@ -584,7 +586,7 @@ public void testCreateDeleteFile() throws CoreException {
// file in linked folder
lChildLinked.delete(IResource.NONE, createTestMonitor());
assertOverlap("1.1", lChildLinked, lChildOverlap);
lChildLinked.create(getRandomContents(), IResource.NONE, createTestMonitor());
lChildLinked.create(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("1.2", lChildLinked, lChildOverlap);
//duplicate file
lOverlap.delete(IResource.NONE, createTestMonitor());
Expand All @@ -598,15 +600,15 @@ public void testCreateDeleteFile() throws CoreException {
assertTrue("2.4", lLinked.exists());
assertFalse("2.5", lLinked.getLocation().toFile().exists());
assertTrue("2.6", lLinked.isSynchronized(IResource.DEPTH_INFINITE));
assertThrows(CoreException.class, () -> lLinked.setContents(getRandomContents(), IResource.NONE, createTestMonitor()));
assertThrows(CoreException.class, () -> lLinked.setContents(createRandomContentsStream(), IResource.NONE, createTestMonitor()));

lOverlap.create(getRandomContents(), IResource.NONE, createTestMonitor());
lOverlap.create(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("2.8", lLinked, lOverlap);

//file in duplicate folder
lChildOverlap.delete(IResource.NONE, createTestMonitor());
assertOverlap("1.1", lChildLinked, lChildOverlap);
lChildOverlap.create(getRandomContents(), IResource.NONE, createTestMonitor());
lChildOverlap.create(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("1.2", lChildLinked, lChildOverlap);
}

Expand Down Expand Up @@ -804,34 +806,34 @@ public void testDeleteProjectContents() throws CoreException {
@Test
public void testFileAppendContents() throws CoreException {
//linked file
lLinked.appendContents(getRandomContents(), IResource.NONE, createTestMonitor());
lLinked.appendContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("1.1", lLinked, lOverlap);

//file in linked folder
lChildLinked.appendContents(getRandomContents(), IResource.NONE, createTestMonitor());
lChildLinked.appendContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("2.1", lChildLinked, lChildOverlap);
//duplicate file
lOverlap.appendContents(getRandomContents(), IResource.NONE, createTestMonitor());
lOverlap.appendContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("3.1", lLinked, lOverlap);
//file in duplicate folder
lChildOverlap.appendContents(getRandomContents(), IResource.NONE, createTestMonitor());
lChildOverlap.appendContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("3.1", lChildLinked, lChildOverlap);
}

@Test
public void testFileSetContents() throws CoreException {
//linked file
lLinked.setContents(getRandomContents(), IResource.NONE, createTestMonitor());
lLinked.setContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("1.1", lLinked, lOverlap);

//file in linked folder
lChildLinked.setContents(getRandomContents(), IResource.NONE, createTestMonitor());
lChildLinked.setContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("2.1", lChildLinked, lChildOverlap);
//duplicate file
lOverlap.setContents(getRandomContents(), IResource.NONE, createTestMonitor());
lOverlap.setContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("3.1", lLinked, lOverlap);
//file in duplicate folder
lChildOverlap.setContents(getRandomContents(), IResource.NONE, createTestMonitor());
lChildOverlap.setContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("3.1", lChildLinked, lChildOverlap);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.eclipse.core.tests.internal.builders;

import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createRandomContentsStream;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createTestMonitor;

import java.util.Map;
Expand Down Expand Up @@ -71,7 +72,7 @@ protected ICommand createCommand(IProjectDescription description, String builder
* Dirties the given file, forcing a build.
*/
protected void dirty(IFile file) throws CoreException {
file.setContents(getRandomContents(), true, true, createTestMonitor());
file.setContents(createRandomContentsStream(), true, true, createTestMonitor());
}

/**
Expand Down
Loading

0 comments on commit a306b1a

Please sign in to comment.