src/test/TestAnnotationTypeCategory.java

Code
Comments
Other
Rev Date Author Line
4460 05 Sep 08 jari 1 /*
4460 05 Sep 08 jari 2   $Id$
4460 05 Sep 08 jari 3
3675 16 Aug 07 jari 4     Copyright (C) 2006 Nicklas Nordborg, Dominic Oyeniran
3675 16 Aug 07 jari 5     Copyright (C) 2007 Nicklas Nordborg
4460 05 Sep 08 jari 6
4460 05 Sep 08 jari 7   This file is part of BASE - BioArray Software Environment.
4460 05 Sep 08 jari 8   Available at http://base.thep.lu.se/
4460 05 Sep 08 jari 9
4460 05 Sep 08 jari 10   BASE is free software; you can redistribute it and/or
4460 05 Sep 08 jari 11   modify it under the terms of the GNU General Public License
4480 05 Sep 08 jari 12   as published by the Free Software Foundation; either version 3
4460 05 Sep 08 jari 13   of the License, or (at your option) any later version.
4460 05 Sep 08 jari 14
4460 05 Sep 08 jari 15   BASE is distributed in the hope that it will be useful,
4460 05 Sep 08 jari 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
4460 05 Sep 08 jari 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4460 05 Sep 08 jari 18   GNU General Public License for more details.
4460 05 Sep 08 jari 19
4460 05 Sep 08 jari 20   You should have received a copy of the GNU General Public License
4514 11 Sep 08 jari 21   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4460 05 Sep 08 jari 22 */
4460 05 Sep 08 jari 23
4460 05 Sep 08 jari 24 import net.sf.basedb.core.*;
4460 05 Sep 08 jari 25
4460 05 Sep 08 jari 26 import java.util.Date;
5340 10 May 10 nicklas 27 import java.util.Set;
4460 05 Sep 08 jari 28
4460 05 Sep 08 jari 29 /**
4460 05 Sep 08 jari 30   Tests the implementation of annotation type category.
4460 05 Sep 08 jari 31   @author Dominic Oyeniran
4460 05 Sep 08 jari 32   @version 2.0
4460 05 Sep 08 jari 33
4460 05 Sep 08 jari 34   @see AnnotationTypeCategory
4460 05 Sep 08 jari 35 */
4460 05 Sep 08 jari 36 public class TestAnnotationTypeCategory 
4460 05 Sep 08 jari 37 {
4460 05 Sep 08 jari 38
4460 05 Sep 08 jari 39   static boolean ok = true;
4460 05 Sep 08 jari 40   
4460 05 Sep 08 jari 41   public static void main(String[] args)
4460 05 Sep 08 jari 42   {
4460 05 Sep 08 jari 43     TestUtil.checkArgs(args);
4460 05 Sep 08 jari 44     TestUtil.begin();
4460 05 Sep 08 jari 45     ok = test_all();
4460 05 Sep 08 jari 46     TestUtil.stop();
4460 05 Sep 08 jari 47   }
4460 05 Sep 08 jari 48
4460 05 Sep 08 jari 49   static boolean test_all()
4460 05 Sep 08 jari 50   {
4460 05 Sep 08 jari 51     write("++Testing annotation type categories");
4460 05 Sep 08 jari 52     write_header();
4460 05 Sep 08 jari 53     // Standard tests: create, load, list annotation type categories
7099 07 Mar 16 nicklas 54     int id = test_create("Test Category 1", true);
7099 07 Mar 16 nicklas 55     int id2 = test_create("Test Category 2", false);
4460 05 Sep 08 jari 56     test_load(id);
4460 05 Sep 08 jari 57
4460 05 Sep 08 jari 58     test_list(-1);
4460 05 Sep 08 jari 59
4460 05 Sep 08 jari 60     // extra test: add categories to annotation type
4544 25 Sep 08 nicklas 61     int id_int = TestAnnotationType.test_create(null, Type.INT, 0, 10, null, Item.SAMPLE, 1, null, true);
4460 05 Sep 08 jari 62     test_add_categories(id_int, id, id2);
4460 05 Sep 08 jari 63     test_list_categories(id_int, 2);
4460 05 Sep 08 jari 64
4460 05 Sep 08 jari 65     if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter();
4460 05 Sep 08 jari 66     test_remove_categories(id_int, id, id2);
4460 05 Sep 08 jari 67     TestAnnotationType.test_delete(id_int);
4460 05 Sep 08 jari 68
4460 05 Sep 08 jari 69     // Standard test: Delete
4460 05 Sep 08 jari 70     test_delete(id);
4460 05 Sep 08 jari 71     test_delete(id2);
4460 05 Sep 08 jari 72     write("++Testing annotation type categories "+(ok ? "OK" : "Failed")+"\n");
4460 05 Sep 08 jari 73     return ok;
4460 05 Sep 08 jari 74   }
4460 05 Sep 08 jari 75
7099 07 Mar 16 nicklas 76   static int test_create(String name, boolean setAll)
4460 05 Sep 08 jari 77   {
4460 05 Sep 08 jari 78     if (!TestUtil.hasPermission(Permission.CREATE, Item.ANNOTATIONTYPECATEGORY)) return 0;
4460 05 Sep 08 jari 79     int id = 0;
4460 05 Sep 08 jari 80     DbControl dc = null;
4460 05 Sep 08 jari 81     try 
4460 05 Sep 08 jari 82     {
4460 05 Sep 08 jari 83       dc = TestUtil.getDbControl();
4460 05 Sep 08 jari 84       AnnotationTypeCategory atgc = AnnotationTypeCategory.getNew(dc);
7099 07 Mar 16 nicklas 85       atgc.setName(name);
4460 05 Sep 08 jari 86       if (setAll)
4460 05 Sep 08 jari 87       {
4460 05 Sep 08 jari 88         atgc.setDescription("Added at " + new Date());
4460 05 Sep 08 jari 89       }
4460 05 Sep 08 jari 90       dc.saveItem(atgc);
4460 05 Sep 08 jari 91       dc.commit();
4460 05 Sep 08 jari 92       dc=TestUtil.getDbControl();
5060 19 Aug 09 nicklas 93       dc.reattachItem(atgc, false);
4460 05 Sep 08 jari 94       id=atgc.getId();
4460 05 Sep 08 jari 95       write_item(id, atgc);
4460 05 Sep 08 jari 96       write("--Create annotation type category OK");
4460 05 Sep 08 jari 97     }
4460 05 Sep 08 jari 98     catch (Throwable ex)
4460 05 Sep 08 jari 99     {
4460 05 Sep 08 jari 100       write("--Create annotation type category FAILED");
4460 05 Sep 08 jari 101       ex.printStackTrace();
4460 05 Sep 08 jari 102       ok = false;
4460 05 Sep 08 jari 103     }
4460 05 Sep 08 jari 104     finally 
4460 05 Sep 08 jari 105     {
4460 05 Sep 08 jari 106       if (dc != null) dc.close();
4460 05 Sep 08 jari 107     }
4460 05 Sep 08 jari 108
4460 05 Sep 08 jari 109     return id; 
4460 05 Sep 08 jari 110   }
4460 05 Sep 08 jari 111
4460 05 Sep 08 jari 112   static void test_load(int id)
4460 05 Sep 08 jari 113   {
4460 05 Sep 08 jari 114     if (id == 0) return;
4460 05 Sep 08 jari 115     DbControl dc = null;
4460 05 Sep 08 jari 116     try
4460 05 Sep 08 jari 117     {
4460 05 Sep 08 jari 118       dc = TestUtil.getDbControl();
4460 05 Sep 08 jari 119       AnnotationTypeCategory atgc = AnnotationTypeCategory.getById(dc, id);
4460 05 Sep 08 jari 120       write_item(0, atgc);
4460 05 Sep 08 jari 121       write("--Load annotation type category OK");
4460 05 Sep 08 jari 122     }
4460 05 Sep 08 jari 123     catch (Throwable ex)
4460 05 Sep 08 jari 124     {
4460 05 Sep 08 jari 125       write("--Load annotation type category FAILED");
4460 05 Sep 08 jari 126       ex.printStackTrace();
4460 05 Sep 08 jari 127       ok = false;
4460 05 Sep 08 jari 128     }
4460 05 Sep 08 jari 129     finally
4460 05 Sep 08 jari 130     {
4460 05 Sep 08 jari 131       if (dc != null) dc.close();
4460 05 Sep 08 jari 132     }
4460 05 Sep 08 jari 133
4460 05 Sep 08 jari 134   }
4460 05 Sep 08 jari 135
4460 05 Sep 08 jari 136   static void test_list(int expectedResults)
4460 05 Sep 08 jari 137   {
4460 05 Sep 08 jari 138     DbControl dc = null;
4460 05 Sep 08 jari 139     try
4460 05 Sep 08 jari 140     {
4460 05 Sep 08 jari 141       dc = TestUtil.getDbControl();
4460 05 Sep 08 jari 142       ItemResultList<AnnotationTypeCategory> list = AnnotationTypeCategory.getQuery().list(dc);
4460 05 Sep 08 jari 143       for (int i = 0; i<list.size(); i++)
4460 05 Sep 08 jari 144       {
4460 05 Sep 08 jari 145         write_item(i, list.get(i));
4460 05 Sep 08 jari 146       }
4460 05 Sep 08 jari 147       if (expectedResults >= 0 && expectedResults != list.size())
4460 05 Sep 08 jari 148       {
4460 05 Sep 08 jari 149         throw new BaseException("Expected "+expectedResults+" results, not "+list.size());
4460 05 Sep 08 jari 150       }
4460 05 Sep 08 jari 151       write("--List annotation types categories OK ("+list.size()+")");
4460 05 Sep 08 jari 152     }
4460 05 Sep 08 jari 153     catch (Throwable ex)
4460 05 Sep 08 jari 154     {
4460 05 Sep 08 jari 155       write("--List annotation types categories FAILED");
4460 05 Sep 08 jari 156       ex.printStackTrace();
4460 05 Sep 08 jari 157       ok = false;
4460 05 Sep 08 jari 158     }
4460 05 Sep 08 jari 159     finally
4460 05 Sep 08 jari 160     {
4460 05 Sep 08 jari 161       if (dc != null) dc.close();
4460 05 Sep 08 jari 162     }
4460 05 Sep 08 jari 163   }
4460 05 Sep 08 jari 164
4460 05 Sep 08 jari 165   static void test_delete(int id)
4460 05 Sep 08 jari 166   {
4460 05 Sep 08 jari 167     if (id == 0) return;
4460 05 Sep 08 jari 168     DbControl dc = null;
4460 05 Sep 08 jari 169     try
4460 05 Sep 08 jari 170     {
4460 05 Sep 08 jari 171       dc = TestUtil.getDbControl();
4460 05 Sep 08 jari 172       AnnotationTypeCategory at = AnnotationTypeCategory.getById(dc, id);
4460 05 Sep 08 jari 173       dc.deleteItem(at);
5340 10 May 10 nicklas 174       Set<ItemProxy> using = at.getUsingItems();
5340 10 May 10 nicklas 175       if (using.size() > 0) 
5340 10 May 10 nicklas 176       {
5340 10 May 10 nicklas 177          throw new BaseException(at + " is used by " + using);
5340 10 May 10 nicklas 178       }
4460 05 Sep 08 jari 179       dc.commit();
4460 05 Sep 08 jari 180       write("--Delete annotation type category OK");
4460 05 Sep 08 jari 181     }
4460 05 Sep 08 jari 182     catch (Throwable ex)
4460 05 Sep 08 jari 183     {
4460 05 Sep 08 jari 184       write("--Delete annotation type category FAILED");
4460 05 Sep 08 jari 185       ex.printStackTrace();
4460 05 Sep 08 jari 186       ok = false;
4460 05 Sep 08 jari 187     }
4460 05 Sep 08 jari 188     finally
4460 05 Sep 08 jari 189     {
4460 05 Sep 08 jari 190       if (dc != null) dc.close();
4460 05 Sep 08 jari 191     }
4460 05 Sep 08 jari 192   }
4460 05 Sep 08 jari 193
4460 05 Sep 08 jari 194
4460 05 Sep 08 jari 195   static void write(String message)
4460 05 Sep 08 jari 196   {
4460 05 Sep 08 jari 197     System.out.println(message);
4460 05 Sep 08 jari 198   }
4460 05 Sep 08 jari 199
4460 05 Sep 08 jari 200   static void write_header ()
4460 05 Sep 08 jari 201   {
4460 05 Sep 08 jari 202     if (!TestUtil.getSilent())
4460 05 Sep 08 jari 203     {
4460 05 Sep 08 jari 204       write("   \tID \tName      \tDescription");
4460 05 Sep 08 jari 205       write("-- \t-- \t--------- \t-----------");
4460 05 Sep 08 jari 206     }
4460 05 Sep 08 jari 207   }
4460 05 Sep 08 jari 208
4460 05 Sep 08 jari 209   static void write_item(int i, AnnotationTypeCategory atgc)
4460 05 Sep 08 jari 210     throws BaseException
4460 05 Sep 08 jari 211   {
4460 05 Sep 08 jari 212     if (!TestUtil.getSilent()) 
4460 05 Sep 08 jari 213       write(i+":\t"+atgc.getId()+"\t"+atgc.getName()+"\t"+atgc.getDescription());
4460 05 Sep 08 jari 214   }
4460 05 Sep 08 jari 215
4460 05 Sep 08 jari 216
4460 05 Sep 08 jari 217   static void test_add_categories(int annotationTypeId, int... categoryIds)
4460 05 Sep 08 jari 218   {
4460 05 Sep 08 jari 219     if (annotationTypeId == 0) return;
4460 05 Sep 08 jari 220     DbControl dc = null;
4460 05 Sep 08 jari 221     try
4460 05 Sep 08 jari 222     {
4460 05 Sep 08 jari 223       dc = TestUtil.getDbControl();
4460 05 Sep 08 jari 224       AnnotationType at = AnnotationType.getById(dc, annotationTypeId);
4460 05 Sep 08 jari 225       for (int categoryId : categoryIds)
4460 05 Sep 08 jari 226       {
4460 05 Sep 08 jari 227         if (categoryId != 0)
4460 05 Sep 08 jari 228         {
4460 05 Sep 08 jari 229           at.addCategory(AnnotationTypeCategory.getById(dc, categoryId));
4460 05 Sep 08 jari 230         }
4460 05 Sep 08 jari 231       }
4460 05 Sep 08 jari 232       dc.commit();
4460 05 Sep 08 jari 233       write("--Add categories to annotation type OK");
4460 05 Sep 08 jari 234
4460 05 Sep 08 jari 235     }
4460 05 Sep 08 jari 236     catch (Throwable ex)
4460 05 Sep 08 jari 237     {
4460 05 Sep 08 jari 238       write("--Add categories to annotation type FAILED");
4460 05 Sep 08 jari 239       ex.printStackTrace();
4460 05 Sep 08 jari 240       ok = false;
4460 05 Sep 08 jari 241     }
4460 05 Sep 08 jari 242     finally
4460 05 Sep 08 jari 243     {
4460 05 Sep 08 jari 244       if (dc != null) dc.close();
4460 05 Sep 08 jari 245     }
4460 05 Sep 08 jari 246   }
4460 05 Sep 08 jari 247
4460 05 Sep 08 jari 248   static void test_remove_categories(int annotationTypeId, int... categoryIds)
4460 05 Sep 08 jari 249   {
4460 05 Sep 08 jari 250     if (annotationTypeId == 0) return;
4460 05 Sep 08 jari 251     DbControl dc = null;
4460 05 Sep 08 jari 252     try
4460 05 Sep 08 jari 253     {
4460 05 Sep 08 jari 254       dc = TestUtil.getDbControl();
4460 05 Sep 08 jari 255       AnnotationType at = AnnotationType.getById(dc, annotationTypeId);
4460 05 Sep 08 jari 256       for (int categoryId : categoryIds)
4460 05 Sep 08 jari 257       {
4460 05 Sep 08 jari 258         if (categoryId != 0)
4460 05 Sep 08 jari 259         {
4460 05 Sep 08 jari 260           at.removeCategory(AnnotationTypeCategory.getById(dc, categoryId));
4460 05 Sep 08 jari 261         }
4460 05 Sep 08 jari 262       }
4460 05 Sep 08 jari 263       dc.commit();
4460 05 Sep 08 jari 264       write("--Remove categories to annotation type OK");
4460 05 Sep 08 jari 265
4460 05 Sep 08 jari 266     }
4460 05 Sep 08 jari 267     catch (Throwable ex)
4460 05 Sep 08 jari 268     {
4460 05 Sep 08 jari 269       write("--Remove categories to annotation type FAILED");
4460 05 Sep 08 jari 270       ex.printStackTrace();
4460 05 Sep 08 jari 271       ok = false;
4460 05 Sep 08 jari 272     }
4460 05 Sep 08 jari 273     finally
4460 05 Sep 08 jari 274     {
4460 05 Sep 08 jari 275       if (dc != null) dc.close();
4460 05 Sep 08 jari 276     }
4460 05 Sep 08 jari 277   }
4460 05 Sep 08 jari 278
4460 05 Sep 08 jari 279   static void test_list_categories(int annotationTypeId, int expectedResults)
4460 05 Sep 08 jari 280   {
4460 05 Sep 08 jari 281     if (annotationTypeId == 0) return;
4460 05 Sep 08 jari 282     DbControl dc = null;
4460 05 Sep 08 jari 283     try
4460 05 Sep 08 jari 284     {
4460 05 Sep 08 jari 285       dc = TestUtil.getDbControl();
4460 05 Sep 08 jari 286       AnnotationType at = AnnotationType.getById(dc, annotationTypeId);
4460 05 Sep 08 jari 287       ItemResultList<AnnotationTypeCategory> list = at.getCategories().list(dc);
4460 05 Sep 08 jari 288       for (int i = 0; i<list.size(); i++)
4460 05 Sep 08 jari 289       {
4460 05 Sep 08 jari 290         write_item(i, list.get(i));
4460 05 Sep 08 jari 291       }
4460 05 Sep 08 jari 292       if (expectedResults >= 0 && expectedResults != list.size())
4460 05 Sep 08 jari 293       {
4460 05 Sep 08 jari 294         throw new BaseException("Expected "+expectedResults+" results, not "+list.size());
4460 05 Sep 08 jari 295       }
4460 05 Sep 08 jari 296       write("--List categories for annotation type OK ("+list.size()+")");
4460 05 Sep 08 jari 297     }
4460 05 Sep 08 jari 298     catch (Throwable ex)
4460 05 Sep 08 jari 299     {
4460 05 Sep 08 jari 300       write("--List categories for annotation type FAILED");
4460 05 Sep 08 jari 301       ex.printStackTrace();
4460 05 Sep 08 jari 302       ok = false;
4460 05 Sep 08 jari 303     }
4460 05 Sep 08 jari 304     finally
4460 05 Sep 08 jari 305     {
4460 05 Sep 08 jari 306       if (dc != null) dc.close();
4460 05 Sep 08 jari 307     }
4460 05 Sep 08 jari 308   }
4460 05 Sep 08 jari 309
4460 05 Sep 08 jari 310 }
4460 05 Sep 08 jari 311