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.builders;
018:
019: import java.util.Arrays;
020:
021: import org.csvbeans.beans.Bean1;
022: import org.csvbeans.builders.CSVBuilder;
023: import org.csvbeans.builders.LineBuilder;
024: import org.csvbeans.builders.LinesWriter;
025: import org.csvbeans.specs.RecordSpecification;
026: import org.csvbeans.specs.RecordSpecificationImpl;
027: import org.csvbeans.specs.SpecificationsFile;
028: import org.jmock.Mock;
029: import org.jmock.MockObjectTestCase;
030: import org.jmock.core.constraint.IsEqual;
031:
032: /**
033: * Test the CSVBuilder class.
034: *
035: * @author Fouad Hamdi
036: * @since 0.1
037: */
038: public class CSVBuilderTest extends MockObjectTestCase {
039: private Mock specifications;
040:
041: private CSVBuilder builder;
042:
043: private Mock lineBuilder;
044:
045: private Mock writer;
046:
047: /**
048: * @see TestCase#setUp()
049: */
050: protected void setUp() throws Exception {
051: specifications = new Mock(SpecificationsFile.class);
052: lineBuilder = new Mock(LineBuilder.class);
053: writer = new Mock(LinesWriter.class);
054: builder = new CSVBuilder();
055: builder.setSpecifications((SpecificationsFile) specifications
056: .proxy());
057: }
058:
059: /**
060: * Test the generation of a CSV file.
061: */
062: public void testBuild() throws Exception {
063: RecordSpecification rec1 = new RecordSpecificationImpl(
064: "§START", "org.csvbeans.Bean1");
065: specifications.expects(atLeastOnce()).method("getRecord").with(
066: new IsEqual("§START")).will(returnValue(rec1));
067: specifications.expects(once()).method("hasHeader").will(
068: returnValue(false));
069: specifications.expects(atLeastOnce()).method(
070: "isEndingSeparatorEnabled").will(returnValue(false));
071: specifications.expects(atLeastOnce()).method("getLineBuilder")
072: .will(returnValue(lineBuilder.proxy()));
073: specifications.expects(atLeastOnce()).method(
074: "clearRecordContext");
075: specifications.expects(once()).method("clearSessionContext");
076: builder.setHeader("Ceci est l'entête\r\n");
077: Bean1 bean1 = new Bean1();
078: Bean1 bean2 = new Bean1();
079: builder.addBeans("§START", Arrays.asList(new Object[] { bean1,
080: bean2 }));
081: lineBuilder.expects(once()).method("build").with(same(bean1))
082: .will(returnValue("toto;tutu;17;1;test"));
083: lineBuilder.expects(once()).method("build").with(same(bean2))
084: .will(returnValue("titi;tata;55;0;"));
085:
086: writer.expects(once()).method("writeLine").with(
087: new IsEqual("§START"));
088: writer.expects(once()).method("writeLine").with(
089: new IsEqual("toto;tutu;17;1;test"));
090: writer.expects(once()).method("writeLine").with(
091: new IsEqual("titi;tata;55;0;"));
092: writer.expects(once()).method("close");
093:
094: builder.build((LinesWriter) writer.proxy());
095: writer.verify();
096: }
097:
098: /**
099: * Test that the generated CSV record ends with a comma.
100: */
101: public void testCommaEndingLine() throws Exception {
102: RecordSpecification rec1 = new RecordSpecificationImpl(
103: "§START", "org.csvbeans.Bean1");
104:
105: specifications.expects(atLeastOnce()).method("getRecord").with(
106: new IsEqual("§START")).will(returnValue(rec1));
107: specifications.expects(atLeastOnce()).method(
108: "isEndingSeparatorEnabled").will(returnValue(true));
109: specifications.expects(atLeastOnce()).method("getLineBuilder")
110: .will(returnValue(lineBuilder.proxy()));
111: specifications.expects(atLeastOnce()).method(
112: "getFieldSeparator").will(returnValue(";"));
113: specifications.expects(atLeastOnce()).method(
114: "clearRecordContext");
115: specifications.expects(once()).method("clearSessionContext");
116:
117: Bean1 bean1 = new Bean1();
118: Bean1 bean2 = new Bean1();
119: lineBuilder.expects(once()).method("build").with(same(bean1))
120: .will(returnValue("toto;tutu"));
121: lineBuilder.expects(once()).method("build").with(same(bean2))
122: .will(returnValue("titi;tata"));
123: builder.addBeans("§START", Arrays.asList(new Object[] { bean1,
124: bean2 }));
125:
126: writer.expects(once()).method("writeLine").with(
127: new IsEqual("§START"));
128: writer.expects(once()).method("writeLine").with(
129: new IsEqual("toto;tutu;"));
130: writer.expects(once()).method("writeLine").with(
131: new IsEqual("titi;tata;"));
132: writer.expects(once()).method("close");
133:
134: builder.build((LinesWriter) writer.proxy());
135: writer.verify();
136: }
137: }
|