nerru86

51Degrees Java API auto update tests

Mar 27th, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.01 KB | None | 0 0
  1. /*
  2.  * Code to test basic auto update functionality of the 51Degrees Java API
  3.  */
  4. package updatetest;
  5.  
  6. import fiftyone.mobile.detection.*;
  7. import fiftyone.mobile.detection.factories.MemoryFactory;
  8. import fiftyone.mobile.detection.factories.StreamFactory;
  9. import java.io.IOException;
  10. import java.nio.file.Files;
  11. import java.nio.file.Path;
  12. import java.nio.file.Paths;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15.  
  16. /**
  17.  *
  18.  * @author mike
  19.  */
  20. public class UpdateTest {
  21.  
  22.     private static final String LICENCE_KEY = "YOUR_KEY";
  23.     private static final String PATH_TO_FILE = "path\\to\\51Degrees.dat";
  24.    
  25.     /**
  26.      * @param args the command line arguments
  27.      */
  28.     public static void main(String[] args) throws AutoUpdateException, IOException, InterruptedException {
  29.         updateWithTimeInterval();
  30.         System.exit(0);
  31.     }
  32.    
  33.     /**
  34.      * Function performs an update with a given time interval in between each
  35.      * attempt.
  36.      */
  37.     private static void updateWithTimeInterval() {
  38.         //Loop break condition.
  39.         boolean update = true;
  40.         //Counter of iterations.
  41.         int counter = 0;
  42.         do {
  43.             counter++;
  44.             System.out.println("Commencing iteration "+counter+".");
  45.             //Perform the update.
  46.             update = update();
  47.            
  48.             if(update) {
  49.                
  50.                 if(verifyUsingMemoryFactory())
  51.                     System.out.println("OK: Data file verified.");
  52.                 else
  53.                     System.out.println("ERROR: Data file not verified.");
  54.                
  55.                 //Try to put thread to sleep for 5 seconds.
  56.                 try {
  57.                     System.out.println("Pausing for 5 seconds.");
  58.                     Thread.sleep(5000);
  59.                 } catch (InterruptedException ex) {
  60.                     System.out.println("ERROR: Pause failed.");
  61.                     Logger.getLogger(UpdateTest.class.getName()).log(Level.SEVERE, null, ex);
  62.                 }
  63.             } else {
  64.                 System.out.println("Update failed.");
  65.             }
  66.            
  67.             System.out.println("Iteration "+counter+" complete.\n");
  68.         } while(update);
  69.         //Print report.
  70.         System.out.println("Total iterations: "+counter);
  71.     }
  72.    
  73.     /**
  74.      * Function performs an update repeatedly until the first fail. Data file
  75.      * gets deleted after every update.
  76.      */
  77.     private static void updateUntilDownloadLimit() {
  78.         //Indicates if update was successful.
  79.         boolean update = true;
  80.         //Counts the successful updates.
  81.         int icount = 0;
  82.        
  83.         do {            
  84.             //Perform update.
  85.             update = update();
  86.            
  87.             //Update sucessful.
  88.             if (update) {
  89.                 if (verifyUsingMemoryFactory())
  90.                     System.out.println("OK: Data file verified.");
  91.                 else
  92.                     System.out.println("ERROR: Data file not verified.");
  93.                
  94.                 if (deleteFile())
  95.                     System.out.println("OK: Data file deleted.");
  96.                 else
  97.                     System.out.println("ERROR: Data file not deleted.");
  98.             }
  99.         } while (update);
  100.         //Print number of successful updates.
  101.         System.out.println("number of successfull attempts: " + icount);
  102.     }
  103.    
  104.     /**
  105.      * function reports the amount of memory allocated to JVM.
  106.      */
  107.     private static void reportMemory() {
  108.         long heapSize = Runtime.getRuntime().totalMemory();
  109.         System.out.println("current heap: " + heapSize);
  110.     }
  111.    
  112.     /**
  113.      * Function attempts to remove a data file at the path specified in
  114.      * PATH_TO_FILE.
  115.      * @return True is successful
  116.      *         False otherwise
  117.      */
  118.     private static boolean deleteFile() {
  119.         //try to delete the file.
  120.         Path p = Paths.get(PATH_TO_FILE);
  121.         try {
  122.             Files.delete(p);
  123.             return true;
  124.         } catch(Exception e) {
  125.             System.out.println("Could not delete file");
  126.         } finally {
  127.             p = null;
  128.         }
  129.         return false;
  130.     }
  131.    
  132.     /**
  133.      * Function verifies the data file is valid by constructing a provider and
  134.      * performing a match. As the name suggests StreamProvider is used.
  135.      * @return True if Provider was initialised and Match performed.
  136.      *         False otherwise.
  137.      */
  138.     private static boolean verifyUsingStreamFactory() {
  139.         try {
  140.             //Initialise provider.
  141.             Provider p = new Provider(StreamFactory.create(PATH_TO_FILE));
  142.             //Perform match. We know the following is mobile.
  143.             Match m = p.match("Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; "
  144.                     + "LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like "
  145.                     + "Gecko) Version/4.0 Mobile Safari/534.30");
  146.             if(m.getValues("IsMobile").toBool()) {
  147.                 p.dataSet.dispose();
  148.                 return true;
  149.             }
  150.         } catch (IOException ex) {
  151.             Logger.getLogger(UpdateTest.class.getName()).log(Level.SEVERE, null, ex);
  152.             return false;
  153.         }
  154.         return false;
  155.     }
  156.    
  157.     /**
  158.      * Function verifies the data file is valid by constructing a provider and
  159.      * performing a match. As the name suggests MemoryProvider is used.
  160.      * @return True if Provider was initialised and Match performed.
  161.      *         False otherwise.
  162.      */
  163.     private static boolean verifyUsingMemoryFactory() {
  164.         try {
  165.             //Initialise provider.
  166.             Provider p = new Provider(MemoryFactory.create(PATH_TO_FILE));
  167.             //Perform match. We know the following is mobile.
  168.             Match m = p.match("Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; "
  169.                     + "LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like "
  170.                     + "Gecko) Version/4.0 Mobile Safari/534.30");
  171.             if(m.getValues("IsMobile").toBool()) {
  172.                 p.dataSet.dispose();
  173.                 return true;
  174.             }
  175.         } catch (IOException ex) {
  176.             Logger.getLogger(UpdateTest.class.getName()).log(Level.SEVERE, null, ex);
  177.             return false;
  178.         }
  179.         return false;
  180.     }
  181.    
  182.     /**
  183.      * Function invokes 51D update method. File saved to the path specified in
  184.      * PATH_TO_FILE variable. LICENSE_KEY variable must not be null.
  185.      * @return True is update successful
  186.      *         False if update failed.
  187.      */
  188.     private static boolean update() {
  189.         //Try to update.
  190.         boolean update = false;
  191.         try {
  192.             //AutoUpdate returns True if success, False if failed.
  193.             update = fiftyone.mobile.detection.AutoUpdate.update(LICENCE_KEY, PATH_TO_FILE);
  194.         } catch(Exception e) {
  195.             System.out.println("Auto update failed" + e.getMessage() + e.getLocalizedMessage() + e.toString());
  196.         }
  197.         return update;
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment