javacommons/utils/tsrc/javasrc/com/nokia/mj/impl/rt/support/ApplicationInfoTest.java
changeset 80 d6dafc5d983f
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*
       
     2 * Copyright (c) 2010 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 package com.nokia.mj.impl.rt.support;
       
    19 
       
    20 
       
    21 
       
    22 import com.nokia.mj.impl.rt.test.UnitTestSuiteCreator;
       
    23 import com.nokia.mj.impl.rt.support.ApplicationInfo;
       
    24 import com.nokia.mj.impl.rt.test.ApplicationInfoImpl;
       
    25 import j2meunit.framework.Test;
       
    26 import j2meunit.framework.TestCase;
       
    27 import j2meunit.framework.TestMethod;
       
    28 import j2meunit.framework.TestSuite;
       
    29 
       
    30 /**
       
    31  * ApplicationInfo unit tests.
       
    32  */
       
    33 public class ApplicationInfoTest extends TestCase implements UnitTestSuiteCreator
       
    34 {
       
    35 
       
    36     // Begin j2meunit test framework setup
       
    37     public TestSuite createTestSuite(String[] args)
       
    38     {
       
    39         TestSuite suite = new TestSuite(this.getClass().getName());
       
    40 
       
    41         String testSelector = "0000000000000000";
       
    42         if (args.length > 1 && args[1] != null)
       
    43         {
       
    44             testSelector = args[1]  + "0000000000000000";
       
    45         }
       
    46 
       
    47 
       
    48         if (testSelector.charAt(0) == '0')
       
    49         {
       
    50             suite.addTest(new ApplicationInfoTest("testAppInfo", new TestMethod()
       
    51             {
       
    52                 public void run(TestCase tc)
       
    53                 {
       
    54                     ((ApplicationInfoTest)tc).testAppInfo();
       
    55                 }
       
    56             }));
       
    57         }
       
    58 
       
    59         if (testSelector.charAt(0) != '0')
       
    60         {
       
    61             suite.addTest(new ApplicationInfoTest("testAppInfoNotExist", new TestMethod()
       
    62             {
       
    63                 public void run(TestCase tc)
       
    64                 {
       
    65                     ((ApplicationInfoTest)tc).testAppInfoNotExist();
       
    66                 }
       
    67             }));
       
    68         }
       
    69 
       
    70         return suite;
       
    71 
       
    72     }
       
    73 
       
    74     public ApplicationInfoTest()
       
    75     {
       
    76     }
       
    77 
       
    78     public ApplicationInfoTest(String aTestName, TestMethod aTestMethod)
       
    79     {
       
    80         super(aTestName, aTestMethod);
       
    81     }
       
    82 
       
    83     // End j2meunit test framework setup
       
    84 
       
    85     protected void setUp()
       
    86     {
       
    87     }
       
    88 
       
    89     protected void tearDown()
       
    90     {
       
    91     }
       
    92 
       
    93     // Test cases
       
    94 
       
    95     private void check(String excpected, String received)
       
    96     {
       
    97         assertTrue("Expected: "+ excpected + ", Received: " + received,
       
    98                    excpected.equals(received));
       
    99     }
       
   100 
       
   101     private void testAppInfo()
       
   102     {
       
   103         try
       
   104         {
       
   105             ApplicationInfo appInfo = ApplicationInfo.getInstance();
       
   106             check(appInfo.getRuntimeType(), ApplicationInfoImpl.TEST_RT_TYPE);
       
   107             check(appInfo.getProtectionDomain(), ApplicationInfoImpl.TEST_PROTECTION_DOMAIN);
       
   108             check(appInfo.getUid().toString(), ApplicationInfoImpl.TEST_APP_UID.toString());
       
   109             check(appInfo.getSuiteUid().toString(), ApplicationInfoImpl.TEST_APP_SUITE_UID.toString());
       
   110             check(appInfo.getSuiteName(), ApplicationInfoImpl.TEST_APP_SUITE_NAME);
       
   111             check(appInfo.getName(), ApplicationInfoImpl.TEST_APP_NAME);
       
   112             check(appInfo.getVendor(), ApplicationInfoImpl.TEST_APP_VENDOR);
       
   113             check(appInfo.getVersion(), ApplicationInfoImpl.TEST_APP_VERSION);
       
   114             check(appInfo.getRootPath(), ApplicationInfoImpl.TEST_PATH);
       
   115             check(appInfo.getMainClass(), ApplicationInfoImpl.MAIN_CLASS);
       
   116             check(appInfo.getAttribute(ApplicationInfoImpl.TEST_KEY), ApplicationInfoImpl.TEST_VALUE);
       
   117         }
       
   118         catch (Throwable t)
       
   119         {
       
   120             t.printStackTrace();
       
   121             assertTrue(t.toString(), false);
       
   122         }
       
   123     }
       
   124 
       
   125     private void testAppInfoNotExist()
       
   126     {
       
   127         try
       
   128         {
       
   129             ApplicationInfo.getInstance().getSuiteName();
       
   130             assertTrue("No exception.", false);
       
   131         }
       
   132         catch (Error re)
       
   133         {
       
   134             int ind = re.toString().indexOf("Not able to instantiate class com.nokia.mj.impl.rt.test2.ApplicationInfoImpl");
       
   135             boolean ok = ind >= 0;
       
   136             if (!ok)
       
   137             {
       
   138                 // Accept also java.lang.ExceptionInInitializerError.
       
   139                 ok = re.toString().equals("java.lang.ExceptionInInitializerError");
       
   140             }
       
   141             assertTrue(re.toString(),  ok);
       
   142         }
       
   143         catch (Throwable t)
       
   144         {
       
   145             t.printStackTrace();
       
   146             assertTrue(t.toString(), false);
       
   147         }
       
   148     }
       
   149 
       
   150 }
       
   151