Updated Branches:
  refs/heads/master e171cb181 -> 443acac92

adding fix and unit tests for cloud-engine-api scariest

Signed-off-by: Hugo Trippaers <[email protected]>


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/f414018a
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/f414018a
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/f414018a

Branch: refs/heads/master
Commit: f414018a603591ac5ed7300445de6f4188d9f7b8
Parents: 0ff9433
Author: wrodrigues <[email protected]>
Authored: Mon Feb 10 18:20:15 2014 +0100
Committer: Hugo Trippaers <[email protected]>
Committed: Fri Feb 14 18:37:44 2014 +0100

----------------------------------------------------------------------
 .../api/storage/type/VolumeTypeBase.java        |  39 +++----
 .../api/storage/type/VolumeTypeHelper.java      |  25 +++--
 .../api/storage/type/VolumeTypeHelperTest.java  | 101 +++++++++++++++++++
 3 files changed, 139 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f414018a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeBase.java
----------------------------------------------------------------------
diff --git 
a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeBase.java
 
b/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeBase.java
index 78ac8e2..1d6a65c 100644
--- 
a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeBase.java
+++ 
b/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeBase.java
@@ -17,31 +17,36 @@
 package org.apache.cloudstack.engine.subsystem.api.storage.type;
 
 public class VolumeTypeBase implements VolumeType {
+
     protected String type = "Unknown";
 
     @Override
-    public boolean equals(Object that) {
-        if (this == that) {
+    public int hashCode() {
+        final int prime = 31;
+        int result = 1;
+        result = prime * result + ((type == null) ? 0 : type.hashCode());
+        return result;
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (this == obj)
             return true;
-        }
-        if (that instanceof String) {
-            if (this.toString().equalsIgnoreCase((String)that)) {
-                return true;
-            }
-        } else if (that instanceof VolumeTypeBase) {
-            VolumeTypeBase th = (VolumeTypeBase)that;
-            if (this.toString().equalsIgnoreCase(th.toString())) {
-                return true;
-            }
-        } else {
+        if (obj == null)
             return false;
-        }
-        return false;
+        if (getClass() != obj.getClass())
+            return false;
+        VolumeTypeBase other = (VolumeTypeBase) obj;
+        if (type == null) {
+            if (other.type != null)
+                return false;
+        } else if (!type.equals(other.type))
+            return false;
+        return true;
     }
 
     @Override
     public String toString() {
         return type;
     }
-
-}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f414018a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelper.java
----------------------------------------------------------------------
diff --git 
a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelper.java
 
b/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelper.java
index 5057203..6b84379 100644
--- 
a/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelper.java
+++ 
b/engine/api/src/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelper.java
@@ -16,26 +16,33 @@
 // under the License.
 package org.apache.cloudstack.engine.subsystem.api.storage.type;
 
+import java.util.Hashtable;
 import java.util.List;
+import java.util.Map;
 
 import javax.inject.Inject;
 
 public class VolumeTypeHelper {
-    static private List<VolumeType> types;
+
     private static VolumeType defaultType = new Unknown();
 
+    private List<VolumeType> types;
+    private final Map<String, VolumeType> mapTypes = new Hashtable<String, 
VolumeType>();
+
     @Inject
     public void setTypes(List<VolumeType> types) {
-        VolumeTypeHelper.types = types;
+        this.types = types;
+
+        mapTypes.clear();
+        for (VolumeType ty : this.types) {
+            mapTypes.put(ty.getClass().getSimpleName().toUpperCase(), ty);
+        }
     }
 
-    public static VolumeType getType(String type) {
-        for (VolumeType ty : types) {
-            if (ty.equals(type)) {
-                return ty;
-            }
+    public VolumeType getType(String type) {
+        if (mapTypes.containsKey(type.toUpperCase())) {
+            return mapTypes.get(type.toUpperCase());
         }
         return VolumeTypeHelper.defaultType;
     }
-
-}
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/f414018a/engine/api/test/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelperTest.java
----------------------------------------------------------------------
diff --git 
a/engine/api/test/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelperTest.java
 
b/engine/api/test/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelperTest.java
new file mode 100644
index 0000000..32df84f
--- /dev/null
+++ 
b/engine/api/test/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelperTest.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.cloudstack.engine.subsystem.api.storage.type;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import junit.framework.Assert;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class VolumeTypeHelperTest {
+
+    private VolumeTypeHelper helper;
+
+    @Before
+    public void setu() {
+        helper = new VolumeTypeHelper();
+
+        List<VolumeType> types = new ArrayList<VolumeType>();
+        types.add(new BaseImage());
+        types.add(new DataDisk());
+        types.add(new Iso());
+        types.add(new Unknown());
+        types.add(new RootDisk());
+        types.add(new VolumeTypeBase());
+
+        helper.setTypes(types);
+    }
+
+    @Test
+    public void testGetTypeBaseImage() throws Exception {
+        VolumeType type = helper.getType("BaseImage");
+
+        Assert.assertTrue(type instanceof BaseImage);
+    }
+
+    @Test
+    public void testGetTypeDataDisk() throws Exception {
+        VolumeType type = helper.getType("DataDisk");
+
+        Assert.assertTrue(type instanceof DataDisk);
+    }
+
+    @Test
+    public void testGetTypeIso() throws Exception {
+        VolumeType type = helper.getType("Iso");
+
+        Assert.assertTrue(type instanceof Iso);
+    }
+
+    @Test
+    public void testGetTypeUnknown() throws Exception {
+        VolumeType type = helper.getType("Unknown");
+
+        Assert.assertTrue(type instanceof Unknown);
+    }
+
+    @Test
+    public void testGetTypeRootDisk() throws Exception {
+        VolumeType type = helper.getType("RootDisk");
+
+        Assert.assertTrue(type instanceof RootDisk);
+    }
+
+    @Test
+    public void testGetTypeVolumeTypeBase() throws Exception {
+        VolumeType type = helper.getType("VolumeTypeBase");
+
+        Assert.assertTrue(type instanceof VolumeTypeBase);
+    }
+
+    @Test
+    public void testGetTypeVolumeString() throws Exception {
+        VolumeType type = helper.getType("String");
+
+        Assert.assertTrue(type instanceof Unknown);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void testGetTypeVolumeNull() throws Exception {
+        helper.getType(null);
+    }
+}
\ No newline at end of file

Reply via email to