001: package jfb.tst.tools.activitymgr.core;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.FileNotFoundException;
006: import java.io.IOException;
007: import java.io.InputStream;
008:
009: import javax.xml.parsers.ParserConfigurationException;
010:
011: import jfb.tools.activitymgr.core.DbException;
012: import jfb.tools.activitymgr.core.ModelException;
013: import jfb.tools.activitymgr.core.ModelMgr;
014: import jfb.tools.activitymgr.core.beans.Collaborator;
015: import jfb.tools.activitymgr.core.beans.Contribution;
016: import jfb.tools.activitymgr.core.beans.Duration;
017: import jfb.tools.activitymgr.core.beans.Task;
018: import jfb.tst.tools.activitymgr.AbstractModelTestCase;
019:
020: import org.apache.log4j.Logger;
021: import org.xml.sax.SAXException;
022: import org.xml.sax.SAXParseException;
023:
024: public class XmlTest extends AbstractModelTestCase {
025:
026: /** Logger */
027: private static Logger log = Logger.getLogger(XmlTest.class);
028:
029: public void testEmptyFile() throws DbException, IOException,
030: ParserConfigurationException, SAXException, ModelException {
031: importTestFile();
032: }
033:
034: public void testMissingDoctype() throws DbException, IOException,
035: ParserConfigurationException, SAXException, ModelException {
036: assertSAXExceptionThrown("Missing DOCTYPE must generate an sax error");
037: }
038:
039: public void testBadXmlFormat() throws DbException, IOException,
040: ParserConfigurationException, SAXException, ModelException {
041: assertSAXExceptionThrown("Bad XML format must generate an sax error");
042: }
043:
044: public void testCollaboratorInDurations() throws DbException,
045: IOException, ParserConfigurationException, SAXException,
046: ModelException {
047: assertSAXExceptionThrown("Wrong collaborator situation in XML file must generate an sax error");
048: }
049:
050: public void testMissingCollaboratorLogin() throws DbException,
051: IOException, ParserConfigurationException, SAXException,
052: ModelException {
053: assertSAXExceptionThrown("Missing collaborator login must generate an sax error");
054: }
055:
056: public void testCreateDurations() throws DbException, IOException,
057: ParserConfigurationException, SAXException, ModelException {
058: // Vérification du nombre initial de durées
059: Duration[] initialDurations = ModelMgr.getDurations();
060:
061: // Import du fichier associé au test
062: importTestFile();
063:
064: // Vérification du nombre de durées après import
065: Duration[] durations = ModelMgr.getDurations();
066: assertNotNull(durations);
067: assertEquals(initialDurations.length + 3, durations.length);
068:
069: // Suppression des durées
070: Duration duration = new Duration();
071: duration.setId(200);
072: ModelMgr.removeDuration(duration);
073: duration.setId(300);
074: ModelMgr.removeDuration(duration);
075: duration.setId(400);
076: ModelMgr.removeDuration(duration);
077: }
078:
079: public void testCreateCollaborators() throws DbException,
080: IOException, ParserConfigurationException, SAXException,
081: ModelException {
082: // Vérification du nombre initial de durées
083: Collaborator[] initialCollaborators = ModelMgr
084: .getCollaborators();
085: assertNotNull(initialCollaborators);
086: assertEquals(0, initialCollaborators.length);
087:
088: // Import du fichier associé au test
089: importTestFile();
090:
091: // Vérification du nombre de durées après import
092: Collaborator[] collaborators = ModelMgr.getCollaborators();
093: assertNotNull(collaborators);
094: assertEquals(2, collaborators.length);
095:
096: // Suppression des durées
097: ModelMgr.removeCollaborator(collaborators[0]);
098: ModelMgr.removeCollaborator(collaborators[1]);
099: }
100:
101: public void testDuplicateCollaboratorLogin() throws DbException,
102: IOException, ParserConfigurationException, SAXException,
103: ModelException {
104: // Vérification du nombre initial de collaborateurs
105: Collaborator[] initialCollaborators = ModelMgr
106: .getCollaborators();
107: assertNotNull(initialCollaborators);
108: assertEquals(0, initialCollaborators.length);
109:
110: // Import du fichier associé au test
111: try {
112: importTestFile();
113: fail("Duplicate login must generate an error");
114: } catch (SAXParseException expected) {
115: // Do nothing...
116: }
117: }
118:
119: public void testCreateTasks() throws DbException, IOException,
120: ParserConfigurationException, SAXException, ModelException {
121: // Vérification du nombre initial de taches
122: Task[] initialTasks = ModelMgr.getSubtasks(null);
123: assertNotNull(initialTasks);
124: assertEquals(0, initialTasks.length);
125:
126: // Import du fichier associé au test
127: importTestFile();
128:
129: // Vérification du nombre de durées après import
130: Task[] tasks = ModelMgr.getSubtasks(null);
131: assertNotNull(tasks);
132: assertEquals(1, tasks.length);
133:
134: // Suppression des durées
135: ModelMgr.removeTask(tasks[0]);
136: }
137:
138: public void testCreateContributions() throws DbException,
139: IOException, ParserConfigurationException, SAXException,
140: ModelException {
141: // Vérification du nombre initial de taches
142: Contribution[] initialContributions = ModelMgr
143: .getContributions(null, null, null, null, null);
144: assertNotNull(initialContributions);
145: assertEquals(0, initialContributions.length);
146:
147: // Import du fichier associé au test
148: importTestFile();
149:
150: // Vérification du nombre de durées après import
151: Contribution[] contributions = ModelMgr.getContributions(null,
152: null, null, null, null);
153: assertNotNull(contributions);
154: assertEquals(2, contributions.length);
155:
156: // Suppression des données
157: ModelMgr.removeContribution(contributions[0]);
158: ModelMgr.removeContribution(contributions[1]);
159: Task[] tasks = ModelMgr.getSubtasks(null);
160: ModelMgr.removeTask(tasks[1]);
161: ModelMgr.removeTask(tasks[0]);
162: Collaborator[] collaborators = ModelMgr.getCollaborators();
163: ModelMgr.removeCollaborator(collaborators[0]);
164: ModelMgr.removeCollaborator(collaborators[1]);
165: Duration durations[] = ModelMgr.getDurations();
166: for (int i = 0; i < durations.length; i++)
167: ModelMgr.removeDuration(durations[i]);
168:
169: }
170:
171: public void testExportAndImport() throws DbException,
172: ModelException, IOException, ParserConfigurationException,
173: SAXException {
174: // Création des objets de test
175: Duration duration = new Duration();
176: duration.setId(100);
177: duration = ModelMgr.createDuration(duration);
178: Collaborator collaborator = new Collaborator();
179: collaborator.setLogin("login");
180: collaborator.setFirstName("FirstName");
181: collaborator.setLastName("LastName");
182: collaborator = ModelMgr.createCollaborator(collaborator);
183: Task parentTask = new Task();
184: parentTask.setCode("Par");
185: parentTask.setName("Parent name");
186: parentTask = ModelMgr.createTask(null, parentTask);
187: Task task = new Task();
188: task.setCode("Code");
189: task.setName("Name");
190: task.setBudget(1);
191: task.setInitiallyConsumed(2);
192: task.setTodo(3);
193: task = ModelMgr.createTask(parentTask, task);
194: Contribution contribution = new Contribution();
195: contribution.setYear(2006);
196: contribution.setMonth(01);
197: contribution.setDay(01);
198: contribution.setContributorId(collaborator.getId());
199: contribution.setTaskId(task.getId());
200: contribution.setDurationId(duration.getId());
201: contribution = ModelMgr.createContribution(contribution);
202:
203: // Export
204: ByteArrayOutputStream out = new ByteArrayOutputStream();
205: ModelMgr.exportToXML(out);
206: out.close();
207: String export = out.toString();
208: log.debug("export :");
209: log.debug(export);
210:
211: // Supression des objets de test
212: ModelMgr.removeContribution(contribution);
213: ModelMgr.removeTask(task);
214: ModelMgr.removeTask(parentTask);
215: ModelMgr.removeCollaborator(collaborator);
216: ModelMgr.removeDuration(duration);
217:
218: // Réimportation
219: ModelMgr.importFromXML(new ByteArrayInputStream(out
220: .toByteArray()));
221:
222: // Nouvel export puis comparaison
223: out = new ByteArrayOutputStream();
224: ModelMgr.exportToXML(out);
225: out.close();
226: String export2 = out.toString();
227: log.debug("export2 :");
228: log.debug(export2);
229:
230: // Comparaison
231: assertEquals(export, export2);
232:
233: // Supression des objets de test ayant été réimportés
234: contribution = ModelMgr.getContributions(null, null, null,
235: null, null)[0];
236: ModelMgr.removeContribution(contribution);
237: task = ModelMgr.getTaskByCodePath("/Par/Code");
238: ModelMgr.removeTask(task);
239: parentTask = ModelMgr.getTaskByCodePath("/Par");
240: ModelMgr.removeTask(parentTask);
241: collaborator = ModelMgr.getCollaborators()[0];
242: ModelMgr.removeCollaborator(collaborator);
243: ModelMgr.removeDuration(duration);
244: }
245:
246: /*
247: * Méthodes privées
248: */
249:
250: private void assertSAXExceptionThrown(String testFailMessage)
251: throws DbException, IOException,
252: ParserConfigurationException, ModelException {
253: try {
254: importTestFile();
255: fail(testFailMessage);
256: } catch (SAXException e) {
257: // Exception normalement levée en cas de mauvais format
258: }
259: }
260:
261: private void importTestFile() throws DbException, IOException,
262: ParserConfigurationException, SAXException, ModelException {
263: String testName = getName();
264: log.debug("testName='" + testName + "'");
265: String filePostfix = testName.substring(4);
266: log.debug("filePostfix='" + filePostfix + "'");
267: String fileName = "XmlTest." + filePostfix + ".xml";
268: log.debug("fileName='" + fileName + "'");
269: // Ouverture du fichier de test
270: InputStream in = XmlTest.class.getResourceAsStream(fileName);
271: if (in == null) {
272: throw new FileNotFoundException(fileName);
273: }
274: // Importation des données
275: ModelMgr.importFromXML(in);
276: // Fermeture du flux
277: in.close();
278: }
279:
280: }
|