View Javadoc

1   /*
2    * Copyright 2010-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.testdoc.core.adapter;
17  
18  import java.io.File;
19  import java.io.FilenameFilter;
20  import java.util.List;
21  
22  import de.smartics.testdoc.core.doc.ScenarioTestDoc;
23  import de.smartics.testdoc.core.doc.Type;
24  import de.smartics.testdoc.core.doc.UnitTestDoc;
25  import de.smartics.testdoc.core.export.AbstractExportAdapter;
26  import de.smartics.testdoc.core.export.ExportException;
27  
28  /**
29   * Exports the documentation fragments to a directory via the APT filer.
30   *
31   * @author <a href="mailto:robert.reiner@smartics.de">Robert Reiner</a>
32   * @version $Revision:591 $
33   */
34  public abstract class AbstractFileBasedExportAdapter extends
35      AbstractExportAdapter
36  {
37    // ********************************* Fields *********************************
38  
39    // --- constants ------------------------------------------------------------
40  
41    // --- members --------------------------------------------------------------
42  
43    /**
44     * Helper to serialize instances of {@link UnitTestDoc} to files.
45     */
46    protected final PersistenceUtils persist = new PersistenceUtils();
47  
48    // ****************************** Initializer *******************************
49  
50    // ****************************** Constructors ******************************
51  
52    /**
53     * Default constructor.
54     */
55    public AbstractFileBasedExportAdapter()
56    {
57    }
58  
59    // ****************************** Inner Classes *****************************
60  
61    // ********************************* Methods ********************************
62  
63    // --- init -----------------------------------------------------------------
64  
65    // --- get&set --------------------------------------------------------------
66  
67    // --- business -------------------------------------------------------------
68  
69    /**
70     * {@inheritDoc}
71     *
72     * @see de.smartics.testdoc.core.export.ExportAdapter#export(de.smartics.testdoc.core.doc.UnitTestDoc)
73     */
74    @Override
75    public void export(final UnitTestDoc testDoc) throws ExportException
76    {
77      final Type testCaseType = calcTestCaseType(testDoc);
78      if (testCaseType != null)
79      {
80        try
81        {
82          final Type uutType = testDoc.getUutType();
83          final File file = createFileRef(testCaseType, uutType);
84          if (file.exists())
85          {
86            final UnitTestDoc storedTestDoc =
87                (UnitTestDoc) persist.deserialize(file);
88            storedTestDoc.addScenarios(testDoc);
89            persist.serialize(storedTestDoc, file);
90          }
91          else
92          {
93            persist.serialize(testDoc, file);
94          }
95        }
96        catch (final IllegalStateException e)
97        {
98          throw new IllegalStateException(
99              "Cannot create resource with APT filer.", e);
100       }
101     }
102   }
103 
104   protected abstract File createFileRef(final Type testCaseType,
105       final Type uutType) throws IllegalStateException;
106 
107   protected abstract File[] createFileRefs(final Type testCaseType)
108     throws IllegalStateException;
109 
110   protected final File[] createFileRefs(final Type testCaseType, final File directory)
111   {
112     final String prefix = testCaseType.toString() + '-';
113     final File[] files = directory.listFiles(new FilenameFilter()
114     {
115       @Override
116       public boolean accept(final File dir, final String name)
117       {
118         return name.startsWith(prefix);
119       }
120     });
121     if (files != null)
122     {
123       return files;
124     }
125     return new File[0];
126   }
127 
128   /**
129    * Assumes that all scenarios belong to the same test case type.
130    */
131   protected final Type calcTestCaseType(final UnitTestDoc testDoc)
132   {
133     final List<ScenarioTestDoc> scenarios = testDoc.getScenarios();
134     if (!scenarios.isEmpty())
135     {
136       final Type testCaseType = scenarios.get(0).getTestCaseType();
137       for (final ScenarioTestDoc scenario : scenarios)
138       {
139         final Type currentType = scenario.getTestCaseType();
140         if (!testCaseType.equals(currentType))
141         {
142           throw new IllegalArgumentException("Not all scenarios are of type '"
143                                              + testCaseType + "': "
144                                              + currentType);
145         }
146       }
147       return testCaseType;
148     }
149     return null;
150   }
151 
152   /**
153    * {@inheritDoc}
154    *
155    * @see de.smartics.testdoc.core.export.ExportAdapter#clear(de.smartics.testdoc.core.doc.Type)
156    */
157   @Override
158   public void clear(final Type testCaseType)
159   {
160     if (testCaseType != null)
161     {
162       try
163       {
164         final File[] fileRefs = createFileRefs(testCaseType);
165         for (final File file : fileRefs)
166         {
167           if (file.exists())
168           {
169             final UnitTestDoc testDoc = (UnitTestDoc) persist.deserialize(file);
170             testDoc.removeScenarios(testCaseType);
171             persist.serialize(testDoc, file);
172           }
173         }
174       }
175       catch (final IllegalStateException e)
176       {
177         throw new ExportException("Cannot clear scenarios for test case type '"
178                                   + testCaseType + "'.", e);
179       }
180     }
181   }
182 
183   // --- object basics --------------------------------------------------------
184 
185 }