[Title] common-eplugin: added common project validation util
authorJihoon Song <jihoon80.song@samsung.com>
Fri, 1 Feb 2013 05:14:04 +0000 (14:14 +0900)
committerJihoon Song <jihoon80.song@samsung.com>
Wed, 6 Feb 2013 08:42:15 +0000 (17:42 +0900)
[Desc.]
[Issue]

Change-Id: Ie8d6f1bbea05f373d4c12d039bb69339a2a05581

12 files changed:
org.tizen.common.feature/feature.xml
org.tizen.common.project/.classpath [new file with mode: 0644]
org.tizen.common.project/.project [new file with mode: 0644]
org.tizen.common.project/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
org.tizen.common.project/META-INF/MANIFEST.MF [new file with mode: 0644]
org.tizen.common.project/build.properties [new file with mode: 0644]
org.tizen.common.project/src/org/tizen/common/project/Activator.java [new file with mode: 0644]
org.tizen.common.project/src/org/tizen/common/project/CommonProjectConstants.java [new file with mode: 0644]
org.tizen.common.project/src/org/tizen/common/project/util/Messages.java [new file with mode: 0644]
org.tizen.common.project/src/org/tizen/common/project/util/ProjectValidationUtil.java [new file with mode: 0644]
org.tizen.common.project/src/org/tizen/common/project/util/messages.properties [new file with mode: 0644]
org.tizen.common.project/test/src/org/tizen/common/project/util/ProjectValidationUtilTest.java [new file with mode: 0644]

index 535edcc..93a769d 100755 (executable)
@@ -1,20 +1,20 @@
-<?xml version="1.0" encoding="UTF-8"?>\r
-<feature\r
-      id="org.tizen.common.feature"\r
-      label="Tizen Common"\r
-      version="1.0.0.qualifier"\r
-      plugin="org.tizen.common"\r
-      license-feature="org.tizen.base.feature">\r
-\r
-   <description>\r
-      Common library for Tizen SDK.\r
-   </description>\r
-\r
-   <copyright>\r
-      Copyright (c)\r
-   </copyright>\r
-\r
-   <license url="license.html">\r
+<?xml version="1.0" encoding="UTF-8"?>
+<feature
+      id="org.tizen.common.feature"
+      label="Tizen Common"
+      version="1.0.0.qualifier"
+      plugin="org.tizen.common"
+      license-feature="org.tizen.base.feature">
+
+   <description>
+      Common library for Tizen SDK.
+   </description>
+
+   <copyright>
+      Copyright (c)
+   </copyright>
+
+   <license url="license.html">
       Tizen SDK
 
 Tizen SDK contains software portions licensed under various open source licenses as well as proprietary components.  All open source software portions (&quot;Open Source Software&quot;) are licensed under the open source licenses that accompany such Open Source Software.
@@ -25,56 +25,63 @@ Except for the Open Source Software contained in Tizen SDK, all other software p
 
 You may access and download Tizen SDK Open Source Software at: http://developer.tizen.org/download/tizenopensdk.tar.gz
 
-BY CLICKING THE &quot;I AGREE&quot; BUTTON OR BY USING ANY PART OF TIZEN SDK, YOU AGREE (ON BEHALF OF YOURSELF AND/OR YOUR COMPANY) TO THE OPEN SOURCE SOFTWARE LICENSE TERMS AND THE TIZEN SOFTWARE DEVELOPMENT KIT LICENSE AGREEMENT. IF YOU DO NOT AGREE WITH THE OPEN SOURCE SOFTWARE LICENSE TERMS OR THE TIZEN SOFTWARE DEVELOPMENT KIT LICENSE AGREEMENT, YOU MAY NOT DOWNLOAD OR USE TIZEN SDK.\r
-   </license>\r
-\r
-   <plugin\r
-         id="org.tizen.common"\r
-         download-size="0"\r
-         install-size="0"\r
-         version="0.0.0"\r
-         unpack="false"/>\r
-\r
-   <plugin\r
-         id="org.tizen.common.connection"\r
-         download-size="0"\r
-         install-size="0"\r
-         version="0.0.0"\r
-         unpack="false"/>\r
-\r
-   <plugin\r
-         id="org.tizen.common.externals"\r
-         download-size="0"\r
-         install-size="0"\r
-         version="0.0.0"\r
-         unpack="false"/>\r
-\r
-   <plugin\r
-         id="org.tizen.common.verrari"\r
-         download-size="0"\r
-         install-size="0"\r
-         version="0.0.0"\r
-         unpack="false"/>\r
-\r
-   <plugin\r
-         id="org.tizen.common.sign"\r
-         download-size="0"\r
-         install-size="0"\r
-         version="0.0.0"\r
-         unpack="false"/>\r
-\r
-   <plugin\r
-         id="org.tizen.common.sdblib"\r
-         download-size="0"\r
-         install-size="0"\r
-         version="0.0.0"\r
-         unpack="false"/>\r
-\r
-   <plugin\r
-         id="org.tizen.common.verrari.realm"\r
-         download-size="0"\r
-         install-size="0"\r
-         version="0.0.0"\r
-         unpack="false"/>\r
-\r
-</feature>\r
+BY CLICKING THE &quot;I AGREE&quot; BUTTON OR BY USING ANY PART OF TIZEN SDK, YOU AGREE (ON BEHALF OF YOURSELF AND/OR YOUR COMPANY) TO THE OPEN SOURCE SOFTWARE LICENSE TERMS AND THE TIZEN SOFTWARE DEVELOPMENT KIT LICENSE AGREEMENT. IF YOU DO NOT AGREE WITH THE OPEN SOURCE SOFTWARE LICENSE TERMS OR THE TIZEN SOFTWARE DEVELOPMENT KIT LICENSE AGREEMENT, YOU MAY NOT DOWNLOAD OR USE TIZEN SDK.
+   </license>
+
+   <plugin
+         id="org.tizen.common"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.tizen.common.connection"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.tizen.common.externals"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.tizen.common.verrari"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.tizen.common.sign"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.tizen.common.sdblib"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.tizen.common.verrari.realm"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+   <plugin
+         id="org.tizen.common.project"
+         download-size="0"
+         install-size="0"
+         version="0.0.0"
+         unpack="false"/>
+
+</feature>
diff --git a/org.tizen.common.project/.classpath b/org.tizen.common.project/.classpath
new file mode 100644 (file)
index 0000000..9ef628e
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
+       <classpathentry kind="src" path="src"/>
+       <classpathentry kind="src" path="test/src"/>
+       <classpathentry kind="output" path="bin"/>
+</classpath>
diff --git a/org.tizen.common.project/.project b/org.tizen.common.project/.project
new file mode 100644 (file)
index 0000000..c9ee476
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+       <name>org.tizen.common.project</name>
+       <comment></comment>
+       <projects>
+       </projects>
+       <buildSpec>
+               <buildCommand>
+                       <name>org.eclipse.jdt.core.javabuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.ManifestBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.SchemaBuilder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
+       </buildSpec>
+       <natures>
+               <nature>org.eclipse.pde.PluginNature</nature>
+               <nature>org.eclipse.jdt.core.javanature</nature>
+       </natures>
+</projectDescription>
diff --git a/org.tizen.common.project/.settings/org.eclipse.jdt.core.prefs b/org.tizen.common.project/.settings/org.eclipse.jdt.core.prefs
new file mode 100644 (file)
index 0000000..e90ce96
--- /dev/null
@@ -0,0 +1,8 @@
+#Thu Jan 31 17:54:08 KST 2013
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
diff --git a/org.tizen.common.project/META-INF/MANIFEST.MF b/org.tizen.common.project/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..4568ad9
--- /dev/null
@@ -0,0 +1,16 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Tizen SDK Common Project Tools
+Bundle-SymbolicName: org.tizen.common.project
+Bundle-Version: 1.0.0.qualifier
+Bundle-Activator: org.tizen.common.project.Activator
+Bundle-Vendor: Tizen SDK
+Require-Bundle: org.eclipse.ui,
+ org.eclipse.core.runtime,
+ org.tizen.common
+Bundle-RequiredExecutionEnvironment: JavaSE-1.6
+Bundle-ActivationPolicy: lazy
+Import-Package: org.eclipse.core.filesystem,
+ org.eclipse.core.resources
+Export-Package: org.tizen.common.project,
+ org.tizen.common.project.util
diff --git a/org.tizen.common.project/build.properties b/org.tizen.common.project/build.properties
new file mode 100644 (file)
index 0000000..34d2e4d
--- /dev/null
@@ -0,0 +1,4 @@
+source.. = src/
+output.. = bin/
+bin.includes = META-INF/,\
+               .
diff --git a/org.tizen.common.project/src/org/tizen/common/project/Activator.java b/org.tizen.common.project/src/org/tizen/common/project/Activator.java
new file mode 100644 (file)
index 0000000..e025216
--- /dev/null
@@ -0,0 +1,50 @@
+package org.tizen.common.project;
+
+import org.eclipse.ui.plugin.AbstractUIPlugin;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The activator class controls the plug-in life cycle
+ */
+public class Activator extends AbstractUIPlugin {
+
+       // The plug-in ID
+       public static final String PLUGIN_ID = "org.tizen.common.project"; //$NON-NLS-1$
+
+       // The shared instance
+       private static Activator plugin;
+       
+       /**
+        * The constructor
+        */
+       public Activator() {
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
+        */
+       public void start(BundleContext context) throws Exception {
+               super.start(context);
+               plugin = this;
+       }
+
+       /*
+        * (non-Javadoc)
+        * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
+        */
+       public void stop(BundleContext context) throws Exception {
+               plugin = null;
+               super.stop(context);
+       }
+
+       /**
+        * Returns the shared instance
+        *
+        * @return the shared instance
+        */
+       public static Activator getDefault() {
+               return plugin;
+       }
+
+}
diff --git a/org.tizen.common.project/src/org/tizen/common/project/CommonProjectConstants.java b/org.tizen.common.project/src/org/tizen/common/project/CommonProjectConstants.java
new file mode 100644 (file)
index 0000000..33420c9
--- /dev/null
@@ -0,0 +1,22 @@
+package org.tizen.common.project;
+
+public final class CommonProjectConstants {
+    
+    public final static int PROJECT_NAME_MIN_LENGTH = 3;
+    public final static int PROJECT_NAME_MAX_LENGTH = 50;
+    public final static String PROJECT_NAME_PATTERN;
+    
+    public final static char[] INVALID_RESOURCE_CHARACTERS;
+    
+    static {
+        PROJECT_NAME_PATTERN = "^[a-zA-Z][a-zA-Z0-9-]{" //$NON-NLS-1$
+                + ( PROJECT_NAME_MIN_LENGTH - 1 ) + "," //$NON-NLS-1$
+                + ( PROJECT_NAME_MAX_LENGTH - 1 ) + "}$"; //$NON-NLS-1$
+            // Regular Expression Note : 
+            //   A first character must have case-insensitive alphabet.
+            //   Next characters have case-insensitive alphabet or numeric or hyphen
+            //   Total character length scope is a minimum of 3 and maximum of 50 characters.
+        
+        INVALID_RESOURCE_CHARACTERS = new char[] { '`', '#', '$', ':' };
+    }
+}
diff --git a/org.tizen.common.project/src/org/tizen/common/project/util/Messages.java b/org.tizen.common.project/src/org/tizen/common/project/util/Messages.java
new file mode 100644 (file)
index 0000000..19f4038
--- /dev/null
@@ -0,0 +1,18 @@
+package org.tizen.common.project.util;
+
+import org.eclipse.osgi.util.NLS;
+
+public class Messages extends NLS {
+    private static final String BUNDLE_NAME = "org.tizen.common.project.util.messages"; //$NON-NLS-1$
+    public static String ValidationUtil_AlreadyExistProject;
+    public static String ValidationUtil_InvalidProjectName;
+    public static String ValidationUtil_InvalidProjectNameLength;
+    public static String ValidationUtil_InvalidSegment;
+    static {
+        // initialize resource bundle
+        NLS.initializeMessages(BUNDLE_NAME, Messages.class);
+    }
+
+    private Messages() {
+    }
+}
diff --git a/org.tizen.common.project/src/org/tizen/common/project/util/ProjectValidationUtil.java b/org.tizen.common.project/src/org/tizen/common/project/util/ProjectValidationUtil.java
new file mode 100644 (file)
index 0000000..10fc4fb
--- /dev/null
@@ -0,0 +1,140 @@
+/*
+ * Common
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * NamKoong Ho <ho.namkoong@samsung.com> 
+ * Jihoon Song <jihoon80.song@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ * Kangho Kim <kh5325.kim@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.project.util;
+
+import java.io.File;
+import java.net.URI;
+
+import org.eclipse.core.filesystem.EFS;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.osgi.util.NLS;
+import org.tizen.common.project.CommonProjectConstants;
+import org.tizen.common.util.Assert;
+import org.tizen.common.util.StringUtil;
+
+/**
+ * Project Validation Util
+ * 
+ * Helper related to the web project validation 
+ * 
+ * @author Jihoon Song{@literal <jihoon80.song@samsung.com>} (S-Core)
+ */
+public abstract class ProjectValidationUtil {
+
+    /**
+     * Validate project name using name pattern
+     * 
+     * @param projectName
+     * @return If it is valid name, return true.
+     */
+    public static boolean isValidName(String projectName) {
+        Assert.notNull( projectName );
+        
+        return projectName.matches( CommonProjectConstants.PROJECT_NAME_PATTERN );
+    }
+    
+    /**
+     * Validate project name length
+     * 
+     * @param projectName
+     * @return If it is valid name, return true.
+     */
+    public static boolean isValidNameLength(String projectName) {
+        Assert.notNull( projectName );
+        
+        final int nameLength = projectName.length();
+        return CommonProjectConstants.PROJECT_NAME_MIN_LENGTH <= nameLength &&
+                nameLength <= CommonProjectConstants.PROJECT_NAME_MAX_LENGTH;
+    }
+    
+    /**
+     * Validate whether segment have invalid char or not
+     * 
+     * @param location project URI
+     * @return If have invalid character, return it. if not, return empty string.
+     */
+    public static String getInvalidChar(URI location) {
+        Assert.notNull( location );
+        
+        if ( EFS.SCHEME_FILE.equals( location.getScheme() ) ) {
+            IPath pathPart = new Path( location.getSchemeSpecificPart() );
+            for ( String segment : pathPart.segments() ) {
+                for ( char invalidChar : CommonProjectConstants.INVALID_RESOURCE_CHARACTERS ) {
+                    if ( segment.indexOf( invalidChar ) != -1 ) {
+                        return String.valueOf( invalidChar );
+                    }
+                }
+            }
+        }
+        
+        return StringUtil.EMPTY_STRING;
+    }
+    
+    /**
+     * Validate whether project really exist, or not
+     * @param location
+     * @return
+     */
+    public static boolean isExistProject(URI location) {
+        Assert.notNull( location );
+        
+        return new File( location ).exists();
+    }
+    
+    /**
+     * Validate a project location without the eclipse default validation
+     * 
+     * @param projectName the project name
+     * @param projectLocation the real full path of this project
+     * @return error message. If no errors, return null.
+     */
+    public static String getValidatedMsgAboutLocation(String projectName, URI projectLocation) {
+        if ( ! isValidNameLength( projectName ) ) {
+            return NLS.bind( Messages.ValidationUtil_InvalidProjectNameLength,
+                    CommonProjectConstants.PROJECT_NAME_MIN_LENGTH - 1,
+                    CommonProjectConstants.PROJECT_NAME_MAX_LENGTH + 1);
+        }
+        
+        if ( ! isValidName( projectName ) ) {
+            return Messages.ValidationUtil_InvalidProjectName;
+        }
+        
+        String invalidChar = getInvalidChar( projectLocation );
+        if ( ! invalidChar.isEmpty() ) {
+            return NLS.bind( Messages.ValidationUtil_InvalidSegment, invalidChar );
+        }
+        
+        if ( isExistProject( projectLocation ) ) {
+            return Messages.ValidationUtil_AlreadyExistProject;
+        }
+        
+        return StringUtil.EMPTY_STRING;
+    }
+}
diff --git a/org.tizen.common.project/src/org/tizen/common/project/util/messages.properties b/org.tizen.common.project/src/org/tizen/common/project/util/messages.properties
new file mode 100644 (file)
index 0000000..6c9d14b
--- /dev/null
@@ -0,0 +1,4 @@
+ValidationUtil_AlreadyExistProject=Project already exists or with a difference case
+ValidationUtil_InvalidProjectName=Invalid character is used in the project name
+ValidationUtil_InvalidProjectNameLength=Project name length should be longer than {0} and shorter than {1} characters
+ValidationUtil_InvalidSegment={0} is an invalid character in resource name.
diff --git a/org.tizen.common.project/test/src/org/tizen/common/project/util/ProjectValidationUtilTest.java b/org.tizen.common.project/test/src/org/tizen/common/project/util/ProjectValidationUtilTest.java
new file mode 100644 (file)
index 0000000..1d21a32
--- /dev/null
@@ -0,0 +1,117 @@
+package org.tizen.common.project.util;
+
+import java.io.File;
+import java.net.URI;
+
+import org.junit.Test;
+import org.tizen.common.util.StringUtil;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+
+public class ProjectValidationUtilTest {
+
+    protected abstract class NullTestSuite {
+        
+        public abstract void testMethod() throws Exception;
+        
+        public void run() throws Exception {
+            try {
+                testMethod();
+                fail( "This method must throw IllegalArgumentException" );
+            } catch (IllegalArgumentException e) {
+                // Success test
+            }
+        }
+    }
+    
+    @Test
+    public void test_isValidProjectName() throws Exception {
+        // Null test
+        new NullTestSuite() {
+            @Override
+            public void testMethod() throws Exception {
+                ProjectValidationUtil.isValidName( null );
+            }
+        }.run();
+        
+        final Object[][] TEST_CASES = new Object[][] {
+                new Object[] { "", false },
+                new Object[] { "ab", false },
+                new Object[] { "1ab", false },
+                new Object[] { "ab_", false },
+                new Object[] { "abc", true },
+                new Object[] { "a1-", true },
+                new Object[] { "abcdefghij0123456789012345678901234567890123456789", true },
+                new Object[] { "abcdefghij01234567890123456789012345678901234567890", false }
+        };
+        
+        for ( Object[] TEST_CASE : TEST_CASES ) {
+            String projectName = (String) TEST_CASE[0];
+            boolean expectResult = (Boolean) TEST_CASE[1];
+            assertEquals( ProjectValidationUtil.isValidName( projectName ), expectResult );
+        }
+    }
+    
+    @Test
+    public void test_isValidProjectNameLength() throws Exception {
+        // Null test
+        new NullTestSuite() {
+            @Override
+            public void testMethod() throws Exception {
+                ProjectValidationUtil.isValidNameLength( null );
+            }
+        }.run();
+        
+        final Object[][] TEST_CASES = new Object[][] {
+                new Object[] { "", false },
+                new Object[] { "ab", false },
+                new Object[] { "1ab", true },
+                new Object[] { "ab_", true },
+                new Object[] { "abc", true },
+                new Object[] { "a1-", true },
+                new Object[] { "abcdefghij0123456789012345678901234567890123456789", true },
+                new Object[] { "abcdefghij01234567890123456789012345678901234567890", false }
+        };
+        
+        for ( Object[] TEST_CASE : TEST_CASES ) {
+            String projectName = (String) TEST_CASE[0];
+            boolean expectResult = (Boolean) TEST_CASE[1];
+            assertEquals( ProjectValidationUtil.isValidNameLength( projectName ), expectResult );
+        }
+    }
+    
+    @Test
+    public void test_isContainedInvalidChar() throws Exception {
+        // Null test
+        new NullTestSuite() {
+            @Override
+            public void testMethod() throws Exception {
+                ProjectValidationUtil.getInvalidChar( null );
+            }
+        }.run();
+        
+        final Object[][] TEST_CASES = new Object[][] {
+                new Object[] { "", StringUtil.EMPTY_STRING },
+                new Object[] { "ab", StringUtil.EMPTY_STRING },
+                new Object[] { "1ab", StringUtil.EMPTY_STRING },
+                new Object[] { "ab_", StringUtil.EMPTY_STRING },
+                new Object[] { "ab`", "`" },
+                new Object[] { "a#b", "#" },
+                new Object[] { "a$b", "$" },
+                new Object[] { "a:v", ":" }
+        };
+        
+        
+        for ( Object[] TEST_CASE : TEST_CASES ) {
+            String segment = (String) TEST_CASE[0];
+            String expectResult = (String) TEST_CASE[1];
+            
+            URI segmentURI = new File( segment ).toURI();
+            
+            assertEquals( ProjectValidationUtil.getInvalidChar( segmentURI ), expectResult );
+        }
+    }
+    
+}