source: ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/CMDComponentSetFilenamesToNullTestRunner.java @ 4550

Last change on this file since 4550 was 4550, checked in by twagoo, 10 years ago

Fixed errors due to schema change allowing only one root element in a profile or component (see r4525)

File size: 7.8 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package clarin.cmdi.componentregistry.rest;
6
7import clarin.cmdi.componentregistry.components.CMDComponentSpec;
8import clarin.cmdi.componentregistry.components.CMDComponentType;
9import java.io.BufferedReader;
10import java.io.FileInputStream;
11import java.io.IOException;
12import java.io.InputStreamReader;
13import java.util.Collections;
14import java.util.List;
15import javax.xml.bind.JAXBException;
16import static org.junit.Assert.*;
17
18/**
19 * non-automated developers. test, with and input (a valid component/prifile xml
20 * file and a path to it) and the output (the file where all filenames are set
21 * to null, in the same directory as the input file)
22 *
23 * @author olhsha
24 * @author George.Georgovassilis@mpi.nl
25 */
26public class CMDComponentSetFilenamesToNullTestRunner {
27
28    /**
29     *
30     * @author olhsha
31     */
32    /**
33     * makes a new component (with filled-in filename)
34     *
35     * @param filename is assigned to the filed "filename" of the new component
36     * @param childcomponents is assigned to the list of child components of the
37     * new component
38     * @return the reference to the new component
39     */
40    protected CMDComponentType makeTestComponent(String filename,
41            List<CMDComponentType> childcomponents) {
42
43        CMDComponentType component = new CMDComponentType();
44        component.setFilename(filename);
45        List<CMDComponentType> kids = component.getCMDComponent();
46        assertFalse(kids == null);
47        assertEquals(kids.size(), 0);
48        if (childcomponents != null) {
49            kids.addAll(childcomponents);
50        }
51        ;
52        return component;
53    }
54
55    /*
56     * checks if all the filenames related to the component are nulls
57     */
58    private void checkNullnessOfFilenamesInComponent(CMDComponentType component) {
59        assertEquals(component.getFilename(), null);
60        checkNullnessOfFilenamesInListOfComponents(component.getCMDComponent());
61    }
62
63    /*
64     * checks if all the filenames related to the list of component are nulls
65     */
66    private void checkNullnessOfFilenamesInListOfComponents(
67            List<CMDComponentType> listofcomponents) {
68        for (CMDComponentType currentcomponent : listofcomponents) {
69            checkNullnessOfFilenamesInComponent(currentcomponent);
70        }
71    }
72
73    /*
74     * returns true if all the filenames related to the input component are not
75     * null
76     */
77    private boolean checkNonNullnessOfFilenamesInComponent(
78            CMDComponentType component) {
79
80        String filename = component.getFilename();
81        System.out.println(filename);
82        boolean check = (filename == null);
83        if (check) {
84            return false;
85        }
86        return checkNonNullnessOfFilenamesInListOfComponents(component
87                .getCMDComponent());
88    }
89
90    /*
91     * returns true if all the filenames related to the list of components are
92     * not null
93     */
94    private boolean checkNonNullnessOfFilenamesInListOfComponents(
95            List<CMDComponentType> listofcomponents) {
96        boolean check;
97        for (CMDComponentType currentcomponent : listofcomponents) {
98            check = checkNonNullnessOfFilenamesInComponent(currentcomponent);
99            if (!check) {
100                return false;
101            }
102        }
103        return true;
104    }
105
106    /*
107     * adding dummy filenames to a component
108     */
109    private void addDummyFilenamesToComponent(CMDComponentType component) {
110        if (component != null) {
111            component.setFilename("Dummy");
112            List<CMDComponentType> listofcomponents = component
113                    .getCMDComponent();
114            addDummyFilenamesToListOfComponents(listofcomponents);
115        }
116    }
117
118    /*
119     * adding dummy filenames to the list of components
120     */
121    private void addDummyFilenamesToListOfComponents(
122            List<CMDComponentType> listofcomponents) {
123        for (CMDComponentType currentcomponent : listofcomponents) {
124            addDummyFilenamesToComponent(currentcomponent);
125        }
126    }
127
128    /*
129     * adds dummy filenames to the content of largeProfile.XML
130     */
131    private CMDComponentSpec makeTestFromFile(String filename)
132            throws IOException, JAXBException {
133        FileInputStream is = new FileInputStream(filename);
134        String profilestring = RegistryTestHelper.getStringFromStream(is);
135        CMDComponentSpec compspec = RegistryTestHelper
136                .getComponentFromString(profilestring); // calling unmarchaller
137        List<CMDComponentType> listofcomponents = Collections.singletonList(compspec.getCMDComponent());
138        addDummyFilenamesToListOfComponents(listofcomponents);
139        assertTrue(checkNonNullnessOfFilenamesInListOfComponents(listofcomponents));
140        return compspec;
141    }
142
143    /*
144     * creating the profile with filenames filled by "Dummy" and writing it into
145     * the file
146     */
147    private void writeDummiedXML(String filenamein, String filenameout)
148            throws IOException, JAXBException {
149        CMDComponentSpec compspec = makeTestFromFile(filenamein);
150        String os = RegistryTestHelper.getXml(compspec);
151        RegistryTestHelper.writeStringToFile(os, filenameout);
152    }
153
154    /*
155     * generic test-from-file read/write
156     */
157    private void setFileNamesToNullInFile(String dirName, String fileNameInit,
158            String fileNameDummied, String fileNameUnDummied)
159            throws IOException, JAXBException {
160
161        String path = RegistryTestHelper.openTestDir(dirName);
162        writeDummiedXML(path + fileNameInit, path + fileNameDummied);
163
164        FileInputStream is = new FileInputStream(path + fileNameDummied);
165        String dummiedcontent = RegistryTestHelper.getStringFromStream(is);
166        CMDComponentSpec compspec = RegistryTestHelper
167                .getComponentFromString(dummiedcontent); // calling unmarchaller
168
169        List<CMDComponentType> listofcomponents = Collections.singletonList(compspec.getCMDComponent());
170
171        IComponentRegistryRestService testrestservice = new ComponentRegistryRestService();
172        testrestservice.setFileNamesFromListToNull(listofcomponents);
173        checkNullnessOfFilenamesInListOfComponents(listofcomponents);
174
175        String os = RegistryTestHelper.getXml(compspec);
176        RegistryTestHelper.writeStringToFile(os, path + fileNameUnDummied);
177    }
178
179    /*
180     * developer test method: nulling filenames in an arbitrary (component) file
181     * user's input: the name of the file, together with the sub-directory
182     * inside target/
183     */
184    public static void main(String args[]) throws java.io.IOException,
185            JAXBException {
186
187        BufferedReader buffer = new BufferedReader(new InputStreamReader(
188                System.in));
189
190        System.out.println("");
191        System.out
192                .print("Sub-directory (of target/) name? (up to 32 symbols): ");
193        String dirName = buffer.readLine();
194        System.out.println("");
195        System.out
196                .println("(Watch out: this is a temorary directory, which is removed after any new clean+build)");
197        System.out.println(dirName);
198
199        System.out.println("");
200        System.out.println("Check if your file is in this temorary directory");
201        System.out.print("and input the file name (up to 32 symbols): ");
202        String fileName = buffer.readLine();
203        System.out.println(fileName);
204
205        System.out.println("Bedankt, ff wachten .. ");
206
207        String fileNameDummied = "Dummied" + fileName;
208        String fileNameUnDummied = "Nulled" + fileName;
209
210        CMDComponentSetFilenamesToNullTestRunner helper = new CMDComponentSetFilenamesToNullTestRunner();
211
212        helper.setFileNamesToNullInFile(dirName, fileName, fileNameDummied,
213                fileNameUnDummied);
214
215        System.out.println("Now look up the directory target/" + dirName);
216    }
217}
Note: See TracBrowser for help on using the repository browser.