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 /** 010 * Mock implementation of <code>Struct</code>. 011 */ 012 public class MockStruct implements Struct, Cloneable 013 { 014 private String sqlTypeName; 015 private List attributes; 016 017 public MockStruct(String sqlTypeName) 018 { 019 this.sqlTypeName = sqlTypeName; 020 attributes = new ArrayList(); 021 } 022 023 public String getSQLTypeName() throws SQLException 024 { 025 return sqlTypeName; 026 } 027 028 public void setSQLTypeName(String sqlTypeName) 029 { 030 this.sqlTypeName = sqlTypeName; 031 } 032 033 public Object[] getAttributes() throws SQLException 034 { 035 return attributes.toArray(); 036 } 037 038 public Object[] getAttributes(Map map) throws SQLException 039 { 040 return getAttributes(); 041 } 042 043 public void addAttribute(Object attribute) 044 { 045 attributes.add(attribute); 046 } 047 048 public void addAttributes(Object[] attributes) 049 { 050 for(int ii = 0; ii < attributes.length; ii++) 051 { 052 addAttribute(attributes[ii]); 053 } 054 } 055 056 public void addAttributes(List attributes) 057 { 058 addAttributes(attributes.toArray()); 059 } 060 061 public void clearAttributes() 062 { 063 attributes.clear(); 064 } 065 066 public boolean equals(Object obj) 067 { 068 if(null == obj) return false; 069 if(!obj.getClass().equals(this.getClass())) return false; 070 MockStruct other = (MockStruct)obj; 071 if(null != sqlTypeName && !sqlTypeName.equals(other.sqlTypeName)) return false; 072 if(null != other.sqlTypeName && !other.sqlTypeName.equals(sqlTypeName)) return false; 073 if(null == attributes && null == other.attributes) return true; 074 if(null == attributes || null == other.attributes) return false; 075 return attributes.equals(other.attributes); 076 } 077 078 public int hashCode() 079 { 080 int hashCode = 0; 081 if(null != sqlTypeName) hashCode += 31 * sqlTypeName.hashCode(); 082 if(null != attributes) hashCode += 31 * attributes.hashCode(); 083 return hashCode; 084 } 085 086 public String toString() 087 { 088 StringBuffer buffer = new StringBuffer("Struct data:\n"); 089 buffer.append("SQLTypeName: " + sqlTypeName + "\n"); 090 for(int ii = 0; ii < attributes.size(); ii++) 091 { 092 buffer.append("Attribute " + ii + ": " + attributes.get(ii)+ "\n"); 093 } 094 buffer.append("\n"); 095 return buffer.toString(); 096 } 097 098 public Object clone() 099 { 100 MockStruct struct = new MockStruct(sqlTypeName); 101 struct.addAttributes(attributes.toArray()); 102 return struct; 103 } 104 }