Back to index

lightning-sunbird  0.9+nobinonly
Static Public Member Functions | Static Public Attributes | Static Package Functions
TestArray Class Reference

Adapted from xpcom/tests/TestArray.cpp. More...

List of all members.

Static Public Member Functions

static void main (String[] args)

Static Public Attributes

static final String NS_ARRAY_CONTRACTID = "@mozilla.org/array;1"

Static Package Functions

static void fillArray (nsIMutableArray aArray, int aCount)
static String assertEqual (long aValue1, long aValue2)
static void dumpArray (nsIMutableArray aArray, int aExpectedCount, int[] aElementIDs, int aExpectedTotal)
static long lastIndexOf (nsIMutableArray aArray, nsISupports aElement)
static void replaceElementAt (nsIMutableArray aArray, nsISupports aElement, int aIndex)
static void removeElement (nsIMutableArray aArray, nsISupports aElement)
static void removeLastElement (nsIMutableArray aArray, nsISupports aElement)

Detailed Description

Adapted from xpcom/tests/TestArray.cpp.

This tests the following parts of the Java<->XPCOM code:

NOTE: The System.gc() calls throughout the testcase are used to tell the JVM that now would be a good time to garbage collect. This helps us see if everything is getting freed properly. However, since the System.gc() call is only a suggestion to the JVM (you can't force garbage collection), this testcase may not work the same on every system. On my system, it usually works as expected (that is, Java objects that are no longer in use are destroyed whenever System.gc() is called). But I have seen some instances where the garbage collector ran later in the testcase.

Definition at line 63 of file TestArray.java.


Member Function Documentation

static String TestArray.assertEqual ( long  aValue1,
long  aValue2 
) [inline, static, package]

Definition at line 196 of file TestArray.java.

  {
    if (aValue1 == aValue2)
      return "OK";
    return "ERROR";
  }

Here is the caller graph for this function:

static void TestArray.dumpArray ( nsIMutableArray  aArray,
int  aExpectedCount,
int[]  aElementIDs,
int  aExpectedTotal 
) [inline, static, package]

Definition at line 203 of file TestArray.java.

  {
    long count = 0;
    if (aArray != null)
      count = aArray.getLength();

    System.out.println("object count " + Foo.gCount + " = " + aExpectedTotal +
                       " " + assertEqual(Foo.gCount, aExpectedTotal));
    System.out.println("array count " + count + " = " + aExpectedCount + " " +
                       assertEqual(count, aExpectedCount));

    for (int index = 0; (index < count) && (index < aExpectedCount); index++) {
      IFoo foo = (IFoo) aArray.queryElementAt(index, IFoo.IFOO_IID);
      System.out.println(index + ": " + aElementIDs[index] + "=" +
                         foo.getId() + " (" +
                         Integer.toHexString(foo.hashCode()) + ") " + 
                         assertEqual(foo.getId(), aElementIDs[index]));
      foo = null;
    }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static void TestArray.fillArray ( nsIMutableArray  aArray,
int  aCount 
) [inline, static, package]

Definition at line 188 of file TestArray.java.

  {
    for (int index = 0; index < aCount; index++) {
      IFoo foo = new Foo(index);
      aArray.appendElement(foo, false);
    }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static long TestArray.lastIndexOf ( nsIMutableArray  aArray,
nsISupports  aElement 
) [inline, static, package]

Definition at line 225 of file TestArray.java.

  {
    for (long i = aArray.getLength() - 1; i >= 0; i--) {
      IFoo foo = (IFoo) aArray.queryElementAt(i, IFoo.IFOO_IID);
      if (foo == aElement)
        return i;
    }
    return -1;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static void TestArray.main ( String[]  args) [inline, static]

Definition at line 66 of file TestArray.java.

                                          {
    System.loadLibrary("javaxpcom");

    String mozillaPath = System.getProperty("MOZILLA_FIVE_HOME");
    if (mozillaPath == null) {
      throw new RuntimeException("MOZILLA_FIVE_HOME system property not set.");
    }

    File localFile = new File(mozillaPath);
    XPCOM.initXPCOM(localFile, null);
    // XPCOM.initXPCOM() only initializes XPCOM.  If you want to initialize
    // Gecko, you would do the following instead:
    //    GeckoEmbed.initEmbedding(localFile, null);

    nsIComponentManager componentManager = XPCOM.getComponentManager();
    nsIMutableArray array = (nsIMutableArray)
      componentManager.createInstanceByContractID(NS_ARRAY_CONTRACTID, null,
                                                  nsIMutableArray.NS_IMUTABLEARRAY_IID);
    if (array == null) {
      throw new RuntimeException("Failed to create nsIMutableArray.");
    }

    fillArray(array, 10);
    System.out.println("Array created:");
    int fillResult[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    dumpArray(array, 10, fillResult, 10);

    // test insert
    IFoo foo = (IFoo) array.queryElementAt(3, IFoo.IFOO_IID);
    array.insertElementAt(foo, 5, false);
    System.out.println("insert 3 at 5:");
    int insertResult[] = {0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
    dumpArray(array, 11, insertResult, 10);
    System.out.println("insert 3 at 0:");
    array.insertElementAt(foo, 0, false);
    int insertResult2[] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9};
    dumpArray(array, 12, insertResult2, 10);
    System.out.println("append 3:");
    array.appendElement(foo, false);
    int appendResult[] = {3, 0, 1, 2, 3, 4, 3, 5, 6, 7, 8, 9, 3};
    dumpArray(array, 13, appendResult, 10);

    // test IndexOf && LastIndexOf
    int expectedIndex[] = {0, 4, 6, 12, -1};
    int count = 0;
    long index = array.indexOf(0, foo);
    System.out.println("IndexOf(foo): " + index + "=" + expectedIndex[count] +
                       " " + assertEqual(index, expectedIndex[count]));
    try {
      do {
        count++;
        index = array.indexOf(index + 1, foo);
        System.out.println("IndexOf(foo): " + index + "=" +
                           expectedIndex[count] + " " +
                           assertEqual(index, expectedIndex[count]));
      } while (true);
    } catch (XPCOMException e) {
      // If array.indexOf() did not find the element, it returns
      // NS_ERROR_FAILURE.
      if (e.errorcode != XPCOM.NS_ERROR_FAILURE) {
        throw e;
      }
    }

    index = lastIndexOf(array, foo);
    count--;
    System.out.println("LastIndexOf(foo): " + index + "=" +
                       expectedIndex[count] + " " + 
                       assertEqual(index, expectedIndex[count]));

    System.out.println("ReplaceElementAt(8):");
    replaceElementAt(array, foo, 8);
    System.gc();
    int replaceResult[] = {3, 0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
    dumpArray(array, 13, replaceResult, 9);

    System.out.println("RemoveElementAt(0):");
    array.removeElementAt(0);
    System.gc();
    int removeResult[] = {0, 1, 2, 3, 4, 3, 5, 3, 7, 8, 9, 3};
    dumpArray(array, 12, removeResult, 9);
    System.out.println("RemoveElementAt(7):");
    array.removeElementAt(7);
    System.gc();
    int removeResult2[] = {0, 1, 2, 3, 4, 3, 5, 7, 8, 9, 3};
    dumpArray(array, 11, removeResult2, 9);
    System.out.println("RemoveElement(foo):");
    removeElement(array, foo);
    System.gc();
    int removeResult3[] = {0, 1, 2, 4, 3, 5, 7, 8, 9, 3};
    dumpArray(array, 10, removeResult3, 9);
    System.out.println("RemoveLastElement(foo):");
    removeLastElement(array, foo);
    System.gc();
    int removeResult4[] = {0, 1, 2, 4, 3, 5, 7, 8, 9};
    dumpArray(array, 9, removeResult4, 9);

    // test clear
    foo = null;  // remove ref now, so everything is cleared
    System.out.println("clear array:");
    array.clear();
    System.gc();
    dumpArray(array, 0, null, 0);
    System.out.println("add 4 new:");
    fillArray(array, 4);
    dumpArray(array, 4, fillResult, 4);

    // test deleting of array
    System.out.println("release array:");
    array = null;
    System.gc();
    dumpArray(array, 0, null, 0);

    localFile = null;
    componentManager = null;
    System.gc();
    XPCOM.shutdownXPCOM(null);
    //    GeckoEmbed.termEmbedding();

    System.out.println("Test Passed.");
  }

Here is the call graph for this function:

static void TestArray.removeElement ( nsIMutableArray  aArray,
nsISupports  aElement 
) [inline, static, package]

Definition at line 243 of file TestArray.java.

  {
    long index = aArray.indexOf(0, aElement);
    aArray.removeElementAt(index);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static void TestArray.removeLastElement ( nsIMutableArray  aArray,
nsISupports  aElement 
) [inline, static, package]

Definition at line 249 of file TestArray.java.

  {
    long index = lastIndexOf(aArray, aElement);
    aArray.removeElementAt(index);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

static void TestArray.replaceElementAt ( nsIMutableArray  aArray,
nsISupports  aElement,
int  aIndex 
) [inline, static, package]

Definition at line 235 of file TestArray.java.

  {
    aArray.removeElementAt(aIndex);
    aArray.insertElementAt(aElement, aIndex, false);
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

final String TestArray.NS_ARRAY_CONTRACTID = "@mozilla.org/array;1" [static]

Definition at line 64 of file TestArray.java.


The documentation for this class was generated from the following file: