/
PostgresAdapterTest.java
199 lines (174 loc) · 6.13 KB
/
PostgresAdapterTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package org.ecoinformatics.datamanager.database;
import java.sql.SQLException;
import java.util.Vector;
import org.ecoinformatics.datamanager.parser.Attribute;
import org.ecoinformatics.datamanager.parser.AttributeList;
import org.ecoinformatics.datamanager.parser.NumericDomain;
import org.ecoinformatics.datamanager.parser.TextDomain;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class PostgresAdapterTest extends TestCase
{
/**
* Constructor
* @param name The name of test suite
*/
public PostgresAdapterTest (String name)
{
super(name);
}
/*
* Class methods
*/
/**
* Create a suite of tests to be run together
*/
public static Test suite()
{
TestSuite suite = new TestSuite();
suite.addTest(new PostgresAdapterTest("initialize"));
suite.addTest(new PostgresAdapterTest("testGenerateInsertSQL"));
return suite;
}
/*
* Instance methods
*/
/**
* Run an initial test that always passes to check that the test harness
* is working.
*/
public void initialize() {
assertTrue(1 == 1);
}
/**
* Establish a testing framework by initializing appropriate objects.
*/
protected void setUp() throws Exception
{
super.setUp();
}
/**
* Release any objects and closes database connections after tests
* are complete.
*/
protected void tearDown() throws Exception
{
super.tearDown();
}
/**
* Tests generating insertSQL method
*/
public void testGenerateInsertSQL() throws SQLException, DataNotMatchingMetadataException
{
PostgresAdapter adapter = new PostgresAdapter();
AttributeList attributeList = null;
String tableName = "table1";
Vector oneRowData = new Vector();
String sql = null;
// Test that a null attribute list throws a SQLException
try
{
sql = adapter.generateInsertSQL(attributeList, tableName, oneRowData);
assertTrue(1 == 2);
}
catch(SQLException e)
{
assertTrue(1 == 1);
}
catch(DataNotMatchingMetadataException e)
{
System.err.println("The error is "+e.getMessage());
}
Attribute attribute1 = null;
TextDomain domain = new TextDomain();
String name1 = "name1";
String id1 = "id1";
String label1 = "label";
String description = "description";
String unit = "unit";
String unitType = "unitType";
String measurementScale = "scale";
attribute1 = new Attribute(id1, name1, label1, description, unit,
unitType, measurementScale, domain);
Attribute attribute2 = null;
String numberType = "natural";
Double min = new Double(1);
Double max = new Double(900);
NumericDomain domain2 = new NumericDomain(numberType, min, max);
String name2 = "name2";
String id2 = "id2";
String label2 = "label";
String description2 = "description";
String unit2 = "unit";
String unitType2 = "unitType";
String measurementScale2 = "scale";
attribute2 = new Attribute(id2, name2, label2, description2, unit2,
unitType2, measurementScale2, domain2);
Attribute attribute3 = null;
numberType = "real";
min = new Double(1);
max = new Double(900);
NumericDomain domain3 = new NumericDomain(numberType, min, max);
String name3 = "name3";
String id3 = "id3";
String label3 = "label";
String description3 = "description";
String unit3 = "unit";
String unitType3 = "unitType";
String measurementScale3 = "scale";
attribute3 = new Attribute(id3, name3, label3, description3, unit3,
unitType3, measurementScale3, domain3);
attributeList = new AttributeList();
attribute1.setDBFieldName(attribute1.getName());
attribute2.setDBFieldName(attribute2.getName());
attribute3.setDBFieldName(attribute3.getName());
attributeList.add(attribute1);
attributeList.add(attribute2);
attributeList.add(attribute3);
// Test that generateInsertSQL() fails when the data vector is empty
try
{
sql = adapter.generateInsertSQL(attributeList, tableName, oneRowData);
// test attribute list is null
assertTrue(1 == 2);
}
catch(SQLException e)
{
assertTrue(1 == 1);
}
catch(DataNotMatchingMetadataException e)
{
System.err.println("The error is "+e.getMessage());
}
String value1= "data1";
String value2 = "1";
String value3 = "data2";
oneRowData.add(value1);
oneRowData.add(value2);
oneRowData.add(value3);
// Test that generateInsertSQL() fails when one of the data values
// is the wrong type
try
{
sql = adapter.generateInsertSQL(attributeList, tableName, oneRowData);
// test attribute list is null
assertTrue(1 == 2);
}
catch(SQLException e)
{
assertTrue(1 == 1);
}
catch(DataNotMatchingMetadataException e)
{
System.err.println("The error is "+e.getMessage());
}
// Test that generateInsertSQL() generates the correct INSERT
// statement when given good values
oneRowData.remove(2);
String correctValue = "2.2";
oneRowData.add(correctValue);
sql = adapter.generateInsertSQL(attributeList, tableName, oneRowData);
assertEquals("INSERT INTO table1(name1,name2,name3) VALUES ('data1',1,2.2);", sql);
}
}