Ignore:
Timestamp:
08/20/13 15:32:37 (11 years ago)
Author:
George.Georgovassilis@mpi.nl
Message:

#360 Refactoring of transaction handling, tests and application context

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/trunk/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/CMDComponentSetFilenamesToNullTestRunner.java

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