javamanager/javainstaller/installer/tsrc/javasrc/com/nokia/mj/impl/installer/downloader/DownloaderTest.java
changeset 21 2a9601315dfc
child 47 f40128debb5d
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.mj.impl.installer.downloader;
       
    20 
       
    21 import com.nokia.mj.impl.fileutils.FileUtility;
       
    22 import com.nokia.mj.impl.installer.utils.InstallerException;
       
    23 import com.nokia.mj.impl.installer.utils.InstallerMain;
       
    24 import com.nokia.mj.impl.installer.utils.Log;
       
    25 import com.nokia.mj.impl.utils.Base64;
       
    26 
       
    27 //import java.net.URL;
       
    28 //import java.net.URLConnection;
       
    29 
       
    30 import j2meunit.framework.Test;
       
    31 import j2meunit.framework.TestCase;
       
    32 import j2meunit.framework.TestMethod;
       
    33 import j2meunit.framework.TestSuite;
       
    34 
       
    35 /**
       
    36  * Downloader unit tests.
       
    37  */
       
    38 public class DownloaderTest extends TestCase implements InstallerMain
       
    39 {
       
    40     private static final String TEST_URL = "http://195.134.231.83:7070/java-server/resources/DS_Snow.jar";
       
    41     private static final int TEST_URL_SIZE = 10241; // test data file size
       
    42 
       
    43     // Begin j2meunit test framework setup
       
    44 
       
    45     public void installerMain(String[] args)
       
    46     {
       
    47         TestSuite suite = new TestSuite(this.getClass().getName());
       
    48 
       
    49         suite.addTest(new DownloaderTest("testIsDownloadUrl", new TestMethod()
       
    50         {
       
    51             public void run(TestCase tc)
       
    52             {
       
    53                 ((DownloaderTest)tc).testIsDownloadUrl();
       
    54             }
       
    55         }));
       
    56 
       
    57         suite.addTest(new DownloaderTest("testGetBasicAuthBase64", new TestMethod()
       
    58         {
       
    59             public void run(TestCase tc)
       
    60             {
       
    61                 ((DownloaderTest)tc).testGetBasicAuthBase64();
       
    62             }
       
    63         }));
       
    64 
       
    65         suite.addTest(new DownloaderTest("testBasicDownloadGcf", new TestMethod()
       
    66         {
       
    67             public void run(TestCase tc)
       
    68             {
       
    69                 ((DownloaderTest)tc).testBasicDownloadGcf();
       
    70             }
       
    71         }));
       
    72 
       
    73         suite.addTest(new DownloaderTest("testDownloadFailureGcf", new TestMethod()
       
    74         {
       
    75             public void run(TestCase tc)
       
    76             {
       
    77                 ((DownloaderTest)tc).testDownloadFailureGcf();
       
    78             }
       
    79         }));
       
    80 
       
    81         com.nokia.mj.impl.utils.OmjTestRunner.run(suite);
       
    82     }
       
    83 
       
    84     public DownloaderTest()
       
    85     {
       
    86     }
       
    87 
       
    88     public DownloaderTest(String aTestName, TestMethod aTestMethod)
       
    89     {
       
    90         super(aTestName, aTestMethod);
       
    91     }
       
    92 
       
    93     public void assertFalse(String aMsg, boolean aCondition)
       
    94     {
       
    95         assertTrue(aMsg, !aCondition);
       
    96     }
       
    97 
       
    98     // End j2meunit test framework setup
       
    99 
       
   100     protected void setUp()
       
   101     {
       
   102     }
       
   103 
       
   104     protected void tearDown()
       
   105     {
       
   106     }
       
   107 
       
   108     /**
       
   109      * Test isDownUrl method.
       
   110      */
       
   111     public void testIsDownloadUrl()
       
   112     {
       
   113         try
       
   114         {
       
   115             String[] urls = new String[]
       
   116             {
       
   117                 "http://a.b.c/A.jad",
       
   118                 "https://a.b.c/A.jad",
       
   119                 "HTTP://a.b.c/A.jad",
       
   120                 "HTTPS://a.b.c/A.jad",
       
   121             };
       
   122             for (int i = 0; i < urls.length; i++)
       
   123             {
       
   124                 assertTrue("Expected true for " + urls[i],
       
   125                            Downloader.isDownloadUrl(urls[i]));
       
   126             }
       
   127 
       
   128             urls = new String[]
       
   129             {
       
   130                 "A.jad",
       
   131                 "file:///A.jad",
       
   132             };
       
   133             for (int i = 0; i < urls.length; i++)
       
   134             {
       
   135                 assertTrue("Expected false for " + urls[i],
       
   136                            !Downloader.isDownloadUrl(urls[i]));
       
   137             }
       
   138         }
       
   139         catch (Exception ex)
       
   140         {
       
   141             ex.printStackTrace();
       
   142             assertTrue("Unexpected exception: " + ex, false);
       
   143         }
       
   144     }
       
   145 
       
   146     /**
       
   147      * Test getBasicAuthBase64 method.
       
   148      */
       
   149     public void testGetBasicAuthBase64()
       
   150     {
       
   151         try
       
   152         {
       
   153             String username = "user";
       
   154             String password = "pwd";
       
   155             String basicAuthEnc =
       
   156                 Downloader.getBasicAuthBase64(username, password);
       
   157             String basicAuthDec =
       
   158                 new String(Base64.decode(basicAuthEnc), "ISO8859_1");
       
   159             assertTrue("Incorrect basic auth credentials: " + basicAuthDec,
       
   160                        basicAuthDec.equals(username + ":" + password));
       
   161         }
       
   162         catch (Exception ex)
       
   163         {
       
   164             ex.printStackTrace();
       
   165             assertTrue("Unexpected exception: " + ex, false);
       
   166         }
       
   167     }
       
   168 
       
   169     /**
       
   170      * Test a basic download case with given Downloader.
       
   171      */
       
   172     public void testBasicDownload(Downloader downloader,
       
   173                                   TestDlListener listener)
       
   174     {
       
   175         try
       
   176         {
       
   177             String filename = "test.dat";
       
   178             DownloadInfo dlInfo = new DownloadInfo(TEST_URL, filename);
       
   179             downloader.start(dlInfo);
       
   180             downloader.waitForCompletion();
       
   181             assertTrue("Result DlInfo is null",
       
   182                        listener.getDownloadInfo() != null);
       
   183             assertTrue("Result DlInfo has an exception: " +
       
   184                        listener.getDownloadInfo().getException(),
       
   185                        listener.getDownloadInfo().getException() == null);
       
   186             assertTrue("Downloaded file has wrong size",
       
   187                        new FileUtility(filename).fileSize() == TEST_URL_SIZE);
       
   188         }
       
   189         catch (Exception ex)
       
   190         {
       
   191             ex.printStackTrace();
       
   192             assertTrue("Unexpected exception: " + ex, false);
       
   193         }
       
   194     }
       
   195     public void testBasicDownloadGcf()
       
   196     {
       
   197         TestDlListener listener = new TestDlListener();
       
   198         Downloader downloader = GcfDownloader.getDownloader(listener);
       
   199         testBasicDownload(downloader, listener);
       
   200     }
       
   201 
       
   202     /**
       
   203      * Test a failing download case with given Downloader.
       
   204      */
       
   205     public void testDownloadFailure(Downloader downloader,
       
   206                                     TestDlListener listener)
       
   207     {
       
   208         try
       
   209         {
       
   210             String filename = "test.dat";
       
   211             DownloadInfo dlInfo =
       
   212                 new DownloadInfo("http://unsupportedurl", filename);
       
   213             downloader.start(dlInfo);
       
   214             downloader.waitForCompletion();
       
   215             assertTrue("Result DlInfo is null",
       
   216                        listener.getDownloadInfo() != null);
       
   217             assertTrue("Result DlInfo does not have an exception",
       
   218                        listener.getDownloadInfo().getException() != null);
       
   219         }
       
   220         catch (Exception ex)
       
   221         {
       
   222             ex.printStackTrace();
       
   223             assertTrue("Unexpected exception: " + ex, false);
       
   224         }
       
   225     }
       
   226     public void testDownloadFailureGcf()
       
   227     {
       
   228         TestDlListener listener = new TestDlListener();
       
   229         Downloader downloader = GcfDownloader.getDownloader(listener);
       
   230         testDownloadFailure(downloader, listener);
       
   231     }
       
   232 
       
   233     private static void sleep(long aMillis)
       
   234     {
       
   235         try
       
   236         {
       
   237             Thread.sleep(aMillis);
       
   238         }
       
   239         catch (InterruptedException ie)
       
   240         {
       
   241         }
       
   242     }
       
   243 
       
   244     private static class TestDlListener implements DownloadListener
       
   245     {
       
   246         private DownloadInfo iDlInfo = null;
       
   247         public DownloadInfo getDownloadInfo()
       
   248         {
       
   249             return iDlInfo;
       
   250         }
       
   251 
       
   252         /** Download has started. */
       
   253         public void started(DownloadInfo aDlInfo)
       
   254         {
       
   255             Log.log("TestDlListener.started: " + aDlInfo);
       
   256         }
       
   257 
       
   258         /** Download has progressed. */
       
   259         public void updateProgress(DownloadInfo aDlInfo)
       
   260         {
       
   261             Log.log("TestDlListener.updateProgress: " + aDlInfo);
       
   262         }
       
   263 
       
   264         /** Download has ended. */
       
   265         public void ended(DownloadInfo aDlInfo)
       
   266         {
       
   267             iDlInfo = aDlInfo;
       
   268             Log.log("TestDlListener.ended: " + aDlInfo);
       
   269         }
       
   270 
       
   271         /**
       
   272          * Downloader needs username and password for HTTP authentication.
       
   273          * This method returns an array of two strings, the first being username
       
   274          * and second being password. If username and password cannot be obtained,
       
   275          * method returns null.
       
   276          *
       
   277          * @param aUrl URL for which username and password is needed.
       
   278          */
       
   279         public String[] getUsernamePassword(String aUrl)
       
   280         {
       
   281             Log.log("TestDlListener.getUsernamePassword: " + aUrl);
       
   282             return new String[] { "guest", "guest" };
       
   283         }
       
   284     }
       
   285 
       
   286     // Begin JavaNetTest
       
   287 
       
   288     /**
       
   289      * Test basic functionality of java.net.URLConnection.
       
   290      * /
       
   291     public void testJavaNet() {
       
   292         String[] args = new String[] {
       
   293             //"http://www.commonly.known.internet.address.com",
       
   294             //"http://test.my.code.com",
       
   295             // The expected values below are for this url:
       
   296             "http://ip.to.test.server:7070/java-server/resources/JarUnderTest.jar",
       
   297         };
       
   298 
       
   299         try {
       
   300             //System.out.println("Connecting to " + args[0]);
       
   301             URL url = new URL(args[0]);
       
   302             //System.out.println("Got url");
       
   303             URLConnection connection = url.openConnection();
       
   304             //System.out.println("Got connection");
       
   305             for (int i = 0; i < 3; i++) {
       
   306                 String h = connection.getHeaderField(i);
       
   307                 //System.out.println("Got header field " + i + ": " + h);
       
   308                 switch (i) {
       
   309                 case 0:
       
   310                     assertTrue("Got unexpected header " + i + ": " + h,
       
   311                                h.equals("HTTP/1.1 200 OK"));
       
   312                     break;
       
   313                 case 1:
       
   314                     assertTrue("Got unexpected header " + i + ": " + h,
       
   315                                h.equals("Apache-Coyote/1.1"));
       
   316                     break;
       
   317                 case 2:
       
   318                     assertTrue("Got unexpected header " + i + ": " + h,
       
   319                                h.equals("W/\"10241-1214380094000\""));
       
   320                     break;
       
   321                 }
       
   322             }
       
   323             String contentType = connection.getContentType();
       
   324             //System.out.println("Got content type " + contentType);
       
   325             assertTrue("Got unexpected content type: " + contentType,
       
   326                        contentType.equals("application/java-archive"));
       
   327             int contentLength = connection.getContentLength();
       
   328             //System.out.println("Got content length " + contentLength);
       
   329             assertTrue("Got unexpected content length: " + contentLength,
       
   330                        contentLength == 10241);
       
   331         } catch (Exception ex) {
       
   332             //System.out.println("Unexpected exception: " + ex);
       
   333             ex.printStackTrace();
       
   334             assertTrue("Unexpected exception: " + ex, false);
       
   335         }
       
   336     }
       
   337     */
       
   338 
       
   339     // End JavaNetTest
       
   340 }