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

Signed-off-by: Hugo Trippaers <htrippaers@schubergphilis.com>
This commit is contained in:
wrodrigues 2014-02-10 18:20:15 +01:00 committed by Hugo Trippaers
parent 0ff943337c
commit f414018a60
3 changed files with 139 additions and 26 deletions

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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);
}
}