001    package com.mockrunner.mock.jdbc;
002    
003    import java.sql.SQLException;
004    import java.sql.Struct;
005    import java.util.ArrayList;
006    import java.util.List;
007    import java.util.Map;
008    
009    import com.mockrunner.base.NestedApplicationException;
010    
011    /**
012     * Mock implementation of <code>Struct</code>.
013     */
014    public class MockStruct implements Struct, Cloneable
015    {
016        private String sqlTypeName;
017        private List attributes;
018        
019        public MockStruct(Object[] attributes)
020        {
021            this(null, attributes);
022        }
023        
024        public MockStruct(String sqlTypeName, Object[] attributes)
025        {
026            this.sqlTypeName = sqlTypeName;
027            this.attributes = new ArrayList();
028            addAttributes(attributes);
029        }
030        
031        public MockStruct(String sqlTypeName)
032        {
033            this(sqlTypeName, new Object[0]);
034        }
035        
036        public String getSQLTypeName() throws SQLException
037        {
038            return sqlTypeName;
039        }
040        
041        public void setSQLTypeName(String sqlTypeName)
042        {
043            this.sqlTypeName = sqlTypeName;
044        }
045    
046        public Object[] getAttributes() throws SQLException
047        {
048            return attributes.toArray();
049        }
050    
051        public Object[] getAttributes(Map map) throws SQLException
052        {
053            return getAttributes();
054        }
055        
056        public void addAttribute(Object attribute)
057        {
058            attributes.add(attribute);
059        }
060        
061        public void addAttributes(Object[] attributes)
062        {
063            for(int ii = 0; ii < attributes.length; ii++)
064            {
065                addAttribute(attributes[ii]);
066            }
067        }
068        
069        public void addAttributes(List attributes)
070        {
071            addAttributes(attributes.toArray());
072        }
073        
074        public void clearAttributes()
075        {
076            attributes.clear();
077        }
078        
079        public boolean equals(Object obj)
080        {
081            if(null == obj) return false;
082            if(!obj.getClass().equals(this.getClass())) return false;
083            MockStruct other = (MockStruct)obj;
084            if(null != sqlTypeName && !sqlTypeName.equals(other.sqlTypeName)) return false;
085            if(null != other.sqlTypeName && !other.sqlTypeName.equals(sqlTypeName)) return false;
086            if(null == attributes && null == other.attributes) return true;
087            if(null == attributes || null == other.attributes) return false;
088            return attributes.equals(other.attributes);
089        }
090    
091        public int hashCode()
092        {
093            int hashCode = 17;
094            if(null != sqlTypeName) hashCode = (31 * hashCode) + sqlTypeName.hashCode();
095            if(null != attributes) hashCode = (31 * hashCode) + attributes.hashCode();
096            return hashCode;
097        }
098    
099        public String toString()
100        {
101            return "Struct data: " + attributes.toString();
102        }
103        
104        public Object clone()
105        {
106            try
107            {
108                MockStruct copy = (MockStruct)super.clone();
109                copy.attributes = new ArrayList();
110                copy.addAttributes(attributes.toArray());
111                return copy;
112            } 
113            catch(CloneNotSupportedException exc)
114            {
115                throw new NestedApplicationException(exc);
116            }
117        }
118    }