api/core/test/src/utilities/MzDataImpSetup.java

Code
Comments
Other
Rev Date Author Line
2103 11 Oct 07 olle 1 /*
2103 11 Oct 07 olle 2  $Id$
2103 11 Oct 07 olle 3
2103 11 Oct 07 olle 4  Copyright (C) 2006, 2007 Gregory Vincic, Olle Mansson
2103 11 Oct 07 olle 5
2103 11 Oct 07 olle 6  This file is part of Proteios.
2103 11 Oct 07 olle 7  Available at http://www.proteios.org/
2103 11 Oct 07 olle 8
2103 11 Oct 07 olle 9  Proteios is free software; you can redistribute it and/or modify it
2103 11 Oct 07 olle 10  under the terms of the GNU General Public License as published by
2103 11 Oct 07 olle 11  the Free Software Foundation; either version 2 of the License, or
2103 11 Oct 07 olle 12  (at your option) any later version.
2103 11 Oct 07 olle 13
2103 11 Oct 07 olle 14  Proteios is distributed in the hope that it will be useful, but
2103 11 Oct 07 olle 15  WITHOUT ANY WARRANTY; without even the implied warranty of
2103 11 Oct 07 olle 16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2103 11 Oct 07 olle 17  General Public License for more details.
2103 11 Oct 07 olle 18
2103 11 Oct 07 olle 19  You should have received a copy of the GNU General Public License
2103 11 Oct 07 olle 20  along with this program; if not, write to the Free Software
2103 11 Oct 07 olle 21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
2103 11 Oct 07 olle 22  02111-1307, USA.
2103 11 Oct 07 olle 23  */
2103 11 Oct 07 olle 24 package utilities;
2103 11 Oct 07 olle 25
2103 11 Oct 07 olle 26 import org.proteios.PropertiesFile;
2103 11 Oct 07 olle 27 import org.proteios.core.Application;
2103 11 Oct 07 olle 28 import org.proteios.core.SessionControl;
2103 11 Oct 07 olle 29 import org.proteios.io.mzdata.MzDataImporter;
2103 11 Oct 07 olle 30 import org.xml.sax.SAXException;
2103 11 Oct 07 olle 31
2103 11 Oct 07 olle 32 import java.net.URL;
2103 11 Oct 07 olle 33
2103 11 Oct 07 olle 34 import javax.xml.stream.XMLStreamException;
2103 11 Oct 07 olle 35
2103 11 Oct 07 olle 36 import junit.extensions.TestSetup;
2103 11 Oct 07 olle 37 import junit.framework.Test;
2103 11 Oct 07 olle 38
2103 11 Oct 07 olle 39 /**
2103 11 Oct 07 olle 40  * This class performs setup operations that only should be
2103 11 Oct 07 olle 41  * performed once for all tests in a test suite containing tests
2103 11 Oct 07 olle 42  * of MzDataImporter items. The tests in this suite are part
2103 11 Oct 07 olle 43  * integration tests, and relies on a fixed mzData XML file to be
2103 11 Oct 07 olle 44  * imported. This import is performed by this class.
2103 11 Oct 07 olle 45  * 
2103 11 Oct 07 olle 46  * @author Olle
2103 11 Oct 07 olle 47  * @version 2.0
2103 11 Oct 07 olle 48  * @proteios.modified $Date$
2103 11 Oct 07 olle 49  */
2103 11 Oct 07 olle 50 public class MzDataImpSetup
2103 11 Oct 07 olle 51     extends TestSetup
2103 11 Oct 07 olle 52 {
2103 11 Oct 07 olle 53   private SessionControl sc = null;
2103 11 Oct 07 olle 54   private String testDataDirURLStr = null;
2103 11 Oct 07 olle 55   private URL testDataDirURL = null;
2103 11 Oct 07 olle 56   private String testDataDir = null;
2103 11 Oct 07 olle 57   private int peakListSetId;
2103 11 Oct 07 olle 58   
2103 11 Oct 07 olle 59
2103 11 Oct 07 olle 60   /**
2103 11 Oct 07 olle 61    * Get the PeakListSetId for the PeakListSet
2103 11 Oct 07 olle 62    * created from the imported mzData file.
2103 11 Oct 07 olle 63    * 
2103 11 Oct 07 olle 64    * @return id int id for the PeakListSet created from the mzdata file.
2103 11 Oct 07 olle 65    */
2103 11 Oct 07 olle 66   public int getPeakListSetId()
2103 11 Oct 07 olle 67   {
2103 11 Oct 07 olle 68     return this.peakListSetId;
2103 11 Oct 07 olle 69   }
2103 11 Oct 07 olle 70
2103 11 Oct 07 olle 71   /**
2103 11 Oct 07 olle 72    * Set the PeakListSetId for the PeakListSet
2103 11 Oct 07 olle 73    * created from the imported mzData file.
2103 11 Oct 07 olle 74    * 
2103 11 Oct 07 olle 75    * @param peakListSetId int id for the PeakListSet created from the mzdata file.
2103 11 Oct 07 olle 76    */
2103 11 Oct 07 olle 77   public void setPeakListSetId(int peakListSetId)
2103 11 Oct 07 olle 78   {
2103 11 Oct 07 olle 79     this.peakListSetId = peakListSetId;
2103 11 Oct 07 olle 80   }
2103 11 Oct 07 olle 81
2103 11 Oct 07 olle 82   public MzDataImpSetup(Test suite)
2103 11 Oct 07 olle 83   {
2103 11 Oct 07 olle 84     super(suite);
2103 11 Oct 07 olle 85   }
2103 11 Oct 07 olle 86
2103 11 Oct 07 olle 87   protected void setUp()
2103 11 Oct 07 olle 88       throws Exception
2103 11 Oct 07 olle 89   {
2103 11 Oct 07 olle 90     Application.start();
2103 11 Oct 07 olle 91     SessionControl sc = null;
2103 11 Oct 07 olle 92     try
2103 11 Oct 07 olle 93     {
2103 11 Oct 07 olle 94       sc = Application.getSessionControl("testSession", "test");
2103 11 Oct 07 olle 95     }
2103 11 Oct 07 olle 96     catch (Exception e)
2103 11 Oct 07 olle 97     {
2103 11 Oct 07 olle 98       //System.out.println("MzDataImpSetup:: setUp: Exception when getting session control: " + e);
2103 11 Oct 07 olle 99       sc = Application.newSessionControl("org.proteios.default", "test", "testSession");
2103 11 Oct 07 olle 100     }
2103 11 Oct 07 olle 101     setSessionControl(sc);
2103 11 Oct 07 olle 102     //dc = sc.newDbControl();
2103 11 Oct 07 olle 103     //if (dc == null)
2103 11 Oct 07 olle 104     //{
2103 11 Oct 07 olle 105     //  System.out.println("MzDataImpSetup:: setUp: dc == null");
2103 11 Oct 07 olle 106     //}
2103 11 Oct 07 olle 107     /*
2103 11 Oct 07 olle 108      * Optional log in to Proteios as specific user.
2103 11 Oct 07 olle 109      * 
2103 11 Oct 07 olle 110      * Note that if you log in here via sc.login(...) for
2103 11 Oct 07 olle 111      * SessionControl 'sc', you must log out via sc.logout()
2103 11 Oct 07 olle 112      * in method tearDown(), if you have more than one test,
2103 11 Oct 07 olle 113      * as otherwise you will get an error when setUp() tries
2103 11 Oct 07 olle 114      * to log in as the same user a second time.
2103 11 Oct 07 olle 115      */
2103 11 Oct 07 olle 116     /*
2103 11 Oct 07 olle 117      * We need to log in to Proteios in this case, as we want
2103 11 Oct 07 olle 118      * to import a given mzData XML file before performing the tests,
2103 11 Oct 07 olle 119      * and the import routine uses the logged in user to set
2103 11 Oct 07 olle 120      * ownership of imported items.
2103 11 Oct 07 olle 121      */
2103 11 Oct 07 olle 122     PropertiesFile pf = new PropertiesFile();
2103 11 Oct 07 olle 123     sc.logout();
2103 11 Oct 07 olle 124     sc.login(pf.getProperty("test.username"), pf.getProperty("test.password"), "", false);
2103 11 Oct 07 olle 125     /*
2103 11 Oct 07 olle 126      * Test-specific Initializations may be placed here. 
2103 11 Oct 07 olle 127      */
2103 11 Oct 07 olle 128     testDataDirURLStr = pf.getProperty("test.data.url");
2103 11 Oct 07 olle 129     /*
2103 11 Oct 07 olle 130      * Path to be used in tests taking
2103 11 Oct 07 olle 131      * a local path as argument.
2103 11 Oct 07 olle 132      */
2103 11 Oct 07 olle 133     testDataDir = null;
2103 11 Oct 07 olle 134     testDataDirURL = null;
2103 11 Oct 07 olle 135     testDataDirURL = new URL(testDataDirURLStr);
2103 11 Oct 07 olle 136     /*
2103 11 Oct 07 olle 137      * If the url String is a relative local path,
2103 11 Oct 07 olle 138      * creating a URL will expand the full path.
2103 11 Oct 07 olle 139      */
2103 11 Oct 07 olle 140     if (testDataDirURL != null && !testDataDirURL.toString().equals("") && testDataDirURL.toString().startsWith("file:"))
2103 11 Oct 07 olle 141     {
2103 11 Oct 07 olle 142       /*
2103 11 Oct 07 olle 143        * Store full local path for use im tests
2103 11 Oct 07 olle 144        * taking a local path as argument. 
2103 11 Oct 07 olle 145        */
2103 11 Oct 07 olle 146       testDataDir = testDataDirURL.getPath();
2103 11 Oct 07 olle 147     }
2103 11 Oct 07 olle 148     //System.out.println(this.getClass().getSimpleName() + "::setUp() testDataDirURL = \"" + testDataDirURL + "\"");
2103 11 Oct 07 olle 149     //System.out.println(this.getClass().getSimpleName() + "::setUp() testDataDir = \"" + testDataDir + "\"");
2103 11 Oct 07 olle 150     /*
2103 11 Oct 07 olle 151      * Create an MzDataImporter, import mzData file
2103 11 Oct 07 olle 152      * with path 'testXmlFilePath', and get id for
2103 11 Oct 07 olle 153      * created PeakListSet.
2103 11 Oct 07 olle 154      */
2103 11 Oct 07 olle 155     MzDataImporter mzDataImporter = new MzDataImporter(sc);
2103 11 Oct 07 olle 156     try
2103 11 Oct 07 olle 157     {
2103 11 Oct 07 olle 158       /*
2103 11 Oct 07 olle 159        * MzData file with path 'testXmlFilePath' should
2103 11 Oct 07 olle 160        * correspond to mzData file mzDataExtraBlocks.xml.
2103 11 Oct 07 olle 161        */
2103 11 Oct 07 olle 162       String testXmlFilePath = new String(testDataDir + "/mzData.xml");
2103 11 Oct 07 olle 163       mzDataImporter.importFile(testXmlFilePath);
2103 11 Oct 07 olle 164       peakListSetId = mzDataImporter.getPeakListSet().getId();
2103 11 Oct 07 olle 165     } catch (SAXException e) {
2103 11 Oct 07 olle 166       fail("MzDataImpSetup:: setUp(): importFile() throwed a SAXException for a valid mzData file.");
2103 11 Oct 07 olle 167     } catch (XMLStreamException e) {
2103 11 Oct 07 olle 168       fail("MzDataImpSetup:: setUp(): importFile() throwed an XMLStreamException for a valid mzData file.");
2103 11 Oct 07 olle 169     }
2103 11 Oct 07 olle 170     /*
2103 11 Oct 07 olle 171      * Log out from Proteios before the first test.
2103 11 Oct 07 olle 172      */
2103 11 Oct 07 olle 173     sc.logout();
2103 11 Oct 07 olle 174   }
2103 11 Oct 07 olle 175
2103 11 Oct 07 olle 176
2103 11 Oct 07 olle 177   protected void tearDown()
2103 11 Oct 07 olle 178       throws Exception
2103 11 Oct 07 olle 179   {
2103 11 Oct 07 olle 180     Application.stop();
2103 11 Oct 07 olle 181     //dc.close();
2103 11 Oct 07 olle 182     sc.logout();
2103 11 Oct 07 olle 183   }
2103 11 Oct 07 olle 184
2103 11 Oct 07 olle 185
2103 11 Oct 07 olle 186   /**
2103 11 Oct 07 olle 187    * Get the SessionControl object.
2103 11 Oct 07 olle 188    * 
2103 11 Oct 07 olle 189    * @return the SessionControl object
2103 11 Oct 07 olle 190    */
2103 11 Oct 07 olle 191   public SessionControl getSessionControl()
2103 11 Oct 07 olle 192   {
2103 11 Oct 07 olle 193     return this.sc;
2103 11 Oct 07 olle 194   }
2103 11 Oct 07 olle 195
2103 11 Oct 07 olle 196   /**
2103 11 Oct 07 olle 197    * Set the SessionControl object.
2103 11 Oct 07 olle 198    * 
2103 11 Oct 07 olle 199    * @param sc the SessionControl object to set.
2103 11 Oct 07 olle 200    */
2103 11 Oct 07 olle 201   public void setSessionControl(SessionControl sc)
2103 11 Oct 07 olle 202   {
2103 11 Oct 07 olle 203     this.sc = sc;
2103 11 Oct 07 olle 204   }
2103 11 Oct 07 olle 205 }