001: package com.ibm.sigtest;
002:
003: import java.io.BufferedReader;
004: import java.io.File;
005: import java.io.FileReader;
006: import java.io.IOException;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Vector;
010:
011: /**
012: * This class stores a name and a collection of class descriptions, to form a
013: * project.
014: * @author Matthew J. Duftler (duftler@us.ibm.com)
015: */
016: public class ProjectDesc {
017:
018: private String name;
019:
020: private List classDescs = new Vector();
021:
022: public ProjectDesc(String name) {
023: this .name = name;
024: }
025:
026: public void addClassDesc(ClassDesc classDesc) {
027: classDescs.add(classDesc);
028: }
029:
030: public ClassDesc getClassDesc(String className) {
031: Iterator iterator = classDescs.iterator();
032:
033: while (iterator.hasNext()) {
034: ClassDesc classDesc = (ClassDesc) iterator.next();
035:
036: if (SigTestUtils.objectsEqual(classDesc.getName(),
037: className)) {
038: return classDesc;
039: }
040: }
041:
042: return null;
043: }
044:
045: public void setClassDescs(List classDescs) {
046: this .classDescs = classDescs;
047: }
048:
049: public List getClassDescs() {
050: return classDescs;
051: }
052:
053: /**
054: * This method compares this project description to the specified one.
055: * @param that the project description to compare this one to
056: * @return a description of the differences, or null if they match perfectly
057: */
058: public String compare(ProjectDesc that) {
059: StringBuffer strBuf = new StringBuffer();
060: Iterator iterator = classDescs.iterator();
061:
062: while (iterator.hasNext()) {
063: ClassDesc this CD = (ClassDesc) iterator.next();
064: ClassDesc thatCD = that.getClassDesc(this CD.getName());
065:
066: if (thatCD != null) {
067: String result = this CD.compare(thatCD);
068:
069: if (result != null) {
070: strBuf.append("\n" + result);
071: }
072: }
073: }
074:
075: List referenceExtras = new Vector();
076: List candidateExtras = new Vector();
077:
078: SigTestUtils.findExtraClasses(this , that, referenceExtras,
079: candidateExtras);
080:
081: if (referenceExtras.size() > 0 || candidateExtras.size() > 0) {
082: strBuf
083: .append("\nProject '"
084: + that.name
085: + "'"
086: + (candidateExtras.size() > 0 ? " has extraneous class"
087: + (candidateExtras.size() > 1 ? "es"
088: : "")
089: + " "
090: + SigTestUtils
091: .getCondensedClassList(candidateExtras)
092: : "")
093: + (referenceExtras.size() > 0 ? (candidateExtras
094: .size() > 0 ? " and" : "")
095: + " is missing class"
096: + (referenceExtras.size() > 1 ? "es"
097: : "")
098: + " "
099: + SigTestUtils
100: .getCondensedClassList(referenceExtras)
101: : "") + ".");
102: }
103:
104: return (strBuf.length() > 0) ? strBuf.toString() : null;
105: }
106:
107: /**
108: * This method compares the specified project file to the classes currently
109: * available on the classpath.
110: * @param file the project file to read from
111: * @return a description of the differences, or null if they match perfectly
112: */
113: public static String compareProjectFile(File projectFile)
114: throws IOException {
115: ProjectDesc riPD = readProjectFile("ReferenceImpl", projectFile);
116: ProjectDesc cdPD = SigTestUtils.getProjectDesc(riPD);
117:
118: return riPD.compare(cdPD);
119: }
120:
121: public String toString() {
122: StringBuffer strBuf = new StringBuffer("Project '" + name
123: + "': ");
124: int size = classDescs.size();
125:
126: for (int i = 0; i < size; i++) {
127: strBuf.append((i > 0 ? ", " : "")
128: + ((ClassDesc) classDescs.get(i)).getName());
129: }
130:
131: return strBuf.toString();
132: }
133:
134: /**
135: * This method reads the specified project file into memory.
136: * @param projectName the name to be used when referring to the project in
137: * error messages
138: * @param file the project file to read from
139: * @return a model of the project
140: */
141: public static ProjectDesc readProjectFile(String projectName,
142: File file) throws IOException {
143: ProjectDesc pd = new ProjectDesc(projectName);
144: FileReader fileReader = new FileReader(file);
145: BufferedReader buf = new BufferedReader(fileReader);
146:
147: while (buf.ready()) {
148: pd.addClassDesc(ClassDesc.parseClassDesc(buf));
149: }
150:
151: buf.close();
152: fileReader.close();
153:
154: return pd;
155: }
156: }
|