Skip to content

Commit

Permalink
Improve array length assertions in resource tests
Browse files Browse the repository at this point in the history
In order to reduce/avoid usage of ambiguous `assertEquals` (hard to
distinguish expected/actual) and prepare for a migration to JUnit 5 with
swapped expected/actual parameters in the `assertEquals` signature, this
change replaces certain `assertEquals` calls with AssertJ assertions:
* Replace `assertEquals(MESSAGE, EXPECTED, ACTUAL.length)` with
`assertThat(ACTUAL).as(MESSAGE).hasSize(EXPECTED))` and remove
obsolete message where possible
* Replace `assertEquals(EXPECTED, ACTUAL.length)` with
`assertThat(ACTUAL).hasSize(EXPECTED))
* Replace `hasSize(0)` with `isEmpty()` to improve failure messages
* Combine array size comparison with array contents comparison where
possible
  • Loading branch information
HeikoKlare committed Jan 11, 2024
1 parent 77f6f06 commit 6a574c3
Show file tree
Hide file tree
Showing 50 changed files with 1,276 additions and 1,594 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*******************************************************************************/
package org.eclipse.core.tests.filesystem;

import static java.util.function.Predicate.not;
import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureDoesNotExist;
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.ensureExists;
import static org.eclipse.core.tests.filesystem.FileSystemTestUtil.getMonitor;
Expand All @@ -33,6 +35,8 @@

import java.io.IOException;
import java.io.OutputStream;
import java.util.function.Consumer;
import java.util.function.Function;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileInfo;
import org.eclipse.core.filesystem.IFileStore;
Expand Down Expand Up @@ -156,40 +160,32 @@ public void testBrokenSymlinkMove() throws Exception {
ensureDoesNotExist(aFile);
ensureDoesNotExist(aDir);
IFileInfo[] infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 4);
assertThat(infos).hasSize(4);

Function<String, Consumer<IFileInfo>> isSymLinkCheckProvider = link -> (info -> {
assertThat(link).isEqualTo(info.getName());
assertThat(info.getAttribute(EFS.ATTRIBUTE_SYMLINK)).isTrue();
});

IFileStore _llFile = baseStore.getChild("_llFile");
IFileStore _llDir = baseStore.getChild("_llDir");
llFile.move(_llFile, EFS.NONE, getMonitor());
llDir.move(_llDir, EFS.NONE, getMonitor());
infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 4);
assertFalse("1.0", containsSymlink(infos, llFile.getName()));
assertFalse("1.1", containsSymlink(infos, llDir.getName()));
assertTrue("1.2", containsSymlink(infos, _llFile.getName()));
assertTrue("1.3", containsSymlink(infos, _llFile.getName()));
assertThat(infos).hasSize(4) //
.noneSatisfy(isSymLinkCheckProvider.apply(llFile.getName()))
.noneSatisfy(isSymLinkCheckProvider.apply(llDir.getName()))
.anySatisfy(isSymLinkCheckProvider.apply(_llFile.getName()));

IFileStore _lFile = baseStore.getChild("_lFile");
IFileStore _lDir = baseStore.getChild("_lDir");
lFile.move(_lFile, EFS.NONE, getMonitor());
lDir.move(_lDir, EFS.NONE, getMonitor());
infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 4);
assertFalse("1.4", containsSymlink(infos, lFile.getName()));
assertFalse("1.5", containsSymlink(infos, lDir.getName()));
assertTrue("1.6", containsSymlink(infos, _lFile.getName()));
assertTrue("1.7", containsSymlink(infos, _lFile.getName()));
}

private boolean containsSymlink(IFileInfo[] infos, String link) {
for (IFileInfo info : infos) {
if (link.equals(info.getName())) {
if (info.getAttribute(EFS.ATTRIBUTE_SYMLINK)) {
return true;
}
}
}
return false;
assertThat(infos).hasSize(4) //
.noneSatisfy(isSymLinkCheckProvider.apply(lFile.getName()))
.noneSatisfy(isSymLinkCheckProvider.apply(lDir.getName()))
.anySatisfy(isSymLinkCheckProvider.apply(_lFile.getName()));
}

// Removing a broken symlink is possible.
Expand All @@ -200,15 +196,15 @@ public void testBrokenSymlinkRemove() throws Exception {
ensureDoesNotExist(aFile);
ensureDoesNotExist(aDir);
IFileInfo[] infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 4);
assertThat(infos).hasSize(4);
llFile.delete(EFS.NONE, getMonitor());
llDir.delete(EFS.NONE, getMonitor());
infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 2);
assertThat(infos).hasSize(2);
lFile.delete(EFS.NONE, getMonitor());
lDir.delete(EFS.NONE, getMonitor());
infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 0);
assertThat(infos).isEmpty();
}

@Test
Expand All @@ -225,7 +221,7 @@ public void testRecursiveSymlink() throws Exception {
assertEquals("l2", i1.getStringAttribute(EFS.ATTRIBUTE_LINK_TARGET));

IFileInfo[] infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 2);
assertThat(infos).hasSize(2);
i1.setAttribute(EFS.ATTRIBUTE_READ_ONLY, true);
boolean exceptionThrown = false;
try {
Expand Down Expand Up @@ -257,7 +253,7 @@ public void testRecursiveSymlink() throws Exception {

l1.delete(EFS.NONE, getMonitor());
infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 1);
assertThat(infos).hasSize(1);
}

@Test
Expand Down Expand Up @@ -304,7 +300,7 @@ public void testSymlinkDirRead() throws Exception {
IFileStore childDir = aDir.getChild("subDir");
ensureExists(childDir, true);
IFileInfo[] infos = llDir.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 1);
assertThat(infos).hasSize(1);
assertTrue(infos[0].isDirectory());
assertFalse(infos[0].getAttribute(EFS.ATTRIBUTE_SYMLINK));
assertNull(infos[0].getStringAttribute(EFS.ATTRIBUTE_LINK_TARGET));
Expand All @@ -319,7 +315,7 @@ public void testSymlinkDirWrite() throws Exception {
IFileStore childFile = llDir.getChild("subFile");
ensureExists(childFile, false);
IFileInfo[] infos = aDir.childInfos(EFS.NONE, getMonitor());
assertEquals(infos.length, 1);
assertThat(infos).hasSize(1);
assertFalse(infos[0].isDirectory());
assertFalse(infos[0].getAttribute(EFS.ATTRIBUTE_SYMLINK));
assertNull(infos[0].getStringAttribute(EFS.ATTRIBUTE_LINK_TARGET));
Expand Down Expand Up @@ -359,21 +355,20 @@ public void _testSymlinkExtendedChars() throws Exception {
mkLink(baseStore, "l" + specialCharName, specialCharName, true);
mkLink(baseStore, "lf" + specialCharName, "ff" + specialCharName, false);
IFileInfo[] infos = baseStore.childInfos(EFS.NONE, getMonitor());
assertEquals("0.1", infos.length, 4);
for (IFileInfo info : infos) {
String infoName = info.getName();
assertTrue("1." + infoName, infoName.endsWith(specialCharName));
assertTrue("2." + infoName, info.exists());
assertThat(infos).hasSize(4);
assertThat(infos).allSatisfy(info -> {
assertThat(info.getName()).endsWith(specialCharName);
assertThat(info).matches(IFileInfo::exists, "exists");
if (info.getName().charAt(1) == 'f') {
assertFalse("3." + infoName, info.isDirectory());
assertThat(info).matches(not(IFileInfo::isDirectory), "is not a directory");
} else {
assertTrue("4." + infoName, info.isDirectory());
assertThat(info).matches(IFileInfo::isDirectory, "is a directory");
}
if (info.getName().charAt(0) == 'l') {
assertTrue("5." + infoName, info.getAttribute(EFS.ATTRIBUTE_SYMLINK));
assertTrue("6." + infoName, info.getStringAttribute(EFS.ATTRIBUTE_LINK_TARGET).endsWith(specialCharName));
assertThat(info).matches(it -> it.getAttribute(EFS.ATTRIBUTE_SYMLINK), "is symlink");
assertThat(info.getStringAttribute(EFS.ATTRIBUTE_LINK_TARGET).endsWith(specialCharName));
}
}
});
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.alias;

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.core.resources.ResourcesPlugin.getWorkspace;
import static org.eclipse.core.tests.harness.FileSystemHelper.getRandomLocation;
import static org.eclipse.core.tests.resources.ResourceTestUtil.assertDoesNotExistInFileSystem;
Expand Down Expand Up @@ -137,19 +138,18 @@ public URI toURI() {
* that both resources are in sync with the file system. The resource names
* in the tree may be different. The resources may not necessarily exist.
*/
public void assertOverlap(String message, IResource resource1, IResource resource2) throws CoreException {
String errMsg = message + resource1.getFullPath().toString();
assertEquals(errMsg + "(location)", resource1.getLocation(), resource2.getLocation());
assertTrue(errMsg + "(sync)", resource1.isSynchronized(IResource.DEPTH_ZERO));
assertTrue(errMsg + "(sync)", resource2.isSynchronized(IResource.DEPTH_ZERO));
public void assertOverlap(IResource resource1, IResource resource2) throws CoreException {
assertThat(resource1.getLocation()).isEqualTo(resource2.getLocation());
assertThat(resource1).matches(it -> it.isSynchronized(IResource.DEPTH_ZERO), "is synchronized");
assertThat(resource2).matches(it -> it.isSynchronized(IResource.DEPTH_ZERO), "is synchronized");

IResource[] children1 = null;
IResource[] children2 = null;
children1 = getSortedChildren(resource1);
children2 = getSortedChildren(resource2);
assertEquals(errMsg + "(child count)", children1.length, children2.length);
assertThat(children1).as("number of children").hasSameSizeAs(children2);
for (int i = 0; i < children2.length; i++) {
assertOverlap(message, children1[i], children2[i]);
assertOverlap(children1[i], children2[i]);
}
}

Expand Down Expand Up @@ -435,12 +435,10 @@ public void testBug256837() throws CoreException {

// now p2 and link2TempFolder should be aliases
IResource[] resources = aliasManager.computeAliases(link2TempFolder, ((Folder) link2TempFolder).getStore());
assertEquals("5.0", 1, resources.length);
assertEquals("6.0", p2, resources[0]);
assertThat(resources).containsExactly(p2);

resources = aliasManager.computeAliases(p2, ((Project) p2).getStore());
assertEquals("7.0", 1, resources.length);
assertEquals("8.0", link2TempFolder, resources[0]);
assertThat(resources).containsExactly(link2TempFolder);
}

@Test
Expand Down Expand Up @@ -509,12 +507,12 @@ public void testCopyFile() throws CoreException {
sourceFile.copy(linkDest.getFullPath(), IResource.NONE, createTestMonitor());
assertTrue("1.1", linkDest.exists());
assertTrue("1.2", overlapDest.exists());
assertOverlap("1.3", linkDest, overlapDest);
assertOverlap(linkDest, overlapDest);

linkDest.delete(IResource.NONE, createTestMonitor());
assertFalse("1.4", linkDest.exists());
assertFalse("1.5", overlapDest.exists());
assertOverlap("1.6", linkDest, overlapDest);
assertOverlap(linkDest, overlapDest);

// duplicate file
linkDest = lLinked;
Expand All @@ -530,7 +528,7 @@ public void testCopyFile() throws CoreException {
sourceFile.copy(overlapDest.getFullPath(), IResource.NONE, createTestMonitor());
assertTrue("2.4", linkDest.exists());
assertTrue("2.5", overlapDest.exists());
assertOverlap("2.6", linkDest, overlapDest);
assertOverlap(linkDest, overlapDest);

// file in duplicate folder
linkDest = fLinked.getFile("CopyDestination");
Expand All @@ -539,12 +537,12 @@ public void testCopyFile() throws CoreException {
sourceFile.copy(overlapDest.getFullPath(), IResource.NONE, createTestMonitor());
assertTrue("3.1", linkDest.exists());
assertTrue("3.2", overlapDest.exists());
assertOverlap("3.3", linkDest, overlapDest);
assertOverlap(linkDest, overlapDest);

overlapDest.delete(IResource.NONE, createTestMonitor());
assertFalse("3.4", linkDest.exists());
assertFalse("3.5", overlapDest.exists());
assertOverlap("3.6", linkDest, overlapDest);
assertOverlap(linkDest, overlapDest);
}

@Test
Expand Down Expand Up @@ -596,9 +594,9 @@ public void testCopyToChild() throws CoreException {
public void testCreateDeleteFile() throws CoreException {
// file in linked folder
lChildLinked.delete(IResource.NONE, createTestMonitor());
assertOverlap("1.1", lChildLinked, lChildOverlap);
assertOverlap(lChildLinked, lChildOverlap);
lChildLinked.create(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("1.2", lChildLinked, lChildOverlap);
assertOverlap(lChildLinked, lChildOverlap);
//duplicate file
lOverlap.delete(IResource.NONE, createTestMonitor());
assertEquals("2.0", lLinked.getLocation(), lOverlap.getLocation());
Expand All @@ -614,13 +612,13 @@ public void testCreateDeleteFile() throws CoreException {
assertThrows(CoreException.class, () -> lLinked.setContents(createRandomContentsStream(), IResource.NONE, createTestMonitor()));

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

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

@Test
Expand All @@ -632,7 +630,7 @@ public void testCreateDeleteFolder() throws CoreException {
assertFalse("1.1", fLinked.getLocation().toFile().exists());

fOverlap.create(IResource.NONE, true, createTestMonitor());
assertOverlap("1.2", fOverlap, fLinked);
assertOverlap(fOverlap, fLinked);

//linked folder
fLinked.delete(IResource.NONE, createTestMonitor());
Expand All @@ -641,20 +639,20 @@ public void testCreateDeleteFolder() throws CoreException {
assertTrue("2.1", fOverlap.getLocation().toFile().exists());

fLinked.createLink(fOverlap.getLocation(), IResource.NONE, createTestMonitor());
assertOverlap("1.4", fOverlap, fLinked);
assertOverlap(fOverlap, fLinked);

//child of linked folders
IFolder child1 = fLinkOverlap1.getFolder("LinkChild");
IFolder child2 = fLinkOverlap2.getFolder(child1.getName());

child1.create(IResource.NONE, true, createTestMonitor());
assertOverlap("3.0", child1, child2);
assertOverlap(child1, child2);
child1.delete(IResource.NONE, createTestMonitor());
assertFalse("3.1", child1.exists());
assertFalse("3.2", child2.exists());

child2.create(IResource.NONE, true, createTestMonitor());
assertOverlap("3.3", child1, child2);
assertOverlap(child1, child2);
child2.delete(IResource.NONE, createTestMonitor());
assertFalse("3.4", child1.exists());
assertFalse("3.5", child2.exists());
Expand Down Expand Up @@ -818,34 +816,34 @@ public void testDeleteProjectContents() throws CoreException {
public void testFileAppendContents() throws CoreException {
//linked file
lLinked.appendContents(createRandomContentsStream(), IResource.NONE, createTestMonitor());
assertOverlap("1.1", lLinked, lOverlap);
assertOverlap(lLinked, lOverlap);

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

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

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

/**
Expand All @@ -860,15 +858,15 @@ public void testMoveFile() throws CoreException {
assertDoesNotExistInWorkspace(lChildLinked);
assertDoesNotExistInWorkspace(lChildOverlap);
assertExistsInWorkspace(destination);
assertOverlap("1.4", lChildLinked, lChildOverlap);
assertOverlap(lChildLinked, lChildOverlap);
assertTrue("1.5", lChildLinked.isSynchronized(IResource.DEPTH_INFINITE));
assertTrue("1.6", destination.isSynchronized(IResource.DEPTH_INFINITE));

destination.move(lChildLinked.getFullPath(), IResource.NONE, createTestMonitor());
assertExistsInWorkspace(lChildLinked);
assertExistsInWorkspace(lChildOverlap);
assertDoesNotExistInWorkspace(destination);
assertOverlap("2.4", lChildLinked, lChildOverlap);
assertOverlap(lChildLinked, lChildOverlap);
//duplicate file
lOverlap.move(destination.getFullPath(), IResource.NONE, createTestMonitor());
assertDoesNotExistInWorkspace(lOverlap);
Expand All @@ -882,19 +880,19 @@ public void testMoveFile() throws CoreException {
assertExistsInWorkspace(lLinked);
assertExistsInWorkspace(lOverlap);
assertDoesNotExistInWorkspace(destination);
assertOverlap("3.8", lLinked, lOverlap);
assertOverlap(lLinked, lOverlap);
//file in duplicate folder
lChildOverlap.move(destination.getFullPath(), IResource.NONE, createTestMonitor());
assertDoesNotExistInWorkspace(lChildLinked);
assertDoesNotExistInWorkspace(lChildOverlap);
assertExistsInWorkspace(destination);
assertOverlap("3.4", lChildLinked, lChildOverlap);
assertOverlap(lChildLinked, lChildOverlap);

destination.move(lChildOverlap.getFullPath(), IResource.NONE, createTestMonitor());
assertExistsInWorkspace(lChildLinked);
assertExistsInWorkspace(lChildOverlap);
assertDoesNotExistInWorkspace(destination);
assertOverlap("3.8", lChildLinked, lChildOverlap);
assertOverlap(lChildLinked, lChildOverlap);
}

}
Loading

0 comments on commit 6a574c3

Please sign in to comment.