1   // $Id: TableXmlProducerTest.java,v 1.3 2004/07/22 08:41:23 powerpete Exp $
2   package com.pnpconsult.zeiterfassung.pdf;
3   
4   import java.util.ArrayList;
5   import java.util.Collection;
6   import java.util.Iterator;
7   
8   import junit.framework.TestCase;
9   import net.sf.hibernate.HibernateException;
10  
11  import com.pnpconsult.zeiterfassung.table.RateRow;
12  import com.pnpconsult.zeiterfassung.table.Row;
13  import com.pnpconsult.zeiterfassung.table.Table;
14  import com.pnpconsult.zeiterfassung.util.NumberUtils;
15  
16  /***
17   * @author <a href="mailto:powerpete@users.sf.net">Moritz Petersen</a>
18   * @version $Id: TableXmlProducerTest.java,v 1.3 2004/07/22 08:41:23 powerpete Exp $
19   */
20  public class TableXmlProducerTest extends TestCase
21  {
22  
23      public TableXmlProducerTest(String name)
24      {
25          super(name);
26      }
27  
28      public void testSource() throws Exception
29      {
30          String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
31              + "<table>"
32              + "<caption>Testprojekt - März 2004 - Mai 2004</caption>"
33              + "<currency>EUR</currency>"
34              + "<tax>16 %</tax>"
35              + "<total-tax>123,45</total-tax>"
36              + "<total>123,45</total>"
37              + "<total-including-tax>123,45</total-including-tax>"
38              + "<row>"
39              + "<description>Beschreibung</description>"
40              + "<target-date>13.05.2004</target-date>"
41              + "<total>800,00</total>"
42              + "<hours>4</hours>"
43              + "<rate>100,00</rate>"
44              + "<user-names>Max Mustermann</user-names>"
45              + "</row>"
46              + "<row>"
47              + "<hours>2</hours>"
48              + "<rate>200,00</rate>"
49              + "<user-names>Max Mustermann</user-names>"
50              + "</row>"
51              + "<row>"
52              + "<description>Beschreibung</description>"
53              + "<target-date>13.05.2004</target-date>"
54              + "<total>400,00</total>"
55              + "<hours>4</hours>"
56              + "<rate>100,00</rate>"
57              + "<user-names>Max Mustermann</user-names>"
58              + "</row>"
59              + "</table>";
60          String xml = new TableXmlProducer(new DummyTable()).getXml();
61          assertEquals(expected, xml);
62      }
63  
64      private class DummyTable extends Table
65      {
66          private final class DummyRow implements Row
67          {
68              private float total;
69              private Collection rateRows = new ArrayList();
70  
71              private final class DummyRateRow extends RateRow
72              {
73                  public DummyRateRow(float hours, float rate)
74                  {
75                      this.hours = hours;
76                      this.rate = rate;
77                      users.add("Max Mustermann");
78                  }
79              }
80  
81              public DummyRow(float hours1, float rate1, float hours2, float rate2)
82              {
83                  total = hours1 * rate1 + hours2 * rate2;
84                  rateRows.add(new DummyRateRow(hours1, rate1));
85                  rateRows.add(new DummyRateRow(hours2, rate2));
86              }
87  
88              public DummyRow(float hours1, float rate1)
89              {
90                  total = hours1 * rate1;
91                  rateRows.add(new DummyRateRow(hours1, rate1));
92              }
93  
94              public String getDescription()
95              {
96                  return "Beschreibung";
97              }
98  
99              public String getTargetDate()
100             {
101                 return "13.05.2004";
102             }
103 
104             public String getTotal()
105             {
106                 return NumberUtils.formatLong(total);
107             }
108 
109             public float computeTotal()
110             {
111                 return total;
112             }
113 
114             public RateRow getRateRowsHead()
115             {
116                 return (RateRow) rateRows.iterator().next();
117             }
118 
119             public Iterator getRateRowsTail()
120             {
121                 Iterator it = rateRows.iterator();
122                 it.next();
123                 return it;
124             }
125 
126             public int getRateRowsSize()
127             {
128                 return rateRows.size();
129             }
130         }
131 
132         public DummyTable() throws HibernateException
133         {
134             super(null);
135         }
136 
137         public String getCaption()
138         {
139             return "Testprojekt - März 2004 - Mai 2004";
140         }
141 
142         public Collection getRows()
143         {
144             Collection rows = new ArrayList();
145             rows.add(new DummyRow(4F, 100F, 2F, 200F));
146             rows.add(new DummyRow(4F, 100F));
147             return rows;
148         }
149 
150         public String getTax()
151         {
152             return "16 %";
153         }
154 
155         public String getTotal()
156         {
157             return "123,45";
158         }
159 
160         public String getTotalIncludingTax()
161         {
162             return "123,45";
163         }
164 
165         public String getTotalTax()
166         {
167             return "123,45";
168         }
169 
170         public String getCurrency()
171         {
172             return "EUR";
173         }
174     }
175 }