-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
refactor: introduce listening energy storage
- Loading branch information
Showing
5 changed files
with
181 additions
and
60 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...m/refinedmods/refinedstorage2/api/network/impl/energy/AbstractListeningEnergyStorage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.energy; | ||
|
||
import com.refinedmods.refinedstorage2.api.core.Action; | ||
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; | ||
|
||
public abstract class AbstractListeningEnergyStorage extends AbstractProxyEnergyStorage { | ||
protected AbstractListeningEnergyStorage(final EnergyStorage delegate) { | ||
super(delegate); | ||
} | ||
|
||
@Override | ||
public long receive(final long amount, final Action action) { | ||
final long received = super.receive(amount, action); | ||
if (received > 0 && action == Action.EXECUTE) { | ||
onStoredChanged(getStored()); | ||
} | ||
return received; | ||
} | ||
|
||
@Override | ||
public long extract(final long amount, final Action action) { | ||
final long extracted = super.extract(amount, action); | ||
if (extracted > 0 && action == Action.EXECUTE) { | ||
onStoredChanged(getStored()); | ||
} | ||
return extracted; | ||
} | ||
|
||
protected abstract void onStoredChanged(long stored); | ||
} |
139 changes: 139 additions & 0 deletions
139
...a/com/refinedmods/refinedstorage2/api/network/impl/energy/ListeningEnergyStorageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package com.refinedmods.refinedstorage2.api.network.impl.energy; | ||
|
||
import com.refinedmods.refinedstorage2.api.core.Action; | ||
import com.refinedmods.refinedstorage2.api.network.energy.EnergyStorage; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class ListeningEnergyStorageTest { | ||
EnergyStorage sut; | ||
int changeCount; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
sut = new AbstractListeningEnergyStorage(new EnergyStorageImpl(100)) { | ||
@Override | ||
protected void onStoredChanged(final long stored) { | ||
changeCount++; | ||
} | ||
}; | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(Action.class) | ||
void shouldReceiveEnergy(final Action action) { | ||
// Act | ||
final long inserted = sut.receive(50, action); | ||
|
||
// Assert | ||
assertThat(inserted).isEqualTo(50); | ||
|
||
if (action == Action.EXECUTE) { | ||
assertThat(sut.getStored()).isEqualTo(50); | ||
assertThat(changeCount).isEqualTo(1); | ||
} else { | ||
assertThat(sut.getStored()).isZero(); | ||
assertThat(changeCount).isZero(); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(Action.class) | ||
void shouldReceiveEnergyAndReachCapacity(final Action action) { | ||
// Act | ||
final long inserted = sut.receive(100, action); | ||
|
||
// Assert | ||
assertThat(inserted).isEqualTo(100); | ||
|
||
if (action == Action.EXECUTE) { | ||
assertThat(sut.getStored()).isEqualTo(100); | ||
assertThat(changeCount).isEqualTo(1); | ||
} else { | ||
assertThat(sut.getStored()).isZero(); | ||
assertThat(changeCount).isZero(); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(Action.class) | ||
void shouldReceiveEnergyAndExceedCapacity(final Action action) { | ||
// Act | ||
final long inserted = sut.receive(101, action); | ||
|
||
// Assert | ||
assertThat(inserted).isEqualTo(100); | ||
|
||
if (action == Action.EXECUTE) { | ||
assertThat(sut.getStored()).isEqualTo(100); | ||
assertThat(changeCount).isEqualTo(1); | ||
} else { | ||
assertThat(sut.getStored()).isZero(); | ||
assertThat(changeCount).isZero(); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(Action.class) | ||
void shouldNotReceiveEnergyWhenFull(final Action action) { | ||
// Arrange | ||
sut.receive(100, Action.EXECUTE); | ||
changeCount = 0; | ||
|
||
// Act | ||
final long inserted = sut.receive(100, action); | ||
|
||
// Assert | ||
assertThat(inserted).isZero(); | ||
assertThat(changeCount).isZero(); | ||
assertThat(sut.getStored()).isEqualTo(100); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(Action.class) | ||
void shouldExtractEnergyPartly(final Action action) { | ||
// Arrange | ||
sut.receive(100, Action.EXECUTE); | ||
changeCount = 0; | ||
|
||
// Act | ||
final long extracted = sut.extract(99, action); | ||
|
||
// Assert | ||
assertThat(extracted).isEqualTo(99); | ||
|
||
if (action == Action.EXECUTE) { | ||
assertThat(sut.getStored()).isEqualTo(1); | ||
assertThat(changeCount).isEqualTo(1); | ||
} else { | ||
assertThat(sut.getStored()).isEqualTo(100); | ||
assertThat(changeCount).isZero(); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(Action.class) | ||
void shouldExtractEnergyCompletely(final Action action) { | ||
// Arrange | ||
sut.receive(50, Action.EXECUTE); | ||
changeCount = 0; | ||
|
||
// Act | ||
final long extracted = sut.extract(51, action); | ||
|
||
// Assert | ||
assertThat(extracted).isEqualTo(50); | ||
|
||
if (action == Action.EXECUTE) { | ||
assertThat(sut.getStored()).isZero(); | ||
assertThat(changeCount).isEqualTo(1); | ||
} else { | ||
assertThat(sut.getStored()).isEqualTo(50); | ||
assertThat(changeCount).isZero(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters