javacommons/utils/tsrc/javasrc/com/nokia/mj/impl/rt/test/JvmPortTest.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.test;
       
    19 
       
    20 
       
    21 import com.nokia.mj.impl.rt.JvmPort;
       
    22 import com.nokia.mj.impl.rt.support.ThreadEventListener;
       
    23 import com.nokia.mj.impl.rt.support.NativeMemoryBlock;
       
    24 
       
    25 public class JvmPortTest extends JvmPort
       
    26 {
       
    27 
       
    28     public static final int LOADSYSTEMLIBRARY_CALL = 0;
       
    29     public static final int LOADAPPLICATIONCLASS_CALL = 1;
       
    30     public static final int RUNYOUNGGENERATIONGC_CALL = 2;
       
    31     public static final int SETTHREADASDAEMON_CALL = 3;
       
    32     public static final int GETRESOURCEASNATIVEMEMORY_CALL = 4;
       
    33     public static final int DISABLERUNTIMEEXIT_CALL = 5;
       
    34     public static final int ENABLERUNTIMEEXIT_CALL = 6;
       
    35     public static final int EXITVM_CALL = 7;
       
    36     public static final int RUNFINALIZATION_CALL = 8;
       
    37     public static final int ENABLEFINALIZATION_CALL = 9;
       
    38     public static final int SETTHREADEVENTLISTENER_CALL = 10;
       
    39     public static final int ADDRESTRICTEDPACKAGEPREFIXES_CALL = 11;
       
    40     public static final int ADDPROTECTEDPACKAGEPREFIXES_CALL = 12;
       
    41     public static final int APPENDTOCLASSPATH_CALL = 13;
       
    42     public static final int SHRINKJAVAHEAPTOMINIMUM_CALL = 14;
       
    43     public static final int EXPANDJAVAHEAP_CALL = 15;
       
    44 
       
    45 
       
    46     public static int[] mCalledArr = new int[16];
       
    47     public static int[] mCalledTotalArr = new int[16];
       
    48 
       
    49     static
       
    50     {
       
    51         clear();
       
    52         for (int i = 0; i < mCalledTotalArr.length; ++i)
       
    53         {
       
    54             mCalledTotalArr[i] = 0;
       
    55         }
       
    56     }
       
    57 
       
    58     public static Object mObj1 = null;
       
    59     public static Object mObj2 = null;
       
    60     public static Object mReturnObj = null;
       
    61     public static Throwable mThrowable = null;
       
    62 
       
    63     public static void clear()
       
    64     {
       
    65         for (int i = 0; i < mCalledArr.length; ++i)
       
    66         {
       
    67             mCalledArr[i] = 0;
       
    68         }
       
    69         mObj1 = null;
       
    70         mObj2 = null;
       
    71         mReturnObj = null;
       
    72         mThrowable = null;
       
    73     }
       
    74 
       
    75     public static void resetInstance()
       
    76     {
       
    77         sInstance = null;
       
    78     }
       
    79 
       
    80     public static boolean allCalled()
       
    81     {
       
    82         for (int i = 0; i < mCalledTotalArr.length; ++i)
       
    83         {
       
    84             if (mCalledTotalArr[i] == 0)
       
    85             {
       
    86                 StringBuffer sb = new StringBuffer();
       
    87                 for (int j = 0; j < mCalledTotalArr.length; ++j)
       
    88                 {
       
    89                     sb.append("\n");
       
    90                     sb.append("  " + (j + 1) + " == " + mCalledTotalArr[j]);
       
    91                 }
       
    92                 System.err.println("All calls:"+ sb.toString());
       
    93 
       
    94                 return false;
       
    95             }
       
    96         }
       
    97         return true;
       
    98     }
       
    99 
       
   100     public static boolean checkCalls(int checkInd)
       
   101     {
       
   102         for (int i = 0; i < mCalledArr.length; ++i)
       
   103         {
       
   104             if (checkInd == i)
       
   105             {
       
   106                 if (mCalledArr[i] != 1)
       
   107                 {
       
   108                     return false;
       
   109                 }
       
   110             }
       
   111             else if (mCalledArr[i] != 0)
       
   112             {
       
   113                 return false;
       
   114             }
       
   115         }
       
   116         return true;
       
   117     }
       
   118 
       
   119     public static String print()
       
   120     {
       
   121         StringBuffer sb = new StringBuffer();
       
   122         for (int i = 0; i < mCalledArr.length; ++i)
       
   123         {
       
   124             sb.append("\n");
       
   125             sb.append("  " + (i + 1) + " == " + mCalledArr[i]);
       
   126         }
       
   127         return sb.toString();
       
   128     }
       
   129 
       
   130     public static void setSystemPropertyImpl(Object key, Object value)
       
   131     {
       
   132         mPropertiesContainer.setSystemProperty(key, value);
       
   133     }
       
   134 
       
   135 
       
   136     public void loadSystemLibrary(String libName)
       
   137     {
       
   138         mCalledArr[LOADSYSTEMLIBRARY_CALL]++;
       
   139         mCalledTotalArr[LOADSYSTEMLIBRARY_CALL]++;
       
   140         mObj1 = libName;
       
   141     }
       
   142 
       
   143     public Class loadApplicationClass(String name) throws ClassNotFoundException
       
   144     {
       
   145         mCalledArr[LOADAPPLICATIONCLASS_CALL]++;
       
   146         mCalledTotalArr[LOADAPPLICATIONCLASS_CALL]++;
       
   147         if (mThrowable != null)
       
   148         {
       
   149             throw(ClassNotFoundException)mThrowable;
       
   150         }
       
   151         mObj1 = name;
       
   152         sInstance = null;
       
   153         return (Class)mReturnObj;
       
   154     }
       
   155 
       
   156     public boolean runYoungGenerationGc()
       
   157     {
       
   158         mCalledArr[RUNYOUNGGENERATIONGC_CALL]++;
       
   159         mCalledTotalArr[RUNYOUNGGENERATIONGC_CALL]++;
       
   160         return ((Boolean)mReturnObj).booleanValue();
       
   161     }
       
   162 
       
   163     public void setThreadAsDaemon(Thread thread,
       
   164                                   boolean daemon)
       
   165     throws IllegalThreadStateException, SecurityException
       
   166     {
       
   167         mCalledArr[SETTHREADASDAEMON_CALL]++;
       
   168         mCalledTotalArr[SETTHREADASDAEMON_CALL]++;
       
   169         if (mThrowable != null)
       
   170         {
       
   171             if (mThrowable instanceof IllegalThreadStateException)
       
   172             {
       
   173                 throw(IllegalThreadStateException)mThrowable;
       
   174             }
       
   175             else
       
   176             {
       
   177                 throw(SecurityException)mThrowable;
       
   178 
       
   179             }
       
   180         }
       
   181         mObj1 = thread;
       
   182         mObj2 = new Boolean(daemon);
       
   183     }
       
   184 
       
   185 
       
   186     public NativeMemoryBlock getResourceAsNativeMemory(String jarPath,
       
   187             String resName)
       
   188     {
       
   189         mCalledArr[GETRESOURCEASNATIVEMEMORY_CALL]++;
       
   190         mCalledTotalArr[GETRESOURCEASNATIVEMEMORY_CALL]++;
       
   191         mObj1 = jarPath;
       
   192         mObj2 = resName;
       
   193         return (NativeMemoryBlock)mReturnObj;
       
   194     }
       
   195 
       
   196 
       
   197     public void disableRuntimeExit()
       
   198     {
       
   199         mCalledArr[DISABLERUNTIMEEXIT_CALL]++;
       
   200         mCalledTotalArr[DISABLERUNTIMEEXIT_CALL]++;
       
   201     }
       
   202 
       
   203     public void enableRuntimeExit()
       
   204     {
       
   205         mCalledArr[ENABLERUNTIMEEXIT_CALL]++;
       
   206         mCalledTotalArr[ENABLERUNTIMEEXIT_CALL]++;
       
   207     }
       
   208 
       
   209     public void exitVm(int status)
       
   210     {
       
   211         mCalledArr[EXITVM_CALL]++;
       
   212         mCalledTotalArr[EXITVM_CALL]++;
       
   213         mObj1 = new Integer(status);
       
   214     }
       
   215 
       
   216     public void runFinalization()
       
   217     {
       
   218         mCalledArr[RUNFINALIZATION_CALL]++;
       
   219         mCalledTotalArr[RUNFINALIZATION_CALL]++;
       
   220     }
       
   221 
       
   222     public void enableFinalization(Class clazz)
       
   223     {
       
   224         mCalledArr[ENABLEFINALIZATION_CALL]++;
       
   225         mCalledTotalArr[ENABLEFINALIZATION_CALL]++;
       
   226         mObj1 = clazz;
       
   227     }
       
   228 
       
   229     public void setThreadEventListener(ThreadEventListener listener)
       
   230     {
       
   231         mCalledArr[SETTHREADEVENTLISTENER_CALL]++;
       
   232         mCalledTotalArr[SETTHREADEVENTLISTENER_CALL]++;
       
   233         mObj1 = listener;
       
   234     }
       
   235 
       
   236     public void addRestrictedPackagePrefixes(String[] packageNames)
       
   237     throws SecurityException
       
   238     {
       
   239         mCalledArr[ADDRESTRICTEDPACKAGEPREFIXES_CALL]++;
       
   240         mCalledTotalArr[ADDRESTRICTEDPACKAGEPREFIXES_CALL]++;
       
   241         if (mThrowable != null)
       
   242         {
       
   243             throw(SecurityException)mThrowable;
       
   244         }
       
   245         mObj1 = packageNames;
       
   246     }
       
   247 
       
   248     public void addProtectedPackagePrefixes(String[] packageNames)
       
   249     throws SecurityException
       
   250     {
       
   251         mCalledArr[ADDPROTECTEDPACKAGEPREFIXES_CALL]++;
       
   252         mCalledTotalArr[ADDPROTECTEDPACKAGEPREFIXES_CALL]++;
       
   253         if (mThrowable != null)
       
   254         {
       
   255             throw(SecurityException)mThrowable;
       
   256         }
       
   257         mObj1 = packageNames;
       
   258     }
       
   259 
       
   260     public void appendToClassPath(String path)
       
   261     {
       
   262         mCalledArr[APPENDTOCLASSPATH_CALL]++;
       
   263         mCalledTotalArr[APPENDTOCLASSPATH_CALL]++;
       
   264         mObj1 = path;
       
   265     }
       
   266 
       
   267     public int shrinkJavaHeapToMinimum()
       
   268     {
       
   269         mCalledArr[SHRINKJAVAHEAPTOMINIMUM_CALL]++;
       
   270         mCalledTotalArr[SHRINKJAVAHEAPTOMINIMUM_CALL]++;
       
   271         return ((Integer)mReturnObj).intValue();
       
   272     }
       
   273 
       
   274     public int expandJavaHeap(int amount)
       
   275     {
       
   276         mCalledArr[EXPANDJAVAHEAP_CALL]++;
       
   277         mCalledTotalArr[EXPANDJAVAHEAP_CALL]++;
       
   278         mObj1 = new Integer(amount);
       
   279         return ((Integer)mReturnObj).intValue();
       
   280     }
       
   281 
       
   282 }