src/test/TestPluginType.java

Code
Comments
Other
Rev Date Author Line
1377 22 Sep 05 nicklas 1 /*
1377 22 Sep 05 nicklas 2   $Id$
1377 22 Sep 05 nicklas 3   
3675 16 Aug 07 jari 4   Copyright (C) 2005 Nicklas Nordborg
4889 06 Apr 09 nicklas 5   Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg
1377 22 Sep 05 nicklas 6   
2304 22 May 06 jari 7   This file is part of BASE - BioArray Software Environment.
2304 22 May 06 jari 8   Available at http://base.thep.lu.se/
1377 22 Sep 05 nicklas 9   
1377 22 Sep 05 nicklas 10   BASE is free software; you can redistribute it and/or
1377 22 Sep 05 nicklas 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
1377 22 Sep 05 nicklas 13   of the License, or (at your option) any later version.
1377 22 Sep 05 nicklas 14   
1377 22 Sep 05 nicklas 15   BASE is distributed in the hope that it will be useful,
1377 22 Sep 05 nicklas 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
1377 22 Sep 05 nicklas 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1377 22 Sep 05 nicklas 18   GNU General Public License for more details.
1377 22 Sep 05 nicklas 19   
1377 22 Sep 05 nicklas 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/>.
1377 22 Sep 05 nicklas 22 */
1377 22 Sep 05 nicklas 23 import net.sf.basedb.core.*;
5340 10 May 10 nicklas 24
1377 22 Sep 05 nicklas 25 import java.util.Date;
5340 10 May 10 nicklas 26 import java.util.Set;
1377 22 Sep 05 nicklas 27
1377 22 Sep 05 nicklas 28 public class TestPluginType
1377 22 Sep 05 nicklas 29 {
1377 22 Sep 05 nicklas 30
1377 22 Sep 05 nicklas 31   static boolean ok = true;
1377 22 Sep 05 nicklas 32   
1377 22 Sep 05 nicklas 33   public static void main(String[] args)
1377 22 Sep 05 nicklas 34   {
1377 22 Sep 05 nicklas 35     TestUtil.checkArgs(args);
1377 22 Sep 05 nicklas 36     TestUtil.begin();
1377 22 Sep 05 nicklas 37     ok = test_all();
1377 22 Sep 05 nicklas 38     TestUtil.stop();
1377 22 Sep 05 nicklas 39   }
1377 22 Sep 05 nicklas 40
1377 22 Sep 05 nicklas 41   static boolean test_all()
1377 22 Sep 05 nicklas 42   {
1377 22 Sep 05 nicklas 43     write("++Testing plugin type");
1377 22 Sep 05 nicklas 44     write_header();
1377 22 Sep 05 nicklas 45     // Standard tests: load and list
1377 22 Sep 05 nicklas 46     int id = test_create("NullPluginInterface", null, true);
1377 22 Sep 05 nicklas 47     int id2 = test_create("JarPluginInterface", "JarPlugin.jar", false);
1377 22 Sep 05 nicklas 48     test_load(id);
1377 22 Sep 05 nicklas 49     test_list();
1529 27 Oct 05 nicklas 50     
1529 27 Oct 05 nicklas 51     if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter();
1377 22 Sep 05 nicklas 52     test_delete(id);
1377 22 Sep 05 nicklas 53     test_delete(id2);
1377 22 Sep 05 nicklas 54
1377 22 Sep 05 nicklas 55     write("++Testing plugin type "+(ok ? "OK" : "Failed")+"\n");
1377 22 Sep 05 nicklas 56     return ok;
1377 22 Sep 05 nicklas 57   }
1377 22 Sep 05 nicklas 58
1377 22 Sep 05 nicklas 59   static int test_create(String interfaceName, String jarPath, boolean setAll)
1377 22 Sep 05 nicklas 60   {
1377 22 Sep 05 nicklas 61     if (!TestUtil.hasPermission(Permission.CREATE, Item.PLUGINTYPE)) return 0;
1377 22 Sep 05 nicklas 62     int id = 0;
1377 22 Sep 05 nicklas 63     DbControl dc = null;
1377 22 Sep 05 nicklas 64     try
1377 22 Sep 05 nicklas 65     {
1377 22 Sep 05 nicklas 66       dc = TestUtil.getDbControl();
1377 22 Sep 05 nicklas 67       PluginType pt = PluginType.getNew(dc, interfaceName, jarPath);
1377 22 Sep 05 nicklas 68       if (setAll)
1377 22 Sep 05 nicklas 69       {
1377 22 Sep 05 nicklas 70         pt.setName("Test plugin type");
1377 22 Sep 05 nicklas 71         pt.setDescription("Added at "+new Date());
1377 22 Sep 05 nicklas 72       }
1377 22 Sep 05 nicklas 73       dc.saveItem(pt);
1377 22 Sep 05 nicklas 74       dc.commit();
1377 22 Sep 05 nicklas 75       id = pt.getId();
1377 22 Sep 05 nicklas 76       dc = TestUtil.getDbControl();
5060 19 Aug 09 nicklas 77       dc.reattachItem(pt, false);
1377 22 Sep 05 nicklas 78       write_item(0, pt);
1377 22 Sep 05 nicklas 79       write("--Create plugin type OK");
1377 22 Sep 05 nicklas 80     }
1377 22 Sep 05 nicklas 81     catch (Throwable ex)
1377 22 Sep 05 nicklas 82     {
1377 22 Sep 05 nicklas 83       write("--Create plugin type FAILED");
1377 22 Sep 05 nicklas 84       ex.printStackTrace();
1377 22 Sep 05 nicklas 85       ok = false;
1377 22 Sep 05 nicklas 86     }
1377 22 Sep 05 nicklas 87     finally
1377 22 Sep 05 nicklas 88     {
1377 22 Sep 05 nicklas 89       if (dc != null) dc.close();
1377 22 Sep 05 nicklas 90     }
1377 22 Sep 05 nicklas 91     return id;
1377 22 Sep 05 nicklas 92   }
1377 22 Sep 05 nicklas 93   
1377 22 Sep 05 nicklas 94   static void test_load(int id)
1377 22 Sep 05 nicklas 95   {
1377 22 Sep 05 nicklas 96     if (id == 0) return;
1377 22 Sep 05 nicklas 97     DbControl dc = null;
1377 22 Sep 05 nicklas 98     try
1377 22 Sep 05 nicklas 99     {
1377 22 Sep 05 nicklas 100       dc = TestUtil.getDbControl();
1377 22 Sep 05 nicklas 101       PluginType pt = PluginType.getById(dc, id);
1377 22 Sep 05 nicklas 102       write_item(0, pt);
1377 22 Sep 05 nicklas 103       write("--Load plugin type OK");
1377 22 Sep 05 nicklas 104     }
1377 22 Sep 05 nicklas 105     catch (Throwable ex)
1377 22 Sep 05 nicklas 106     {
1377 22 Sep 05 nicklas 107       write("--Load plugin type FAILED");
1377 22 Sep 05 nicklas 108       ex.printStackTrace();
1377 22 Sep 05 nicklas 109       ok = false;
1377 22 Sep 05 nicklas 110     }
1377 22 Sep 05 nicklas 111     finally
1377 22 Sep 05 nicklas 112     {
1377 22 Sep 05 nicklas 113       if (dc != null) dc.close();
1377 22 Sep 05 nicklas 114     }
1377 22 Sep 05 nicklas 115   }
1377 22 Sep 05 nicklas 116
1377 22 Sep 05 nicklas 117   static void test_list()
1377 22 Sep 05 nicklas 118   {
1377 22 Sep 05 nicklas 119     DbControl dc = null;
1377 22 Sep 05 nicklas 120     try
1377 22 Sep 05 nicklas 121     {
1377 22 Sep 05 nicklas 122       dc = TestUtil.getDbControl();
1418 07 Oct 05 nicklas 123       ItemResultList<PluginType> l = PluginType.getQuery().list(dc);
1377 22 Sep 05 nicklas 124       for (int i = 0; i<l.size(); i++)
1377 22 Sep 05 nicklas 125       {
1377 22 Sep 05 nicklas 126         write_item(i, l.get(i));
1377 22 Sep 05 nicklas 127       }
1377 22 Sep 05 nicklas 128       write("--List plugin types OK ("+l.size()+")");
1377 22 Sep 05 nicklas 129     }
1377 22 Sep 05 nicklas 130     catch (Throwable ex)
1377 22 Sep 05 nicklas 131     {
1377 22 Sep 05 nicklas 132       write("--List plugin types FAILED");
1377 22 Sep 05 nicklas 133       ex.printStackTrace();
1377 22 Sep 05 nicklas 134       ok = false;
1377 22 Sep 05 nicklas 135     }
1377 22 Sep 05 nicklas 136     finally
1377 22 Sep 05 nicklas 137     {
1377 22 Sep 05 nicklas 138       if (dc != null) dc.close();
1377 22 Sep 05 nicklas 139     }
1377 22 Sep 05 nicklas 140   }
1377 22 Sep 05 nicklas 141   
1377 22 Sep 05 nicklas 142   static void test_delete(int id)
1377 22 Sep 05 nicklas 143   {
1377 22 Sep 05 nicklas 144     if (id == 0) return;
1377 22 Sep 05 nicklas 145     DbControl dc = null;
1377 22 Sep 05 nicklas 146     try
1377 22 Sep 05 nicklas 147     {
1377 22 Sep 05 nicklas 148       dc = TestUtil.getDbControl();
1377 22 Sep 05 nicklas 149       PluginType pt = PluginType.getById(dc, id);
1377 22 Sep 05 nicklas 150       dc.deleteItem(pt);
5340 10 May 10 nicklas 151       Set<ItemProxy> using = pt.getUsingItems();
5340 10 May 10 nicklas 152       if (using.size() > 0) 
5340 10 May 10 nicklas 153       {
5340 10 May 10 nicklas 154          throw new BaseException(pt + " is used by " + using);
5340 10 May 10 nicklas 155       }
1377 22 Sep 05 nicklas 156       dc.commit();
1377 22 Sep 05 nicklas 157       write("--Delete plugin type OK");
1377 22 Sep 05 nicklas 158     }
1377 22 Sep 05 nicklas 159     catch (Throwable ex)
1377 22 Sep 05 nicklas 160     {
1377 22 Sep 05 nicklas 161       write("--Delete plugin type FAILED");
1377 22 Sep 05 nicklas 162       ex.printStackTrace();
1377 22 Sep 05 nicklas 163       ok = false;
1377 22 Sep 05 nicklas 164     }
1377 22 Sep 05 nicklas 165     finally
1377 22 Sep 05 nicklas 166     {
1377 22 Sep 05 nicklas 167       if (dc != null) dc.close();
1377 22 Sep 05 nicklas 168     }
1377 22 Sep 05 nicklas 169   }
1377 22 Sep 05 nicklas 170
1377 22 Sep 05 nicklas 171   static void write_header()
1377 22 Sep 05 nicklas 172   {
1377 22 Sep 05 nicklas 173     if (!TestUtil.getSilent())
1377 22 Sep 05 nicklas 174     {
1377 22 Sep 05 nicklas 175       write("   \tID \tName      \tInterface\tJar path\tDescription");
1377 22 Sep 05 nicklas 176       write("-- \t-- \t--------- \t---------\t--------\t-----------");
1377 22 Sep 05 nicklas 177     }
1377 22 Sep 05 nicklas 178   }
1377 22 Sep 05 nicklas 179   
1377 22 Sep 05 nicklas 180   static void write_item(int i, PluginType pt)
1377 22 Sep 05 nicklas 181     throws BaseException
1377 22 Sep 05 nicklas 182   {
1377 22 Sep 05 nicklas 183     if (!TestUtil.getSilent()) System.out.println(i+":\t"+pt.getId()+"\t"+pt.getName()+"\t"+
1377 22 Sep 05 nicklas 184       pt.getInterfaceName()+"\t"+pt.getJarPath()+"\t"+pt.getDescription());
1377 22 Sep 05 nicklas 185   }
1377 22 Sep 05 nicklas 186   
1377 22 Sep 05 nicklas 187   static void write(String message)
1377 22 Sep 05 nicklas 188   {
1377 22 Sep 05 nicklas 189     System.out.println(message);
1377 22 Sep 05 nicklas 190   }
1377 22 Sep 05 nicklas 191 }