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 net.sourceforge.xmlfit.fit.IReplace;
11  import net.sourceforge.xmlfit.fit.ReplaceParser;
12  
13  import com.seitenbau.testing.aliasconfigurationfixture.Alias;
14  import com.seitenbau.testing.aliasconfigurationfixture.AliasConfiguration;
15  import com.seitenbau.testing.aliasconfigurationfixture.IndexAlias;
16  
17  /**
18   * Special Fixture defining a mapping for <target/> Values.
19   */
20  public class AliasFixture extends Fixture
21  {
22    private static final String CMD_LOAD_MAPPING = "#loadMapping";
23  
24    private static final String CMD_START = "start";
25  
26    private static final String SPECIAL_NULL = "#{NULL}";
27  
28    private static final String SPECIAL_EMPTY = "#{EMPTY}";
29  
30    private HashMap<String, String> fAliasMap = null;
31    
32    private static HashMap<String, String> fStaticAliasMap = null;
33  
34    private Fixture fDelegateFixture;
35  
36    public void setDelegateFixture(Fixture delegateFixture)
37    {
38      fDelegateFixture = delegateFixture;
39    }
40  
41    public AliasFixture()
42    {
43    }
44  
45    @Override
46    public void doTables(Parse tables)
47    {
48      super.doTables(tables);
49    }
50  
51    @Override
52    public void doCells(Parse cells)
53    {
54      if (cells.text().equals(CMD_LOAD_MAPPING))
55      {
56        loadMapping(cells);
57      }
58      else if (cells.text().equals(CMD_START) && fDelegateFixture == null)
59      {
60        start(cells);
61      }
62      else
63      {
64        // create wrapped Parse Object
65        Parse mappedCells = ReplaceParser.wrap(cells, new IReplace()
66        {
67          public String replaceAll(String source, int index)
68          {
69            return replace(source, index);
70          }
71        });
72        // DELEGATE CALLS
73        fDelegateFixture.doCells(mappedCells);
74      }
75    }
76  
77    private void start(Parse cells)
78    {
79      String fixtureName = cells.more.text();
80      try
81      {
82        Fixture fixture = (Fixture) Class.forName(fixtureName).newInstance();
83        fDelegateFixture = fixture;
84  
85        String filename = cells.more.more.text();
86        if (filename != null && filename.length() > 0)
87        {
88          loadMapping(filename);
89        }
90        fixture.counts = counts;
91      }
92      catch (Exception e)
93      {
94        exception(cells, e);
95      }
96  
97    }
98  
99    /**
100    * Callback when text() is called, and could be replaced
101    * 
102    * @param value
103    * @param index
104    * @return
105    */
106   public String replace(String value, int index)
107   {
108     if (index == 1) // Nur selekor ersetzen
109     {
110       String newValue = getMapping().get(value);
111       if (newValue != null)
112       {
113         return newValue;
114       }
115     }
116     if (value.equals(SPECIAL_EMPTY))
117     {
118       return "";
119     }
120     if (value.equals(SPECIAL_NULL))
121     {
122       return null;
123     }
124     return value;
125   }
126   
127   public void loadMapping(Parse cells)
128   {
129     String filename = cells.more.text();
130     loadMapping(filename);
131   }
132 
133   public HashMap<String, String> getMapping()
134   {
135     if (fAliasMap == null)
136     {
137       return new HashMap<String, String>();
138     }
139     return fAliasMap;
140   }
141 
142   @SuppressWarnings("unchecked")
143   public void loadMapping(String filename)
144   {
145     fAliasMap = new HashMap<String, String>();
146     fStaticAliasMap= new HashMap<String, String>();
147     try
148     {
149       JAXBContext ctx = JAXBContext.newInstance(AliasConfiguration.class
150           .getPackage().getName());
151       Unmarshaller um = ctx.createUnmarshaller();
152       InputStream stream = AliasConfiguration.class
153           .getResourceAsStream(filename);
154       JAXBElement<AliasConfiguration> obj = (JAXBElement<AliasConfiguration>) um
155           .unmarshal(stream);
156       AliasConfiguration cfg = obj.getValue();
157       for (Alias alias : cfg.getAliasAndIndexAlias())
158       {
159         if (alias instanceof IndexAlias)
160         {
161         }
162         else
163         {
164           String name = alias.getName();
165           String value = alias.getValue();
166           fAliasMap.put(name, value);
167           fStaticAliasMap.put(name,value);
168         }
169       }
170     }
171     catch (Exception e)
172     {
173       e.printStackTrace();
174       throw new RuntimeException(e);
175     }
176   }
177 
178 }