Added tests for DataSource.

deliverable_9
asdfasdf 6 years ago
parent 30ea1ec59c
commit 202ee6e220

@ -4,6 +4,7 @@ import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Objects;
public class DataSource {
private HashMap<String, StockType> targets;
@ -13,6 +14,7 @@ public class DataSource {
private String defapi;
public DataSource(String name, String url, String apiKey, String defapi) {
this.targets = new HashMap<String, StockType>();
this.name = name;
this.url = url;
this.apiKey = apiKey;
@ -23,7 +25,7 @@ public class DataSource {
// add this to target if not already done
//Modifies: this, stype
public void addStype(StockType stype) {
String sname = stype.getClass().getName();
String sname = stype.getName();
if (!this.targets.containsKey(sname)) {
targets.put(sname,stype);
stype.addSource(this);
@ -34,10 +36,34 @@ public class DataSource {
// del this to target if not already done
//Modifies: this, stype
public void delStype(StockType stype) {
String sname = stype.getClass().getName();
String sname = stype.getName();
if (this.targets.containsKey(sname)) {
targets.remove(sname,stype);
stype.delSource(this);
}
}
public String getName() {
return name;
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof DataSource)) {
return false;
}
DataSource temp = (DataSource) obj;
if (temp.getName().equals(name)) {
return true;
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(name);
}
}

@ -0,0 +1,39 @@
package data;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class DataSourceTest {
private StockType testStype;
private DataSource testSource;
@BeforeEach
public void runBefore() {
testStype = StypeMap.getStype("NYSE");
testSource = new DataSource("Test Source", "asdf://asdfasdf",
"asdfasdfasdf", "asdfasdf");
}
@Test
public void testAddDel() {
testStype.addSource(testSource);
testSource.delStype(testStype);
}
@Test
public void testEqualsHash() {
DataSource testSource2 = new DataSource("Test Source", "asdf://asdfasdf",
"asdfasdfasdf", "asdfasdf");
DataSource testSource3 = new DataSource("Test asdf", "asdf://asdfasdf",
"asdfasdfasdf", "asdfasdf");
String asdf = "asdf";
assertEquals(testSource, testSource2);
assertFalse(testSource.equals(testSource3));
assertFalse(testSource.equals(asdf));
assertEquals(testSource.hashCode(), testSource2.hashCode());
assertFalse(testSource.hashCode() == testSource3.hashCode());
}
}
Loading…
Cancel
Save