Permalink
Browse files

added a shitload of asserts

git-svn-id: svn+ssh://scheduler.csc.calpoly.edu/repo@2600 13b268f7-8392-4c9d-bc7d-1e80a86bc2f9
  • Loading branch information...
1 parent 5baf5ea commit 77216870033137115aabdbbe24e44ce52c1eca7e eovadia committed Apr 30, 2012
@@ -14,7 +14,7 @@
import scheduler.model.db.IDBUsedEquipment;
import scheduler.model.db.IDatabase.NotFoundException;
-public class Course extends Identified {
+public class Course extends ModelObject {
private final Model mModel;
IDBCourse underlyingCourse;
@@ -47,12 +47,13 @@
// PERSISTENCE FUNCTIONS
public Course insert() throws DatabaseException {
- assert (isTransient());
- assert (document != null);
+ assert(isTransient());
+ assert(document != null);
mModel.courseCache.insert(this);
putOfferedDayPatternsIntoDB();
putUsedEquipmentIntoDB();
putAssociationIntoDB();
+ preInsertOrUpdateSanityCheck();
return this;
}
@@ -64,6 +65,7 @@ public void update() throws DatabaseException {
putUsedEquipmentIntoDB();
putOfferedDayPatternsIntoDB();
putAssociationIntoDB();
+ preInsertOrUpdateSanityCheck();
mModel.courseCache.update(this);
}
@@ -295,9 +297,9 @@ private void loadLectureAndTethered() throws DatabaseException {
if (lectureLoaded)
return;
- assert(lecture == null);
- assert(cachedTetheredToLecture == null);
- assert(underlyingCourse.getType() != null);
+ assert lecture == null : "lecture is null";
+ assert cachedTetheredToLecture == null : "cachedtetheredtolecture is null";
+ assert underlyingCourse.getType() != null : "type is null";
// System.out.println("is lab? " + underlyingCourse.getType().equals("LAB"));
@@ -369,4 +371,29 @@ public String toString() {
return this.getDepartment() + " " + this.getCatalogNumber() + " - " + this.getName();
}
+
+
+
+ public void preInsertOrUpdateSanityCheck() {
+ assert getName() != null : "name null";
+ assert getCatalogNumber() != null : "cat num null";
+ assert getDepartment() != null : "dept null";
+ assert getWTU() != null : "wtu null";
+ assert getSCU() != null : "scu null";
+ assert getNumSections() != null : "num sections null";
+ assert getType() != null : "type null";
+ assert getMaxEnrollment() != null : "enroll null";
+ assert getNumHalfHoursPerWeek() != null : "halfhours null";
+
+ if (usedEquipmentLoaded)
+ assert usedEquipmentDescriptions != null : "usedequipment descriptions null";
+ if (offeredDayPatternsLoaded)
+ assert offeredDayPatterns != null : "offereddaypatterns null";
+ if (lectureLoaded) {
+ assert lecture != null : "lec null";
+ assert cachedTetheredToLecture != null : "cached null";
+ }
+ if (documentLoaded)
+ assert document != null : "doc null";
+ }
}
@@ -8,7 +8,7 @@
import scheduler.model.db.IDBLocation;
import scheduler.model.db.IDatabase.NotFoundException;
-public class Document extends Identified {
+public class Document extends ModelObject {
private final Model model;
private boolean tbaLocationLoaded;
@@ -35,6 +35,7 @@
// PERSISTENCE FUNCTIONS
public Document insert() throws DatabaseException {
+ preInsertOrUpdateSanityCheck();
model.documentCache.insert(this);
return this;
}
@@ -47,6 +48,7 @@ public void update() throws DatabaseException {
disassociateWorkingCopy();
if (originalLoaded && original != null)
model.database.associateWorkingCopyWithOriginal(underlyingDocument, original.underlyingDocument);
+ preInsertOrUpdateSanityCheck();
model.documentCache.update(this);
}
@@ -191,4 +193,18 @@ public Document getWorkingCopy() throws DatabaseException {
return null;
return model.documentCache.decorateAndPutIfNotPresent(workingCopy);
}
+
+
+ @Override
+ public void preInsertOrUpdateSanityCheck() {
+ assert getName() != null : "name null";
+
+ if (tbaLocationLoaded)
+ assert tbaLocation != null : "tba loc null";
+
+ if (staffInstructorLoaded)
+ assert staffInstructor != null : "staff null";
+
+ // original can be null
+ }
}
@@ -11,7 +11,7 @@
import scheduler.model.db.IDBTimePreference;
import scheduler.model.db.IDatabase.NotFoundException;
-public class Instructor extends Identified {
+public class Instructor extends ModelObject {
public static final int DEFAULT_PREF = 5;
private final Model model;
@@ -40,6 +40,7 @@
public Instructor insert() throws DatabaseException {
assert(document != null);
+ preInsertOrUpdateSanityCheck();
model.instructorCache.insert(this);
putTimePreferencesIntoDB();
putCoursePreferencesIntoDB();
@@ -49,6 +50,7 @@ public Instructor insert() throws DatabaseException {
public void update() throws DatabaseException {
removeTimePreferencesFromDB();
removeCoursePreferencesFromDB();
+ preInsertOrUpdateSanityCheck();
model.instructorCache.update(this);
putTimePreferencesIntoDB();
putCoursePreferencesIntoDB();
@@ -232,4 +234,25 @@ public boolean equals(Object other) {
Instructor instructor = (Instructor)other;
return this.underlyingInstructor.equals(instructor.underlyingInstructor);
}
+
+
+ @Override
+ public void preInsertOrUpdateSanityCheck() {
+ assert getFirstName() != null : "firstname null";
+
+ assert getLastName() != null : "lastname null";
+
+ assert getUsername() != null : "username null";
+
+ assert getMaxWTU() != null : "maxwtu null";
+
+ if (documentLoaded)
+ assert document != null : "doc null";
+
+ if (timePreferencesLoaded)
+ assert timePreferences != null : "timeprefs null";
+
+ if (coursePreferencesLoaded)
+ assert coursePreferences != null : "courseprefs null";
+ }
}
@@ -9,7 +9,7 @@
import scheduler.model.db.IDBProvidedEquipment;
import scheduler.model.db.IDatabase.NotFoundException;
-public class Location extends Identified {
+public class Location extends ModelObject {
private final Model model;
IDBLocation underlyingLocation;
@@ -31,13 +31,15 @@
// PERSISTENCE FUNCTIONS
public Location insert() throws DatabaseException {
+ preInsertOrUpdateSanityCheck();
model.locationCache.insert(this);
putProvidedEquipmentIntoDB();
return this;
}
public void update() throws DatabaseException {
removeProvidedEquipmentFromDB(underlyingLocation);
+ preInsertOrUpdateSanityCheck();
model.database.updateLocation(underlyingLocation);
putProvidedEquipmentIntoDB();
}
@@ -133,5 +135,18 @@ public boolean equals(Object other) {
Location loc = (Location)other;
return this.underlyingLocation.equals(loc.underlyingLocation);
}
+
+ @Override
+ public void preInsertOrUpdateSanityCheck() {
+ assert getRoom() != null : "room null";
+ assert getType() != null : "type null";
+ assert getMaxOccupancy() != null : "maxocc null";
+
+ if (documentLoaded)
+ assert document != null : "doc null";
+
+ if (providedEquipmentLoaded)
+ assert providedEquipmentDescriptions != null : "provided equipment descs null";
+ }
}
@@ -31,7 +31,7 @@
import scheduler.model.db.IDatabase.NotFoundException;
public class Model {
- abstract static class Cache<DecoratedT extends Identified, UnderlyingT extends IDBObject> {
+ abstract static class Cache<DecoratedT extends ModelObject, UnderlyingT extends IDBObject> {
HashMap<Integer, DecoratedT> cache = new HashMap<Integer, DecoratedT>();
private DecoratedT decorateAndPut(UnderlyingT underlying) {
@@ -1,7 +1,9 @@
package scheduler.model;
-public abstract class Identified {
+public abstract class ModelObject {
public abstract Integer getID();
+
+ public abstract void preInsertOrUpdateSanityCheck();
public boolean isTransient() { return getID() == null; }
}
@@ -5,7 +5,7 @@
import scheduler.model.db.DatabaseException;
import scheduler.model.db.IDBSchedule;
-public class Schedule extends Identified {
+public class Schedule extends ModelObject {
private final Model model;
IDBSchedule underlyingSchedule;
@@ -28,11 +28,13 @@
public Schedule insert() throws DatabaseException{
assert(document != null);
+ preInsertOrUpdateSanityCheck();
model.database.insertSchedule(document.underlyingDocument, underlyingSchedule);
return this;
}
public void update() throws DatabaseException {
+ preInsertOrUpdateSanityCheck();
model.database.updateSchedule(underlyingSchedule);
}
@@ -70,5 +72,12 @@ public Schedule setDocument(Document newDocument) {
documentLoaded = true;
return this;
}
+
+ @Override
+ public void preInsertOrUpdateSanityCheck() {
+ // TODO Auto-generated method stub
+ if (documentLoaded)
+ assert document != null : "doc null";
+ }
}
@@ -6,7 +6,7 @@
import scheduler.model.db.DatabaseException;
import scheduler.model.db.IDBScheduleItem;
-public class ScheduleItem extends Identified {
+public class ScheduleItem extends ModelObject {
private final Model model;
IDBScheduleItem underlying;
@@ -44,6 +44,7 @@ public void insert() throws DatabaseException {
assert(locationLoaded);
assert(instructorLoaded);
+ preInsertOrUpdateSanityCheck();
model.itemCache.insert(this);
}
@@ -64,6 +65,7 @@ public void update() throws DatabaseException {
model.database.associateScheduleItemLab(lecture.underlying, underlying);
}
+ preInsertOrUpdateSanityCheck();
model.itemCache.update(this);
}
@@ -225,4 +227,24 @@ public String toString ()
}*/
return r;
}
+
+
+ @Override
+ public void preInsertOrUpdateSanityCheck() {
+ assert getDays() != null : "days null";
+
+ if (scheduleLoaded)
+ assert schedule != null : "sched null";
+
+ if (courseLoaded)
+ assert course != null : "course null";
+
+ if (locationLoaded)
+ assert location != null : "location null";
+
+ if (instructorLoaded)
+ assert instructor != null : "instructor null";
+
+ // lecture can be null
+ }
}
@@ -3,7 +3,7 @@
import scheduler.model.db.DatabaseException;
import scheduler.model.db.IDBUser;
-public class User extends Identified {
+public class User extends ModelObject {
private final Model model;
final IDBUser underlyingUser;
@@ -19,11 +19,13 @@
// PERSISTENCE FUNCTIONS
public User insert() throws DatabaseException {
+ preInsertOrUpdateSanityCheck();
model.userCache.insert(this);
return this;
}
public void update() throws DatabaseException {
+ preInsertOrUpdateSanityCheck();
model.userCache.update(this);
}
@@ -41,4 +43,9 @@ public void delete() throws DatabaseException {
public void setAdmin(boolean isAdmin) { underlyingUser.setAdmin(isAdmin); }
public Integer getID() { return underlyingUser.getID(); }
+
+ @Override
+ public void preInsertOrUpdateSanityCheck() {
+ assert getUsername() != null;
+ }
}
@@ -99,6 +99,17 @@ Record readCourseIntoRecord(CourseGWT course) {
return record;
}
+ private <T> T assertNotNull(T thing) {
+ assert thing != null;
+ return thing;
+ }
+
+ private String emptyStringIfNull(String str) {
+ if (str == null)
+ return "";
+ return str;
+ }
+
CourseGWT readRecordIntoCourse(Record record) {
String dayCombinationsStringsCombined = record.getAttributeAsString("dayCombinations");
@@ -120,16 +131,16 @@ CourseGWT readRecordIntoCourse(Record record) {
CourseGWT course = new CourseGWT(
record.getAttribute("isSchedulable").equals("true"),
- record.getAttribute("name"),
- record.getAttribute("catalogNumber"),
- record.getAttribute("department"),
- record.getAttribute("wtu"),
- record.getAttribute("scu"),
- record.getAttribute("numSections"),
- record.getAttribute("type"),
- record.getAttribute("maxEnrollment"),
+ emptyStringIfNull(record.getAttribute("name")),
+ emptyStringIfNull(record.getAttribute("catalogNumber")),
+ emptyStringIfNull(record.getAttribute("department")),
+ emptyStringIfNull(record.getAttribute("wtu")),
+ emptyStringIfNull(record.getAttribute("scu")),
+ emptyStringIfNull(record.getAttribute("numSections")),
+ emptyStringIfNull(record.getAttribute("type")),
+ emptyStringIfNull(record.getAttribute("maxEnrollment")),
Integer.parseInt(record.getAttribute("lectureID")), // lecture ID
- record.getAttribute("hoursPerWeek"),
+ emptyStringIfNull(record.getAttribute("hoursPerWeek")),
dayCombinations, // day combinations
record.getAttributeAsInt("id"), // id
"true".equals(record.getAttribute("isTethered")),
Oops, something went wrong.

0 comments on commit 7721687

Please sign in to comment.