001    package org.hackystat.sensor.ant.test;
002    
003    import java.io.File;
004    import java.io.FileFilter;
005    import java.util.ArrayList;
006    import java.util.List;
007    
008    import org.hackystat.sensorbase.client.SensorBaseClient;
009    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataIndex;
010    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorDataRef;
011    import org.hackystat.sensorbase.server.Server;
012    import org.junit.AfterClass;
013    import org.junit.BeforeClass;
014    
015    /**
016     * Provides a helper method for Ant sensor test case development. 
017     * @author Philip Johnson
018     *
019     */
020    public class AntSensorTestHelper {
021      
022      /** The test user. */
023      protected static final String user = "TestAntSensors@hackystat.org";
024      /** The test host. */
025      protected static String host;
026      /** The test sensorbase server. */
027      protected static Server server;
028    
029      /**
030       * Starts the server going for these tests, and makes sure our test user is registered. 
031       * @throws Exception If problems occur setting up the server. 
032       */
033      @BeforeClass
034      public static void setupServer() throws Exception {
035        server = Server.newTestInstance();
036        host = server.getHostName();
037        SensorBaseClient.registerUser(host, user);
038      }
039      
040      /**
041       * Gets rid of the sent sensor data and the user. 
042       * @throws Exception If problems occur setting up the server.
043       */
044      @AfterClass 
045      public static void teardownServer() throws Exception {
046        // Now delete all data sent by this user.
047        SensorBaseClient client = new SensorBaseClient(host, user, user);
048        // First, delete all sensor data sent by this user. 
049        SensorDataIndex index = client.getSensorDataIndex(user);
050        for (SensorDataRef ref : index.getSensorDataRef()) {
051          client.deleteSensorData(user, ref.getTimestamp());
052        }
053        // Now delete the user too.
054        client.deleteUser(user);
055      }
056      
057      
058      /**
059       * Returns a list of XML files in the passed testFileDir directory. 
060       * @param testFileDir The directory in which the test files are found.
061       * @return The list of XML files. 
062       * @throws Exception If the test directory cannot be found.
063       */
064      public List<File> getXmlFiles(String testFileDir) throws Exception {
065        File directory = new File(testFileDir);
066        
067        // Make sure this is a directory, otherwise fail.
068        if (!directory.isDirectory()) {
069          throw new Exception ("The testFilePath passed was not a directory: " + testFileDir);
070        }
071        
072        File[] files = directory.listFiles();
073    
074        // create a file filter that only accepts xml files
075        FileFilter filter = new FileFilter() {
076          public boolean accept(File pathname) {
077            if (pathname.getName().endsWith(".xml")) {
078              return true;
079            }
080            return false;
081          }
082        };
083        
084        List<File> fileList = new ArrayList<File>();
085        // Generate the list of XML files. 
086        for (int j = 0; j < files.length; j++) {
087          if (filter.accept(files[j])) {
088            fileList.add(files[j]);
089          }
090        }
091        return fileList;
092      } 
093    
094    }