001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql;
022:
023: import java.util.Properties;
024:
025: import net.sf.hajdbc.Database;
026:
027: import org.testng.annotations.DataProvider;
028: import org.testng.annotations.Test;
029:
030: /**
031: * @author Paul Ferraro
032: *
033: */
034: @SuppressWarnings({"unchecked","nls"})
035: public abstract class AbstractTestDatabase<T extends Database, U>
036: implements Database<U> {
037: protected abstract T createDatabase(String id);
038:
039: @DataProvider(name="object")
040: protected Object[][] objectProvider() {
041: return new Object[][] {
042: new Object[] { this .createDatabase("1") },
043: new Object[] { this .createDatabase("2") },
044: new Object[] { new Object() }, new Object[] { null } };
045: }
046:
047: /**
048: * @see java.lang.Object#equals(java.lang.Object)
049: */
050: @Override
051: @Test(dataProvider="object")
052: public boolean equals(Object object) {
053: Database database = this .createDatabase("1");
054:
055: boolean equals = database.equals(object);
056:
057: boolean expected = (object != null)
058: && Database.class.isInstance(object)
059: && Database.class.cast(object).getId().equals("1");
060:
061: assert equals == expected : equals;
062:
063: return equals;
064: }
065:
066: /**
067: * @see java.lang.Object#hashCode()
068: */
069: @Override
070: @Test
071: public int hashCode() {
072: Database database = this .createDatabase("1");
073:
074: int hashCode = database.hashCode();
075:
076: int expected = database.getId().hashCode();
077:
078: assert hashCode == expected : hashCode;
079:
080: return hashCode;
081: }
082:
083: /**
084: * @see java.lang.Object#toString()
085: */
086: @Override
087: @Test
088: public String toString() {
089: Database database = this .createDatabase("1");
090:
091: String string = database.toString();
092:
093: assert string.equals("1") : string;
094:
095: return string;
096: }
097:
098: /**
099: * @see net.sf.hajdbc.Database#clean()
100: */
101: @Test
102: public void clean() {
103: Database database = this .createDatabase("1");
104:
105: database.clean();
106:
107: assert !database.isDirty();
108:
109: database.setWeight(1);
110: database.clean();
111:
112: assert !database.isDirty();
113: }
114:
115: /**
116: * @see net.sf.hajdbc.Database#isDirty()
117: */
118: @Test
119: public boolean isDirty() {
120: Database database = this .createDatabase("1");
121:
122: boolean dirty = database.isDirty();
123:
124: assert dirty;
125:
126: database.clean();
127:
128: dirty = database.isDirty();
129:
130: assert !dirty;
131:
132: return dirty;
133: }
134:
135: @DataProvider(name="string")
136: protected Object[][] stringProvider() {
137: return new Object[][] { new Object[] { "test" } };
138: }
139:
140: /**
141: * @see net.sf.hajdbc.InactiveDatabaseMBean#removeProperty(java.lang.String)
142: */
143: @Test(dataProvider="string")
144: public void removeProperty(String name) {
145: Database database = this .createDatabase("1");
146:
147: database.setProperty(name, "value");
148:
149: String value = database.getProperties().getProperty(name);
150:
151: assert value.equals("value") : value;
152:
153: database.clean();
154:
155: database.removeProperty(name);
156:
157: value = database.getProperties().getProperty(name);
158:
159: assert value == null;
160:
161: assert database.isDirty();
162:
163: database.clean();
164: database.removeProperty(name);
165:
166: assert !database.isDirty();
167: }
168:
169: /**
170: * @see net.sf.hajdbc.InactiveDatabaseMBean#setPassword(java.lang.String)
171: */
172: @Test(dataProvider="string")
173: public void setPassword(String password) {
174: Database database = this .createDatabase("1");
175:
176: database.setPassword(password);
177:
178: String value = database.getPassword();
179:
180: assert value.equals(password) : value;
181:
182: database.clean();
183:
184: database.setPassword(password);
185:
186: value = database.getPassword();
187:
188: assert value.equals(password);
189:
190: assert !database.isDirty();
191:
192: database.setPassword(null);
193:
194: assert database.isDirty();
195:
196: value = database.getPassword();
197:
198: assert value == null : value;
199: }
200:
201: @DataProvider(name="property")
202: protected Object[][] propertyProvider() {
203: return new Object[][] { new Object[] { "name", "value" },
204: new Object[] { "name", null },
205: new Object[] { null, "value" },
206: new Object[] { null, null } };
207: }
208:
209: /**
210: * @see net.sf.hajdbc.InactiveDatabaseMBean#setProperty(java.lang.String, java.lang.String)
211: */
212: @Test(dataProvider="property")
213: public void setProperty(String name, String value) {
214: Database database = this .createDatabase("1");
215:
216: database.clean();
217:
218: boolean accepted = (name != null) && (value != null);
219:
220: try {
221: database.setProperty(name, value);
222:
223: assert accepted;
224:
225: String propertyValue = database.getProperties()
226: .getProperty(name);
227:
228: assert propertyValue.equals(value) : propertyValue;
229:
230: database.clean();
231:
232: database.setProperty(name, value);
233:
234: propertyValue = database.getProperties().getProperty(name);
235:
236: assert propertyValue.equals(value) : propertyValue;
237:
238: assert !database.isDirty();
239:
240: database.setProperty(name, "");
241:
242: assert database.isDirty();
243:
244: propertyValue = database.getProperties().getProperty(name);
245:
246: assert propertyValue.equals("") : propertyValue;
247: } catch (IllegalArgumentException e) {
248: assert !accepted;
249:
250: assert !database.isDirty();
251: }
252: }
253:
254: /**
255: * @see net.sf.hajdbc.InactiveDatabaseMBean#setUser(java.lang.String)
256: */
257: @Test(dataProvider="string")
258: public void setUser(String user) {
259: Database database = this .createDatabase("1");
260:
261: database.setUser(user);
262:
263: String value = database.getUser();
264:
265: assert value.equals(user) : value;
266:
267: database.clean();
268:
269: database.setUser(user);
270:
271: value = database.getUser();
272:
273: assert value.equals(user);
274:
275: assert !database.isDirty();
276:
277: database.setUser(null);
278:
279: assert database.isDirty();
280:
281: value = database.getUser();
282:
283: assert value == null : value;
284: }
285:
286: @DataProvider(name="int")
287: protected Object[][] intProvider() {
288: return new Object[][] { new Object[] { 1 } };
289: }
290:
291: /**
292: * @see net.sf.hajdbc.InactiveDatabaseMBean#setWeight(int)
293: */
294: @Test(dataProvider="int")
295: public void setWeight(int weight) {
296: Database database = this .createDatabase("1");
297:
298: database.setWeight(weight);
299:
300: int value = database.getWeight();
301:
302: assert value == weight : value;
303:
304: database.clean();
305:
306: database.setWeight(weight);
307:
308: value = database.getWeight();
309:
310: assert value == weight : value;
311:
312: assert !database.isDirty();
313:
314: database.setWeight(0);
315:
316: assert database.isDirty();
317:
318: value = database.getWeight();
319:
320: assert value == 0 : value;
321:
322: database.clean();
323:
324: try {
325: database.setWeight(-1);
326:
327: assert false;
328: } catch (IllegalArgumentException e) {
329: assert true;
330: }
331:
332: assert !database.isDirty();
333: }
334:
335: @DataProvider(name="boolean")
336: protected Object[][] booleanProvider() {
337: return new Object[][] { new Object[] { true } };
338: }
339:
340: /**
341: * @see net.sf.hajdbc.InactiveDatabaseMBean#setLocal(boolean)
342: */
343: @Test(dataProvider="boolean")
344: public void setLocal(boolean local) {
345: Database database = this .createDatabase("1");
346:
347: database.setLocal(local);
348:
349: boolean value = database.isLocal();
350:
351: assert value == local : value;
352:
353: database.clean();
354:
355: database.setLocal(local);
356:
357: value = database.isLocal();
358:
359: assert value == local : value;
360:
361: assert !database.isDirty();
362:
363: database.setLocal(false);
364:
365: assert database.isDirty();
366:
367: value = database.isLocal();
368:
369: assert !value;
370:
371: database.clean();
372: }
373:
374: @DataProvider(name="database")
375: protected Object[][] databaseProvider() {
376: return new Object[][] {
377: new Object[] { this .createDatabase("1") },
378: new Object[] { this .createDatabase("1") },
379: new Object[] { this .createDatabase("2") } };
380: }
381:
382: /**
383: * @see java.lang.Comparable#compareTo(T)
384: */
385: @SuppressWarnings("unchecked")
386: @Test(dataProvider="database")
387: public int compareTo(Database<U> object) {
388: Database database = this .createDatabase("1");
389:
390: int compared = database.compareTo(object);
391:
392: int expected = database.getId().compareTo(object.getId());
393:
394: assert compared == expected : compared;
395:
396: return compared;
397: }
398:
399: /**
400: * @see net.sf.hajdbc.ActiveDatabaseMBean#getId()
401: */
402: @Test
403: public String getId() {
404: Database database = this .createDatabase("1");
405:
406: String id = database.getId();
407:
408: assert id.equals("1") : id;
409:
410: return id;
411: }
412:
413: /**
414: * @see net.sf.hajdbc.ActiveDatabaseMBean#getPassword()
415: */
416: @Test
417: public String getPassword() {
418: Database database = this .createDatabase("1");
419:
420: String password = database.getPassword();
421:
422: assert password == null : password;
423:
424: database.setPassword("password");
425:
426: password = database.getPassword();
427:
428: assert password.equals("password") : password;
429:
430: return password;
431: }
432:
433: /**
434: * @see net.sf.hajdbc.ActiveDatabaseMBean#getProperties()
435: */
436: @Test
437: public Properties getProperties() {
438: Database database = this .createDatabase("1");
439:
440: Properties properties = database.getProperties();
441:
442: assert properties.isEmpty() : properties;
443:
444: database.setProperty("name", "value");
445:
446: properties = database.getProperties();
447:
448: assert properties.size() == 1 : properties.size();
449:
450: String value = properties.getProperty("name");
451:
452: assert value.equals("value") : value;
453:
454: database.removeProperty("name");
455:
456: properties = database.getProperties();
457:
458: assert properties.isEmpty();
459:
460: return properties;
461: }
462:
463: /**
464: * @see net.sf.hajdbc.ActiveDatabaseMBean#getUser()
465: */
466: @Test
467: public String getUser() {
468: Database database = this .createDatabase("1");
469:
470: String user = database.getUser();
471:
472: assert user == null : user;
473:
474: database.setUser("user");
475:
476: user = database.getUser();
477:
478: assert user.equals("user") : user;
479:
480: return user;
481: }
482:
483: /**
484: * @see net.sf.hajdbc.ActiveDatabaseMBean#getWeight()
485: */
486: @Test
487: public int getWeight() {
488: Database database = this .createDatabase("1");
489:
490: int weight = database.getWeight();
491:
492: assert weight == 1 : weight;
493:
494: database.setWeight(0);
495:
496: weight = database.getWeight();
497:
498: assert weight == 0 : weight;
499:
500: return weight;
501: }
502:
503: /**
504: * @see net.sf.hajdbc.ActiveDatabaseMBean#isLocal()
505: */
506: @Test
507: public boolean isLocal() {
508: Database database = this .createDatabase("1");
509:
510: boolean local = database.isLocal();
511:
512: assert !local;
513:
514: database.setLocal(true);
515:
516: local = database.isLocal();
517:
518: assert local;
519:
520: return local;
521: }
522: }
|