From f414018a603591ac5ed7300445de6f4188d9f7b8 Mon Sep 17 00:00:00 2001 From: wrodrigues Date: Mon, 10 Feb 2014 18:20:15 +0100 Subject: [PATCH] adding fix and unit tests for cloud-engine-api scariest Signed-off-by: Hugo Trippaers --- .../api/storage/type/VolumeTypeBase.java | 39 ++++--- .../api/storage/type/VolumeTypeHelper.java | 25 +++-- .../storage/type/VolumeTypeHelperTest.java | 101 ++++++++++++++++++ 3 files changed, 139 insertions(+), 26 deletions(-) create mode 100644 engine/api/test/org/apache/cloudstack/engine/subsystem/api/storage/type/VolumeTypeHelperTest.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 78ac8e23966..1d6a65c6921 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 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 50572033042..6b8437980b6 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 types; + private static VolumeType defaultType = new Unknown(); + private List types; + private final Map mapTypes = new Hashtable(); + @Inject public void setTypes(List 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 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 00000000000..32df84ff984 --- /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 types = new ArrayList(); + 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