Update to JUnit 5

This commit is contained in:
Zach Levis committed 2021-03-25 20:43:08 -07:00
1 parent b508b46b94
commit 6166ef01e2
16 files changed
+487 -300

No files matched your search

+9 -2
View File
@@ -102,9 +102,12 @@ dependencies {
// Math library
api("org.spongepowered:math:2.0.0-SNAPSHOT")
testImplementation("junit:junit:4.13.1")
testImplementation(platform("org.junit:junit-bom:5.7.1"))
testImplementation("org.junit.jupiter:junit-jupiter-api")
testImplementation("org.junit.jupiter:junit-jupiter-params")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testImplementation("org.hamcrest:hamcrest:2.2")
testImplementation("org.mockito:mockito-core:3.6.28")
testImplementation("org.mockito:mockito-core:3.7.7")
}
val spongeSnapshotRepo: String? by project
val spongeReleaseRepo: String? by project
@@ -178,6 +181,10 @@ tasks {
}
}
test {
useJUnitPlatform()
}
//
// val shadowJar by registering(ShadowJar::class) {
// archiveClassifier.set("shaded")
@@ -27,10 +27,12 @@ package org.spongepowered.api.event.lifecycle;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.network.channel.Channel;
import org.spongepowered.api.registry.DuplicateRegistrationException;
import org.spongepowered.api.util.annotation.eventgen.NoFactoryMethod;
/**
* Lifecycle event to indicate when network channels should be created and registered.
*/
@NoFactoryMethod
public interface RegisterChannelEvent extends LifecycleEvent {
/**
@@ -26,10 +26,12 @@ package org.spongepowered.api.event.lifecycle;
import org.spongepowered.api.data.DataManager;
import org.spongepowered.api.data.DataRegistration;
import org.spongepowered.api.util.annotation.eventgen.NoFactoryMethod;
/**
* Lifecycle event for registering custom {@link DataRegistration data} within the {@link DataManager}.
*/
@NoFactoryMethod
public interface RegisterDataEvent extends LifecycleEvent {
RegisterDataEvent register(DataRegistration registration);
@@ -26,7 +26,9 @@ package org.spongepowered.api.event.lifecycle;
import org.spongepowered.api.datapack.DataPackSerializable;
import org.spongepowered.api.event.GenericEvent;
import org.spongepowered.api.util.annotation.eventgen.NoFactoryMethod;
@NoFactoryMethod
public interface RegisterDataPackValueEvent<T extends DataPackSerializable> extends LifecycleEvent, GenericEvent<T> {
RegisterDataPackValueEvent<T> register(T serializable);
@@ -24,23 +24,25 @@
*/
package org.spongepowered.api.data;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.hamcrest.Matchers.not;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.spongepowered.api.data.persistence.DataQuery;
import java.util.ArrayList;
import java.util.List;
public class DataQueryTest {
class DataQueryTest {
/**
* Method: of()
*/
@Test
public void testOf() {
void testOf() {
DataQuery.of();
DataQuery.of("derp");
}
@@ -49,110 +51,110 @@ public class DataQueryTest {
* Method: of(char separator, String path)
*/
@Test
public void testOfForSeparatorPath() {
void testOfForSeparatorPath() {
DataQuery first = DataQuery.of('.', "this.test.equals");
DataQuery second = DataQuery.of(',', "this,test,equals");
DataQuery complex = DataQuery.of('\u8482', "this\u8482test\u8482equals");
DataQuery test = DataQuery.of("this", "test", "equals");
assertThat(first.equals(test), is(true));
assertThat(first.equals(second), is(true));
assertThat(first.equals(complex), is(true));
assertThat(second.equals(complex), is(true));
assertThat(second.equals(first), is(true));
assertThat(second.equals(test), is(true));
MatcherAssert.assertThat(first, is(equalTo(test)));
MatcherAssert.assertThat(first, is(equalTo(second)));
MatcherAssert.assertThat(first, is(equalTo(complex)));
MatcherAssert.assertThat(second, is(equalTo(complex)));
MatcherAssert.assertThat(second, is(equalTo(first)));
MatcherAssert.assertThat(second, is(equalTo(test)));
}
/**
* Method: of(String... parts)
*/
@Test
public void testOfParts() {
void testOfParts() {
DataQuery first = DataQuery.of("this", "parts", "equal");
DataQuery second = DataQuery.of('.', "this.parts.equal");
assertThat(first.equals(second), is(true));
assertThat(second.equals(first), is(true));
MatcherAssert.assertThat(first, is(equalTo(second)));
MatcherAssert.assertThat(second, is(equalTo(first)));
}
/**
* Method: parts()
*/
@Test
public void testGetParts() {
void testGetParts() {
final DataQuery query = DataQuery.of("this", "parts", "test");
final List<String> parts = new ArrayList<>();
parts.add("this");
parts.add("parts");
parts.add("test");
assertThat(query.parts().equals(parts), is(true));
MatcherAssert.assertThat(query.parts(), is(equalTo(parts)));
}
/**
* Method: then(DataQuery that)
*/
@Test
public void testThen() {
void testThen() {
final DataQuery query = DataQuery.of("this", "testing");
final DataQuery other = DataQuery.of("this");
final DataQuery test = other.then(DataQuery.of("testing"));
assertThat(query.equals(test), is(true));
assertThat(test.equals(query), is(true));
MatcherAssert.assertThat(query, is(equalTo(test)));
MatcherAssert.assertThat(test, is(equalTo(query)));
}
/**
* Method: queryParts()
*/
@Test
public void testGetQueryParts() {
void testGetQueryParts() {
final DataQuery full = DataQuery.of("this", "test", "query");
final DataQuery part1 = DataQuery.of("this");
final DataQuery part2 = DataQuery.of("test");
final DataQuery part3 = DataQuery.of("query");
final List<DataQuery> parts = full.queryParts();
final List<DataQuery> built = ImmutableList.of(part1, part2, part3);
assertThat(parts.equals(built), is(true));
assertThat(built.equals(parts), is(true));
assertThat(built.containsAll(parts), is(true));
assertThat(parts.containsAll(built), is(true));
MatcherAssert.assertThat(parts, equalTo(built));
MatcherAssert.assertThat(built, equalTo(parts));
MatcherAssert.assertThat(built.containsAll(parts), is(true));
MatcherAssert.assertThat(parts.containsAll(built), is(true));
}
/**
* Method: pop()
*/
@Test
public void testPop() {
void testPop() {
DataQuery prePopped = DataQuery.of("this", "test", "query");
DataQuery expected = DataQuery.of("this", "test");
assertThat(prePopped.pop().equals(expected), is(true));
MatcherAssert.assertThat(prePopped.pop(), equalTo(expected));
DataQuery empty = DataQuery.of();
DataQuery emptyPopped = empty.pop();
assertThat(emptyPopped.equals(empty), is(true));
MatcherAssert.assertThat(emptyPopped, equalTo(empty));
DataQuery single = DataQuery.of("single");
assertThat(single.pop().equals(empty), is(true));
MatcherAssert.assertThat(single.pop(), equalTo(empty));
}
/**
* Method: last()
*/
@Test
public void testLast() {
void testLast() {
final DataQuery full = DataQuery.of("first", "test");
final DataQuery lastExpected = DataQuery.of("test");
assertThat(full.last().equals(lastExpected), is(true));
assertThat(lastExpected.equals(full.last()), is(true));
MatcherAssert.assertThat(full.last(), equalTo(lastExpected));
MatcherAssert.assertThat(lastExpected, equalTo(full.last()));
}
/**
* Method: equals(Object obj)
*/
@Test
public void testEquals() {
void testEquals() {
DataQuery query1 = DataQuery.of("test");
DataQuery query2 = DataQuery.of("test");
DataQuery nonEqual = DataQuery.of("nope");
assertThat(query1.equals(query1), is(true));
assertThat(query1.equals(query2), is(true));
assertThat(query1.equals(nonEqual), is(false));
MatcherAssert.assertThat(query1, equalTo(query1));
MatcherAssert.assertThat(query1, equalTo(query2));
MatcherAssert.assertThat(query1, is(not(nonEqual)));
}
}
@@ -24,20 +24,20 @@
*/
package org.spongepowered.api.data;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.spongepowered.api.data.DataTransactionResult.Type;
public class DataTransactionBuilderTest {
class DataTransactionBuilderTest {
@Test
public void testCorrectTypeWhenAbsorbingResult() {
assertEquals(Type.UNDEFINED, this.absorbedType(Type.UNDEFINED, Type.UNDEFINED));
assertEquals(Type.SUCCESS, this.absorbedType(Type.SUCCESS, Type.UNDEFINED));
assertEquals(Type.FAILURE, this.absorbedType(Type.FAILURE, Type.SUCCESS));
assertEquals(Type.ERROR, this.absorbedType(Type.FAILURE, Type.ERROR));
assertEquals(Type.CANCELLED, this.absorbedType(Type.FAILURE, Type.CANCELLED));
void testCorrectTypeWhenAbsorbingResult() {
Assertions.assertEquals(Type.UNDEFINED, this.absorbedType(Type.UNDEFINED, Type.UNDEFINED));
Assertions.assertEquals(Type.SUCCESS, this.absorbedType(Type.SUCCESS, Type.UNDEFINED));
Assertions.assertEquals(Type.FAILURE, this.absorbedType(Type.FAILURE, Type.SUCCESS));
Assertions.assertEquals(Type.ERROR, this.absorbedType(Type.FAILURE, Type.ERROR));
Assertions.assertEquals(Type.CANCELLED, this.absorbedType(Type.FAILURE, Type.CANCELLED));
}
private Type absorbedType(Type builderType, Type resultType) {
@@ -24,97 +24,101 @@
*/
package org.spongepowered.api.event;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.equalToObject;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import com.google.common.collect.ImmutableList;
import org.junit.Test;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.spongepowered.api.matcher.SpongeMatchers;
import java.util.List;
import java.util.Optional;
public class CauseTest {
class CauseTest {
@Test
public void testPopulatedCause() {
void testPopulatedCause() {
Cause.builder().append("foo").build(EventContext.empty());
}
@Test(expected = NullPointerException.class)
public void testNullCause() {
Cause.builder().append(null);
@Test
void testNullCause() {
Assertions.assertThrows(NullPointerException.class, () -> Cause.builder().append(null));
}
@Test
public void testWithCause() {
void testWithCause() {
final Cause old = Cause.of(EventContext.empty(), "foo");
final Cause newCause = old.with("bar");
assertThat(old, not(newCause));
MatcherAssert.assertThat(old, is(not(newCause)));
List<?> list = newCause.all();
assertThat(list.contains("foo"), is(true));
assertThat(list.contains("bar"), is(true));
}
@Test(expected = NullPointerException.class)
public void testWithNullCause() {
final Cause old = Cause.of(EventContext.empty(), "foo");
old.with((Object) null);
MatcherAssert.assertThat(list, containsInAnyOrder("foo", "bar"));
}
@Test
public void testToString() {
void testWithNullCause() {
final Cause old = Cause.of(EventContext.empty(), "foo");
Assertions.assertThrows(NullPointerException.class, () -> old.with((Object) null));
}
@Test
void testToString() {
final Cause cause = Cause.builder().append("foo").append("bar").append(1).append(2).build(EventContext.empty());
final String causeString = cause.toString();
assertThat(causeString.isEmpty(), is(false));
MatcherAssert.assertThat(causeString, not(emptyString()));
}
@Test
public void testBefore() {
void testBefore() {
final Cause cause = Cause.builder().append("foo").append(1).append(2).build(EventContext.empty());
final Optional<?> optional = cause.before(Integer.class);
assertThat(optional.isPresent(), is(true));
assertThat(optional.get(), is("foo"));
MatcherAssert.assertThat(optional, is(SpongeMatchers.present()));
MatcherAssert.assertThat(optional, SpongeMatchers.valueIs(equalToObject("foo")));
}
@Test
public void testAfter() {
void testAfter() {
final Cause cause = Cause.builder().append("foo").append(1).append(2).build(EventContext.empty());
final Optional<?> optional = cause.after(Integer.class);
assertThat(optional.isPresent(), is(true));
assertThat(optional.get(), is(2));
MatcherAssert.assertThat(optional, is(SpongeMatchers.present()));
MatcherAssert.assertThat(optional, SpongeMatchers.valueIs(equalToObject(2)));
}
@Test
public void testNoneAfter() {
void testNoneAfter() {
final Cause cause = Cause.builder().append("foo").append(1).build(EventContext.empty());
final Optional<?> optional = cause.after(Integer.class);
assertThat(optional.isPresent(), is(false));
MatcherAssert.assertThat(optional, is(not(SpongeMatchers.present())));
}
@Test
public void testNoneBefore() {
void testNoneBefore() {
final Cause cause = Cause.builder().append("foo").append(1).build(EventContext.empty());
final Optional<?> optional = cause.before(String.class);
assertThat(optional.isPresent(), is(false));
MatcherAssert.assertThat(optional, is(not(SpongeMatchers.present())));
}
@Test
public void testNoneOf() {
void testNoneOf() {
final Cause cause = Cause.builder().append("foo").append(1).append(2).append(3).build(EventContext.empty());
assertThat(cause.noneOf(Integer.class), hasSize(1));
assertThat(cause.noneOf(Integer.class).get(0), is("foo"));
MatcherAssert.assertThat(cause.noneOf(Integer.class), hasSize(1));
MatcherAssert.assertThat(cause.noneOf(Integer.class).get(0), is("foo"));
}
@Test
public void testListedArray() {
void testListedArray() {
final List<String> fooList = ImmutableList.of("foo", "bar", "baz", "floof");
final Cause cause = Cause.builder().append("foo").append("bar").append("baz").append("floof").build(EventContext.empty());
final List<String> stringList = cause.allOf(String.class);
assertThat(stringList.isEmpty(), is(false));
assertThat(stringList.equals(fooList), is(true));
MatcherAssert.assertThat(stringList, is(not(empty())));
MatcherAssert.assertThat(stringList, is(fooList));
}
}
@@ -25,12 +25,14 @@
package org.spongepowered.api.event;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.closeTo;
import static org.junit.Assert.assertThat;
import com.google.common.collect.Lists;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.event.cause.entity.damage.DamageFunction;
import org.spongepowered.api.event.cause.entity.damage.DamageModifier;
@@ -40,47 +42,47 @@ import java.util.List;
import java.util.Map;
import java.util.function.DoubleUnaryOperator;
public class SpongeAbstractDamageEntityEventTest {
class SpongeAbstractDamageEntityEventTest {
private static final double ERROR = 0.03;
@Test
public void testParams() {
void testParams() {
Entity targetEntity = this.mockParam(Entity.class);
int originalDamage = 5;
DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(),"none"),
targetEntity, Lists.newArrayList(), originalDamage);
assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalFinalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalFinalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.finalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.baseDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.finalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.baseDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
}
@Test
public void testSetBaseDamage() {
void testSetBaseDamage() {
Entity targetEntity = this.mockParam(Entity.class);
int originalDamage = 5;
DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(),"none"),
targetEntity, Lists.newArrayList(), originalDamage);
assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalFinalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalFinalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
event.setBaseDamage(20);
assertThat(event.baseDamage(), is(closeTo(20, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.finalDamage(), is(closeTo(20, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.baseDamage(), is(closeTo(20, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.finalDamage(), is(closeTo(20, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalFinalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalFinalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
}
@Test
public void testUseModifiers() {
void testUseModifiers() {
Entity targetEntity = this.mockParam(Entity.class);
final int originalDamage = 1;
@@ -99,28 +101,28 @@ public class SpongeAbstractDamageEntityEventTest {
targetEntity, originalFunctions, originalDamage);
final List<DamageFunction> originalFunctions1 = event.originalFunctions();
assertThat(originalFunctions1, is(Matchers.equalTo(originalFunctions)));
MatcherAssert.assertThat(originalFunctions1, is(Matchers.equalTo(originalFunctions)));
assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalFinalDamage(), is(closeTo(originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalFinalDamage(), is(closeTo(originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
Map<DamageModifier, Double> originalDamages = event.originalDamages();
assertThat(originalDamages.size(), is(originalFunctions.size()));
MatcherAssert.assertThat(originalDamages, is(aMapWithSize(originalFunctions.size())));
assertThat(originalDamages.get(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(originalDamages.get(secondModifier), is(closeTo(secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(originalDamages.get(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(originalDamages.get(secondModifier), is(closeTo(secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalModifierDamage(secondModifier), is(closeTo(secondModifierDamage,
MatcherAssert.assertThat(event.originalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalModifierDamage(secondModifier), is(closeTo(secondModifierDamage,
SpongeAbstractDamageEntityEventTest.ERROR
)));
assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
MatcherAssert.assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
}
@Test
public void testSetModifiers() {
void testSetModifiers() {
Entity targetEntity = this.mockParam(Entity.class);
final int originalDamage = 1;
@@ -143,32 +145,32 @@ public class SpongeAbstractDamageEntityEventTest {
DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(),"none"),
targetEntity, originalFunctions, originalDamage);
assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
MatcherAssert.assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
DoubleUnaryOperator newFunction = p -> p;
event.setDamage(firstModifer, newFunction);
assertThat(event.damage(firstModifer), is(closeTo(firstChangedDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.damage(secondModifier), is(closeTo(secondChangedDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.damage(firstModifer), is(closeTo(firstChangedDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.damage(secondModifier), is(closeTo(secondChangedDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalModifierDamage(secondModifier), is(closeTo(secondModifierDamage,
MatcherAssert.assertThat(event.originalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalModifierDamage(secondModifier), is(closeTo(secondModifierDamage,
SpongeAbstractDamageEntityEventTest.ERROR
)));
assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalFinalDamage(), is(closeTo(originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.finalDamage(), is(closeTo(modifiedFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalFinalDamage(), is(closeTo(originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.finalDamage(), is(closeTo(modifiedFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
MatcherAssert.assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
assertThat(event.modifiers(),
MatcherAssert.assertThat(event.modifiers(),
is(Matchers.equalTo(Lists.newArrayList(DamageFunction.of(firstModifer, newFunction), originalFunctions.get(1)))));
}
@Test
public void testAddModifier() {
void testAddModifier() {
Entity targetEntity = this.mockParam(Entity.class);
final int originalDamage = 1;
@@ -196,32 +198,32 @@ public class SpongeAbstractDamageEntityEventTest {
DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(),"none"), targetEntity, originalFunctions
, originalDamage);
assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
MatcherAssert.assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
assertThat(event.isModifierApplicable(thirdModifier), is(false));
MatcherAssert.assertThat(event.isModifierApplicable(thirdModifier), is(false));
event.setDamage(thirdModifier, thirdFunction);
assertThat(event.damage(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.damage(secondModifier), is(closeTo(secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.damage(thirdModifier), is(closeTo(thirdDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.damage(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.damage(secondModifier), is(closeTo(secondModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.damage(thirdModifier), is(closeTo(thirdDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalModifierDamage(secondModifier), is(closeTo(secondModifierDamage,
MatcherAssert.assertThat(event.originalModifierDamage(firstModifer), is(closeTo(firstModifierDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalModifierDamage(secondModifier), is(closeTo(secondModifierDamage,
SpongeAbstractDamageEntityEventTest.ERROR
)));
assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalFinalDamage(), is(closeTo(originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.finalDamage(), is(closeTo(modifiedFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalDamage(), is(closeTo(originalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.originalFinalDamage(), is(closeTo(originalFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
MatcherAssert.assertThat(event.finalDamage(), is(closeTo(modifiedFinalDamage, SpongeAbstractDamageEntityEventTest.ERROR)));
assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
MatcherAssert.assertThat(event.originalFunctions(), is(Matchers.equalTo(originalFunctions)));
assertThat(event.modifiers(), is(Matchers.equalTo(newFunctions)));
MatcherAssert.assertThat(event.modifiers(), is(Matchers.equalTo(newFunctions)));
}
@Test
public void testModifiersApplicable() {
void testModifiersApplicable() {
Entity targetEntity = this.mockParam(Entity.class);
DamageModifier firstModifer = this.mockParam(DamageModifier.class);
@@ -233,21 +235,21 @@ public class SpongeAbstractDamageEntityEventTest {
DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(), "none"), targetEntity,
originalFunctions, 0);
assertThat(event.isModifierApplicable(firstModifer), is(true));
assertThat(event.isModifierApplicable(secondModifier), is(true));
assertThat(event.isModifierApplicable(this.mockParam(DamageModifier.class)), is(false));
MatcherAssert.assertThat(event.isModifierApplicable(firstModifer), is(true));
MatcherAssert.assertThat(event.isModifierApplicable(secondModifier), is(true));
MatcherAssert.assertThat(event.isModifierApplicable(this.mockParam(DamageModifier.class)), is(false));
}
@Test(expected = IllegalArgumentException.class)
public void testNotApplicableModifer() {
@Test
void testNotApplicableModifer() {
DamageEntityEvent event = SpongeEventFactory.createDamageEntityEvent(Cause.of(EventContext.empty(), "none"), this.mockParam(Entity.class),
Lists.newArrayList(), 0);
DamageModifier modifier = this.mockParam(DamageModifier.class);
assertThat(event.isModifierApplicable(modifier), is(false));
MatcherAssert.assertThat(event.isModifierApplicable(modifier), is(false));
event.originalModifierDamage(modifier);
Assertions.assertThrows(IllegalArgumentException.class, () -> event.originalModifierDamage(modifier));
}
@SuppressWarnings("unchecked")
@@ -26,32 +26,32 @@ package org.spongepowered.api.event;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Test;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.event.data.ChangeDataHolderEvent;
public class SpongeAbstractEventTest {
class SpongeAbstractEventTest {
// @Test
public void testChangeBlockEvent_filter() {
void testChangeBlockEvent_filter() {
}
@Test
public void testValueChangeEvent() {
void testValueChangeEvent() {
DataTransactionResult original = DataTransactionResult.failNoData();
DataTransactionResult modified = DataTransactionResult.successNoData();
ChangeDataHolderEvent.ValueChange event = SpongeEventFactory.createChangeDataHolderEventValueChange(
Cause.of(EventContext.empty(), "none"), original, this.mockParam(DataHolder.Mutable.class));
assertThat(event.originalChanges(), is(equalTo(original)));
MatcherAssert.assertThat(event.originalChanges(), is(equalTo(original)));
event.proposeChanges(modified);
assertThat(event.endResult(), is(equalTo(modified)));
MatcherAssert.assertThat(event.endResult(), is(equalTo(modified)));
}
@SuppressWarnings("unchecked")
@@ -24,16 +24,16 @@
*/
package org.spongepowered.api.event;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.withSettings;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;
import org.mockito.stubbing.Answer;
import org.spongepowered.api.data.DataHolder;
@@ -49,6 +49,7 @@ import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.configurate.util.Types;
import org.spongepowered.configurate.util.UnmodifiableCollections;
import org.spongepowered.math.vector.Vector3d;
import java.lang.reflect.Array;
@@ -59,24 +60,33 @@ import java.net.InetSocketAddress;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;
//@RunWith(Parameterized.class)
public class SpongeEventFactoryTest {
class SpongeEventFactoryTest {
private static final Set<Class<?>> excludedEvents = ImmutableSet.of(DamageEntityEvent.class,
GoalEvent.class, GoalEvent.Add.class, GoalEvent.Remove.class, AttackEntityEvent.class);
/**
* Events with types that aren't able to be mocked for one reason or another.
*/
private static final Set<Class<?>> EXCLUDED_EVENTS = UnmodifiableCollections.toSet(
DamageEntityEvent.class,
GoalEvent.class,
GoalEvent.Add.class,
GoalEvent.Remove.class,
AttackEntityEvent.class
);
private static final Set<String> excludedMethods = ImmutableSet.of("entitySnapshots");
private static final Set<String> EXCLUDED_METHODS = UnmodifiableCollections.toSet("entitySnapshots");
// We need to keep a reference to any mocked World passed into a Location,
// to ensure that it is not GC'd for the duration of a test. This list
private static List<World> worlds = new ArrayList<>();
private static List<World<?, ?>> worlds = new ArrayList<>();
private static final Answer<Object> EVENT_MOCKING_ANSWER = (invoc -> {
// We use the original mock type to try to get more information about
@@ -93,39 +103,34 @@ public class SpongeEventFactoryTest {
return SpongeEventFactoryTest.mockParam(returnType);
});
@Parameterized.Parameters(name = "{0}")
public static List<Object[]> getMethods() {
ImmutableList.Builder<Object[]> methods = ImmutableList.builder();
for (Method method : SpongeEventFactory.class.getMethods()) {
if (method.getName().startsWith("create") && Modifier.isStatic(method.getModifiers())
&& !SpongeEventFactoryTest.excludedEvents.contains(method.getReturnType())) {
methods.add(new Object[]{method.getReturnType().getName(), method});
}
}
return methods.build();
static Stream<Object[]> eventMethods() {
return Arrays.stream(SpongeEventFactory.class.getMethods())
.filter(method -> method.getName().startsWith("create") && Modifier.isStatic(method.getModifiers())
&& !SpongeEventFactoryTest.EXCLUDED_EVENTS.contains(method.getReturnType()))
.map(method -> new Object[] { method.getReturnType().getName(), method});
}
@Parameterized.Parameter
public String event;
@Parameterized.Parameter(1)
public Method method;
// @Test
public void testCreate() {
@DisplayName("Creating events")
@MethodSource("eventMethods")
@ParameterizedTest(name = "Creating {0}")
void testCreate(final String event, final Method method) {
try {
// We only care about keeping extends around for the duration
// of this particular event.
SpongeEventFactoryTest.worlds.clear();
Type[] paramTypes = this.method.getGenericParameterTypes();
Type[] paramTypes = method.getGenericParameterTypes();
Object[] params = new Object[paramTypes.length];
for (int i = 0; i < paramTypes.length; i++) {
params[i] = SpongeEventFactoryTest.mockParam(paramTypes[i]);
}
Object testEvent = this.method.invoke(null, params);
Object testEvent = method.invoke(null, params);
for (Method eventMethod : testEvent.getClass().getMethods()) {
if (SpongeEventFactoryTest.excludedMethods.contains(eventMethod.getName())) {
if (SpongeEventFactoryTest.EXCLUDED_METHODS.contains(eventMethod.getName())) {
continue;
}
if (eventMethod.isDefault()) { // no need to test methods with known implementations
continue;
}
@@ -137,7 +142,7 @@ public class SpongeEventFactoryTest {
}
if (eventMethod.getReturnType() != void.class) {
assertNotNull("The return type of " + eventMethod + " was null!", eventMethod.invoke(testEvent, params));
Assertions.assertNotNull(eventMethod.invoke(testEvent, params), "The return type of " + eventMethod + " was null!");
}
} catch (Exception e) {
@@ -148,14 +153,14 @@ public class SpongeEventFactoryTest {
+ " class dynamically creates concrete classes at "
+ "runtime. However, as this means that errors may only become known at runtime, this test ensures that problems "
+ "are caught during development.)\n\n"
+ "The failure of this test is in regards to invocation of a method of the '" + this.method.getReturnType().getName()
+ "The failure of this test is in regards to invocation of a method of the '" + method.getReturnType().getName()
+ "' event.\n\n"
+ "Reasons for failure include:\n"
+ "(1) The called method does not conform to format that the class generator expects for getters or setters,"
+ "and is not implemented by the abstract class used as the superclass of the generated event."
+ "See the wrapped exception for more details.\n"
+ "\tSolution: Modify the method name and/or signature to follow the expected getter/sett er semantics,"
+ "or annotate the event with @ImplementedBy to indicate the abstract class used as the superclass."
+ "or annotate the event with @ImplementedBy to indicate the abstract class used as the superclass.\n"
+ "(2) A bug in the class generator was found\n"
+ "\tSolution: Look into event-impl-gen.\n",
e);
@@ -163,24 +168,23 @@ public class SpongeEventFactoryTest {
}
} catch (Exception e) {
throw new RuntimeException(
"Runtime creation of the '" + this.method.getReturnType().getName() + "' event failed\n\n"
"Runtime creation of the '" + method.getReturnType().getName() + "' event failed\n\n"
+ "(To avoid the need to create numerous boilerplate concrete classes for Sponge's many event "
+ "interfaces, the " + SpongeEventFactory.class.getSimpleName()
+ " class dynamically creates concrete classes at "
+ "runtime. However, as this means that errors may only become known at runtime, this test ensures that problems "
+ "are caught during development.)\n\n"
+ "The failure of this test is in regards to creation of the '" + this.method.getReturnType().getName()
+ "The failure of this test is in regards to creation of the '" + method.getReturnType().getName()
+ "' event.\n\n"
+ "Reasons for failure include:\n"
+ "(1) The event was changed and there are new, removed, or modified properties (most likely)\n"
+ "\tSolution: Make appropriate changes to " + SpongeEventFactory.class.getName() + "." + this.method.getName()
+ "\tSolution: Make appropriate changes to " + SpongeEventFactory.class.getName() + "." + method.getName()
+ "(). "
+ "See the wrapped exception for more details.\n"
+ "(2) A bug in the class generator was found\n"
+ "\tSolution: Look into event-impl-gen.\n"
+ "(3) A method that does not follow getter/setter semantics (getProp(), isBool(), setProp()) "
+ "was added (i.e. blockList())\n"
+ "\tSolution: Revisit " + this.method.getReturnType().getName() + " and its supertypes. If the method in question "
+ "(3) A method that does not follow getter/setter semantics (prop(), setProp()) was added\n"
+ "\tSolution: Revisit " + method.getReturnType().getName() + " and its supertypes. If the method in question "
+ "must exist, then the event factory is capable of accepting a base class to build the "
+ "runtime concrete class upon (i.e. " + AbstractEvent.class.getName()
+ " is the supertype of all generated event classes).\n", e);
@@ -190,7 +194,8 @@ public class SpongeEventFactoryTest {
@SuppressWarnings({"unchecked", "rawtypes"})
@Nullable
public static Object mockParam(final Type paramType) {
if (paramType == Class.class) {
final Class<?> erasedType = GenericTypeReflector.erase(paramType);
if (erasedType == Class.class) {
return PEBKACException.class;
} else if (paramType == byte.class || paramType == Byte.class) {
return (byte) 0;
@@ -217,12 +222,12 @@ public class SpongeEventFactoryTest {
return array;
} else if (paramType == String.class) {
return "Cupcakes";
} else if (paramType == Optional.class) {
} else if (erasedType == Optional.class) {
return Optional.empty();
} else if (paramType == OptionalInt.class) {
return OptionalInt.empty();
} else if (GenericTypeReflector.isSuperType(Enum.class, paramType)) {
return GenericTypeReflector.erase(paramType).getEnumConstants()[0];
return erasedType.getEnumConstants()[0];
} else if (GenericTypeReflector.isSuperType(ServerLocation.class, paramType)) {
ServerWorld world = (ServerWorld) SpongeEventFactoryTest.mockParam(ServerWorld.class);
// Make sure we keep a reference to the World,
@@ -249,7 +254,7 @@ public class SpongeEventFactoryTest {
return Duration.ZERO;
} else if (paramType == Instant.class) {
return Instant.now();
} else if (paramType == TypeToken.class) {
} else if (erasedType == TypeToken.class) {
return TypeToken.get(Object.class);
} else if (paramType == Color.class) {
return Color.BLACK;
@@ -0,0 +1,48 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.matcher;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
import java.util.Optional;
final class OptionalIsPresent<V> extends TypeSafeMatcher<Optional<V>> {
@Override
protected boolean matchesSafely(final Optional<V> item) {
return item.isPresent();
}
@Override
public void describeTo(final Description description) {
description.appendText("a present optional");
}
@Override
protected void describeMismatchSafely(final Optional<V> item, final Description mismatchDescription) {
mismatchDescription.appendText("was not present");
}
}
@@ -0,0 +1,63 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.matcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
import java.util.Optional;
final class OptionalValueIs<V> extends TypeSafeDiagnosingMatcher<Optional<V>> {
private final Matcher<? super V> elementMatcher;
OptionalValueIs(final Matcher<? super V> elementMatcher) {
this.elementMatcher = elementMatcher;
}
@Override
protected boolean matchesSafely(final Optional<V> item, final Description mismatchDescription) {
if (!item.isPresent()) {
mismatchDescription.appendText("was not present");
return false;
}
if (!this.elementMatcher.matches(item.get())) {
mismatchDescription.appendText("was present but ");
this.elementMatcher.describeMismatch(item.get(), mismatchDescription);
return false;
}
return true;
}
@Override
public void describeTo(final Description description) {
description.appendText("a present Optional with value ")
.appendDescriptionOf(this.elementMatcher);
}
}
@@ -0,0 +1,55 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.matcher;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import java.util.Objects;
import java.util.Optional;
public final class SpongeMatchers {
private static final OptionalIsPresent<Object> OPTIONAL_IS_PRESENT = new OptionalIsPresent<>();
@SuppressWarnings({"unchecked", "rawtypes"})
public static <V> Matcher<Optional<V>> present() {
return (Matcher) SpongeMatchers.OPTIONAL_IS_PRESENT;
}
public static <V> Matcher<Optional<V>> valueIs(final Matcher<? super V> valueMatcher) {
Objects.requireNonNull(valueMatcher, "valueMatcher");
return new OptionalValueIs<>(valueMatcher);
}
public static <V> Matcher<Optional<V>> valueIs(final V value) {
Objects.requireNonNull(value, "value");
return new OptionalValueIs<>(Matchers.equalTo(value));
}
private SpongeMatchers() {
}
}
@@ -24,18 +24,18 @@
*/
package org.spongepowered.api.service.permission;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.spongepowered.api.util.Tristate;
import java.util.HashMap;
import java.util.Map;
public class NodeTreeTest {
class NodeTreeTest {
@Test
public void testAsMap() {
void testAsMap() {
final Map<String, Boolean> testPermissions = new HashMap<>();
testPermissions.put("generate.rainbow", true);
testPermissions.put("generate.sunset", false);
@@ -44,11 +44,11 @@ public class NodeTreeTest {
NodeTree oldTree = NodeTree.of(testPermissions);
assertEquals(testPermissions, oldTree.asMap());
Assertions.assertEquals(testPermissions, oldTree.asMap());
}
@Test
public void testWithValue() throws Exception {
void testWithValue() throws Exception {
final Map<String, Boolean> testPermissions = new HashMap<>();
testPermissions.put("generate.rainbow", true);
testPermissions.put("generate.sunset", false);
@@ -56,14 +56,14 @@ public class NodeTreeTest {
testPermissions.put("generate.thunderstorm.explosive", false);
NodeTree oldTree = NodeTree.of(testPermissions);
assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
Assertions.assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
NodeTree newTree = oldTree.withValue("generate.thunderstorm.explosive", Tristate.TRUE);
assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
assertEquals(Tristate.TRUE, newTree.get("generate.thunderstorm.explosive"));
Assertions.assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
Assertions.assertEquals(Tristate.TRUE, newTree.get("generate.thunderstorm.explosive"));
}
@Test
public void testWithAll() throws Exception {
void testWithAll() throws Exception {
final Map<String, Boolean> testPermissions = new HashMap<>();
testPermissions.put("generate.rainbow", true);
testPermissions.put("generate.sunset", false);
@@ -79,18 +79,18 @@ public class NodeTreeTest {
NodeTree newTree = oldTree.withAll(newPermissions);
assertEquals(Tristate.FALSE, oldTree.get("generate.sunset.red"));
assertEquals(Tristate.TRUE, newTree.get("generate.sunset.red"));
Assertions.assertEquals(Tristate.FALSE, oldTree.get("generate.sunset.red"));
Assertions.assertEquals(Tristate.TRUE, newTree.get("generate.sunset.red"));
assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
assertEquals(Tristate.UNDEFINED, newTree.get("generate.thunderstorm.explosive"));
Assertions.assertEquals(Tristate.FALSE, oldTree.get("generate.thunderstorm.explosive"));
Assertions.assertEquals(Tristate.UNDEFINED, newTree.get("generate.thunderstorm.explosive"));
assertEquals(Tristate.UNDEFINED, oldTree.get("something.new"));
assertEquals(Tristate.FALSE, newTree.get("something.new"));
Assertions.assertEquals(Tristate.UNDEFINED, oldTree.get("something.new"));
Assertions.assertEquals(Tristate.FALSE, newTree.get("something.new"));
}
@Test
public void testCreateFromValues() throws Exception {
void testCreateFromValues() throws Exception {
final Map<String, Boolean> testPermissions = new HashMap<>();
testPermissions.put("generate.rainbow", true);
testPermissions.put("generate.sunset", false);
@@ -99,12 +99,12 @@ public class NodeTreeTest {
NodeTree nodes = NodeTree.of(testPermissions, Tristate.UNDEFINED);
assertEquals(Tristate.TRUE, nodes.get("generate.rainbow"));
assertEquals(Tristate.TRUE, nodes.get("generate.rainbow.double"));
assertEquals(Tristate.FALSE, nodes.get("generate.sunset"));
assertEquals(Tristate.FALSE, nodes.get("generate.sunset.east"));
assertEquals(Tristate.TRUE, nodes.get("generate.thunderstorm"));
assertEquals(Tristate.FALSE, nodes.get("generate.thunderstorm.explosive"));
assertEquals(Tristate.UNDEFINED, nodes.get("random.perm"));
Assertions.assertEquals(Tristate.TRUE, nodes.get("generate.rainbow"));
Assertions.assertEquals(Tristate.TRUE, nodes.get("generate.rainbow.double"));
Assertions.assertEquals(Tristate.FALSE, nodes.get("generate.sunset"));
Assertions.assertEquals(Tristate.FALSE, nodes.get("generate.sunset.east"));
Assertions.assertEquals(Tristate.TRUE, nodes.get("generate.thunderstorm"));
Assertions.assertEquals(Tristate.FALSE, nodes.get("generate.thunderstorm.explosive"));
Assertions.assertEquals(Tristate.UNDEFINED, nodes.get("random.perm"));
}
}
@@ -24,235 +24,230 @@
*/
package org.spongepowered.api.util;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.Optional;
public class CoerceTest {
@Test
public void testAsCharacter() {
void testAsCharacter() {
String character = "myChar";
Optional<Character> optional = Coerce.asChar(character);
assertTrue(optional.isPresent());
assertTrue(optional.get() == 'm');
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals('m', (char) optional.get());
char myChar = 'm';
Optional<Character> optional1 = Coerce.asChar(myChar);
assertTrue(optional1.isPresent());
assertTrue(optional1.get() == 'm');
Assertions.assertTrue(optional1.isPresent());
Assertions.assertEquals('m', (char) optional1.get());
int myInt = 1;
Optional<Character> optional2 = Coerce.asChar(myInt);
assertTrue(optional2.isPresent());
assertTrue(optional2.get() == '1');
Assertions.assertTrue(optional2.isPresent());
Assertions.assertEquals('1', (char) optional2.get());
Optional<Character> nullOptional = Coerce.asChar(null);
assertTrue(!nullOptional.isPresent());
Assertions.assertFalse(nullOptional.isPresent());
}
@Test
public void testAsBoolean() throws Exception {
void testAsBoolean() {
Object myBool = Boolean.TRUE;
Optional<Boolean> optional = Coerce.asBoolean(myBool);
assertTrue(optional.isPresent());
assertTrue(optional.get());
Assertions.assertTrue(optional.isPresent());
Assertions.assertTrue(optional.get());
Optional<Boolean> nullBoolean = Coerce.asBoolean(null);
assertTrue(!nullBoolean.isPresent());
Assertions.assertFalse(nullBoolean.isPresent());
Optional<Boolean> invalidBoolean = Coerce.asBoolean("foo");
assertTrue(!invalidBoolean.isPresent());
Assertions.assertFalse(invalidBoolean.isPresent());
}
@Test
public void testAsByte() {
void testAsByte() {
String character = "1";
Optional<Byte> optional = Coerce.asByte(character);
assertTrue(optional.isPresent());
assertTrue(optional.get() == 1);
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals(1, (byte) optional.get());
char myChar = '1';
Optional<Byte> optional1 = Coerce.asByte(myChar);
assertTrue(optional1.isPresent());
assertTrue(optional1.get() == 1);
Assertions.assertTrue(optional1.isPresent());
Assertions.assertEquals(1, (byte) optional1.get());
String invalidString = "foo";
Optional<Byte> invalidOptional = Coerce.asByte(invalidString);
assertTrue(!invalidOptional.isPresent());
Assertions.assertFalse(invalidOptional.isPresent());
int myInt = 1;
Optional<Byte> optional2 = Coerce.asByte(myInt);
assertTrue(optional2.isPresent());
assertTrue(optional2.get() == 1);
Assertions.assertTrue(optional2.isPresent());
Assertions.assertEquals(1, (byte) optional2.get());
Optional<Byte> nullOptional = Coerce.asByte(null);
assertTrue(!nullOptional.isPresent());
Assertions.assertFalse(nullOptional.isPresent());
}
@Test
public void testAsShort() {
void testAsShort() {
String character = "1";
Optional<Short> optional = Coerce.asShort(character);
assertTrue(optional.isPresent());
assertTrue(optional.get() == 1);
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals(1, (short) optional.get());
char myChar = '1';
Optional<Short> optional1 = Coerce.asShort(myChar);
assertTrue(optional1.isPresent());
assertTrue(optional1.get() == 1);
Assertions.assertTrue(optional1.isPresent());
Assertions.assertEquals(1, (short) optional1.get());
String invalidString = "foo";
Optional<Short> invalidOptional = Coerce.asShort(invalidString);
assertTrue(!invalidOptional.isPresent());
Assertions.assertFalse(invalidOptional.isPresent());
int myInt = 1;
Optional<Short> optional2 = Coerce.asShort(myInt);
assertTrue(optional2.isPresent());
assertTrue(optional2.get() == 1);
Assertions.assertTrue(optional2.isPresent());
Assertions.assertEquals(1, (short) optional2.get());
Optional<Short> nullOptional = Coerce.asShort(null);
assertTrue(!nullOptional.isPresent());
Assertions.assertFalse(nullOptional.isPresent());
}
@Test
public void testAsLong() {
void testAsLong() {
String character = "1";
Optional<Long> optional = Coerce.asLong(character);
assertTrue(optional.isPresent());
assertTrue(optional.get() == 1);
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals(1, (long) optional.get());
char myChar = '1';
Optional<Long> optional1 = Coerce.asLong(myChar);
assertTrue(optional1.isPresent());
assertTrue(optional1.get() == 1);
Assertions.assertTrue(optional1.isPresent());
Assertions.assertEquals(1, (long) optional1.get());
String invalidString = "foo";
Optional<Long> invalidOptional = Coerce.asLong(invalidString);
assertTrue(!invalidOptional.isPresent());
Assertions.assertFalse(invalidOptional.isPresent());
int myInt = 1;
Optional<Long> optional2 = Coerce.asLong(myInt);
assertTrue(optional2.isPresent());
assertTrue(optional2.get() == 1);
Assertions.assertTrue(optional2.isPresent());
Assertions.assertEquals(1, (long) optional2.get());
Optional<Long> nullOptional = Coerce.asLong(null);
assertTrue(!nullOptional.isPresent());
Assertions.assertFalse(nullOptional.isPresent());
}
@Test
public void testAsFloat() {
void testAsFloat() {
String character = "1";
Optional<Float> optional = Coerce.asFloat(character);
assertTrue(optional.isPresent());
assertTrue(optional.get() == 1);
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals(1, optional.get(), 0.0);
char myChar = '1';
Optional<Float> optional1 = Coerce.asFloat(myChar);
assertTrue(optional1.isPresent());
assertTrue(optional1.get() == 1);
Assertions.assertTrue(optional1.isPresent());
Assertions.assertEquals(1, optional1.get(), 0.0);
String invalidString = "foo";
Optional<Float> invalidOptional = Coerce.asFloat(invalidString);
assertTrue(!invalidOptional.isPresent());
Assertions.assertFalse(invalidOptional.isPresent());
int myInt = 1;
Optional<Float> optional2 = Coerce.asFloat(myInt);
assertTrue(optional2.isPresent());
assertTrue(optional2.get() == 1);
Assertions.assertTrue(optional2.isPresent());
Assertions.assertEquals(1, optional2.get(), 0.0);
Optional<Float> nullOptional = Coerce.asFloat(null);
assertTrue(!nullOptional.isPresent());
Assertions.assertFalse(nullOptional.isPresent());
}
@Test
public void testAsDouble() {
void testAsDouble() {
String character = "1";
Optional<Double> optional = Coerce.asDouble(character);
assertTrue(optional.isPresent());
assertTrue(optional.get() == 1);
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals(1, optional.get(), 0.0);
char myChar = '1';
Optional<Double> optional1 = Coerce.asDouble(myChar);
assertTrue(optional1.isPresent());
assertTrue(optional1.get() == 1);
Assertions.assertTrue(optional1.isPresent());
Assertions.assertEquals(1, optional1.get(), 0.0);
String invalidString = "foo";
Optional<Double> invalidOptional = Coerce.asDouble(invalidString);
assertTrue(!invalidOptional.isPresent());
Assertions.assertFalse(invalidOptional.isPresent());
int myInt = 1;
Optional<Double> optional2 = Coerce.asDouble(myInt);
assertTrue(optional2.isPresent());
assertTrue(optional2.get() == 1);
Assertions.assertTrue(optional2.isPresent());
Assertions.assertEquals(1, optional2.get(), 0.0);
Optional<Double> nullOptional = Coerce.asDouble(null);
assertTrue(!nullOptional.isPresent());
Assertions.assertFalse(nullOptional.isPresent());
}
@Test
public void testAsInteger() {
void testAsInteger() {
String character = "1";
Optional<Integer> optional = Coerce.asInteger(character);
assertTrue(optional.isPresent());
assertTrue(optional.get() == 1);
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals(1, (int) optional.get());
char myChar = '1';
Optional<Integer> optional1 = Coerce.asInteger(myChar);
assertTrue(optional1.isPresent());
assertTrue(optional1.get() == 1);
Assertions.assertTrue(optional1.isPresent());
Assertions.assertEquals(1, (int) optional1.get());
String invalidString = "foo";
Optional<Integer> invalidOptional = Coerce.asInteger(invalidString);
assertTrue(!invalidOptional.isPresent());
Assertions.assertFalse(invalidOptional.isPresent());
int myInt = 1;
Optional<Integer> optional2 = Coerce.asInteger(myInt);
assertTrue(optional2.isPresent());
assertTrue(optional2.get() == 1);
Assertions.assertTrue(optional2.isPresent());
Assertions.assertEquals(1, (int) optional2.get());
Optional<Integer> nullOptional = Coerce.asInteger(null);
assertTrue(!nullOptional.isPresent());
Assertions.assertFalse(nullOptional.isPresent());
}
@Test
public void testAsString() {
void testAsString() {
String character = "1";
Optional<String> optional = Coerce.asString(character);
assertTrue(optional.isPresent());
assertTrue(optional.get().equals("1"));
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals("1", optional.get());
char myChar = '1';
Optional<String> optional1 = Coerce.asString(myChar);
assertTrue(optional1.isPresent());
assertTrue(optional1.get().equals("1"));
Assertions.assertTrue(optional1.isPresent());
Assertions.assertEquals("1", optional1.get());
String invalidString = "foo";
Optional<String> message = Coerce.asString(invalidString);
assertTrue(message.isPresent());
assertTrue(message.get().equals("foo"));
Assertions.assertTrue(message.isPresent());
Assertions.assertEquals("foo", message.get());
int myInt = 1;
Optional<String> optional2 = Coerce.asString(myInt);
assertTrue(optional2.isPresent());
assertTrue(optional2.get().equals("1"));
Assertions.assertTrue(optional2.isPresent());
Assertions.assertEquals("1", optional2.get());
Optional<String> nullOptional = Coerce.asString(null);
assertTrue(!nullOptional.isPresent());
Assertions.assertFalse(nullOptional.isPresent());
}
}
@@ -24,8 +24,8 @@
*/
package org.spongepowered.api.util.weighted;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.List;
@@ -38,7 +38,7 @@ public class TestWeightedTable {
table.add(new WeightedObject<>(this, 4));
rand.enqueue(0);
List<Object> results = table.get(rand);
Assert.assertEquals(this, results.get(0));
Assertions.assertEquals(this, results.get(0));
}
}