Skip to content

Commit

Permalink
Migrate resources utils tests to JUnit 5 #903
Browse files Browse the repository at this point in the history
Migrates the tests in org.eclipse.core.tests.internal.utils to JUnit 5.
- Exchange JUnit 4 test annotations
- Replace JUnit 4 assertions with JUnit 5 or AssertJ assertions

Contributes to
#903
  • Loading branch information
HeikoKlare committed Sep 19, 2024
1 parent b432795 commit 0b69e82
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,31 @@
*******************************************************************************/
package org.eclipse.core.tests.internal.utils;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.net.URI;
import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.URIUtil;
import org.eclipse.core.internal.utils.FileUtil;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.tests.harness.FileSystemHelper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Tests for {@link FileUtil} class.
*/
public class FileUtilTest {
private IPath baseTestDir;

@Before
@BeforeEach
public void setUp() throws Exception {
baseTestDir = FileSystemHelper.getRandomLocation();
baseTestDir.toFile().mkdirs();
}

@After
@AfterEach
public void tearDown() throws Exception {
FileSystemHelper.clear(baseTestDir.toFile());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,15 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.eclipse.core.tests.resources.ResourceTestUtil.createRandomString;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.eclipse.core.internal.utils.ObjectMap;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class ObjectMapTest {
private static final int MAXIMUM = 100;
Expand All @@ -44,17 +42,17 @@ public void testPut() {
for (int i = 0; i < values.length; i++) {
Integer key = Integer.valueOf(i);
map.put(key, values[i]);
assertTrue("2.0." + i, map.containsKey(key));
assertTrue("2.1." + i, map.containsValue(values[i]));
assertEquals("2.2." + i, i + 1, map.size());
assertThat(map).containsKey(key);
assertThat(map).containsValue(values[i]);
assertThat(map).hasSize(i + 1);
}

// make sure they are all still there
assertEquals("3.0", MAXIMUM, map.size());
assertThat(map).hasSize(MAXIMUM);
for (int i = 0; i < values.length; i++) {
Integer key = Integer.valueOf(i);
assertTrue("3.1." + i, map.containsKey(key));
assertNotNull("3.2." + i, map.get(key));
assertThat(map).containsKey(key);
assertNotNull(map.get(key), "" + i);
}
}

Expand All @@ -72,18 +70,18 @@ public void testRemove() {

// remove each element
for (int i = MAXIMUM - 1; i >= 0; i--) {
Object key = Integer.valueOf(i);
Integer key = Integer.valueOf(i);
map.remove(key);
assertTrue("2.0." + i, !map.containsKey(key));
assertEquals("2.1," + i, i, map.size());
assertThat(map).doesNotContainKey(key);
assertThat(map).hasSize(i);
// check that the others still exist
for (int j = 0; j < i; j++) {
assertTrue("2.2." + j, map.containsKey(Integer.valueOf(j)));
assertThat(map).containsKey(Integer.valueOf(j));
}
}

// all gone?
assertEquals("3.0", 0, map.size());
assertThat(map).isEmpty();
}

@Test
Expand All @@ -92,14 +90,14 @@ public void testContains() {
ObjectMap<Integer, Object> map = populateMap(values);

for (int i = 0; i < MAXIMUM; i++) {
assertTrue("2.0." + i, map.containsKey(Integer.valueOf(i)));
assertTrue("2.1." + i, map.containsValue(values[i]));
assertThat(map).containsKey(Integer.valueOf(i));
assertThat(map).containsValue(values[i]);
}

assertFalse("3.0", map.containsKey(Integer.valueOf(MAXIMUM + 1)));
assertFalse("3.1", map.containsKey(Integer.valueOf(-1)));
assertFalse("3.2", map.containsValue(null));
assertFalse("3.3", map.containsValue(createRandomString()));
assertThat(map).doesNotContainKey(Integer.valueOf(MAXIMUM + 1));
assertThat(map).doesNotContainKey(Integer.valueOf(-1));
assertThat(map).doesNotContainValue(null);
assertThat(map).doesNotContainValue(createRandomString());
}

@Test
Expand All @@ -109,7 +107,7 @@ public void testValues() {

Collection<Object> result = map.values();
for (int i = 0; i < MAXIMUM; i++) {
assertTrue("2.0." + i, result.contains(values[i]));
assertThat(result).contains(values[i]);
}
}

Expand All @@ -118,7 +116,7 @@ public void testKeySet() {
Object[] values = new Object[MAXIMUM];
ObjectMap<Integer, Object> map = populateMap(values);
Set<Integer> keys = map.keySet();
assertEquals("1.0", MAXIMUM, keys.size());
assertThat(keys).hasSize(MAXIMUM);
}

@Test
Expand All @@ -127,7 +125,7 @@ public void testEntrySet() {
ObjectMap<Integer, Object> map = populateMap(values);
Set<Map.Entry<Integer, Object>> entries = map.entrySet();
for (int i = 0; i < MAXIMUM; i++) {
assertTrue("1.0." + i, contains(entries, values[i]));
assertTrue(contains(entries, values[i]), "" + i);
}
}

Expand Down

0 comments on commit 0b69e82

Please sign in to comment.