[Title] Added DirectorySearchableRealm, TizenWebUIFWRealm. Added UNIT TEST code
authorgyeongseok.seo <gyeongseok.seo@samsung.com>
Mon, 20 May 2013 10:29:19 +0000 (19:29 +0900)
committergyeongseok.seo <gyeongseok.seo@samsung.com>
Mon, 20 May 2013 10:29:19 +0000 (19:29 +0900)
[Desc.]
[Issue]

Change-Id: I61fe19bbc279be1fb936c001e660362d255945f1

org.tizen.common.verrari.realm/src/org/tizen/common/verrari/realm/CommonRealm.java
org.tizen.common.verrari.realm/src/org/tizen/common/verrari/realm/DirectorySearchableRealm.java [new file with mode: 0644]
org.tizen.common.verrari.realm/src/org/tizen/common/verrari/realm/StandardRealm.java
org.tizen.common.verrari.realm/src/org/tizen/common/verrari/realm/TizenWebUIFWRealm.java [new file with mode: 0644]
org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/DirectoryTemplate.java
org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/config.json
org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/realm/DirectoryRealmTest.java
org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/realm/TizenWebUIFWRealmTest.java [new file with mode: 0644]
org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/FileMock.java

index 4e577f1..4109197 100644 (file)
@@ -123,6 +123,14 @@ implements Realm, AttributeConfiguable
         }
     }
     
+    public void setAttributes(String key, String value) {
+        if ( null == this.attributes )
+        {
+            this.attributes = new HashMap<String, String>();
+        }
+        this.attributes.put(key, value);
+    }
+    
     /**
      * make template query
      * 
diff --git a/org.tizen.common.verrari.realm/src/org/tizen/common/verrari/realm/DirectorySearchableRealm.java b/org.tizen.common.verrari.realm/src/org/tizen/common/verrari/realm/DirectorySearchableRealm.java
new file mode 100644 (file)
index 0000000..a24e0d7
--- /dev/null
@@ -0,0 +1,174 @@
+/*
+ *  Verrari - Realm
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: 
+ * GyeongSeok Seo <gyeongseok.seo@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.common.verrari.realm;
+
+import static org.tizen.common.util.IOUtil.tryClose;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Collection;
+import java.util.HashSet;
+
+import org.tizen.common.verrari.AttributeContainer;
+import org.tizen.common.verrari.Condition;
+import org.tizen.common.verrari.Realm;
+import org.tizen.common.verrari.SearchableRealm;
+import org.tizen.common.verrari.Template;
+import org.tizen.common.verrari.Condition.Operation;
+import org.tizen.common.verrari.TemplateException;
+import org.tizen.common.verrari.template.DirectoryTemplate;
+
+/**
+ * <p>
+ * DirectorySearchableRealm
+ * 
+ * Built-in {@link Realm} to provide RI
+ * 
+ * </p>
+ * 
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ * 
+ * @see SearchableRealm
+ */
+public class
+DirectorySearchableRealm
+extends DirectoryRealm
+implements SearchableRealm
+{
+    /* (non-Javadoc)
+     * @see org.tizen.common.verrari.SearchableRealm#search(org.tizen.common.verrari.Condition)
+     */
+    @Override
+    public Collection<AttributeContainer>
+    search(
+        final Condition condition
+    )
+    throws IOException
+    {
+        final HashSet<AttributeContainer> ret = new HashSet<AttributeContainer>();
+        
+        String type = getAttribute( RealmConstants.ATTR_SEARCH_TYPE );
+        String urlStr = getAttribute( RealmConstants.ATTR_SEARCH_URL );
+        logger.debug( "Type :{}, URL :{}", type, urlStr );
+        if ( RealmConstants.SEARCH_TYPE_DIR.equals( type ) )
+        {
+            URL url;
+            try
+            {
+                url = new URL( urlStr );
+            }
+            catch (
+                final MalformedURLException e
+            )
+            {
+                throw new IllegalArgumentException( "Invalid URL :" + urlStr, e );
+            }
+            if ( !"file".equals( url.getProtocol() ) ) 
+            {
+                throw new UnsupportedOperationException( "Check search url. It must start with \"file://\"" );
+            }
+            
+            final Operation op = condition.getOperation();
+            final String keyword = (String) condition.getRight();
+            
+            final String path = url.getFile();
+            final File dir = new File( path );
+            logger.debug( "Path :{}", path );
+            final File[] files = dir.listFiles();
+            for ( final File file : files )
+            {
+                // directory check
+                if ( !file.isDirectory() ) {
+                    continue ;
+                }
+                logger.debug( "File :{}", file.getCanonicalPath() );
+
+                final Template template = getTemplate( file );
+                if ( template instanceof DirectoryTemplate ) {
+                    try {
+                        ((DirectoryTemplate) template).loadManifestRecursively( file );
+                    } catch (TemplateException e) {
+                        logger.error( e.toString() );
+                        continue ;
+                    }
+                }
+                final Collection<String> keys = template.getAttributeKeys();
+                switch ( op )
+                {
+                case Contains: {
+                    try
+                    {
+                        for ( final String key : keys )
+                        {
+                            final String value = template.getAttribute( key );
+                            if ( value.contains( keyword ) )
+                            {
+                                ret.add( template );
+                                break;
+                            }
+                        }
+                    }
+                    finally
+                    {
+                        tryClose( file );
+                    }
+                    
+                    break;
+                }
+                case CaseInsensitiveContains: {
+                    try
+                    {
+                        for ( final String key : keys )
+                        {
+                            final String value = template.getAttribute( key );
+                            if ( value.toLowerCase().contains( keyword.toLowerCase() ) )
+                            {
+                                ret.add( template );
+                                break;
+                            }
+                        }
+                    }
+                    finally
+                    {
+                        tryClose( file );
+                    }
+                    
+                    break;
+                }
+
+                default:
+                    break;
+                }
+            }
+            
+        }
+        
+        return ret;
+    }
+}
index 1c44821..5d25047 100644 (file)
@@ -49,6 +49,6 @@ extends CompositeRealm
     public StandardRealm() {
         super();
         addRealm( new JarSearchableRealm() );
-        addRealm( new DirectoryRealm() );
+        addRealm( new TizenWebUIFWRealm() );
     }
 }
diff --git a/org.tizen.common.verrari.realm/src/org/tizen/common/verrari/realm/TizenWebUIFWRealm.java b/org.tizen.common.verrari.realm/src/org/tizen/common/verrari/realm/TizenWebUIFWRealm.java
new file mode 100644 (file)
index 0000000..a4cf0c0
--- /dev/null
@@ -0,0 +1,166 @@
+/*
+ *  Verrari - Realm
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: 
+ * GyeongSeok Seo <gyeongseok.seo@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.common.verrari.realm;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.tizen.common.core.application.InstallPathConfig;
+import org.tizen.common.util.FileUtil;
+import org.tizen.common.util.StringUtil;
+import org.tizen.common.verrari.AttributeContainer;
+import org.tizen.common.verrari.Condition;
+import org.tizen.common.verrari.Realm;
+import org.tizen.common.verrari.SearchableRealm;
+import org.tizen.common.verrari.Template;
+
+/**
+ * <p>
+ * TizenWebUIFWRealm
+ * 
+ * Built-in {@link Realm} to provide RI
+ * 
+ * </p>
+ * 
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ * 
+ * @see SearchableRealm
+ */
+public class
+TizenWebUIFWRealm
+extends DirectorySearchableRealm
+{
+    public String[] getExistTemplates() {
+        File root = getTizenWebUIFWRealmFile();
+
+        ArrayList<String> array = new ArrayList<String>();
+        for ( File dir : root.listFiles() ) {
+            if ( dir.isDirectory() ) {
+                boolean isTemplate = false;
+                for ( File list : dir.listFiles() ) {
+                    if ( list.isDirectory() && "META-INF".equals( list.getName() ) ) {
+                        for ( File sublist : list.listFiles() ) {
+                            if ( !sublist.isDirectory() && "MANIFEST.MF".equals( sublist.getName() ) ) {
+                                isTemplate = true;
+                                break;
+                            }
+                        }
+                    }
+                }
+
+                if ( isTemplate ) {
+                    array.add( dir.getName() );
+                }
+            }
+        }
+        return array.toArray( new String[]{} );
+    }
+
+    @Override
+    public Template getTemplate(String id) throws IOException {
+        String realmPath = getTizenWebUIFWRealmPath();
+        if ( !id.contains( "web-ui-fw") || !isExistRealm( realmPath ) ) {
+            return super.getTemplate(id);
+        }
+        
+        if ( id.contains( "web-ui-fw" ) ) {
+            // default - latest version
+            String templates [] = getExistTemplates();
+            if ( templates.length == 0 ) {
+                return null;
+            } else if ( templates.length == 1 ) {
+                return getTemplate( new File( realmPath + File.separator + templates[0] ) );
+            } else if ( templates.length > 1 ) {
+                String num = StringUtil.getOnlyNumerics( id );
+                String lastest = null;
+                for( String template : templates ) {
+                    try {
+                        String temp = StringUtil.getOnlyNumerics( template );
+                        // using user input version
+                        if ( num.equals( temp ) ) {
+                            return getTemplate( new File( realmPath + File.separator + template ) );
+                        }
+
+                        // latest version
+                        Double current = Double.parseDouble( temp );
+                        if ( StringUtil.isEmpty( lastest ) ) {
+                            lastest = template;
+                            continue;
+                        } else {
+                            temp = StringUtil.getOnlyNumerics( lastest );
+                            Double big = Double.parseDouble(temp);
+                            if ( big < current ) {
+                                lastest = template;
+                            }
+                        }
+                    } catch ( NumberFormatException e ) {
+                        logger.error( "Template["+template+"] not supported version style {}", e.toString() );
+                        continue;
+                    }
+                }
+
+                if ( !StringUtil.isEmpty( lastest ) ) {
+                    return getTemplate( new File( realmPath + File.separator + lastest ) );
+                }
+            }
+        }
+        return super.getTemplate(id);
+    }
+
+    /* (non-Javadoc)
+     * @see org.tizen.common.verrari.realm.DirectorySearchableRealm#search(org.tizen.common.verrari.Condition)
+     */
+    @Override
+    public Collection<AttributeContainer> search(Condition condition)
+            throws IOException {
+        // Tizen Web UI FW Realm is directory and fixed path
+        String type = RealmConstants.SEARCH_TYPE_DIR;
+        String urlStr = getTizenWebUIFWRealmPath();
+        logger.debug( "Type :{}, URL :{}", type, urlStr );
+
+        setAttributes( RealmConstants.ATTR_SEARCH_TYPE, type );
+        setAttributes( RealmConstants.ATTR_SEARCH_URL, urlStr );
+
+        return super.search( condition );
+    }
+
+    public boolean isExistRealm( String realmPath ) {
+        return FileUtil.isExist( realmPath );
+    }
+
+    public File getTizenWebUIFWRealmFile() {
+        return new File( getTizenWebUIFWRealmPath() );
+    }
+
+    public String getTizenWebUIFWRealmPath() {
+        String platformPath = InstallPathConfig.getPlatformVersionPath();
+        return platformPath + File.separator + "web-ui-fw";
+    }
+}
index 9f9d3a1..28a1a65 100644 (file)
@@ -248,6 +248,41 @@ extends CommonTemplate
     }
 
     /**
+     * directory template's MANIFES.MF loading.
+     * It working on recursive
+     * 
+     * @param root
+     * @param models
+     * @throws IOException
+     * @throws TemplateException
+     */
+    public
+    void
+    loadManifestRecursively( File root ) throws IOException, TemplateException {
+        File [] lists = root.listFiles();
+
+        for ( File file : lists ) {
+            if ( file.isDirectory() ) {
+                loadManifestRecursively( file );
+                continue;
+            }
+            String name = file.getName();
+
+            if ( null == file.getParentFile() &&
+                    !( "META-INF".equals( file.getParentFile().getName() ) ) ) {
+                continue;
+            }
+
+            if ( "MANIFEST.MF".equals( name ) )
+            {
+                InputStream fin = getInputStream( file );
+                loadAttribute( fin );
+                tryClose( fin );
+            }
+        }
+    }
+
+    /**
      * copy file and template file classification
      * 
      * @param root
index 328dc71..14406cf 100755 (executable)
@@ -5,7 +5,9 @@
         {\r
             "id":"tizen-jar-realm",\r
             "template-query-url": "cp:///org/tizen/common/verrari/template/<{id}>",\r
-            "category-query-url": "cp:///org/tizen/common/verrari/category/<{id}>"\r
+            "category-query-url": "cp:///org/tizen/common/verrari/category/<{id}>",\r
+            "search-query-type": "directory",\r
+            "search-query-url": "file:///$${CLI_HOME}/realm/template"\r
         },\r
         {\r
             "id":"tizen-directory-realm",\r
index 79963e7..39132b3 100644 (file)
@@ -61,7 +61,7 @@ extends AbstractTestCase
      * @see DirectoryRealm#getTemplate(String)
      */
     @SuppressWarnings("unchecked")
-       @Test
+    @Test
     public void
     test_getTemplate()
     throws Exception
diff --git a/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/realm/TizenWebUIFWRealmTest.java b/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/realm/TizenWebUIFWRealmTest.java
new file mode 100644 (file)
index 0000000..b8349e6
--- /dev/null
@@ -0,0 +1,122 @@
+/*
+ *  Verrari - Realm
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: 
+ * GyeongSeok Seo <gyeongseok.seo@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+package org.tizen.common.verrari.realm;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.junit.Test;
+import org.tizen.common.verrari.AbstractTestCase;
+import org.tizen.common.verrari.Template;
+import org.tizen.common.verrari.template.FileMock;
+
+/**
+ * <p>
+ * TizenWebUIFWRealmTest
+ * 
+ * Built-in {@link TizenWebUIFWRealm} to provide RI
+ * 
+ * </p>
+ * 
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ */
+public class
+TizenWebUIFWRealmTest
+extends AbstractTestCase
+{
+    /**
+     * Test {@link TizenWebUIFWRealm#getExistTemplates()}
+     *
+     * @throws Exception in case of failure in test
+     *
+     * @see TizenWebUIFWRealm#getExistTemplates()
+     */
+    @Test
+    public void
+    test_getExistTemplates()
+    throws Exception
+    {
+        final TizenWebUIFWRealm realm = new TizenWebUIFWRealm() {
+            @Override
+            public File getTizenWebUIFWRealmFile() {
+                File rootDFileMock = mock( File.class );
+                when( rootDFileMock.isDirectory() ).thenReturn( true );
+                when( rootDFileMock.getAbsolutePath() ).thenReturn( "web-ui-fw" );
+                
+                File file = FileMock.getFileMock();
+                when( rootDFileMock.listFiles() ).thenReturn( new File [] { file } );
+                return rootDFileMock;
+            }
+        };
+        
+        String [] template = realm.getExistTemplates();
+        assertNotNull( template );
+    }
+    
+    /**
+     * Test {@link TizenWebUIFWRealm#getTemplate(String)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see TizenWebUIFWRealm#getTemplate(String)
+     */
+    @Test
+    public void
+    test_getTemplate()
+    throws Exception
+    {
+        final TizenWebUIFWRealm realm = new TizenWebUIFWRealm() {
+            @Override
+            public Template getTemplate(File file) throws IOException {
+                assertEquals( "/web-ui-fw/0.2.24", file.getPath() );
+                return null;
+            }
+
+            @Override
+            public String[] getExistTemplates() {
+                return new String[] { "0.1.1", "0.1.2", "0.2.24" };
+            }
+
+            @Override
+            public String getTizenWebUIFWRealmPath() {
+                return "/web-ui-fw";
+            }
+
+            @Override
+            public boolean isExistRealm( String realmPath ) {
+                return true;
+            }
+        };
+        
+        realm.getTemplate( "web-ui-fw" );
+        realm.getTemplate( "web-ui-fw (0.2.24)" );
+    }
+}
index 0765dd5..e07bafc 100644 (file)
@@ -27,8 +27,10 @@ package org.tizen.common.verrari.template;
 
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
+import static org.mockito.Matchers.any;
 
 import java.io.File;
+import java.io.FilenameFilter;
 
 /**
  * FileMock.
@@ -111,8 +113,10 @@ public class FileMock {
         // root mock
         File rootDFileMock = mock( File.class );
         when( rootDFileMock.isDirectory() ).thenReturn( true );
+        when( rootDFileMock.getName() ).thenReturn( "0.1.1" );
         when( rootDFileMock.getAbsolutePath() ).thenReturn( "0.1.1" );
         when( rootDFileMock.listFiles() ).thenReturn( new File [] { themeDMock, metaDMock } );
+        when( rootDFileMock.list( (FilenameFilter)any() ) ).thenCallRealMethod();
 
         return rootDFileMock;
     }