bug 9651: checked in missing files

This commit is contained in:
Alex Huang 2011-05-31 10:27:50 -07:00
parent 7c74c3a51d
commit 8754f8fab8
4 changed files with 259 additions and 0 deletions

View File

@ -0,0 +1,57 @@
package com.cloud.network.dao;
import java.util.ArrayList;
import java.util.List;
import junit.framework.TestCase;
import com.cloud.network.Network.GuestIpType;
import com.cloud.network.NetworkVO;
import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.Networks.Mode;
import com.cloud.network.Networks.TrafficType;
import com.cloud.utils.component.ComponentLocator;
public class NetworkDaoTest extends TestCase {
public void testTags() {
NetworkDaoImpl dao = ComponentLocator.inject(NetworkDaoImpl.class);
dao.expunge(1001l);
NetworkVO network = new NetworkVO(1001, TrafficType.Control, GuestIpType.Direct, Mode.Dhcp, BroadcastDomainType.Native, 1, 1, 1, 1, 1001, "Name", "DisplayText", false, true);
network.setGuruName("guru_name");
List<String> tags = new ArrayList<String>();
tags.add("a");
tags.add("b");
network.setTags(tags);
network = dao.persist(network);
List<String> saveTags = network.getTags();
assert(saveTags.size() == 2 && saveTags.contains("a") && saveTags.contains("b"));
NetworkVO retrieved = dao.findById(1001l);
List<String> retrievedTags = retrieved.getTags();
assert(retrievedTags.size() == 2 && retrievedTags.contains("a") && retrievedTags.contains("b"));
List<String> updateTags = new ArrayList<String>();
updateTags.add("e");
updateTags.add("f");
retrieved.setTags(updateTags);
dao.update(retrieved.getId(), retrieved);
retrieved = dao.findById(1001l);
retrievedTags = retrieved.getTags();
assert(retrievedTags.size() == 2 && retrievedTags.contains("e") && retrievedTags.contains("f"));
dao.expunge(1001l);
}
public void testListBy() {
NetworkDaoImpl dao = ComponentLocator.inject(NetworkDaoImpl.class);
dao.listBy(1l, 1l, 1l, "192.168.192.0/24");
}
}

View File

@ -0,0 +1,107 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.utils.db;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.JoinColumn;
import com.cloud.utils.exception.CloudRuntimeException;
public class EcInfo {
protected String insertSql;
protected String selectSql;
protected String clearSql;
protected Class<?> targetClass;
protected Class<?> rawClass;
public EcInfo(Attribute attr, Attribute idAttr) {
attr.attache = this;
ElementCollection ec = attr.field.getAnnotation(ElementCollection.class);
targetClass = ec.targetClass();
Class<?> type = attr.field.getType();
if (type.isArray()) {
rawClass = null;
} else {
ParameterizedType pType = (ParameterizedType)attr.field.getGenericType();
Type rawType = pType.getRawType();
Class<?> rawClazz = (Class<?>)rawType;
try {
if (!Modifier.isAbstract(rawClazz.getModifiers()) && !rawClazz.isInterface() && rawClazz.getConstructors().length != 0 && rawClazz.getConstructor() != null) {
rawClass = rawClazz;
} else if (Set.class == rawClazz) {
rawClass = HashSet.class;
} else if (List.class == rawClazz) {
rawClass = ArrayList.class;
} else if (Collection.class == Collection.class) {
rawClass = ArrayList.class;
} else {
assert (false) : " We don't know how to create this calss " + rawType.toString() + " for " + attr.field.getName();
}
} catch (NoSuchMethodException e) {
throw new CloudRuntimeException("Write your own support for " + rawClazz + " defined by " + attr.field.getName());
}
}
CollectionTable ct = attr.field.getAnnotation(CollectionTable.class);
assert (ct.name().length() > 0) : "Please sepcify the table for " + attr.field.getName();
StringBuilder selectBuf = new StringBuilder("SELECT ");
StringBuilder insertBuf = new StringBuilder("INSERT INTO ");
StringBuilder clearBuf = new StringBuilder("DELETE FROM ");
clearBuf.append(ct.name()).append(" WHERE ");
selectBuf.append(attr.columnName);
selectBuf.append(" FROM ").append(ct.name()).append(", ").append(attr.table);
selectBuf.append(" WHERE ");
insertBuf.append(ct.name()).append("(");
StringBuilder valuesBuf = new StringBuilder("SELECT ");
for (JoinColumn jc : ct.joinColumns()) {
selectBuf.append(ct.name()).append(".").append(jc.name()).append("=");
if (jc.referencedColumnName().length() == 0) {
selectBuf.append(idAttr.table).append(".").append(idAttr.columnName);
valuesBuf.append(idAttr.table).append(".").append(idAttr.columnName);
clearBuf.append(ct.name()).append(".").append(jc.name()).append("=?");
} else {
selectBuf.append(attr.table).append(".").append(jc.referencedColumnName());
valuesBuf.append(attr.table).append(".").append(jc.referencedColumnName()).append(",");
}
selectBuf.append(" AND ");
insertBuf.append(jc.name()).append(", ");
valuesBuf.append(", ");
}
selectSql = selectBuf.append(idAttr.table).append(".").append(idAttr.columnName).append("=?").toString();
insertBuf.append(attr.columnName).append(") ");
valuesBuf.append("? FROM ").append(attr.table);
valuesBuf.append(" WHERE ").append(idAttr.table).append(".").append(idAttr.columnName).append("=?");
insertSql = insertBuf.append(valuesBuf).toString();
clearSql = clearBuf.toString();
}
}

View File

@ -0,0 +1,39 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package javax.persistence;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = { METHOD, FIELD })
@Retention(value = RetentionPolicy.RUNTIME)
public @interface CollectionTable {
String catalog() default "";
JoinColumn[] joinColumns() default {};
String name() default "";
String schema() default "";
UniqueConstraint[] uniqueConstraints() default {};
}

View File

@ -0,0 +1,56 @@
package com.cloud.utils.db;
import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import junit.framework.TestCase;
import org.apache.log4j.Logger;
public class ElementCollectionTest extends TestCase {
static final Logger s_logger = Logger.getLogger(ElementCollectionTest.class);
ArrayList<String> ar = null;
List<String> lst = null;
Collection<String> coll = null;
String[] array = null;
public void testArrayList() throws Exception {
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
}
Class<?> type1 = field.getType();
Object collection = null;
if (!type1.isArray()) {
ParameterizedType type = (ParameterizedType)field.getGenericType();
Type rawType = type.getRawType();
Class<?> rawClazz = (Class<?>)rawType;
if (!Modifier.isAbstract(rawClazz.getModifiers()) && !rawClazz.isInterface() && rawClazz.getConstructors().length != 0 && rawClazz.getConstructor() != null) {
collection = rawClazz.newInstance();
}
if (collection == null) {
if (Collection.class.isAssignableFrom(rawClazz)) {
collection = new ArrayList();
} else if (Set.class.isAssignableFrom(rawClazz)) {
collection = new HashSet();
}
}
} else {
collection = Array.newInstance(String.class, 1);
}
field.set(this, collection);
assert (field.get(this) != null);
}
}
}