Changeset 2194


Ignore:
Timestamp:
09/03/12 15:01:10 (12 years ago)
Author:
olhsha
Message:

refactoring of after the discussion with Twan

Location:
ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry
Files:
11 added
10 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestService.java

    r2153 r2194  
    11package clarin.cmdi.componentregistry.rest;
    22
    3 import clarin.cmdi.componentregistry.CMDComponentTypeWrapper;
     3
    44import clarin.cmdi.componentregistry.ComponentRegistry;
    55import clarin.cmdi.componentregistry.ComponentRegistryException;
     
    799799               
    800800                // Olha: removing filename from spec before it gets extended!!! recursion over all the components
    801                 CMDComponentTypeWrapper setnull = new CMDComponentTypeWrapper(spec);
     801                setFileNamesFromListToNull(spec.getCMDComponent());
    802802               
    803803               
     
    938938   
    939939   
     940    /// two muchually recursive methods below are used to set filenames of components (and their child components) to null
     941    /*
     942     * @param List<CMDComponentType> listofcomponents the list of components whose filenames (and the children's names) are to be set to null
     943     */
     944    public void setFileNamesFromListToNull(List<CMDComponentType> listofcomponents){
     945     
     946        for (CMDComponentType currentcomponent : listofcomponents) {
     947                setFileNamesToNullCurrent(currentcomponent);
     948                }
     949   
     950    }
     951   
     952    /*
     953     * @param CMDComponentType currentcomponent the component whose filename (and whose children filenames) is to be set to null
     954     */
     955   
     956    public void setFileNamesToNullCurrent(CMDComponentType currentcomponent){
     957     
     958      currentcomponent.setFilename(null);
     959      setFileNamesFromListToNull(currentcomponent.getCMDComponent());
     960   
     961    }
     962   
     963    ////////////////////////////////////////////////
     964   
     965   
    940966}
  • ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/RssCreatorTest.java

    r2173 r2194  
    1414import java.io.ByteArrayOutputStream;
    1515import java.io.File;
     16import java.io.FileInputStream;
    1617import java.io.FileOutputStream;
    1718import java.io.IOException;
     
    115116   
    116117   
    117     /////////////////////////////////////////
    118     // writing ByteArrayOutputStream into the file filename
    119       private void writeStreamToFile(ByteArrayOutputStream os, String filename) throws IOException, JAXBException {
    120          
    121         FileOutputStream fop = null;
    122         File file;
    123        
    124         file = new File(filename);
    125         fop = new FileOutputStream(file);
    126        
    127         fop.write(os.toByteArray());
    128        
    129         fop.flush();
    130         fop.close();
    131  
    132         System.out.println("the rss is written into the file and saved");
    133  
    134        
    135       }
    136    
    137118   
    138119   
     
    160141        MDMarshaller.marshal(cdesc, os);
    161142       
    162         writeStreamToFile(os, filename);
     143        RegistryTestHelper.writeStreamToFile(os, filename);
    163144       
    164145    }
     
    192173    public void testRssChannelAndMarshal() throws IOException, JAXBException {
    193174       
     175         String dirName="MyTestXmls";
     176         File testDir = new File(dirName);
     177         testDir.mkdir();
     178       
     179         String path = new File(testDir, dirName).getAbsolutePath();
     180         
     181         String comp1="Component1.xml";
     182         String comp2="Component2.xml";
     183         String rssOut="rssTest.xml";
     184                 
     185       
     186       
     187       
    194188        ComponentDescription cdesc1=makeTestComponent1();
    195         writeComponentIntoFile(cdesc1, "src/test/resources/xml/Component1.xml");
     189        writeComponentIntoFile(cdesc1, path+comp1);
    196190       
    197191        ComponentDescription cdesc2=makeTestComponent2();
    198         writeComponentIntoFile(cdesc2, "src/test/resources/xml/Component2.xml");
    199        
    200        
    201        
    202         InputStream  is1 = RegistryTestHelper.getComponentContent(RegistryTestHelper.getProfileContentFromFile("/xml/Component1.xml"));
     192        writeComponentIntoFile(cdesc2, path+comp2);
     193       
     194       
     195       
     196        FileInputStream  is1 = new FileInputStream(path+comp1);
    203197        ComponentDescription desc1 = MDMarshaller.unmarshal(ComponentDescription.class, is1, null) ;
    204198       
    205         InputStream  is2 = RegistryTestHelper.getComponentContent(RegistryTestHelper.getProfileContentFromFile("/xml/Component2.xml"));
     199        FileInputStream  is2 = new FileInputStream(path+comp2);
    206200        ComponentDescription desc2 = MDMarshaller.unmarshal(ComponentDescription.class, is2, null) ;
    207201       
     
    229223        MDMarshaller.marshal(rss, osrss);
    230224       
    231         writeStreamToFile(osrss, "src/test/resources/xml/rssTest.xml");
    232        
    233        
    234     }
    235    
    236    
    237      
    238    
    239      
    240    
    241    
    242     //////////////////////////////////////////////
    243     /* @Test
    244       public  void fileLargeProfileTestToRssItem() throws IOException, JAXBException {
    245          
    246         InputStream largeProfileStream = RegistryTestHelper.class.getResourceAsStream("/xml/largeProfile.xml");
    247          
    248         AbstractDescription desc=MDMarshaller.unmarshal(ComponentDescription.class, largeProfileStream, null);
    249        
    250         assertTrue(desc !=null);
    251          
    252         RssCreator creator = new RssCreator(desc);
    253         RssItem rss = creator.toRssItem();
    254        
    255         //assertTrue(rss !=null);
    256        
    257        
    258         System.out.println(rss.getAuthor());
    259         System.out.println(rss.getDescription());
    260         System.out.println(rss.getLink());
    261         System.out.println(rss.getPubDate());
    262         System.out.println(rss.getTitle());
    263        
    264        
    265         writeRssToFile(rss, "src/test/resources/xml/rssOfLargeProfile.xml");
    266        
    267        
    268        
    269          
    270       }
    271       */
     225        RegistryTestHelper.writeStreamToFile(osrss, path+rssOut);
     226       
     227       
     228    }
     229   
     230   
     231   
    272232}
  • ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/ComponentRegistryRestServiceTest.java

    r2152 r2194  
    55import clarin.cmdi.componentregistry.ComponentStatus;
    66import clarin.cmdi.componentregistry.components.CMDComponentSpec;
     7import clarin.cmdi.componentregistry.components.CMDComponentType;
    78import clarin.cmdi.componentregistry.impl.database.ComponentRegistryBeanFactory;
    89import clarin.cmdi.componentregistry.impl.database.ComponentRegistryTestDatabase;
     
    1718import com.sun.jersey.api.representation.Form;
    1819import com.sun.jersey.multipart.FormDataMultiPart;
    19 import java.awt.image.ComponentSampleModel;
    2020import java.io.ByteArrayInputStream;
    2121import java.util.Date;
     
    11431143   
    11441144   
     1145   
     1146    /////////////////////////////////
     1147    @Test
     1148    public void testSetFilenamesToNullOnTestComponent() throws Exception {
     1149       
     1150       CMDComponentSpec spec = RegistryTestHelper.getTestComponent();
     1151       
     1152       
     1153       List<CMDComponentType> listofcomponents = spec.getCMDComponent();
     1154       CMDComponentSetFilenamesToNullTestHelper helper = new CMDComponentSetFilenamesToNullTestHelper();
     1155       
     1156       helper.addDummyFilenamesToListOfComponents(listofcomponents);
     1157       assertTrue(helper.checkNonNullnessOfFilenamesInListOfComponents(listofcomponents));
     1158       CMDComponentType compaux = helper.makeTestComponent();
     1159       listofcomponents.add(compaux);
     1160       
     1161       
     1162       ComponentRegistryRestService restservice = helper.getTestRestService();
     1163       restservice.setFileNamesFromListToNull(spec.getCMDComponent());
     1164       helper.checkNullnessOfFilenamesInListOfComponents(listofcomponents);
     1165    }
     1166   
     1167    ///////////////////////////////////
     1168     @Test
     1169     public void testSetFilenamesToNullOnLargeProfile() throws Exception {
     1170     
     1171     
     1172      String largeprofilestring = RegistryTestHelper.getLargeProfileContent();
     1173      CMDComponentSpec spec=RegistryTestHelper.getComponentFromString(largeprofilestring); // calling unmarchaller
     1174     
     1175     
     1176      List<CMDComponentType> listofcomponents = spec.getCMDComponent();
     1177      CMDComponentSetFilenamesToNullTestHelper helper = new CMDComponentSetFilenamesToNullTestHelper();
     1178       
     1179       helper.addDummyFilenamesToListOfComponents(listofcomponents);
     1180       assertTrue(helper.checkNonNullnessOfFilenamesInListOfComponents(listofcomponents));
     1181       
     1182       ComponentRegistryRestService restservice = helper.getTestRestService();
     1183       restservice.setFileNamesFromListToNull(spec.getCMDComponent());
     1184       helper.checkNullnessOfFilenamesInListOfComponents(listofcomponents);
     1185     
     1186    }
     1187   
    11451188}
  • ComponentRegistry/branches/ComponentRegistry-1.13.0-olha/ComponentRegistry/src/test/java/clarin/cmdi/componentregistry/rest/RegistryTestHelper.java

    r2170 r2194  
    2020import clarin.cmdi.componentregistry.model.ProfileDescription;
    2121import java.io.BufferedReader;
     22import java.io.File;
     23import java.io.FileOutputStream;
    2224import java.io.InputStreamReader;
    2325
     
    2729 */
    2830public final class RegistryTestHelper {
     31
     32   
    2933
    3034    private RegistryTestHelper() {
     
    153157    }
    154158
     159   
     160    public static String getStringFromStream(InputStream largeProfileStream) throws IOException {
     161        try {
     162            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(largeProfileStream));
     163            StringBuilder profileStringBuilder = new StringBuilder();
     164            String line;
     165            while (null != (line = bufferedReader.readLine())) {
     166                profileStringBuilder.append(line);
     167            }
     168            return profileStringBuilder.toString();
     169        } finally {
     170            largeProfileStream.close();
     171        }
     172    }
     173    ///////////////////////////////////////////////////////
     174   
    155175    public static String getLargeProfileContent() throws IOException {
    156176        InputStream largeProfileStream = RegistryTestHelper.class.getResourceAsStream("/xml/largeProfile.xml");
    157         try {
    158             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(largeProfileStream));
    159             StringBuilder profileStringBuilder = new StringBuilder();
    160             String line;
    161             while (null != (line = bufferedReader.readLine())) {
    162                 profileStringBuilder.append(line);
    163             }
    164             return profileStringBuilder.toString();
    165         } finally {
    166             largeProfileStream.close();
    167         }
    168     }
    169    
    170    
    171     // Olha was here
    172     // a method similar to getLargeProfileContent(), but allowing to get profile from any fil, not only largeProfile.xml
    173     public static String getProfileContentFromFile(String filename) throws IOException {
    174         InputStream largeProfileStream = RegistryTestHelper.class.getResourceAsStream(filename);
    175         try {
    176             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(largeProfileStream));
    177             StringBuilder profileStringBuilder = new StringBuilder();
    178             String line;
    179             while (null != (line = bufferedReader.readLine())) {
    180                 profileStringBuilder.append(line);
    181             }
    182             return profileStringBuilder.toString();
    183         } finally {
    184             largeProfileStream.close();
    185         }
    186     }
     177        return getStringFromStream(largeProfileStream);
     178    }
     179   
     180   
     181   
    187182
    188183    //////////////////////
     
    281276        return matcher.find() && !matcher.find(); //find only one
    282277    }
     278   
     279   
     280     // helping method writing byte[] bytes into the filename 
     281      public static void writeBytesToFile(byte[] bytes, String filename) throws IOException, JAXBException {
     282                 
     283        File file = new File(filename);
     284        FileOutputStream fop = new FileOutputStream(file);
     285       
     286        fop.write(bytes);
     287       
     288        fop.flush();
     289        fop.close();
     290 
     291       
     292      }
     293   
     294   
     295   
     296      //helping method writing String str  into the file filename
     297      public static void writeStringToFile(String str, String filename) throws IOException, JAXBException {
     298         
     299        writeBytesToFile(str.getBytes(), filename);
     300 
     301       
     302      }
     303     
     304      /////////////////////////////////////////
     305    // writing ByteArrayOutputStream into the file filename
     306      public static void writeStreamToFile(ByteArrayOutputStream os, String filename) throws IOException, JAXBException {
     307         
     308        writeBytesToFile(os.toByteArray(), filename);
     309 
     310       
     311      }
    283312}
Note: See TracChangeset for help on using the changeset viewer.