1
0
Fork 0
mirror of https://github.com/correl/mage.git synced 2025-04-13 09:11:06 -09:00

Added unit tests and javadoc for Counter class.

This commit is contained in:
poixen 2015-11-30 21:44:57 +01:00
parent afc523b7c7
commit ad0fb6b7eb
3 changed files with 225 additions and 9 deletions
Mage/src
main/java/mage/counters
test/java/mage/counters

View file

@ -28,64 +28,145 @@
package mage.counters; package mage.counters;
import org.apache.log4j.Logger;
import java.io.Serializable; import java.io.Serializable;
/** /**
*
* @author BetaSteward_at_googlemail.com * @author BetaSteward_at_googlemail.com
*/ */
public class Counter implements Serializable { public class Counter implements Serializable {
protected String name; private static final Logger logger = Logger.getLogger(Counter.class);
protected final String name;
protected int count; protected int count;
public Counter(String name) {
/**
* Creates a {@link Counter} with the provided {@code name} and a default value of 1
*
* @param name the name of this counter.
*/
public Counter(final String name) {
this.name = name; this.name = name;
this.count = 1; this.count = 1;
} }
public Counter(String name, int count) {
/**
* Creates a {@link Counter} with the provided {@code name} and {@code count}
*
* @param name the name of this counter.
* @param count the value of this counter.
*/
public Counter(final String name, final int count) {
this.name = name; this.name = name;
this.count = count; this.count = count;
} }
/**
* Creates a {@link Counter} from an existing {@link Counter} object.
*
* @param counter the {@link Counter} to create a copy from.
*/
public Counter(final Counter counter) { public Counter(final Counter counter) {
this.name = counter.name; this.name = counter.name;
this.count = counter.count; this.count = counter.count;
} }
public void add() { /**
* Increases the {@code count} by 1
*/
public void increase() {
count++; count++;
} }
/**
* Adds the passed in {@code amount} to the {@code count}
*
* @param amount the value to add to the {@code count}
*/
public void add(int amount) { public void add(int amount) {
count += amount; count += amount;
} }
public void remove() {
/**
* Decreases the {@code count} by one. Will not allow the count to be less than 0.
* If an attempt is made to make the count be less than zero, the call will be logged.
*/
public void decrease() {
if (count > 0) { if (count > 0) {
count--; count--;
} else {
logger.warn("An attempt was made to set the counter '" + name +
"' to less than 0. Setting to 0.");
} }
} }
/**
* Decreases the {@code count} by tne passed in {@code amount}. Will not allow the count
* to be less than 0. If an attempt is made to make the count be less than zero, the call will be logged.
*/
public void remove(int amount) { public void remove(int amount) {
if (count > amount) { if (count > amount) {
count -= amount; count -= amount;
} } else {
else { logger.warn("An attempt was made to set the counter '" + name +
"' to less than 0. Setting to 0.");
count = 0; count = 0;
} }
} }
/**
* Returns the name of this {@link Counter}
*
* @return the name of this {@link Counter}
*/
public String getName() { public String getName() {
return name; return name;
} }
/**
* Returns the count of this {@link Counter}
*
* @return the count of this {@link Counter}
*/
public int getCount() { public int getCount() {
return count; return count;
} }
/**
* Returns a deep copy of this object.
*
* @return a deep copy of this object.
*/
public Counter copy() { public Counter copy() {
return new Counter(this); return new Counter(this);
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Counter counter = (Counter) o;
if (count != counter.count) return false;
return !(name != null ? !name.equals(counter.name) : counter.name != null);
}
@Override
public int hashCode() {
int result = name != null ? name.hashCode() : 0;
result = 31 * result + count;
return result;
}
} }

View file

@ -56,7 +56,7 @@ public class Counters extends HashMap<String, Counter> implements Serializable {
if (!this.containsKey(name)) { if (!this.containsKey(name)) {
this.put(name, new Counter(name)); this.put(name, new Counter(name));
} }
this.get(name).add(); this.get(name).increase();
} }
public void addCounter(String name, int amount) { public void addCounter(String name, int amount) {

View file

@ -0,0 +1,135 @@
package mage.counters;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Custom unit tests for {@link Counter}
*/
public class CounterTest {
private Counter counter;
@Before
public void setUp() {
counter = new Counter("test", 1);
}
@Test
public void shouldIncreaseCounter() {
// given
// when
counter.increase();
// then
assertEquals(2, counter.getCount());
assertEquals("test", counter.getName());
}
@Test
public void shouldAddMana() {
// given
// when
counter.add(5);
// then
assertEquals(6, counter.getCount());
}
@Test
public void shouldDecreaseCounter() {
// given
// when
counter.decrease();
// then
assertEquals(0, counter.getCount());
}
@Test
public void shouldNotDecreaseToLessThanZero() {
// given
// when
counter.decrease();
counter.decrease();
// then
assertEquals(0, counter.getCount());
}
@Test
public void shouldRemoveCounters() {
// given
// when
counter.remove(1);
// then
assertEquals(0, counter.getCount());
}
@Test
public void shouldNotRemoveMoreCountersThanAvailable() {
// given
// when
counter.remove(10);
// then
assertEquals(0, counter.getCount());
}
@Test
public void shouldReturnCopy() {
// given
// when
Counter copy = counter.copy();
// then
assertEquals(copy, counter);
assertFalse(copy == counter);
}
@Test
public void shouldCreateCounterFromCounter() {
// given
// when
Counter copy = new Counter(counter);
// then
assertEquals(1, copy.getCount());
assertEquals("test", copy.getName());
}
@Test
public void shouldCreatDefaultCounter() {
// given
// when
Counter defaultCounter = new Counter("default");
// then
assertEquals(1, defaultCounter.getCount());
assertEquals("default", defaultCounter.getName());
}
}