src/test/TestParameterType.java

Code
Comments
Other
Rev Date Author Line
740 09 Jun 05 samuel 1 /*
740 09 Jun 05 samuel 2   $Id$
740 09 Jun 05 samuel 3
3675 16 Aug 07 jari 4   Copyright (C) 2005 Samuel Andersson, Johan Enell, Nicklas Nordborg
4889 06 Apr 09 nicklas 5   Copyright (C) 2006 Johan Enell, Jari Häkkinen, Nicklas Nordborg
740 09 Jun 05 samuel 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/
740 09 Jun 05 samuel 9
740 09 Jun 05 samuel 10   BASE is free software; you can redistribute it and/or
740 09 Jun 05 samuel 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
740 09 Jun 05 samuel 13   of the License, or (at your option) any later version.
740 09 Jun 05 samuel 14
740 09 Jun 05 samuel 15   BASE is distributed in the hope that it will be useful,
740 09 Jun 05 samuel 16   but WITHOUT ANY WARRANTY; without even the implied warranty of
740 09 Jun 05 samuel 17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
740 09 Jun 05 samuel 18   GNU General Public License for more details.
740 09 Jun 05 samuel 19
740 09 Jun 05 samuel 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/>.
740 09 Jun 05 samuel 22 */
1138 26 Aug 05 enell 23 import net.sf.basedb.core.BaseException;
1138 26 Aug 05 enell 24 import net.sf.basedb.core.DateParameterType;
1138 26 Aug 05 enell 25 import net.sf.basedb.core.DbControl;
1138 26 Aug 05 enell 26 import net.sf.basedb.core.FileParameterType;
1138 26 Aug 05 enell 27 import net.sf.basedb.core.FloatParameterType;
1138 26 Aug 05 enell 28 import net.sf.basedb.core.IntegerParameterType;
1138 26 Aug 05 enell 29 import net.sf.basedb.core.InvalidDataException;
1138 26 Aug 05 enell 30 import net.sf.basedb.core.ItemParameterType;
1138 26 Aug 05 enell 31 import net.sf.basedb.core.News;
1138 26 Aug 05 enell 32 import net.sf.basedb.core.ParameterType;
1211 02 Sep 05 enell 33 import net.sf.basedb.core.StringParameterType;
819 23 Jun 05 samuel 34 import net.sf.basedb.core.data.IntegerParameterValueData;
740 09 Jun 05 samuel 35
740 09 Jun 05 samuel 36 import java.util.List;
740 09 Jun 05 samuel 37 import java.util.ArrayList;
1759 12 Jan 06 nicklas 38 import java.util.Arrays;
740 09 Jun 05 samuel 39
740 09 Jun 05 samuel 40 public class TestParameterType
740 09 Jun 05 samuel 41 {
740 09 Jun 05 samuel 42
740 09 Jun 05 samuel 43   static boolean ok = true;
740 09 Jun 05 samuel 44   public static void main(String[] args)
740 09 Jun 05 samuel 45   {
740 09 Jun 05 samuel 46     TestUtil.checkArgs(args);
740 09 Jun 05 samuel 47     TestUtil.begin();
740 09 Jun 05 samuel 48     ok = test_all();
740 09 Jun 05 samuel 49     TestUtil.stop();
740 09 Jun 05 samuel 50   }
740 09 Jun 05 samuel 51
740 09 Jun 05 samuel 52   static boolean test_all()
740 09 Jun 05 samuel 53   {
740 09 Jun 05 samuel 54     write("++Testing parameter types");
740 09 Jun 05 samuel 55     // Standard tests: create, load, list
803 21 Jun 05 samuel 56     int newsId = TestNews.test_create(true);
803 21 Jun 05 samuel 57     int newsId2 = TestNews.test_create(true);
6875 20 Apr 15 nicklas 58     List<ParameterType<?>> parameters = test_create(newsId);
803 21 Jun 05 samuel 59     test_validation(parameters, newsId, newsId2);
740 09 Jun 05 samuel 60     test_list(parameters);
1529 27 Oct 05 nicklas 61     if (TestUtil.waitBeforeDelete()) TestUtil.waitForEnter();
803 21 Jun 05 samuel 62     TestNews.test_delete(newsId2);
803 21 Jun 05 samuel 63     TestNews.test_delete(newsId);
740 09 Jun 05 samuel 64
740 09 Jun 05 samuel 65     write("++Testing parameter types "+(ok ? "OK" : "Failed")+"\n");
740 09 Jun 05 samuel 66     return ok;
740 09 Jun 05 samuel 67   }
740 09 Jun 05 samuel 68
6875 20 Apr 15 nicklas 69   static List<ParameterType<?>> test_create(int newsId)
740 09 Jun 05 samuel 70   {
6875 20 Apr 15 nicklas 71     List<ParameterType<?>> parameters = new ArrayList<ParameterType<?>>();
1554 02 Nov 05 enell 72     IntegerParameterType intType = new IntegerParameterType(null, 100, null, false);
740 09 Jun 05 samuel 73     parameters.add(intType);
740 09 Jun 05 samuel 74     
1554 02 Nov 05 enell 75     FloatParameterType floatType = new FloatParameterType(null, 234.324F, null, false);
740 09 Jun 05 samuel 76     parameters.add(floatType);
740 09 Jun 05 samuel 77
742 10 Jun 05 samuel 78     DateParameterType dateType = new DateParameterType();
742 10 Jun 05 samuel 79     parameters.add(dateType);
742 10 Jun 05 samuel 80     
1554 02 Nov 05 enell 81     StringParameterType stringType = new StringParameterType(37, "nothing", false);
1211 02 Sep 05 enell 82     parameters.add(stringType);
1190 01 Sep 05 nicklas 83
799 20 Jun 05 samuel 84     FileParameterType fileType = new FileParameterType();
799 20 Jun 05 samuel 85     parameters.add(fileType);
803 21 Jun 05 samuel 86
803 21 Jun 05 samuel 87     DbControl dc = null;
803 21 Jun 05 samuel 88     try
803 21 Jun 05 samuel 89     {
803 21 Jun 05 samuel 90       dc = TestUtil.getDbControl();
1759 12 Jan 06 nicklas 91       
1759 12 Jan 06 nicklas 92       ItemParameterType<News> itemType = new ItemParameterType<News>(News.class, Arrays.asList(News.getById(dc, newsId)));
803 21 Jun 05 samuel 93       parameters.add(itemType);
803 21 Jun 05 samuel 94     }
803 21 Jun 05 samuel 95     catch (Throwable ex)
803 21 Jun 05 samuel 96     {
1759 12 Jan 06 nicklas 97       write("--Load MIME types FAILED");
803 21 Jun 05 samuel 98       ex.printStackTrace();
803 21 Jun 05 samuel 99       ok = false;
803 21 Jun 05 samuel 100     }
803 21 Jun 05 samuel 101     finally
803 21 Jun 05 samuel 102     {
803 21 Jun 05 samuel 103       if (dc != null) dc.close();
803 21 Jun 05 samuel 104     }
799 20 Jun 05 samuel 105     
740 09 Jun 05 samuel 106     return parameters;
740 09 Jun 05 samuel 107   }
740 09 Jun 05 samuel 108   
6875 20 Apr 15 nicklas 109   static void test_validation(List<ParameterType<?>> parameters, int newsId, int newsId2)
740 09 Jun 05 samuel 110   {
740 09 Jun 05 samuel 111     try
740 09 Jun 05 samuel 112     {
6875 20 Apr 15 nicklas 113       for (ParameterType<?> param : parameters)
740 09 Jun 05 samuel 114       {
749 13 Jun 05 samuel 115         if (param instanceof IntegerParameterType)
740 09 Jun 05 samuel 116         {
819 23 Jun 05 samuel 117           IntegerParameterValueData intValue = new IntegerParameterValueData();
819 23 Jun 05 samuel 118           intValue.getValues().add(100);
1190 01 Sep 05 nicklas 119           param.validate("integer", intValue.getValues());
7516 02 Nov 18 nicklas 120           param.validate("integer", Integer.valueOf(-234));
740 09 Jun 05 samuel 121           
740 09 Jun 05 samuel 122           // Testing with wrong value
740 09 Jun 05 samuel 123           boolean valid = true;
740 09 Jun 05 samuel 124           try
740 09 Jun 05 samuel 125           {
7516 02 Nov 18 nicklas 126             param.validate("integer", Integer.valueOf(101));
740 09 Jun 05 samuel 127           }
740 09 Jun 05 samuel 128           catch (InvalidDataException ex)
740 09 Jun 05 samuel 129           {
740 09 Jun 05 samuel 130             valid = false;
740 09 Jun 05 samuel 131           }
749 13 Jun 05 samuel 132           if (valid) // Should not be valid
1759 12 Jan 06 nicklas 133           {
740 09 Jun 05 samuel 134             throw new BaseException();
1759 12 Jan 06 nicklas 135           }
740 09 Jun 05 samuel 136         }
749 13 Jun 05 samuel 137         else if (param instanceof FloatParameterType)
740 09 Jun 05 samuel 138         {
7516 02 Nov 18 nicklas 139           param.validate("float", Float.valueOf(50.0f));
740 09 Jun 05 samuel 140         }
1211 02 Sep 05 enell 141         else if (param instanceof StringParameterType)
743 10 Jun 05 samuel 142         {
1190 01 Sep 05 nicklas 143           param.validate("string", "The bear was walking down the stairs.");
743 10 Jun 05 samuel 144           
743 10 Jun 05 samuel 145           // Testing with wrong value
743 10 Jun 05 samuel 146           boolean valid = true;
743 10 Jun 05 samuel 147           try
743 10 Jun 05 samuel 148           {
1190 01 Sep 05 nicklas 149             param.validate("float", "Running like a fool, but still no response." );
743 10 Jun 05 samuel 150           }
743 10 Jun 05 samuel 151           catch (InvalidDataException ex)
743 10 Jun 05 samuel 152           {
743 10 Jun 05 samuel 153             valid = false;
743 10 Jun 05 samuel 154           }
749 13 Jun 05 samuel 155           if (valid) // Should not be valid
743 10 Jun 05 samuel 156             throw new BaseException();
743 10 Jun 05 samuel 157         }
803 21 Jun 05 samuel 158         else if (param instanceof ItemParameterType)
803 21 Jun 05 samuel 159         {
803 21 Jun 05 samuel 160           DbControl dc = null;
803 21 Jun 05 samuel 161           try
803 21 Jun 05 samuel 162           {
803 21 Jun 05 samuel 163             dc = TestUtil.getDbControl();
803 21 Jun 05 samuel 164             News n = News.getById(dc, newsId);
1190 01 Sep 05 nicklas 165             param.validate("news", n);
803 21 Jun 05 samuel 166             
803 21 Jun 05 samuel 167             // Testing with wrong value
803 21 Jun 05 samuel 168             boolean valid = true;
803 21 Jun 05 samuel 169             News n2 = News.getById(dc, newsId2);
803 21 Jun 05 samuel 170             try
803 21 Jun 05 samuel 171             {
1190 01 Sep 05 nicklas 172               param.validate("news", n2);
803 21 Jun 05 samuel 173             }
803 21 Jun 05 samuel 174             catch (InvalidDataException ex)
803 21 Jun 05 samuel 175             {
803 21 Jun 05 samuel 176               valid = false;
803 21 Jun 05 samuel 177             }
803 21 Jun 05 samuel 178             if (valid) // Should not be valid
803 21 Jun 05 samuel 179               throw new BaseException();
803 21 Jun 05 samuel 180           }
803 21 Jun 05 samuel 181           finally
803 21 Jun 05 samuel 182           {
803 21 Jun 05 samuel 183             if (dc != null) dc.close();
803 21 Jun 05 samuel 184           }
803 21 Jun 05 samuel 185         }
740 09 Jun 05 samuel 186       }
1180 31 Aug 05 nicklas 187       write("--Testing parameter type validation OK");
740 09 Jun 05 samuel 188     }
816 23 Jun 05 nicklas 189     catch (Throwable ex)
740 09 Jun 05 samuel 190     {
740 09 Jun 05 samuel 191       ok = false;
1180 31 Aug 05 nicklas 192       write("--Testing parameter type validation FAILED");
749 13 Jun 05 samuel 193       ex.printStackTrace();
740 09 Jun 05 samuel 194     }
740 09 Jun 05 samuel 195   }
740 09 Jun 05 samuel 196   
6875 20 Apr 15 nicklas 197   static void test_list(List<ParameterType<?>> parameters)
740 09 Jun 05 samuel 198   {
803 21 Jun 05 samuel 199     if (!TestUtil.getSilent())
740 09 Jun 05 samuel 200     {
803 21 Jun 05 samuel 201       write("List parameter types");
803 21 Jun 05 samuel 202       write("--------------------");
6875 20 Apr 15 nicklas 203       for (ParameterType<?> param : parameters)
803 21 Jun 05 samuel 204       {
803 21 Jun 05 samuel 205         write(param.toString());
803 21 Jun 05 samuel 206       }
740 09 Jun 05 samuel 207     }
740 09 Jun 05 samuel 208   }
740 09 Jun 05 samuel 209
740 09 Jun 05 samuel 210   static void write(String message)
740 09 Jun 05 samuel 211   {
740 09 Jun 05 samuel 212     System.out.println(message);
740 09 Jun 05 samuel 213   }
740 09 Jun 05 samuel 214 }
740 09 Jun 05 samuel 215