diff --git a/server/test/com/cloud/network/dao/NetworkDaoTest.java b/server/test/com/cloud/network/dao/NetworkDaoTest.java new file mode 100644 index 00000000000..1d51fc45433 --- /dev/null +++ b/server/test/com/cloud/network/dao/NetworkDaoTest.java @@ -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 tags = new ArrayList(); + + tags.add("a"); + tags.add("b"); + network.setTags(tags); + + network = dao.persist(network); + List saveTags = network.getTags(); + assert(saveTags.size() == 2 && saveTags.contains("a") && saveTags.contains("b")); + + NetworkVO retrieved = dao.findById(1001l); + List retrievedTags = retrieved.getTags(); + assert(retrievedTags.size() == 2 && retrievedTags.contains("a") && retrievedTags.contains("b")); + + List updateTags = new ArrayList(); + 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"); + } + +} diff --git a/utils/src/com/cloud/utils/db/EcInfo.java b/utils/src/com/cloud/utils/db/EcInfo.java new file mode 100644 index 00000000000..cdbeb2441fa --- /dev/null +++ b/utils/src/com/cloud/utils/db/EcInfo.java @@ -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 . + * + */ +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(); + } +} diff --git a/utils/src/javax/persistence/CollectionTable.java b/utils/src/javax/persistence/CollectionTable.java new file mode 100644 index 00000000000..2b6ab84816d --- /dev/null +++ b/utils/src/javax/persistence/CollectionTable.java @@ -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 . + * + */ +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 {}; +} diff --git a/utils/test/com/cloud/utils/db/ElementCollectionTest.java b/utils/test/com/cloud/utils/db/ElementCollectionTest.java new file mode 100644 index 00000000000..c1e766f17fb --- /dev/null +++ b/utils/test/com/cloud/utils/db/ElementCollectionTest.java @@ -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 ar = null; + List lst = null; + Collection 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); + } + } +}