001: /*
002: * Copyright 2004-2005 Fouad HAMDI.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.csvbeans.specs;
018:
019: import java.io.FileInputStream;
020: import java.util.List;
021:
022: import junit.framework.TestCase;
023:
024: import org.csvbeans.builders.BuildingStrategy;
025: import org.csvbeans.builders.CSVBuilder;
026: import org.csvbeans.impl.CSVSpecificationsFile;
027: import org.csvbeans.parsers.CSVParser;
028: import org.csvbeans.parsers.ParsingStrategy;
029:
030: /**
031: * @author Fouad Hamdi
032: */
033: public class SpecificationsFileTest extends TestCase {
034: private CSVSpecificationsFile parser;
035:
036: /*
037: * @see TestCase#setUp()
038: */
039: protected void setUp() throws Exception {
040: super .setUp();
041: }
042:
043: /*
044: * @see TestCase#tearDown()
045: */
046: protected void tearDown() throws Exception {
047: super .tearDown();
048: }
049:
050: /**
051: * Teste le parsing d'un fichier de mapping.
052: *
053: * @throws Exception
054: */
055: public void testParsing() throws Exception {
056: parser = new CSVSpecificationsFile(new FileInputStream(
057: "src/test/org/csvbeans/mapping.xml"));
058: parser.parse();
059: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
060: .getRecord("SIMPLE");
061: assertNotNull(enregistrement);
062: assertEquals("SIMPLE", enregistrement.getTag());
063: assertEquals("org.csvbeans.beans.Bean1", enregistrement
064: .getClassName());
065:
066: assertEquals(5, enregistrement.numberOfFields());
067:
068: List champs = enregistrement.getFields();
069: assertNotNull(champs);
070: FieldSpecificationImpl champ = (FieldSpecificationImpl) champs
071: .get(0);
072: assertEquals("champ1", champ.getName());
073: assertEquals(10, champ.getMaximumLength());
074:
075: champ = (FieldSpecificationImpl) champs.get(1);
076: assertEquals("champ2", champ.getName());
077: assertEquals(4, champ.getMaximumLength());
078:
079: champ = (FieldSpecificationImpl) champs.get(2);
080: assertEquals("champ3", champ.getName());
081: assertEquals(2, champ.getMaximumLength());
082:
083: champ = (FieldSpecificationImpl) champs.get(3);
084: assertEquals("champ4", champ.getName());
085: assertEquals(1, champ.getMaximumLength());
086:
087: BeanSpecificationImpl bean = (BeanSpecificationImpl) champs
088: .get(4);
089: assertEquals("org.csvbeans.beans.Bean2", bean.getClassName());
090: assertEquals("champX", bean.getName());
091: champs = bean.getFields();
092: assertNotNull(champs);
093: assertEquals(1, champs.size());
094:
095: champ = (FieldSpecificationImpl) champs.get(0);
096: assertEquals("champ1", champ.getName());
097: assertEquals(5, champ.getMaximumLength());
098:
099: assertTrue(parser.getBuildingStrategy() instanceof CSVBuilder);
100: assertTrue(parser.getParsingStrategy() instanceof CSVParser);
101: }
102:
103: public void testMelange() throws Exception {
104: parser = new CSVSpecificationsFile(new FileInputStream(
105: "src/test/org/csvbeans/mapping.xml"));
106: parser.parse();
107: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
108: .getRecord("MELANGE");
109: assertNotNull(enregistrement);
110: assertEquals(5, enregistrement.numberOfFields());
111: List champs = enregistrement.getFields();
112: assertNotNull(champs);
113: FieldSpecificationImpl champ = (FieldSpecificationImpl) champs
114: .get(0);
115: assertEquals("champ1", champ.getName());
116: assertEquals(10, champ.getMaximumLength());
117:
118: champ = (FieldSpecificationImpl) champs.get(1);
119: assertEquals("champ2", champ.getName());
120: assertEquals(4, champ.getMaximumLength());
121:
122: champ = (FieldSpecificationImpl) champs.get(3);
123: assertEquals("champ3", champ.getName());
124: assertEquals(2, champ.getMaximumLength());
125:
126: champ = (FieldSpecificationImpl) champs.get(4);
127: assertEquals("champ4", champ.getName());
128: assertEquals(1, champ.getMaximumLength());
129:
130: BeanSpecificationImpl bean = (BeanSpecificationImpl) champs
131: .get(2);
132: assertEquals("org.csvbeans.beans.Bean2", bean.getClassName());
133: assertEquals("champ5", bean.getName());
134: champs = bean.getFields();
135: assertNotNull(champs);
136: assertEquals(1, champs.size());
137:
138: champ = (FieldSpecificationImpl) champs.get(0);
139: assertEquals("champX", champ.getName());
140: assertEquals(5, champ.getMaximumLength());
141: }
142:
143: public void testLectureListe() throws Exception {
144: parser = new CSVSpecificationsFile(new FileInputStream(
145: "src/test/org/csvbeans/mapping.xml"));
146: parser.parse();
147: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
148: .getRecord("LISTE");
149: List champs = enregistrement.getFields();
150: assertNotNull(enregistrement);
151: assertEquals(5, enregistrement.numberOfFields());
152:
153: FieldSpecificationImpl champ1 = (FieldSpecificationImpl) champs
154: .get(0);
155: assertEquals("champ1", champ1.getName());
156:
157: FieldSpecificationImpl champ = (FieldSpecificationImpl) champs
158: .get(1);
159: assertEquals(10, champ.getMaximumLength());
160: assertEquals("element", champ.getName());
161: assertTrue(champ.isListElement());
162: assertEquals(3, champ.getListSize());
163:
164: FieldSpecificationImpl champ5 = (FieldSpecificationImpl) champs
165: .get(2);
166: assertEquals("champ5", champ5.getName());
167: }
168:
169: public void testValeursAutorisees() throws Exception {
170: parser = new CSVSpecificationsFile(new FileInputStream(
171: "src/test/org/csvbeans/mapping.xml"));
172: parser.parse();
173: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
174: .getRecord("VALEURS_AUTORISEES");
175: assertNotNull(enregistrement);
176: assertEquals(1, enregistrement.numberOfFields());
177: List champs = enregistrement.getFields();
178: FieldSpecification champ = (FieldSpecification) champs.get(0);
179: assertNotNull(champ);
180: assertTrue(champ.isAuthorizedValue("5"));
181: assertTrue(champ.isAuthorizedValue("6"));
182: for (int i = 0; i < 10; i++) {
183: if (i != 5 && i != 6) {
184: assertFalse(champ.isAuthorizedValue(new Integer(i)
185: .toString()));
186: }
187: }
188: }
189:
190: public void testChampsObligatoires() throws Exception {
191: parser = new CSVSpecificationsFile(new FileInputStream(
192: "src/test/org/csvbeans/mapping.xml"));
193: parser.parse();
194: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
195: .getRecord("OBLIGATOIRE");
196: assertNotNull(enregistrement);
197: assertEquals(2, enregistrement.numberOfFields());
198: List champs = enregistrement.getFields();
199:
200: FieldSpecification champ = (FieldSpecification) champs.get(0);
201: assertNotNull(champ);
202: assertFalse(champ.isRequired());
203:
204: champ = (FieldSpecification) champs.get(1);
205: assertNotNull(champ);
206: assertTrue(champ.isRequired());
207: }
208:
209: public void testParsingBeansComposesDeBeans() throws Exception {
210: parser = new CSVSpecificationsFile(new FileInputStream(
211: "src/test/org/csvbeans/mapping.xml"));
212: parser.parse();
213: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
214: .getRecord("COMPOSITES");
215: assertNotNull(enregistrement);
216: assertEquals(3, enregistrement.numberOfFields());
217: List champs = enregistrement.getFields();
218: assertEquals(2, champs.size());
219: BeanSpecification desc = (BeanSpecification) champs.get(0);
220: assertNotNull(desc);
221: List champsBean = desc.getFields();
222: assertNotNull(champsBean);
223: assertEquals(2, champsBean.size());
224: assertTrue(champsBean.get(1) instanceof BeanSpecificationImpl);
225: }
226:
227: public void testListeBeans() throws Exception {
228: parser = new CSVSpecificationsFile(new FileInputStream(
229: "src/test/org/csvbeans/mapping.xml"));
230: parser.parse();
231: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
232: .getRecord("LISTE_BEANS");
233: assertEquals(4, enregistrement.numberOfFields());
234: List champs = enregistrement.getFields();
235: assertEquals(1, champs.size());
236: for (int i = 0; i < champs.size(); i++) {
237: BeanSpecification desc = (BeanSpecification) champs.get(i);
238: assertNotNull(desc);
239: }
240: }
241:
242: public void testDigit() throws Exception {
243: parser = new CSVSpecificationsFile(new FileInputStream(
244: "src/test/org/csvbeans/mapping.xml"));
245: parser.parse();
246: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
247: .getRecord("VALEURS_AUTORISEES");
248: assertNotNull(enregistrement);
249: assertEquals(1, enregistrement.numberOfFields());
250: List champs = enregistrement.getFields();
251: FieldSpecification champ = (FieldSpecification) champs.get(0);
252: assertNotNull(champ);
253: }
254:
255: public void testConstants() throws Exception {
256: parser = new CSVSpecificationsFile(new FileInputStream(
257: "src/test/org/csvbeans/mapping.xml"));
258: parser.parse();
259: RecordSpecificationImpl enregistrement = (RecordSpecificationImpl) parser
260: .getRecord("CONSTANTS");
261: assertNotNull(enregistrement);
262: assertEquals(1, enregistrement.numberOfFields());
263: assertTrue(enregistrement.hasConstants());
264: }
265:
266: public void testStrategies() throws Exception {
267: parser = new CSVSpecificationsFile(new FileInputStream(
268: "src/test/org/csvbeans/mapping.xml"));
269: parser.parse();
270: ParsingStrategy csvParser = parser.getParsingStrategy();
271: assertEquals("value1", csvParser.getPropertyValue("prop1"));
272: BuildingStrategy csvBuilder = parser.getBuildingStrategy();
273: assertEquals("value2", csvBuilder.getPropertyValue("prop2"));
274: }
275: }
|