[Title] Fixed method name and logic, Added Unit Test Case
authorgyeongseok.seo <gyeongseok.seo@samsung.com>
Tue, 14 May 2013 09:28:24 +0000 (18:28 +0900)
committergyeongseok.seo <gyeongseok.seo@samsung.com>
Tue, 14 May 2013 09:29:52 +0000 (18:29 +0900)
[Desc.] Unit Test Using - Mock File, when needed inputstream then real file's stream using
[Issue]

Change-Id: I606286d9b01719180afa21491591ad9bbcb6cc73

org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/DirectoryTemplate.java
org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/DirectoryTemplateTest.java

index 40fdd02..fd232ad 100644 (file)
@@ -38,6 +38,7 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Locale;
@@ -90,14 +91,22 @@ extends CommonTemplate
      * @throws TemplateException If template mapping is invalid
      */
     public DirectoryTemplate( String path ) throws TemplateException {
-        if ( StringUtil.isEmpty( path ) ||
-                !( this.templateRootFile = new File( path ) ).isDirectory() ) {
-            throw new TemplateException();
-        }
-        this.templateRootPath = this.templateRootFile.getAbsolutePath();
+        this( new File( path ) );
     }
 
     /**
+     * get file's {@link InputStream},
+     * caller must close inputStream
+     * 
+     * @param file
+     * @return InputStream
+     * @throws FileNotFoundException
+     */
+    protected InputStream getInputStream(File file) throws IOException {
+        return new FileInputStream(file);
+    }
+    
+    /**
      * constructor with template contents in file, file is must directory
      * 
      * @param file in template contents
@@ -105,7 +114,7 @@ extends CommonTemplate
      * @throws TemplateException If template mapping is invalid
      */
     public DirectoryTemplate( File file ) throws TemplateException {
-        if ( null == file || !file.isDirectory() ) {
+        if ( null == file || !file.isDirectory() || StringUtil.isEmpty( file.getAbsolutePath() ) ) {
             throw new TemplateException();
         }
         this.templateRootFile = file;
@@ -115,24 +124,22 @@ extends CommonTemplate
     /**
      * pre-changing MANIFEST.MF file, because of MANIFEST.MF file also marking to template engine
      * 
-     * @param manifestfile
+     * @param in - manifestfile's inputstream
      * @param models
      * @throws IOException
      * @throws TemplateException
      */
-    private
+    protected
     void
-    templateManifest( File manifestfile, IModelProvider models ) throws IOException, TemplateException
+    templateManifest( InputStream in, IModelProvider models ) throws IOException, TemplateException
     {
         // try MANIFEST.MF generate
-        FileInputStream fin = new FileInputStream( manifestfile );
-
         final ITemplateEngine engine = TemplateEngineFactory.getInstance().create();
         final TemplateManager templates = new TemplateManager();
         engine.setTemplateProvider( templates );
 
         InMemoryTemplate manifestTemplate = 
-                new InMemoryTemplate( getBytes(fin), "utf-8", BufferFactory.getInstance() );
+                new InMemoryTemplate( getBytes(in), "utf-8", BufferFactory.getInstance() );
         templates.addTemplate( "MANIFEST.MF", manifestTemplate );
 
         final ByteArrayOutputStream out = new ByteArrayOutputStream();
@@ -145,7 +152,7 @@ extends CommonTemplate
         } catch (Exception e) {
             throw new TemplateException( e.getCause() );
         } finally {
-            tryClose( fin, out, is );
+            tryClose( in, out, is );
         }
     }
     
@@ -158,16 +165,16 @@ extends CommonTemplate
      */
     protected
     void
-    recursiveBundleLoad( File root ) throws IOException {
+    loadBundleRecursively( File root ) throws IOException {
         File [] lists = root.listFiles();
 
         for ( File file : lists ) {
             if ( file.isDirectory() ) {
-                recursiveBundleLoad( file );
+                loadBundleRecursively( file );
                 continue;
             }
             String name = file.getName();
-            FileInputStream in = null;
+            InputStream in = null;
             
             try {
                 if ( null == file.getParentFile() &&
@@ -177,7 +184,7 @@ extends CommonTemplate
 
                 if ( "messages.properties".equals( name ) )
                 {
-                    in = new FileInputStream(file);
+                    in = getInputStream( file );
                     defaultBundle = new PropertyResourceBundle( in );
                 }
                 else if ( name.startsWith( "messages_" ) && name.endsWith( ".properties" ) )
@@ -188,7 +195,7 @@ extends CommonTemplate
                     final String country = (2==fragments.length)?fragments[1]:""; 
 
                     Locale locale = new Locale( lang, country );
-                    in = new FileInputStream(file);
+                    in = getInputStream( file );
                     final ResourceBundle bundle = new PropertyResourceBundle( in );
                     bundles.put( locale, bundle );
                 }
@@ -211,12 +218,12 @@ extends CommonTemplate
      */
     protected
     void
-    recursiveManifestLoad( File root, IModelProvider models ) throws IOException, TemplateException {
+    loadManifestRecursively( File root, IModelProvider models ) throws IOException, TemplateException {
         File [] lists = root.listFiles();
 
         for ( File file : lists ) {
             if ( file.isDirectory() ) {
-                recursiveManifestLoad( file, models );
+                loadManifestRecursively( file, models );
                 continue;
             }
             String name = file.getName();
@@ -228,7 +235,9 @@ extends CommonTemplate
 
             if ( "MANIFEST.MF".equals( name ) )
             {
-                templateManifest( file, models );
+                InputStream fin = getInputStream( file );
+                templateManifest( fin, models );
+                tryClose( fin );
             }
         }
     }
@@ -244,12 +253,12 @@ extends CommonTemplate
      */
     protected
     void
-    recursiveAddTemplate( File root, TemplateManager templates, Filter templateFilter, Filter copyFilter ) throws IOException {
+    addTemplateRecursively( File root, TemplateManager templates, Filter templateFilter, Filter copyFilter ) throws IOException {
         File [] lists = root.listFiles();
 
         for ( File file : lists ) {
             if ( file.isDirectory() ) {
-                recursiveAddTemplate( file, templates, templateFilter, copyFilter );
+                addTemplateRecursively( file, templates, templateFilter, copyFilter );
                 continue;
             }
             String relativePath = file.getAbsolutePath().replace( templateRootPath+System.getProperty("file.separator"), "");
@@ -257,7 +266,7 @@ extends CommonTemplate
 
             logger.trace( "Name :{}", name );
             
-            FileInputStream in = new FileInputStream( file );
+            InputStream in = getInputStream( file );
             final byte[] contents = getBytes( in );
             templates.addTemplate( name, new InMemoryTemplate(contents, "utf-8", BufferFactory.getInstance() ) );
 
@@ -287,10 +296,10 @@ extends CommonTemplate
         context.set( this );
 
         // bundles setting
-        recursiveBundleLoad( rootFile );
+        loadBundleRecursively( rootFile );
 
         // preload MANIFEST.MF
-        recursiveManifestLoad( rootFile, models );
+        loadManifestRecursively( rootFile, models );
 
     }
 
@@ -321,7 +330,7 @@ extends CommonTemplate
         final Filter copyFilter = createFilter( StandardRealm.separate( copies ), null );
         
         // Process files
-        recursiveAddTemplate( rootFile, templates, templateFilter, copyFilter );
+        addTemplateRecursively( rootFile, templates, templateFilter, copyFilter );
 
         engine.setTemplateProvider( templates );
     }
index a639ce2..202f947 100644 (file)
@@ -27,13 +27,21 @@ package org.tizen.common.verrari.template;
 
 import static org.junit.Assert.*;
 
+import java.io.ByteArrayInputStream;
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.URL;
 import java.util.Collection;
-import java.util.Iterator;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Matchers.anyString;
 
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
+import org.tizen.common.file.Filter;
 import org.tizen.common.Factory;
 import org.tizen.common.config.Preference;
 import org.tizen.common.core.command.ExecutionContext;
@@ -44,6 +52,13 @@ import org.tizen.common.file.VirtualFileHandler;
 import org.tizen.common.verrari.AbstractTestCase;
 import org.tizen.common.verrari.IModelProvider;
 
+import static org.tizen.common.verrari.template.TemplateConstants.ATTR_NAME;
+import static org.tizen.common.verrari.template.TemplateConstants.ATTR_MAPPING;
+import static org.tizen.common.verrari.template.TemplateConstants.ATTR_VERSION;
+import static org.tizen.common.verrari.template.TemplateConstants.ATTR_COPY;
+import static org.tizen.common.verrari.template.TemplateConstants.ATTR_EXCLUDE;
+
+import static org.tizen.common.util.IOUtil.tryClose;
 import static org.tizen.common.util.ObjectUtil.nvl;
 
 /**
@@ -63,7 +78,7 @@ extends AbstractTestCase
     protected static final Object NULL = new Object();
     protected DirectoryTemplate template = null;
     protected IModelProvider models = null;
-    protected String path = "./test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1";
+    protected File rootDFileMock = null;
 
     /**
      * Test {@link DirectoryTemplate#getAttribute(String)}
@@ -78,18 +93,135 @@ extends AbstractTestCase
     test_getAttribute()
     throws Exception
     {
-        File rootFile = new File( path );
-        template.preload( rootFile, models );
+        final URL u = new URL( "cp:///web-ui-fw/0.1.1/META-INF/MANIFEST.MF" );
+        final InputStream in = u.openStream();
+        template.templateManifest( in, models );
 
         Collection<String> keys = template.getAttributeKeys();
-        Iterator<String> iter = keys.iterator();
-        
-        while ( iter.hasNext() ) {
-            String key = iter.next();
-            
-            String attribute = template.getAttribute( key );
-            assertNotNull( attribute );
+        assertFalse( keys.isEmpty() );
+
+        assertEquals( "web-ui-fw", template.getAttribute( ATTR_NAME ) );
+        assertEquals( "0\\.1\\.1_Theme/minified/(.*) -> $1,0\\\\.1\\\\.1_Theme/origin/(.*) -> $1", template.getAttribute( ATTR_MAPPING ) );
+        assertEquals( "0.1.1", template.getAttribute( ATTR_VERSION ) );
+        assertEquals( "0.1.1_Theme/original/*", template.getAttribute( ATTR_COPY ) );
+        assertEquals( "META-INF/*.*", template.getAttribute( ATTR_EXCLUDE ) );
+    }
+
+    /**
+     * Test {@link DirectoryTemplate#templateManifest(InputStream, IModelProvider)
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see DirectoryTemplate#templateManifest(InputStream, IModelProvider)
+     */
+    @Test
+    public
+    void
+    test_templateManifest()
+    throws Exception
+    {
+        InputStream in = null;
+        try {
+            String prefix = "Manifest-Version: 1.0\n";
+            String ori = ": $${SRCOPTIMIZE|'minified'}\n";
+            String expected = "original";
+            String testAttrCopy = prefix + ATTR_COPY + ori;
+            String testAttrName = prefix + ATTR_NAME + ori;
+
+            in = new ByteArrayInputStream( testAttrCopy.getBytes() );
+            template.templateManifest( in, models );
+            assertEquals(expected, template.getAttribute( ATTR_COPY ) );
+            tryClose( in );
+
+            in = new ByteArrayInputStream( testAttrName.getBytes() );
+            template.templateManifest( in, models );
+            assertEquals(expected, template.getAttribute( ATTR_NAME ) );
+        } finally {
+            tryClose( in );
         }
+        
+    }
+
+    /**
+     * Test {@link DirectoryTemplate#loadManifestRecursively(File, IModelProvider)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see DirectoryTemplate#loadManifestRecursively(File, IModelProvider))
+     */
+    @Test
+    public
+    void
+    test_loadManifestRecursively()
+    throws Exception
+    {
+        template.loadManifestRecursively( rootDFileMock, models );
+        Collection<String> keys = template.getAttributeKeys();
+        assertFalse( keys.isEmpty() );
+        
+        assertEquals( "0.1.1_Theme/original/*", template.getAttribute( ATTR_COPY ) );
+    }
+
+    /**
+     * Test {@link DirectoryTemplate#loadBundleRecursively(File)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see DirectoryTemplate#loadBundleRecursively(File))
+     */
+    @Test
+    public
+    void
+    test_loadBundleRecursively()
+    throws Exception
+    {
+        // SRCOPTIMIZE = Select Source Type [minified, original] ?
+        String result = template.getMessage( "SRCOPTIMIZE" );
+        assertNull( result );
+        
+        template.loadBundleRecursively( rootDFileMock );
+        result = template.getMessage( "SRCOPTIMIZE" );
+        assertNotNull( result );
+        assertEquals( "Select Source Type [minified, original] ?", result );
+    }
+
+    /**
+     * Test {@link DirectoryTemplate#addTemplateRecursively(File, TemplateManager, org.tizen.common.file.Filter, org.tizen.common.file.Filter)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see DirectoryTemplate#addTemplateRecursively(File, TemplateManager, org.tizen.common.file.Filter, org.tizen.common.file.Filter)
+     */
+    @Test
+    public
+    void
+    test_addTemplateRecursively()
+    throws Exception
+    {
+        TemplateManager templates1 = new TemplateManager();
+        TemplateManager templates2 = new TemplateManager();
+
+        File mockFile = mock( File.class );
+        when( mockFile.listFiles() ).thenReturn( new File [] {} );
+        
+        Filter mockFilter = mock( Filter.class );
+        when( mockFilter.accept( anyString(), anyString()) ).thenReturn( true );
+        
+        Filter mockFilter2 = mock( Filter.class );
+        when( mockFilter2.accept( anyString(), anyString()) ).thenReturn( false );
+        
+        template.addTemplateRecursively( mockFile, templates1, mockFilter, mockFilter);
+        assertTrue( template.copyNames.size() == 0 );
+        assertTrue( template.templateNames.size() == 0 );
+
+        template.addTemplateRecursively( rootDFileMock, templates1, mockFilter, mockFilter);
+        assertTrue( template.copyNames.size() > 0 );
+        assertTrue( template.templateNames.size() == 0 );
+
+        template.addTemplateRecursively( rootDFileMock, templates2, mockFilter, mockFilter2);
+        template.copyNames.clear();
+        assertTrue( template.copyNames.size() == 0 );
+        assertTrue( template.templateNames.size() > 0 );
     }
 
     /**
@@ -105,8 +237,7 @@ extends AbstractTestCase
     test_preload()
     throws Exception
     {
-        File rootFile = new File( path );
-        template.preload( rootFile, models );
+        template.preload( rootDFileMock, models );
 
         String expected = "0.1.1_Theme/original/*";
         String actual = template.getAttribute( TemplateConstants.ATTR_COPY );
@@ -126,9 +257,25 @@ extends AbstractTestCase
     test_load()
     throws Exception
     {
-        File rootFile = new File( path );
-        template.preload( rootFile, models );
-        template.load( rootFile );
+        // test current state
+        assertNull( template.engine );
+        assertTrue( template.attrs.size() == 0 );
+        assertNull( template.mapper );
+
+        // just try load
+        template.load( rootDFileMock );
+        assertNotNull( template.engine );
+        assertTrue( template.attrs.size() == 0 ); // correct operation because of not called preload method
+        assertNotNull( template.mapper );
+        
+        // success addTempalte
+        template.engine = null;
+        template.mapper = null;
+        template.preload( rootDFileMock, models );
+        template.load( rootDFileMock );
+        assertNotNull( template.engine );
+        assertTrue( template.attrs.size() > 0 );
+        assertNotNull( template.mapper );
     }
 
     /**
@@ -138,12 +285,97 @@ extends AbstractTestCase
     @Before
     public void setUp() throws Exception
     {
-        // test template root path setting
-        File file = new File( path );
-        this.path = file.getAbsolutePath();
+        super.setUp();
+
+        /*
+         * test directory template mock setting.
+         * mock directory structure, (d) directory, (f) file
+         * - 0.1.1 (d)
+         *    - 0.1.1_Theme (d)
+         *      - minified (d)
+         *        - min.js (f)
+         *      - original (d)
+         *        - ori.js (f)
+         *    - META-INF (d)
+         *      - MANIFEST.MF (f)
+         *      - messages.properties (f)
+         */
+        // Theme mock
+        File minJsFMock = mock ( File.class );
+        File themeMinDMock = mock ( File.class );
+        when( minJsFMock.isDirectory() ).thenReturn( false ); // theme - minified - min.js
+        when( minJsFMock.getAbsolutePath() ).thenReturn( "0.1.1/0.1.1_Theme/minified/min.js" );
+        when( minJsFMock.getParentFile() ).thenReturn( themeMinDMock );
+        when( minJsFMock.getName() ).thenReturn( "min.js" );
+        when( themeMinDMock.isDirectory() ).thenReturn( true ); // theme - minified
+        when( themeMinDMock.listFiles() ).thenReturn( new File [] { minJsFMock } );
+        when( themeMinDMock.getName() ).thenReturn( "minified" );
+        
+        File oriJsFMock = mock ( File.class );
+        File themeOriDMock = mock ( File.class );
+        when( oriJsFMock.isDirectory() ).thenReturn( false ); // theme - original - ori.js
+        when( oriJsFMock.getAbsolutePath() ).thenReturn( "0.1.1/0.1.1_Theme/original/ori.js" );
+        when( oriJsFMock.getParentFile() ).thenReturn( themeOriDMock );
+        when( oriJsFMock.getName() ).thenReturn( "ori.js" );
+        when( themeOriDMock.isDirectory() ).thenReturn( true ); // theme - original
+        when( themeOriDMock.listFiles() ).thenReturn( new File [] { oriJsFMock } );
+        when( themeOriDMock.getName() ).thenReturn( "original" );
+        
+        File themeDMock = mock ( File.class );
+        when( themeDMock.isDirectory() ).thenReturn( true );
+        when( themeDMock.listFiles() ).thenReturn( new File [] { themeMinDMock, themeOriDMock } );
+        when( themeDMock.getName() ).thenReturn( "0.1.1_Theme" );
+
+        // META-INF mock
+        File metaDMock = mock ( File.class );
+        File manifestFMock = mock ( File.class );
+        when( manifestFMock.isDirectory() ).thenReturn( false );
+        when( manifestFMock.getAbsolutePath() ).thenReturn( "0.1.1/META-INF/MANIFEST.MF" );
+        when( manifestFMock.getParentFile() ).thenReturn( metaDMock );
+        when( manifestFMock.getName() ).thenReturn( "MANIFEST.MF" );
 
-        // test template class setting
-        this.template = new DirectoryTemplate( path );
+        File messagesFMock = mock ( File.class );
+        when( messagesFMock.isDirectory() ).thenReturn( false );
+        when( messagesFMock.getAbsolutePath() ).thenReturn( "0.1.1/META-INF/messages.properties" );
+        when( messagesFMock.getParentFile() ).thenReturn( metaDMock );
+        when( messagesFMock.getName() ).thenReturn( "messages.properties" );
+
+        when( metaDMock.isDirectory() ).thenReturn( true );
+        when( metaDMock.listFiles() ).thenReturn( new File [] { manifestFMock, messagesFMock } );
+        when( metaDMock.getName() ).thenReturn( "META-INF" );
+
+        // root mock
+        rootDFileMock = mock( File.class );
+        when( rootDFileMock.isDirectory() ).thenReturn( true );
+        when( rootDFileMock.getAbsolutePath() ).thenReturn( "0.1.1" );
+        when( rootDFileMock.listFiles() ).thenReturn( new File [] { themeDMock, metaDMock} );
+
+        // test mock template class setting
+        this.template = new DirectoryTemplate( rootDFileMock ) {
+            @Override
+            protected InputStream getInputStream(File file) throws IOException {
+                // input stream mock
+                String name = file.getName();
+                URL u = null;
+                if ( "MANIFEST.MF".equals( name ) )
+                {
+                    u = new URL( "cp:///web-ui-fw/0.1.1/META-INF/MANIFEST.MF" );
+                }
+                else if ( "messages.properties".equals( name) )
+                {
+                    u = new URL( "cp:///web-ui-fw/0.1.1/META-INF/messages.properties" );
+                }
+                else if ( "min.js".equals( name) )
+                {
+                    u = new URL( "cp:///web-ui-fw/0.1.1/0.1.1_Theme/minified/min.js" );
+                }
+                else if ( "ori.js".equals( name) )
+                {
+                    u = new URL( "cp:///web-ui-fw/0.1.1/0.1.1_Theme/original/ori.js" );
+                }
+                return u.openStream();
+            }
+        };
 
         // test template model setting 
         final VirtualFileHandler fileHandler = new VirtualFileHandler();
@@ -161,6 +393,7 @@ extends AbstractTestCase
     @After
     public void tearDown()
     {
+        super.tearDown();
     }
 
     /**