Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/acs/damsel/srvr/db/TestAssetDB.java


1   package org.acs.damsel.srvr.db;
2   
3   import java.sql.*;
4   import java.util.*;
5   
6   import org.acs.damsel.srvr.*;
7   import org.acs.damsel.srvr.asset.*;
8   import org.acs.damsel.srvr.collection.*;
9   import org.acs.damsel.srvr.collection.Collection;
10  import org.acs.damsel.srvr.schema.*;
11  import org.acs.damsel.srvr.user.*;
12  import org.apache.log4j.*;
13  import junit.framework.*;
14  import org.acs.damsel.srvr.group.*;
15  
16  public class TestAssetDB
17      extends TestCase {
18    private AssetDB assetDB = null;
19    private static Logger log = Logger.getLogger(AssetDB.class);
20  
21    public TestAssetDB(String name) {
22      super(name);
23      BasicConfigurator.resetConfiguration();
24      PropertyConfigurator.configure(Config.instance().getLogPropertiesFileName());
25    }
26  
27    protected void setUp() throws Exception {
28      super.setUp();
29      assetDB = AssetDB.instance();
30    }
31  
32    protected void tearDown() throws Exception {
33      assetDB = null;
34      super.tearDown();
35    }
36  
37    public void testConstructor() {
38      this.assertNotNull(assetDB);
39    }
40  
41    private void printTable(Vector results) {
42      Vector row = null;
43      String str = null;
44      String logStr = "";
45      for (Iterator i = results.iterator(); i.hasNext(); ) {
46        row = (Vector) i.next();
47        for (Iterator j = row.iterator(); j.hasNext(); ) {
48          str = (String) j.next();
49          logStr += str;
50        }
51        log.info(logStr);
52        logStr = "";
53      }
54    }
55  
56    /* Test getAllAssets method by adding a new asset to the collection view and
57       checking if it is a member of the assets retrieved form the AssetsTable */
58  
59    public void testGetAllAssets() {
60      CollectionView results = new CollectionView();
61  
62      try {
63        Asset a = new Asset();
64        AssetDescriptor fileNameAD = new AssetDescriptor();
65        AssetDescriptor titleAD = new AssetDescriptor();
66        fileNameAD.setTag("FileName");
67        fileNameAD.setValue("testGetAllAssets.jpg");
68        titleAD.setTag("Title");
69        titleAD.setValue("TestGetAllAssets");
70        AssetDescriptorCollection adc = new AssetDescriptorCollection();
71        adc.addAssetDescriptor(fileNameAD);
72        adc.addAssetDescriptor(titleAD);
73        a.setAssetDescriptors(adc);
74        assetDB.addAsset(a, "TestGetAllAssetsCollection");
75        results = assetDB.getAllAssets();
76        this.assertNotNull(results);
77        this.assertFalse(results.size() == 0);
78        boolean checkFilename = false;
79        boolean checkTitle = false;
80        for (int i = 0; i < results.size(); i++) {
81          try {
82            if (results.returnAsset(i).getAssetDescriptors().getValue(
83                "Filename").equals("testGetAllAssets.jpg")) {
84              checkFilename = true;
85            }
86            if (results.returnAsset(i).getAssetDescriptors().getValue("Title").
87                equals("TestGetAllAssets")) {
88              checkTitle = true;
89            }
90          }
91          catch (NullPointerException ex) {
92            // it's ok to not do anything with the exception because we are only
93            // concerned with checkFilename and checkTitle
94          }
95  
96        }
97        this.assertTrue(checkFilename);
98        this.assertTrue(checkTitle);
99        Iterator i = results.iterator();
100       Asset ass;
101       while (i.hasNext()) {
102         ass = (Asset) i.next();
103         this.assertNotNull(ass);
104       }
105       assetDB.deleteAsset(a);
106       assetDB.removeAssetFromAllCollections(a);
107     }
108     catch (SQLException ex) {
109       this.fail("Caught unexpected SQLException during testSimpleSearch");
110       ex.printStackTrace();
111     }
112   }
113 
114   /* Test addAsset method */
115   public void testAddAsset() {
116     log.info("Begin testAddAsset");
117     try {
118       Asset a = new Asset();
119       AssetDescriptor fileNameAD = new AssetDescriptor();
120       AssetDescriptor titleAD = new AssetDescriptor();
121       fileNameAD.setTag("FileName");
122       fileNameAD.setValue("testAddAsset.jpg");
123       titleAD.setTag("Title");
124       titleAD.setValue("testAddAsset");
125       AssetDescriptorCollection adc = new AssetDescriptorCollection();
126       adc.addAssetDescriptor(fileNameAD);
127       adc.addAssetDescriptor(titleAD);
128       a.setAssetDescriptors(adc);
129       int size = assetDB.sizeDB();
130       this.assertEquals(1, assetDB.addAsset(a, "TestAddAssetCollection"));
131       this.assertEquals(size + 1, assetDB.sizeDB());
132       this.assertEquals(0, assetDB.addAsset(a, "TestAddAssetCollection"));
133       this.assertNotNull(assetDB.getAsset("testAddAsset.jpg"));
134       this.assertEquals(1, assetDB.deleteAsset(a));
135       assetDB.removeAssetFromAllCollections(a);
136     }
137     catch (SQLException ex) {
138       this.fail("Unexpected SQLException caught in testAddAsset.");
139       ex.printStackTrace();
140     }
141     log.info("Exit testAddAsset");
142   }
143 
144   /* Test deleteAsset method */
145   /* @todo Do a more complete test. Check for size and is Asset still in repository */
146   public void testDeleteAsset() {
147     log.info("Begin testDeleteAsset");
148     try {
149       Asset a = new Asset();
150       AssetDescriptor fileNameAD = new AssetDescriptor();
151       AssetDescriptor titleAD = new AssetDescriptor();
152       fileNameAD.setTag("FileName");
153       fileNameAD.setValue("testDeleteAsset.jpg");
154       titleAD.setTag("Title");
155       titleAD.setValue("TestDeleteAsset");
156       AssetDescriptorCollection adc = new AssetDescriptorCollection();
157       adc.addAssetDescriptor(fileNameAD);
158       adc.addAssetDescriptor(titleAD);
159       a.setAssetDescriptors(adc);
160       Collection collection = new Collection();
161       collection.setCollectionName("TestDeleteAssetCollection");
162       this.assertEquals(1, assetDB.addAsset(a, collection.getCollectionName()));
163       this.assertEquals(1, assetDB.deleteAsset(a));
164       this.assertFalse(assetDB.isAssetInDB(a));
165       assetDB.removeAssetFromAllCollections(a);
166     }
167     catch (SQLException ex) {
168       this.fail("Unexpected SQLException caught in testDeleteAsset.");
169       ex.printStackTrace();
170     }
171     log.info("End testDeleteAsset");
172   }
173 
174   /* Test addSchema Method*/
175   public void testAddSchema() {
176     try {
177       Schema s = new Schema();
178       MetaDataTag fileNameTag = new MetaDataTag();
179       MetaDataTag titleTag = new MetaDataTag();
180       fileNameTag.setName("FileName");
181       titleTag.setName("Title");
182       s.addTag(fileNameTag);
183       s.addTag(titleTag);
184       s.setName("testAddSchema");
185       s.setOwnerName("admin");
186       s.setPermissionID("79");
187       // 3 rows in the SchemaTagsTable are expected to have been modified by
188       // addSchema: FileName, Title and WebFileName (by default)
189       this.assertEquals(3, assetDB.addSchema(s));
190       assetDB.deleteSchema(s);
191     }
192     catch (SchemaException ex) {
193       this.fail(
194           "Caught extremely unexpected SchemaException during testAddSchema");
195       ex.printStackTrace();
196     }
197     catch (SQLException ex) {
198       this.fail("Caught unexpected SQLException during testAddSchema");
199       ex.printStackTrace();
200     }
201   }
202 
203   public void testAddSchemaTag(){
204     try {
205       Schema s = new Schema();
206       s.setName("testAddSchemaTag");
207       s.setOwnerName("testAddSchemaTagUser");
208       s.setPermissionID("79");
209       assetDB.addSchema(s);
210       MetaDataTag fileNameTag = new MetaDataTag();
211       MetaDataTag titleTag = new MetaDataTag();
212       fileNameTag.setName("testAddSchemaTag1");
213       titleTag.setName("testAddSchemaTag2");
214       this.assertEquals(1, assetDB.addSchemaTag(fileNameTag, s.getName()));
215       this.assertEquals(1, assetDB.addSchemaTag(titleTag, s.getName()));
216       this.assertEquals(0, assetDB.addSchemaTag(titleTag, s.getName()));
217       assetDB.deleteSchema(s);
218     }
219     catch (SQLException ex) {
220       this.fail("Caught unexpected SQLException during testAddSchemaTag "+ex.getMessage());
221       ex.printStackTrace();
222     }
223   }
224 
225   public void testDeleteSchemaTag(){
226     try {
227       MetaDataTag fileNameTag = new MetaDataTag();
228       MetaDataTag titleTag = new MetaDataTag();
229       String str = "testDeleteSchemaTag";
230       fileNameTag.setName("testDeleteSchemaTag1");
231       titleTag.setName("testDeleteSchemaTag2");
232       assetDB.addSchemaTag(fileNameTag, str);
233       assetDB.addSchemaTag(titleTag, str);
234       this.assertEquals(1, assetDB.deleteSchemaTag(fileNameTag, str));
235       this.assertEquals(1, assetDB.deleteSchemaTag(titleTag, str));
236       this.assertEquals(0, assetDB.deleteSchemaTag(fileNameTag, str));
237       this.assertFalse(assetDB.isSchemaTagInTable(fileNameTag, str));
238     }
239     catch (SQLException ex) {
240       this.fail("threw a sql exception : " + ex.getMessage());
241     }
242   }
243 
244   public void testDeleteSchema() {
245     try {
246       Schema s = new Schema();
247       s.setName("testDeleteSchema");
248       s.setOwnerName("admin");
249       s.setPermissionID("79");
250       MetaDataTag fileNameTag = new MetaDataTag();
251       MetaDataTag titleTag = new MetaDataTag();
252       fileNameTag.setName("FileName");
253       titleTag.setName("Title");
254       s.addTag(fileNameTag);
255       s.addTag(titleTag);
256 
257       assetDB.addSchema(s);
258       this.assertEquals(1, assetDB.deleteSchema(s));
259     }
260     catch (SQLException ex) {
261       this.fail("Caught unexpected SQLException during testDeleteSchema - " +
262                 ex.getMessage());
263       ex.printStackTrace();
264     }
265     catch (SchemaException ex1) {
266       this.fail("Caught unexpected Schema Exception during testDeleteSchema.");
267     }
268   }
269 
270   public void testGetSchema() {
271     try {
272       Schema s = new Schema();
273       MetaDataTag fileNameTag = new MetaDataTag();
274       MetaDataTag titleTag = new MetaDataTag();
275       fileNameTag.setName("FileName");
276       titleTag.setName("Title");
277       s.addTag(fileNameTag);
278       s.addTag(titleTag);
279       s.setName("newSchema");
280       assetDB.addSchema(s);
281       Schema sch = assetDB.getSchema("newSchema");
282       this.assertNotNull(sch);
283       this.assertEquals("newSchema", sch.getName());
284       assetDB.deleteSchema(s);
285     }
286     catch (SchemaException ex) {
287       this.fail("Caught unexpected SchemaException during testGetSchema");
288       ex.printStackTrace();
289     }
290     catch (SQLException ex) {
291       this.fail("Caught unexpected SQLException during testGetSchema");
292       ex.printStackTrace();
293     }
294   }
295 
296   public void testGetSchemaOfCollection() {
297     try {
298       Collection myCol = new Collection();
299       myCol.setCollectionName("tempCollect");
300       myCol.setDescription("This is the description");
301       myCol.setGroupName("Default");
302       myCol.setOwnerName("Bell, Danny");
303       myCol.setPermissionID("4");
304       myCol.setRepositoryName("Default");
305       myCol.setSchemaName("Default");
306       assetDB.addCollection(myCol);
307       Schema s = assetDB.getSchemaOfCollection(myCol.getCollectionName());
308       this.assertNotNull(s);
309       this.assertEquals("Default", s.getName());
310       assetDB.deleteCollection(myCol);
311     }
312     catch (SchemaException ex) {
313       this.fail("Caught unexpected SchemaException during testGetSchema");
314     }
315     catch (SQLException ex) {
316       this.fail("Caught unexpected SQLException during testGetSchema");
317     }
318   }
319 
320   public void testGetCollectionsWithSchema() {
321     Vector v = new Vector();
322     try {
323       Collection myCol = new Collection();
324       Collection myCol2 = new Collection();
325       myCol.setCollectionName("tempCollect");
326       myCol.setSchemaName("TestGetCollectionsSchema");
327       myCol2.setCollectionName("tempCollect2");
328       myCol2.setSchemaName("TestGetCollectionsSchema");
329       assetDB.addCollection(myCol);
330       assetDB.addCollection(myCol2);
331       v = assetDB.getCollectionsWithSchema(
332           "TestGetCollectionsSchema");
333       this.assertEquals(2, v.size());
334       this.assertTrue(v.contains(myCol.getCollectionName()));
335       this.assertTrue(v.contains(myCol2.getCollectionName()));
336       assetDB.deleteCollection(myCol);
337       assetDB.deleteCollection(myCol2);
338     }
339     catch (SQLException ex) {
340       this.fail("SQLException Caught in testGetCollectionsWithSchema" +
341                 ex.getMessage());
342     }
343     catch (SchemaException ex2){
344       this.fail("Caught unexpected SchemaException in testDelete Collection");
345     }
346   }
347 
348   public void testGetAsset() {
349     try {
350       Asset a = new Asset();
351       AssetDescriptor fileNameAD = new AssetDescriptor();
352       AssetDescriptor titleAD = new AssetDescriptor();
353       fileNameAD.setTag("FileName");
354       fileNameAD.setValue("rockyRoad.jpg");
355       titleAD.setTag("Title");
356       titleAD.setValue("testTitle");
357       AssetDescriptorCollection adc = new AssetDescriptorCollection();
358       adc.addAssetDescriptor(fileNameAD);
359       adc.addAssetDescriptor(titleAD);
360       a.setAssetDescriptors(adc);
361       assetDB.addAsset(a, "TColl");
362       Asset b = assetDB.getAsset("rockyRoad.jpg");
363       this.assertEquals("rockyRoad.jpg", b.getFileName());
364       this.assertEquals("rockyRoad.jpg",
365                         b.getAssetDescriptors().getValue("FileName"));
366       this.assertEquals("Title", b.getAssetDescriptors().getTag("testTitle"));
367       assetDB.deleteAsset(a);
368       assetDB.removeAssetFromAllCollections(a);
369     }
370     catch (SQLException ex) {
371       this.fail("Caught unexpected SQLException during testGetAsset");
372       ex.printStackTrace();
373     }
374   }
375 
376   public void testGetAssetFromCollection() {
377     try {
378       Asset a = new Asset();
379       AssetDescriptor fileNameAD = new AssetDescriptor();
380       AssetDescriptor titleAD = new AssetDescriptor();
381 
382       fileNameAD.setTag("FileName");
383       fileNameAD.setValue("road.jpg");
384 
385       titleAD.setTag("Title");
386       titleAD.setValue("Calle");
387 
388       AssetDescriptorCollection adc = new AssetDescriptorCollection();
389       adc.addAssetDescriptor(fileNameAD);
390       adc.addAssetDescriptor(titleAD);
391       a.setAssetDescriptors(adc);
392 
393       Collection spanColl = new Collection();
394       spanColl.setCollectionName("SpanColl");
395       spanColl.setDescription("This is the description");
396       spanColl.setGroupName("Michelle");
397       spanColl.setOwnerName("Danny");
398       spanColl.setPermissionID("4");
399       spanColl.setRepositoryName("Default");
400       spanColl.setSchemaName("Default");
401 
402       assetDB.addCollection(spanColl);
403 
404       assetDB.addAsset(a, "SpanColl");
405 
406       Asset b = assetDB.getAssetFromCollection("road.jpg", "SpanColl");
407 
408       this.assertNotNull(b);
409       this.assertEquals("FileName", b.getAssetDescriptors().getTag("road.jpg"));
410       this.assertEquals("Calle", b.getAssetDescriptors().getValue("Title"));
411 
412       assetDB.deleteCollection(spanColl);
413       assetDB.deleteAsset(a);
414       assetDB.removeAssetFromAllCollections(a);
415 
416     }
417     catch (SQLException ex) {
418       this.fail(
419           "Caught unexpected SQLException during testGetAssetFromCollection");
420       ex.printStackTrace();
421     }
422 
423     catch (SchemaException ex) {
424       this.fail(
425           "Caught unexpected SchemaException during testGetAssetFromCollection");
426       ex.printStackTrace();
427     }
428   }
429 
430   public void testGetCollectionNames() {
431     Vector v = null;
432     try {
433       v = assetDB.getCollectionNames();
434     }
435     catch (SQLException ex) {
436       this.fail("Caught unexpected SQLException during testGetCollectionNames");
437       ex.printStackTrace();
438     }
439     this.assertFalse(v.isEmpty());
440     for (Iterator i = v.iterator(); i.hasNext(); ) {
441       log.info(i.next());
442     }
443   }
444 
445   public void testGetSchemaNames() {
446     Vector v = null;
447     try {
448       Schema s = new Schema();
449       MetaDataTag fileNameTag = new MetaDataTag();
450       MetaDataTag titleTag = new MetaDataTag();
451       fileNameTag.setName("FileName");
452       titleTag.setName("Title");
453       s.addTag(fileNameTag);
454       s.addTag(titleTag);
455       s.setName("testGetSchemaNames");
456       assetDB.addSchema(s);
457       v = assetDB.getSchemaNames();
458       this.assertTrue(v.contains(s.getName()));
459       assetDB.deleteSchema(s);
460       v = assetDB.getSchemaNames();
461       this.assertFalse(v.contains(s.getName()));
462     }
463     catch (SQLException ex) {
464       this.fail("Unexpected SQLException caught in testGetSchemaNames " +
465                 ex.getMessage());
466     }
467     catch (SchemaException ex2) {
468       this.fail("Unexpected SchemaException caught in testGetSchemaNames " +
469                 ex2.getMessage());
470     }
471   }
472 
473   public void testIsAssetInDB() {
474     try {
475       Asset a = new Asset();
476       AssetDescriptor fileNameAD = new AssetDescriptor();
477       AssetDescriptor titleAD = new AssetDescriptor();
478       fileNameAD.setTag("FileName");
479       fileNameAD.setValue("rocks.jpg");
480       titleAD.setTag("Title");
481       titleAD.setValue("testTitle");
482       AssetDescriptorCollection adc = new AssetDescriptorCollection();
483       adc.addAssetDescriptor(fileNameAD);
484       adc.addAssetDescriptor(titleAD);
485       a.setAssetDescriptors(adc);
486 
487       this.assertFalse(assetDB.isAssetInDB(a));
488     }
489     catch (SQLException ex) {
490       this.fail("Caught unexpected SQLException during testIsAssetInDB");
491       ex.printStackTrace();
492     }
493 
494     try {
495       Asset b = new Asset();
496       AssetDescriptor fileNameAD = new AssetDescriptor();
497       AssetDescriptor titleAD = new AssetDescriptor();
498       fileNameAD.setTag("FileName");
499       fileNameAD.setValue("test.jpg");
500       titleAD.setTag("Title");
501       titleAD.setValue("testTitle");
502       AssetDescriptorCollection adc = new AssetDescriptorCollection();
503       adc.addAssetDescriptor(fileNameAD);
504       adc.addAssetDescriptor(titleAD);
505       b.setAssetDescriptors(adc);
506       assetDB.addAsset(b, "TestCollection");
507       this.assertTrue(assetDB.isAssetInDB(b));
508       assetDB.deleteAsset(b);
509       assetDB.removeAssetFromAllCollections(b);
510     }
511     catch (SQLException ex) {
512       this.fail("Caught unexpected SQLException during testIsAssetInDB");
513       ex.printStackTrace();
514     }
515   }
516 
517   public void testGetUser() throws SQLException {
518     User user = new User();
519     user.setFirstName("Christy");
520     user.setLastName("Garcia");
521     user.setMiddleInitial("");
522     user.setOrganization("Spelman College");
523     user.setPassword("foo2");
524     user.setUserName("michelleF");
525     assetDB.addUser(user);
526     User user1 = assetDB.getUser("michelleF", "foo2");
527 
528     this.assertNotNull(user1);
529     this.assertEquals("michelleF", user.getUserName());
530     this.assertEquals("Spelman College", user.getOrganization());
531     assetDB.deleteUser(user);
532   }
533 
534   public void testAddUser() {
535     User user = new User();
536     user.setFirstName("Christy");
537     user.setLastName("Garcia");
538     user.setMiddleInitial("");
539     user.setOrganization("SU");
540     user.setPassword("foo");
541     user.setUserName("christy");
542     try {
543       this.assertEquals(1, assetDB.addUser(user));
544       this.assertEquals(1, assetDB.deleteUser(user));
545     }
546     catch (SQLException ex) {
547       this.fail("Caught unexpected SQLException in testAddUser");
548       ex.printStackTrace();
549     }
550   }
551 
552   public void testGetAllAssetsFromCollection() {
553     Collection c = new Collection("TestGetAllAssetsFromCollectionCollection");
554     Asset one = new Asset();
555     one.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("FileName",
556         "one.jpg"));
557     one.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Author",
558         "Mike"));
559     one.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Title",
560         "TestOne"));
561     Asset two = new Asset();
562     two.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("FileName",
563         "two.jpg"));
564     two.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Author",
565         "Mike"));
566     two.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Title",
567         "TestTwo"));
568     Asset three = new Asset();
569     three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
570         "FileName", "three.jpg"));
571     three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Author",
572         "Mike"));
573     three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Title",
574         "TestThree"));
575     Asset four = new Asset();
576     four.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
577         "FileName", "four.jpg"));
578     four.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Author",
579         "Mike"));
580     four.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Title",
581         "TestFour"));
582     c.addAsset(one);
583     c.addAsset(two);
584     c.addAsset(three);
585     c.addAsset(four);
586 
587     try {
588       CollectionView results = assetDB.getAllAssetsFromCollection(
589           "TestGetAllAssetsFromCollectionCollection");
590       this.assertEquals(4, results.size());
591       Iterator i = results.iterator();
592       Asset tempAss;
593       while (i.hasNext()) {
594         tempAss = (Asset) i.next();
595         this.assertNotNull(tempAss);
596       }
597       /* Remove the Assets from the database */
598       assetDB.deleteAsset(one);
599       assetDB.deleteAsset(two);
600       assetDB.deleteAsset(three);
601       assetDB.deleteAsset(four);
602       assetDB.removeAssetFromAllCollections(one);
603       assetDB.removeAssetFromAllCollections(two);
604       assetDB.removeAssetFromAllCollections(three);
605       assetDB.removeAssetFromAllCollections(four);
606     }
607     catch (SQLException ex) {
608       this.fail(
609           "Caught unexpected SQLException in testAllAssetsFromCollection " +
610           ex.getMessage());
611     }
612     catch (SchemaException ex) {
613       this.fail(
614           "Caught unexpected SchemaException in testAllAssetsFromCollection " +
615           ex.getMessage());
616     }
617   }
618 
619   /*Test isAssetInCollection */
620   public void testIsAssetInCollection() {
621     try {
622       Collection collection = new Collection(
623           "TestIsAssetInCollectionCollection");
624       Asset three = new Asset();
625       three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
626           "FileName", "three.jpg"));
627       three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
628           "Author", "Mike"));
629       three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
630           "Title", "TestThree"));
631       collection.addAsset(three);
632       Asset one = new Asset();
633       one.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
634           "FileName", "one.jpg"));
635       one.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Author",
636           "Mike"));
637       one.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Title",
638           "TestOne"));
639       collection.addAsset(one);
640       /* Checking to see if the asset just inserted is in the collection */
641       this.assertTrue(assetDB.isAssetInCollection(one.getFileName(),
642           "TestIsAssetInCollectionCollection"));
643       this.assertTrue(assetDB.isAssetInCollection(three.getFileName(),
644           "TestIsAssetInCollectionCollection"));
645 
646       Collection newCollection = new Collection(
647           "Not the collection you were thinking of");
648       Asset two = new Asset();
649       two.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
650           "FileName", "two.jpg"));
651       two.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Author",
652           "Mike"));
653       two.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor("Title",
654           "TestTwo"));
655       newCollection.addAsset(two);
656       /* Checking to see if the asset just inserted into a different collection
657         is not in the collection */
658       this.assertFalse(assetDB.isAssetInCollection(two.getFileName(),
659           "TestIsAssetInCollectionCollection"));
660       /* Remove the Assets from the database */
661       assetDB.deleteAsset(one);
662       assetDB.removeAssetFromAllCollections(one);
663       assetDB.deleteAsset(two);
664       assetDB.removeAssetFromAllCollections(two);
665       assetDB.deleteAsset(three);
666       assetDB.removeAssetFromAllCollections(three);
667       this.assertFalse(assetDB.isAssetInCollection(one.getFileName(),
668           "TestIsAssetInCollectionCollection"));
669       this.assertFalse(assetDB.isAssetInCollection(three.getFileName(),
670           "TestIsAssetInCollectionCollection"));
671       this.assertFalse(assetDB.isAssetInCollection(two.getFileName(),
672           "TestIsAssetInCollectionCollection"));
673     }
674     catch (SQLException ex) {
675       this.fail("Unexpected SQL Exception caught in testContainsAgain : " +
676                 ex.getMessage());
677     }
678 
679   }
680 
681   public void testAddAndDeleteUserToGroup() {
682     User user = new User();
683     user.setUserName("addAndDeleteUser");
684     try {
685       this.assertEquals(1, assetDB.addUser(user));
686       this.assertEquals(1, assetDB.addUserToGroup("addAndDeleteUser", "Administators"));
687       this.assertEquals(1, assetDB.deleteUser(user));
688       this.assertFalse(assetDB.isUserInDB(user));
689     }
690     catch (SQLException ex) {
691       this.fail("Caught unexpected SQLException in testAddAndDeleteUserToGroup");
692       ex.printStackTrace();
693     }
694   }
695 
696   public void testAddGroup() {
697     Group group = new Group();
698     group.setGroupName("testGroup");
699     group.setDescription("this is a test Group");
700     try {
701       assetDB = AssetDB.instance();
702       this.assertEquals(1, assetDB.addGroup(group));
703 
704     }
705     catch (SQLException ex) {
706       this.fail("Caught unexpected SQLException in testAddGroup: " +
707                 ex.getMessage());
708       ex.printStackTrace();
709     }
710   }
711 
712   public void testIsGroupInDB() {
713     Group group = new Group();
714     Group ug = new Group();
715     group.setGroupName("testGroup");
716     ug.setGroupName("TestUG");
717     group.setDescription("this is a test Group");
718     try {
719       assetDB = AssetDB.instance();
720       this.assertTrue(assetDB.isGroupInDB(group));
721       this.assertFalse(assetDB.isGroupInDB(ug));
722     }
723     catch (SQLException ex) {
724       this.fail("Caught unexpected SQLException in testIsGroupInDB" + ex.getMessage());
725     }
726   }
727 
728   public void testGetGroupNames() {
729     Vector v = null;
730     try {
731         assetDB = AssetDB.instance();
732         User user = new User();
733         user.setUserName("berryn");
734         Group group = new Group();
735         group.setGroupName("TesterGroup");
736         assetDB.addGroup(group);
737         assetDB.addUserToGroup(user.getUserName(), "TesterGroup");
738         assetDB.addUser(user);
739         v = assetDB.getGroupNames();
740         this.assertTrue(v.contains("TesterGroup"));
741         assetDB.deleteUser(user);
742         assetDB.deleteGroup("TesterGroup");
743         v = assetDB.getGroupNames();
744         this.assertFalse(v.contains("TesterGroup"));
745       }
746      catch (SQLException ex) {
747        this.fail("SQLException Caught in testGetGroupNames"+ex.getMessage());
748       }
749   }
750 
751   public void testDeleteGroup() {
752     Group group = new Group();
753     group.setGroupName("testGroup");
754     try {
755       assetDB = AssetDB.instance();
756       this.assertEquals(1, assetDB.deleteGroup(group.getGroupName()));
757     }
758     catch (SQLException ex) {
759       this.fail("Caught unexpected SQLException in testDeleteGroup");
760       ex.printStackTrace();
761     }
762   }
763 
764   public void testDeleteUser() {
765     User user = new User();
766     user.setUserName("christy");
767   }
768 
769   /* Tests sizeDB method */
770   public void testSizeDB() {
771     log.info("Begin testSizeDB");
772     try {
773       int size = assetDB.sizeDB();
774       Asset one = new Asset();
775       one.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
776           "FileName", "testSizeDB1.jpg"));
777       Asset two = new Asset();
778       two.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
779           "FileName", "testSizeDB2.jpg"));
780       Asset three = new Asset();
781       three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
782           "FileName", "testSizeDB3.jpg"));
783 
784       if (assetDB.addAsset(one, "TestSizeDBCollection") == 0) {
785         size--;
786       }
787       if (assetDB.addAsset(two, "TestSizeDBCollection") == 0) {
788         size--;
789       }
790       if (assetDB.addAsset(three, "TestSizeDBCollection") == 0) {
791         size--;
792       }
793       this.assertEquals(size + 3, assetDB.sizeDB());
794       assetDB.deleteAsset(one);
795       this.assertEquals(size + 2, assetDB.sizeDB());
796       assetDB.deleteAsset(two);
797       this.assertEquals(size + 1, assetDB.sizeDB());
798       assetDB.deleteAsset(three);
799       this.assertEquals(size, assetDB.sizeDB());
800       assetDB.removeAssetFromAllCollections(one);
801       assetDB.removeAssetFromAllCollections(two);
802       assetDB.removeAssetFromAllCollections(three);
803     }
804     catch (SQLException ex) {
805       this.fail("Caught unexpected SQLException in testSizeDB : " +
806                 ex.getMessage());
807     }
808     log.info("End testSizeDB");
809   }
810 
811   public void testAddCollection() throws SQLException {
812     Collection collection = new Collection();
813     collection.setCollectionName("tester");
814     collection.setOwnerName("Christy");
815 
816     try {
817       this.assertEquals(1, assetDB.addCollection(collection));
818       this.assertEquals(1,assetDB.deleteCollection(collection));
819     }
820     catch (SQLException ex) {
821       this.fail("Caught unexpected SQLException in testAddCollection");
822     }
823     catch (SchemaException ex2){
824       this.fail("Caught unexpected SchemaException in testDelete Collection");
825     }
826   }
827 
828   public void testDeleteCollection() {
829     try {
830       Collection collection = new Collection();
831       collection.setCollectionName("testDeleteCollection");
832       collection.setOwnerName("testDeleteCollectionOwner");
833       collection.setPermissionID("79");
834 
835       Schema aView = new Schema();
836       aView.setName("testDeleteCollectionAssetViewMask");
837       aView.setOwnerName("testDeleteCollectionAssetViewOwner");
838       aView.setPermissionID("79");
839       MetaDataTag tag = new MetaDataTag();
840       tag.setName("testDeleteCollectionAssetViewMaskTag");
841       aView.addTag(tag);
842 
843       Schema rView = new Schema();
844       rView.setName("testDeleteCollectionResultsMask");
845       rView.setOwnerName("testDeleteCollectionResultsOwner");
846       rView.setPermissionID("79");
847       tag.setName("testDeleteCollectionResultsMaskTag");
848       rView.addTag(tag);
849       this.assertEquals(1, assetDB.addCollection(collection));
850       this.assertEquals(1, assetDB.deleteCollection(collection));
851       this.assertEquals(0, assetDB.deleteSchema(aView));
852       this.assertEquals(0, assetDB.deleteSchema(rView));
853     }
854     catch (SQLException ex) {
855       this.fail("Caught unexpected SQLException in testDeleteCollection");
856     }
857     catch (SchemaException ex2) {
858       this.fail("Caught unexpected SchemaException in testDelete Collection");
859     }
860   }
861 
862   public void testIsCollectionInDB() {
863     try {
864       Collection collection = new Collection();
865       collection.setCollectionName("tester");
866       collection.setOwnerName("Michelle");
867       assetDB.addCollection(collection);
868       this.assertTrue(assetDB.isCollectionInDB(collection));
869       assetDB.deleteCollection(collection);
870     }
871     catch (SQLException ex) {
872       this.fail("Unexpected SQL error in testIsCollectionInDB.");
873       ex.printStackTrace();
874     }
875     catch (SchemaException ex2){
876       this.fail("Caught unexpected SchemaException in testDelete Collection");
877     }
878   }
879 
880   public void testIsSchemaInDB() {
881     try {
882       Schema s = new Schema();
883       MetaDataTag fileNameTag = new MetaDataTag();
884       MetaDataTag titleTag = new MetaDataTag();
885       fileNameTag.setName("FileName");
886       titleTag.setName("Title");
887       s.addTag(fileNameTag);
888       s.addTag(titleTag);
889       s.setName("mySchema");
890       assetDB.addSchema(s);
891       this.assertTrue(assetDB.isSchemaInDB(s));
892       assetDB.deleteSchema(s);
893     }
894     catch (SQLException ex) {
895       this.fail("Unexpected SQL error in testIsSchemaInDB.");
896       ex.printStackTrace();
897     }
898     catch (SchemaException ex) {
899       this.fail("Unexpected Schema error in testIsSchemaInDB.");
900       ex.printStackTrace();
901     }
902 
903   }
904 
905   public void testIsSchemaTagInTable(){
906     try {
907       Schema schema = new Schema();
908       MetaDataTag testTag = new MetaDataTag();
909       schema.setName("testIsSchemaTagInTableSchema");
910       schema.setOwnerName("testIsSchemaTagInTableOwner");
911       schema.setPermissionID("79");
912       testTag.setName("testIsSchemaTagInTable");
913       schema.addTag(testTag);
914       assetDB.addSchemaTag(testTag, schema.getName());
915       this.assertTrue(assetDB.isSchemaTagInTable(testTag, schema.getName()));
916       assetDB.deleteSchema(schema);
917       this.assertFalse(assetDB.isSchemaTagInTable(testTag, schema.getName()));
918     }
919     catch (SQLException ex) {
920       this.fail("Unexpected SQL error in testIsSchemaTagInTable " +
921                 ex.getMessage());
922       ex.printStackTrace();
923     }
924     catch (SchemaException ex2) {
925       this.fail("Unexpected SchemaException in testIsSchemaTagInTable " +
926                 ex2.getMessage());
927       ex2.printStackTrace();
928     }
929   }
930 
931   public void testIsUserInDB() {
932     try {
933       User user = new User();
934       user.setFirstName("Al");
935       user.setLastName("Pacino");
936       user.setMiddleInitial("");
937       user.setOrganization("Mafia");
938       user.setPassword("family");
939       user.setUserName("alpacino");
940       assetDB.addUser(user);
941       this.assertTrue(assetDB.isUserInDB(user));
942       assetDB.deleteUser(user);
943     }
944     catch (SQLException ex) {
945       this.fail("Unexpected SQL error in testIsUserInDB.");
946       ex.printStackTrace();
947     }
948   }
949 
950   /* Test removing Asset from Collection */
951   public void testRemoveAssetFromCollection() {
952     try {
953       Collection collection = new Collection(
954           "TestRemoveAssetFromCollectionCollection");
955       Asset three = new Asset();
956       three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
957           "FileName", "three.jpg"));
958       three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
959           "Author", "Mike"));
960       three.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
961           "Title", "TestThree"));
962       collection.addAsset(three);
963       /* RemoveAssetFromCollection should decrement the size of the Collection but not the
964            repository since the Asset is only removed from the AssetsCollectionsTable, not
965           the AssetTable. Therefore, two sizes must be maintained and checked in the testing
966           method. */
967       int size = assetDB.sizeDB();
968       int collectionSize = collection.size();
969       this.assertTrue(assetDB.removeAssetFromCollection(three,
970           collection.getCollectionName()));
971       this.assertEquals(collectionSize - 1, collection.size());
972       this.assertEquals(size, assetDB.sizeDB());
973       Asset one = new Asset();
974       one.getAssetDescriptors().addAssetDescriptor(new AssetDescriptor(
975           "FileName", "neverused.jpg"));
976       this.assertFalse(assetDB.removeAssetFromCollection(one,
977           collection.getCollectionName()));
978       this.assertEquals(collectionSize - 1, collection.size());
979       this.assertEquals(size, assetDB.sizeDB());
980       assetDB.deleteAsset(one);
981       assetDB.deleteAsset(three);
982       assetDB.removeAssetFromAllCollections(one);
983       assetDB.removeAssetFromAllCollections(three);
984     }
985     catch (SQLException ex) {
986       this.fail(
987           "Unexpected SQL Exception caught in TestRemoveAssetFromCollection : " +
988           ex.getMessage());
989     }
990   }
991 
992   /*
993    *   This test adds a user, adds a group, adds a user to a group and checks
994    * that the user and group were added and that the user was added to the group
995    * Then the user and the group is deleted.  The user is automatically deleted from
996    * the users groups table
997    */
998   public void testAddUserToGroup(){
999     User user = new User();
1000    user.setUserName("TestAddUser");
1001    Group group = new Group();
1002    group.setGroupName("TestGroup");
1003
1004    try {
1005      this.assertEquals(1, assetDB.instance().addUser(user));
1006      this.assertEquals(1, assetDB.instance().addGroup(group));
1007      this.assertEquals(1, assetDB.instance().addUserToGroup(user.getUserName(), "TestGroup"));
1008      this.assertTrue(assetDB.instance().getUsersInGroup("TestGroup").contains("TestAddUser"));
1009      this.assertEquals(1, assetDB.instance().deleteUser(user));
1010      this.assertEquals(1,assetDB.instance().deleteGroup("TestGroup"));
1011    }
1012    catch (SQLException ex) {
1013    }
1014  }
1015
1016  /*
1017   * Test for get users in group
1018   */
1019  public void testGetUsersInGroup(){
1020    User user = new User();
1021    user.setUserName("testInsert");
1022    Group ugrp = new Group();
1023    ugrp.setGroupName("TestGroup");
1024    try {
1025      this.assertEquals(1, assetDB.instance().addGroup(ugrp));
1026      this.assertEquals(1, assetDB.instance().addUser(user));
1027      this.assertEquals(1, assetDB.instance().addUserToGroup(user.getUserName(), "TestGroup"));
1028      this.assertTrue(assetDB.instance().getUsersInGroup("TestGroup").contains(
1029          "testInsert"));
1030      this.assertEquals(1, assetDB.instance().deleteGroup("TestGroup"));
1031      this.assertEquals(1, assetDB.instance().deleteUser(user));
1032    }
1033    catch (SQLException ex) {
1034      this.fail("SQLException");
1035    }
1036
1037  }
1038  /* TestRemoveAssetFromAllCollections! */
1039  public void testRemoveAssetFromAllCollections() {
1040    try {
1041      /* Create/add one dummy collection */
1042      Collection collection = new Collection();
1043      collection.setCollectionName(
1044          "TestRemoveAssetFromAllCollectionsCollection");
1045      assetDB.addCollection(collection);
1046      /* Create/add another dummy collection */
1047      Collection collection2 = new Collection();
1048      collection2.setCollectionName(
1049          "TestRemoveAssetFromAllCollectionsCollection2");
1050      assetDB.addCollection(collection2);
1051      /* Create/add a dummy asset */
1052      Asset asset = new Asset();
1053      AssetDescriptor ad = new AssetDescriptor();
1054      AssetDescriptorCollection adc = new AssetDescriptorCollection();
1055      ad.setTag("FileName");
1056      ad.setValue("testRemoveAssetFromAllCollections.jpg");
1057      adc.addAssetDescriptor(ad);
1058      asset.setAssetDescriptors(adc);
1059      assetDB.addAsset(asset, collection.getCollectionName());
1060      assetDB.addAsset(asset, collection2.getCollectionName());
1061
1062      /* Remove the asset from all collections and make sure it is removed from
1063       both of the collection */
1064      this.assertTrue(assetDB.removeAssetFromAllCollections(asset) != 0);
1065      this.assertTrue(assetDB.isAssetInDB(asset));
1066      this.assertFalse(assetDB.isAssetInCollection(asset.getFileName(),
1067          collection.getCollectionName()));
1068      this.assertFalse(assetDB.isAssetInCollection(asset.getFileName(),
1069          collection2.getCollectionName()));
1070
1071      assetDB.deleteCollection(collection);
1072      assetDB.deleteCollection(collection2);
1073      assetDB.deleteAsset(asset);
1074      assetDB.removeAssetFromAllCollections(asset);
1075    }
1076    catch (SQLException ex) {
1077      this.fail(
1078          "Unexpected SQLException caught in testRemoveAssetFromAllCollections " +
1079          ex.getMessage());
1080    }
1081    catch (SchemaException ex2){
1082      this.fail("Caught unexpected SchemaException in testDelete Collection");
1083    }
1084  }
1085
1086  /* @todo refactor variable names to include unique naming */
1087  public void testGetCollection() {
1088    try {
1089      Collection collection = new Collection();
1090      collection.setCollectionName("forresterCollection");
1091      collection.setOwnerName("MForrester");
1092      collection.setRepositoryName("MichellesRepository");
1093      collection.setPermissionID("127");
1094      collection.setSchemaName("collSchema");
1095      assetDB.addCollection(collection);
1096
1097      Collection tempCol = assetDB.getCollection("forresterCollection");
1098      this.assertEquals("forresterCollection", tempCol.getCollectionName());
1099      this.assertEquals("MForrester", tempCol.getOwnerName());
1100      this.assertEquals("127", tempCol.getPermissionID());
1101      this.assertEquals("MichellesRepository", tempCol.getRepositoryName());
1102      this.assertEquals("collSchema", tempCol.getSchemaName());
1103      assetDB.deleteCollection(collection);
1104    }
1105    catch (SQLException ex) {
1106      this.fail("Error in testGetCollection.");
1107      ex.printStackTrace();
1108    }
1109    catch (SchemaException ex2){
1110      this.fail("Caught unexpected SchemaException in testDelete Collection");
1111    }
1112  }
1113
1114  public void testCollectionSize() {
1115    try {
1116      int size = assetDB.getNumberOfCollectionsInDB();
1117      Collection c = new Collection("testCollectionSizeCollection");
1118      assetDB.addCollection(c);
1119      this.assertEquals(size + 1, assetDB.getNumberOfCollectionsInDB());
1120      assetDB.deleteCollection(c);
1121      this.assertEquals(size, assetDB.getNumberOfCollectionsInDB());
1122    }
1123    catch (SQLException ex) {
1124      this.fail("Unexpected SQL Exception in testCollectionSize.");
1125      ex.printStackTrace();
1126    }
1127    catch (SchemaException ex2){
1128      this.fail("Caught unexpected SchemaException in testDelete Collection");
1129    }
1130  }
1131
1132  public void testGetNumberOfCollectionsInDB() {
1133    try {
1134      int countBeforeAdding = 0;
1135      int countAfterAdding = 0;
1136      countBeforeAdding = assetDB.getNumberOfCollectionsInDB();
1137      Collection tempCol = new Collection("testGetNumberOfCollectionsInDB");
1138
1139      tempCol.setDescription("This is just for testing.");
1140      tempCol.setGroupName("Dannys Group");
1141      tempCol.setOwnerName("Danny");
1142
1143      assetDB.addCollection(tempCol);
1144      countAfterAdding = assetDB.getNumberOfCollectionsInDB();
1145      this.assertEquals(countBeforeAdding + 1, countAfterAdding);
1146      assetDB.deleteCollection(tempCol);
1147      countAfterAdding = assetDB.getNumberOfCollectionsInDB();
1148      this.assertEquals(countBeforeAdding, countAfterAdding);
1149    }
1150    catch (SQLException ex) {
1151      ex.printStackTrace();
1152      this.fail("Unexpected SQL Exception in testGetNumberOfCollectionsInDB");
1153    }
1154    catch (SchemaException ex2){
1155      this.fail("Caught unexpected SchemaException in testDelete Collection");
1156    }
1157  }
1158
1159  public void testSetSchemaOfCollection() {
1160    try {
1161      Collection tempCol = new Collection("testSetSchemaOfCollection");
1162      tempCol.setDescription("This is just for testing.");
1163      tempCol.setGroupName("Dannys Group");
1164      tempCol.setOwnerName("Danny");
1165      assetDB.addCollection(tempCol);
1166      Schema mySchema = assetDB.getSchema("Default");
1167      assetDB.setSchemaOfCollection(tempCol, mySchema);
1168      Schema newSchema = assetDB.getSchemaOfCollection(
1169          "testSetSchemaOfCollection");
1170      this.assertEquals(mySchema, newSchema);
1171      assetDB.deleteCollection(tempCol);
1172    }
1173    catch (SchemaException ex) {
1174      this.fail(
1175          "Caught unexpected Schema Exception in testSetSchemaOfCollection.");
1176      ex.printStackTrace();
1177    }
1178    catch (SQLException ex) {
1179      this.fail("Caught unexpected SQL Exception in testSetSchemaOfCollection.");
1180      ex.printStackTrace();
1181    }
1182  }
1183
1184  public void testUpdateSchema(){
1185    try {
1186      Schema s = new Schema();
1187      s.setName("testUpdateSchema");
1188      s.setGroupName("testUpdateSchemaGroup");
1189      s.setOwnerName("testUpdateSchemaOwner");
1190      s.setPermissionID("79");
1191      assetDB.addSchema(s);
1192      s.setGroupName("NewTestUpdateSchemaGroup");
1193      s.setPermissionID("95");
1194      Schema tester = assetDB.getSchema(s.getName());
1195      this.assertEquals(1, assetDB.updateSchema(s));
1196      tester = assetDB.getSchema(s.getName());
1197      this.assertTrue(tester.getPermissionID().equals("95"));
1198      this.assertTrue(tester.getGroupName().equals("NewTestUpdateSchemaGroup"));
1199      assetDB.deleteSchema(s);
1200    }
1201    catch (SQLException ex) {
1202      this.fail("Caught unexpected SQL Exception in testUpdateSchema :"+ex.getMessage());
1203      ex.printStackTrace();
1204    }
1205    catch (SchemaException ex2) {
1206      this.fail("Caught unexpected SchemaException in testUpdateSchema :"+ex2.getMessage());
1207      ex2.printStackTrace();
1208    }
1209  }
1210
1211  public void testUpdateUser() {
1212    try {
1213      User user = new User();
1214      user.setUserName("testUpdateUser");
1215      user.setFirstName("John");
1216      user.setMiddleInitial("Q");
1217      user.setLastName("Public");
1218      user.setPassword(DBUtils.instance().hash(""));
1219      assetDB.addUser(user);
1220
1221      user = new User();
1222      user.setUserName("testUpdateUser");
1223      user.setFirstName("Jane");
1224      user.setMiddleInitial("H");
1225      user.setLastName("Generic");
1226      user.setPassword(DBUtils.instance().hash(""));
1227      assetDB.updateUser(user);
1228
1229      user = assetDB.getUser("testUpdateUser", "");
1230      this.assertNotNull(user);
1231      this.assertEquals(user.getFirstName(), "Jane");
1232      this.assertEquals(user.getMiddleInitial(), "H");
1233      this.assertEquals(user.getLastName(), "Generic");
1234
1235      assetDB.deleteUser(user);
1236    }
1237    catch (SQLException ex) {
1238      this.fail("Caught unexpected SQL Exception in testUpdateUser: " +
1239                ex.getMessage());
1240      ex.printStackTrace();
1241    }
1242  }
1243
1244  /* Test the addAssetToFavorites method */
1245  public void testAddAssetToFavorites() {
1246    try {
1247      // Create/populate a user
1248      User user = new User();
1249      user.setUserName("testAddAssetToFavoritesUser");
1250      // Create/populate an asset and add to database
1251      Asset asset1 = new Asset();
1252      AssetDescriptor ad = new AssetDescriptor();
1253      AssetDescriptorCollection adc = new AssetDescriptorCollection();
1254      ad.setTag("FileName");
1255      ad.setValue("testAddAssetToFavorites.jpg");
1256      adc.addAssetDescriptor(ad);
1257      asset1.setAssetDescriptors(adc);
1258      assetDB.addAsset(asset1, "AllAssets");
1259      this.assertEquals(1, assetDB.addAssetToFavorites(asset1, user));
1260      DBUtils dbUtils = null;
1261      dbUtils = DBUtils.instance();
1262      CollectionView userFavs = dbUtils.getUsersFavorites(user);
1263      this.assertTrue(userFavs.contains(asset1));
1264      assetDB.removeAssetFromFavorites(asset1, user);
1265      assetDB.deleteAsset(asset1);
1266      assetDB.removeAssetFromAllCollections(asset1);
1267    }
1268    catch (SQLException ex) {
1269      this.fail("Unexpected SQLException thrown in testAddAssetToFavorites : " +
1270                ex.getMessage());
1271    }
1272  }
1273
1274  /* Test the removeAssetFromFavorites method */
1275  public void testRemoveAssetFromFavorites() {
1276    try {
1277      // Create/populate a user
1278      User user = new User();
1279      user.setUserName("testRemoveAssetFromFavoritesUser");
1280      // Create/populate an asset and add to database
1281      Asset asset1 = new Asset();
1282      AssetDescriptor ad = new AssetDescriptor();
1283      AssetDescriptorCollection adc = new AssetDescriptorCollection();
1284      ad.setTag("FileName");
1285      ad.setValue("testRemoveAssetFromFavorites.jpg");
1286      adc.addAssetDescriptor(ad);
1287      asset1.setAssetDescriptors(adc);
1288      assetDB.addAsset(asset1, "AllAssets");
1289      assetDB.addAssetToFavorites(asset1, user);
1290      DBUtils dbUtils = null;
1291
1292      dbUtils = DBUtils.instance();
1293      CollectionView userFavs = dbUtils.getUsersFavorites(user);
1294      this.assertTrue(userFavs.contains(asset1));
1295      this.assertEquals(1, assetDB.removeAssetFromFavorites(asset1, user));
1296      userFavs = dbUtils.getUsersFavorites(user);
1297      this.assertFalse(userFavs.contains(asset1));
1298      assetDB.removeAssetFromAllCollections(asset1);
1299      assetDB.deleteAsset(asset1);
1300    }
1301    catch (SQLException ex) {
1302      this.fail(
1303          "Unexpected SQLException thrown in testRemoveAssetFromFavorites : " +
1304          ex.getMessage());
1305    }
1306  }
1307
1308  /*
1309   * Create two assets and two users, add one asset to one user, one to the other.
1310   * Assert that each has their respective asset and that the other does not have
1311   * their asset.
1312   */
1313  public void testIsAssetInFavorites() {
1314    try {
1315      // Create/populate two users
1316      User user = new User();
1317      user.setUserName("testIsAssetInFavoritesUser");
1318      User user2 = new User();
1319      user2.setUserName("testIsAssetInFavoritesUser2");
1320
1321      // Create/populate an asset and add to database
1322      Asset asset1 = new Asset();
1323      AssetDescriptor ad = new AssetDescriptor();
1324      AssetDescriptorCollection adc = new AssetDescriptorCollection();
1325      ad.setTag("FileName");
1326      ad.setValue("testIsAssetInFavorites.jpg");
1327      adc.addAssetDescriptor(ad);
1328      asset1.setAssetDescriptors(adc);
1329      assetDB.addAsset(asset1, "AllAssets");
1330      assetDB.addAssetToFavorites(asset1, user);
1331
1332      // Create/populate another asset and add to database
1333      Asset asset2 = new Asset();
1334      AssetDescriptor ad2 = new AssetDescriptor();
1335      AssetDescriptorCollection adc2 = new AssetDescriptorCollection();
1336
1337      ad2.setTag("FileName");
1338      ad2.setValue("testIsAssetInFavorites2.jpg");
1339      adc2.addAssetDescriptor(ad2);
1340      asset2.setAssetDescriptors(adc2);
1341      assetDB.addAsset(asset2, "AllAssets");
1342      assetDB.addAssetToFavorites(asset2, user2);
1343
1344      this.assertTrue(assetDB.isAssetInFavorites(asset1, user));
1345      this.assertFalse(assetDB.isAssetInFavorites(asset2, user));
1346      this