Skip to content

Commit

Permalink
Fix broken unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
koczadly committed Apr 27, 2021
1 parent 416c8e5 commit c84d298
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package uk.oczadly.karl.csgsi.state.components.grenade;

import com.google.gson.annotations.Expose;
import uk.oczadly.karl.csgsi.state.components.Coordinate;
import uk.oczadly.karl.csgsi.state.components.EnumValue;
import uk.oczadly.karl.csgsi.state.components.PlayerSteamID;

/**
* Represents a standard grenade.
*/
public class BasicGrenade implements Grenade {

@Expose private EnumValue<Type> type;
@Expose private PlayerSteamID owner;
@Expose private double lifetime;


@Override
public final EnumValue<Type> getType() {
return type;
}

@Override
public final PlayerSteamID getOwner() {
return owner;
}

@Override
public final double getLifetime() {
return lifetime;
}


@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"type=" + type +
", owner=" + owner +
", lifetime=" + lifetime + '}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,29 @@
* This class represents a single grenade.
* <p>Available types:</p>
* <ul>
* <li>{@link Grenade} (base type)</li>
* <li>{@link BasicGrenade} (base type, or for unknown grenades)</li>
* <li>{@link ProjectileGrenade}</li>
* <li>{@link EffectGrenade}</li>
* <li>{@link IncendiaryGrenade}</li>
* </ul>
*/
@JsonAdapter(Grenade.Adapter.class)
public class Grenade {

private EnumValue<Type> type;
@Expose private PlayerSteamID owner;
@Expose private double lifetime;
public interface Grenade {

/**
* @return the type of grenade
*/
public EnumValue<Type> getType() {
return type;
}
EnumValue<Type> getType();

/**
* @return the Steam ID of the player who threw this grenade
*/
public PlayerSteamID getOwner() {
return owner;
}
PlayerSteamID getOwner();

/**
* @return how many seconds have elapsed since the grenade was thrown
*/
public double getLifetime() {
return lifetime;
}

@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"type=" + type +
", owner=" + owner +
", lifetime=" + lifetime +
'}';
}
double getLifetime();


/**
Expand Down Expand Up @@ -91,11 +72,11 @@ static class Adapter implements JsonDeserializer<Grenade> {
public Grenade deserialize(JsonElement json, java.lang.reflect.Type typeOfT,
JsonDeserializationContext context) throws JsonParseException {
JsonObject obj = json.getAsJsonObject();
EnumValue<Grenade.Type> gType = EnumValue.of(obj.get("type").getAsString(), Grenade.Type.class, Util.GSON);
Class<? extends Grenade> classType = gType.isResolved() ? gType.get().getObjectClass() : Grenade.class;
Grenade grenade = context.deserialize(obj, classType);
grenade.type = gType;
return grenade;
EnumValue<Grenade.Type> gType = EnumValue.of(
obj.get("type").getAsString(), Grenade.Type.class, Util.GSON);
Class<? extends Grenade> classType = gType.isResolved()
? gType.get().getObjectClass() : BasicGrenade.class;
return context.deserialize(obj, classType);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Represents a set of flames on the ground, from a thrown molotov or incendiary grenade.
*/
public class IncendiaryGrenade extends Grenade {
public class IncendiaryGrenade extends BasicGrenade {

@Expose private Map<String, Coordinate> flames;
private volatile Coordinate approxPos = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* Represents a standard projectile grenade, with a position and velocity.
*/
public class ProjectileGrenade extends Grenade {
public class ProjectileGrenade extends BasicGrenade {

@Expose private Coordinate position;
@Expose private Coordinate velocity;
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/uk/oczadly/karl/csgsi/GSIServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ public void testObserverNotification() throws Exception {
assertSame(uriPath, observer2.context.getUriPath());
assertSame(i1, observer1.context.getTimestamp());
assertSame(i1, observer2.context.getTimestamp());
assertSame(previous, observer1.context.getPreviousState());
assertSame(previous, observer2.context.getPreviousState());
assertSame(i2, observer1.context.getPreviousTimestamp());
assertSame(i2, observer2.context.getPreviousTimestamp());
assertEquals(300, observer1.context.getMillisSinceLastState());
assertEquals(300, observer2.context.getMillisSinceLastState());
assertSame(previous, observer1.context.getPreviousState().get());
assertSame(previous, observer2.context.getPreviousState().get());
assertSame(i2, observer1.context.getPreviousTimestamp().get());
assertSame(i2, observer2.context.getPreviousTimestamp().get());
assertEquals(300, observer1.context.getMillisSinceLastState().getAsInt());
assertEquals(300, observer2.context.getMillisSinceLastState().getAsInt());
assertEquals(43, observer1.context.getSequentialCounter());
assertEquals(43, observer2.context.getSequentialCounter());
assertEquals(authTokens, observer1.context.getAuthTokens());
Expand Down

0 comments on commit c84d298

Please sign in to comment.