source: ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/CMDComponentTypeWrapperTest.java @ 2174

Last change on this file since 2174 was 2174, checked in by olhsha, 12 years ago

#50 fix is refactored and the test is refined (the result is written in the file). Next: try the fix on the specs generated via TomCat?.
#181 how to send the generated channel to the RssReader?? where to get "practical abstract description files", largeProfile is not marshallable to abstract/profile/component!!

File size: 9.2 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package clarin.cmdi.componentregistry;
6
7import clarin.cmdi.componentregistry.components.CMDComponentSpec;
8import clarin.cmdi.componentregistry.components.CMDComponentType;
9import clarin.cmdi.componentregistry.model.AbstractDescription;
10import clarin.cmdi.componentregistry.rest.RegistryTestHelper;
11import java.io.BufferedReader;
12import java.io.ByteArrayOutputStream;
13import java.io.File;
14import java.io.FileOutputStream;
15import java.io.IOException;
16import java.io.InputStream;
17import java.io.InputStreamReader;
18import java.util.List;
19import javax.xml.bind.JAXBException;
20import org.junit.Test;
21import static org.junit.Assert.*;
22
23/**
24 *
25 * @author olhsha
26 */
27
28public class CMDComponentTypeWrapperTest {
29
30    //////////////////////////////////////////////////
31    private CMDComponentType copyCMDComponentType(CMDComponentType newcomponent) {
32       
33       CMDComponentType component= new CMDComponentType();
34       
35       // filing in filename
36       component.setFilename(newcomponent.getFilename());
37       
38       // filing in the list of child components
39       List<CMDComponentType> childcomponents = component.getCMDComponent();
40       boolean emptynesscheck=childcomponents.isEmpty();
41       assertTrue(emptynesscheck);
42       
43       List<CMDComponentType> newchildcomponents = newcomponent.getCMDComponent();
44       for (CMDComponentType currentcomponent: newchildcomponents){
45           childcomponents.add(copyCMDComponentType(currentcomponent));
46       }
47       
48       
49       return component;
50    }
51   
52   
53    ////////////////////////////////////////////
54   
55   
56    private CMDComponentType makeTestComponent(){
57       CMDComponentType component= new CMDComponentType();
58       
59       // fil-in filename
60      component.setFilename("mini0.619");
61      assertEquals(component.getFilename(), "mini0.619");
62     
63       // fil-in child components
64      List<CMDComponentType> childcomponents = component.getCMDComponent();
65      boolean emptynesscheck=childcomponents.isEmpty();
66      assertTrue(emptynesscheck);
67     
68      String[] filenames = {"Guilherme", "Peter", "Twan", "Olha"};
69      for (String currentname : filenames){
70         CMDComponentType currentcomponent = new CMDComponentType();
71         currentcomponent.setFilename(currentname);
72         assertEquals(currentcomponent.getFilename(), currentname);
73         childcomponents.add(currentcomponent);
74       }
75       
76       
77       return component;
78    }
79   
80   
81   
82   
83    /////////////////////////////////////////////
84    // checks if all the filenames related to the component are nulls
85    private void checkNullnessOfFilenamesInComponent(CMDComponentType component){
86       
87       
88      assertEquals(component.getFilename(), null);
89      checkNullnessOfFilenamesInListOfComponents(component.getCMDComponent()); 
90     
91     
92    }
93   
94    ///////////////////////////////
95    // checks if all the filenames related to the list of component are nulls
96    private void checkNullnessOfFilenamesInListOfComponents(List<CMDComponentType> listofcomponents){
97       
98     
99      for (CMDComponentType currentcomponent : listofcomponents){
100          checkNullnessOfFilenamesInComponent(currentcomponent);
101      }
102     
103     
104    }
105   
106   
107    /////////////////////////////////////////////////
108    // returns true if all the filenames related to the input component are not null
109    private boolean checkNonNullnessOfFilenamesInComponent(CMDComponentType component){
110     
111      String filename = component.getFilename();
112      System.out.println(filename);
113      boolean check=(filename == null); 
114     
115      if (check) return false;
116     
117      return checkNonNullnessOfFilenamesInListOfComponents(component.getCMDComponent());
118    }
119   
120    ////////////////// /////////////
121    // returns true if all thefilenames related to the list of components are not null
122    private boolean checkNonNullnessOfFilenamesInListOfComponents(List<CMDComponentType> listofcomponents){
123       
124        boolean check;
125     
126      for (CMDComponentType currentcomponent : listofcomponents){
127          check = checkNonNullnessOfFilenamesInComponent(currentcomponent);
128          if(!check) return false;
129      }
130     
131     return true;
132    }
133   
134    ////////////////////////////////////////
135   
136   
137    @Test   
138    public void setFileNamesToNullTestComponent(){
139       
140     
141     
142      // check  a fresh (empty)  CMDComponentType
143      CMDComponentType emptycomponent= new CMDComponentType();
144      CMDComponentTypeWrapper emptywrapper = new CMDComponentTypeWrapper(emptycomponent);
145      emptywrapper.setFileNamesToNull(); 
146      assertEquals(emptycomponent.getFilename(), null);
147     
148     
149     
150      // check  the test component  CMDComponentType
151     
152      CMDComponentType component= makeTestComponent();
153      assertTrue(checkNonNullnessOfFilenamesInComponent(component));
154     
155      CMDComponentTypeWrapper wrapper = new CMDComponentTypeWrapper(component);
156      wrapper.setFileNamesToNull();
157      checkNullnessOfFilenamesInComponent(component);
158     
159    }
160   
161    ///////////////////////////////////////
162   
163    @Test   
164    public void setFileNamesToNullTestSpec(){
165     
166      // make test spec
167      CMDComponentSpec componentspec= new CMDComponentSpec(); 
168       
169      CMDComponentType component= makeTestComponent();
170      CMDComponentType anothercomponent= copyCMDComponentType(component);
171     
172     
173      List<CMDComponentType> listofcomponents = componentspec.getCMDComponent();
174      listofcomponents.add(component);
175      listofcomponents.add(anothercomponent);
176     
177      // run the nuller
178      CMDComponentTypeWrapper wrapper = new CMDComponentTypeWrapper(componentspec);
179      wrapper.setFileNamesToNull();
180     
181      checkNullnessOfFilenamesInListOfComponents(listofcomponents);
182    }
183     
184   
185   
186    //////////////////////////////////////////////////////////////
187    /// Testing on the profile XML file
188    // There are a few auxiliary methods preceeding the test method, which is at the end
189   
190    // adding dummy filenames to a component
191    private void addDummyFilenamesToComponent(CMDComponentType component){
192       
193   
194        if (component != null) {
195        component.setFilename("Dummy");
196       
197        List<CMDComponentType> listofcomponents = component.getCMDComponent();
198        addDummyFilenamesToListOfComponents(listofcomponents);
199       
200        }
201    }
202   
203    // adding dummy filenames to the list of  components
204    private void addDummyFilenamesToListOfComponents(List<CMDComponentType> listofcomponents){
205       
206       
207        for (CMDComponentType currentcomponent : listofcomponents) { 
208                addDummyFilenamesToComponent(currentcomponent);
209                } 
210   
211       
212       
213    }
214   
215      // adds dummy filenames to the content of largeProfile.XML 
216      private  CMDComponentSpec makeTestSpecFromLargeProfile() throws IOException, JAXBException {
217         
218          String largeprofilestring = RegistryTestHelper.getLargeProfileContent(); // reading from the file
219          CMDComponentSpec compspec=RegistryTestHelper.getComponentFromString(largeprofilestring); // calling unmarchaller
220         
221          List<CMDComponentType> listofcomponents = compspec.getCMDComponent();
222          addDummyFilenamesToListOfComponents(listofcomponents);
223         
224          assertTrue(checkNonNullnessOfFilenamesInListOfComponents(listofcomponents));
225         
226          return compspec;
227         
228      }
229     
230       //writing compspec  into the file filename
231      private void writeSpecToFile(CMDComponentSpec compspec, String filename) throws IOException, JAXBException {
232         
233        String  os = RegistryTestHelper.getXml(compspec);
234       
235        FileOutputStream fop = null;
236        File file;
237       
238        file = new File(filename);
239        fop = new FileOutputStream(file);
240       
241        fop.write(os.getBytes());
242       
243        fop.flush();
244        fop.close();
245 
246        System.out.println("Done");
247 
248       
249      }
250     
251     
252      // creating the profile with filenames filled by "Dummy" and writing it into the file
253      public void writeDummiedXML() throws IOException, JAXBException{
254         
255          CMDComponentSpec compspec=makeTestSpecFromLargeProfile();
256          writeSpecToFile(compspec, "src/test/resources/xml/largeProfileDummyFilenames.xml");
257         
258      }
259       
260     
261     
262      /////////////////////////////////////////////
263      // testing the nuller on the XML file
264           
265      @Test
266     
267      public void setFileNamesToNullTestFile() throws IOException, JAXBException {
268         
269         writeDummiedXML();
270         
271         String dummiedcontent = RegistryTestHelper.getProfileContentFromFile("/xml/largeProfileDummyFilenames.xml");
272         CMDComponentSpec newcompspec  = RegistryTestHelper.getComponentFromString(dummiedcontent); // calling unmarchaller
273         
274         
275         CMDComponentTypeWrapper wrapper = new CMDComponentTypeWrapper(newcompspec);
276         
277         wrapper.setFileNamesToNull();
278         
279         checkNullnessOfFilenamesInListOfComponents(newcompspec.getCMDComponent());
280         writeSpecToFile(newcompspec, "src/test/resources/xml/largeProfileUndummied.xml");
281         
282         
283      }
284     
285     
286     
287    }
288   
289   
290   
291   
292
Note: See TracBrowser for help on using the repository browser.