View Javadoc

1   package fit;
2   
3   import java.io.InputStream;
4   import java.util.HashMap;
5   
6   import javax.xml.bind.JAXBContext;
7   import javax.xml.bind.JAXBElement;
8   import javax.xml.bind.Unmarshaller;
9   
10  import com.seitenbau.testing.aliasconfigurationfixture.Alias;
11  import com.seitenbau.testing.aliasconfigurationfixture.AliasConfiguration;
12  import com.seitenbau.testing.aliasconfigurationfixture.IndexAlias;
13  
14  import net.sourceforge.xmlfit.fit.IReplace;
15  import net.sourceforge.xmlfit.fit.ReplaceParser;
16  import fit.ColumnFixture;
17  import fit.Parse;
18  import fit.TypeAdapter;
19  
20  public class ColumnSpecialValuesFixture extends ColumnFixture
21  {
22  
23    public static final String SPECIAL_NULL = "#{NULL}";
24  
25    public static final String SPECIAL_EMPTY = "#{EMPTY}";
26  
27    private HashMap<String, String> fAliasMap = null;
28  
29    @Override
30    public void doTable(Parse parse)
31    {
32      beforeTable(parse);
33      super.doTable(parse);
34      afterTable(parse);
35    }
36  
37    @Override
38    public void doRow(Parse parse)
39    {
40      beforeRow(parse);
41      super.doRow(parse);
42      afterRow(parse);
43    }
44  
45    @Override
46    public void doCells(Parse cells)
47    {
48      super.doCells(cells);
49    }
50  
51    @Override
52    public void check(Parse cells, TypeAdapter a)
53    {
54      // create wrapped Parse Object
55      Parse cell = ReplaceParser.wrap(cells, new IReplace()
56      {
57        public String replaceAll(String source, int index)
58        {
59          return replace(source, index);
60        }
61      });
62      String unmapped = cells.text();
63      if (unmapped.equals(SPECIAL_EMPTY))
64      {
65        compareEmpty(cell, a, "");
66      }
67      else if (unmapped.equals(SPECIAL_NULL))
68      {
69        compareNull(cell, a);
70      }
71      else
72      {
73        super.check(cell, a);
74      }
75    }
76  
77    public void wrong(Parse cell, String actual)
78    {
79      String key = null;
80      if (cell instanceof ReplaceParser)
81      {
82        key = ((ReplaceParser) cell).getUndecoratedParseObject().text();
83      }
84      if (key == null || !getMapping().containsKey(key))
85      {
86        super.wrong(cell, actual);
87        return;
88      }
89  
90      wrong(cell);
91      cell.addToBody(label("expected") + "<hr>" + "key [" + key
92          + "] resolved as [" + cell.text() + "] " + "<hr>" + escape(actual)
93          + label("actual"));
94    }
95  
96    protected String replace(String value, int index)
97    {
98      String newValue = getMapping().get(value);
99      if (newValue != null)
100     {
101       return newValue;
102     }
103     if (value.equals(SPECIAL_EMPTY))
104     {
105       return "";
106     }
107     if (value.equals(SPECIAL_NULL))
108     {
109       return null;
110     }
111     return value;
112   }
113 
114   public void compareNull(Parse cell, TypeAdapter a)
115   {
116     try
117     {
118       Object result = a.get();
119       if (result == null)
120       {
121         right(cell);
122       }
123       else
124       {
125         wrong(cell, a.toString(result));
126       }
127     }
128     catch (Exception e)
129     {
130       exception(cell, e);
131     }
132   }
133 
134   public void compareEmpty(Parse cell, TypeAdapter a, String text)
135   {
136     try
137     {
138       Object result = a.get();
139       if (a.equals(a.parse(text), result))
140       {
141         right(cell);
142       }
143       else
144       {
145         wrong(cell, a.toString(result));
146       }
147     }
148     catch (Exception e)
149     {
150       exception(cell, e);
151     }
152   }
153 
154   public HashMap<String, String> getMapping()
155   {
156     if (fAliasMap == null)
157     {
158       loadMapping("/element-alias-mapping.xml");
159     }
160     return fAliasMap;
161   }
162 
163   @SuppressWarnings("unchecked")
164   public void loadMapping(String filename)
165   {
166     fAliasMap = new HashMap<String, String>();
167     try
168     {
169       JAXBContext ctx = JAXBContext.newInstance(AliasConfiguration.class
170           .getPackage().getName());
171       Unmarshaller um = ctx.createUnmarshaller();
172       InputStream stream = AliasConfiguration.class
173           .getResourceAsStream(filename);
174       JAXBElement<AliasConfiguration> obj = (JAXBElement<AliasConfiguration>) um
175           .unmarshal(stream);
176       AliasConfiguration cfg = obj.getValue();
177       for (Alias alias : cfg.getAliasAndIndexAlias())
178       {
179         if (alias instanceof IndexAlias)
180         {
181         }
182         else
183         {
184           String name = alias.getName();
185           String value = alias.getValue();
186           fAliasMap.put(name, value);
187         }
188       }
189     }
190     catch (Exception e)
191     {
192       e.printStackTrace();
193       throw new RuntimeException(e);
194     }
195   }
196 
197   protected void afterTable(Parse parse)
198   {
199   }
200 
201   protected void beforeTable(Parse parse)
202   {
203   }
204 
205   protected void afterRow(Parse parse)
206   {
207   }
208 
209   protected void beforeRow(Parse parse)
210   {
211   }
212 
213 }