B-1_14-DEV merge back to main
This commit is contained in:
@@ -6,67 +6,195 @@ import com.jacob.test.BaseTestCase;
|
||||
|
||||
/**
|
||||
* SafeArrayTest Program
|
||||
*
|
||||
* This is more of an exerciser. It doesn't verify that it gets back
|
||||
* what it expects like a junit test would
|
||||
*
|
||||
* This is more of an exerciser. It doesn't verify that it gets back what it
|
||||
* expects like a junit test would
|
||||
* <p>
|
||||
* May need to run with some command line options (including from inside Eclipse).
|
||||
* Look in the docs area at the Jacob usage document for command line options.
|
||||
*
|
||||
* May need to run with some command line options (including from inside
|
||||
* Eclipse). Look in the docs area at the Jacob usage document for command line
|
||||
* options.
|
||||
*
|
||||
*/
|
||||
public class SafeArrayBasicTest extends BaseTestCase {
|
||||
public void testBasicSafeArray(){
|
||||
//System.runFinalizersOnExit(true);
|
||||
SafeArray sa = new SafeArray(Variant.VariantVariant, 3);
|
||||
|
||||
sa.fromShortArray(new short[] { 1, 2, 3 });
|
||||
System.out.println("sa short=" + sa);
|
||||
int[] ai = sa.toIntArray();
|
||||
for (int i = 0; i < ai.length; i++) {
|
||||
System.out.println("toInt=" + ai[i]);
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testShortSafeArray() {
|
||||
short sourceData[] = new short[] { 1, 2, 3 };
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
|
||||
saUnderTest.fromShortArray(sourceData);
|
||||
short[] extractedFromSafeArray = saUnderTest.toShortArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
|
||||
}
|
||||
double[] ad = sa.toDoubleArray();
|
||||
for (int i = 0; i < ad.length; i++) {
|
||||
System.out.println("toDouble=" + ad[i]);
|
||||
|
||||
assertEquals("single get failed: ", sourceData[2], saUnderTest
|
||||
.getShort(2));
|
||||
|
||||
// test conversion
|
||||
int[] extractedFromSafeArrayInt = saUnderTest.toIntArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArrayInt[i]);
|
||||
}
|
||||
sa.fromIntArray(new int[] { 100000, 200000, 300000 });
|
||||
System.out.println("sa int=" + sa);
|
||||
ai = sa.toIntArray();
|
||||
for (int i = 0; i < ai.length; i++) {
|
||||
System.out.println("toInt=" + ai[i]);
|
||||
// test conversion
|
||||
double[] extractedFromSafeArrayDouble = saUnderTest.toDoubleArray();
|
||||
for (int i = 0; i < extractedFromSafeArrayDouble.length; i++) {
|
||||
assertEquals("" + i, new Double(sourceData[i]).doubleValue(),
|
||||
extractedFromSafeArrayDouble[i]);
|
||||
}
|
||||
ad = sa.toDoubleArray();
|
||||
for (int i = 0; i < ad.length; i++) {
|
||||
System.out.println("toDouble=" + ad[i]);
|
||||
// test conversion
|
||||
Variant extractedFromSafeArrayVariant[] = saUnderTest.toVariantArray();
|
||||
for (int i = 0; i < extractedFromSafeArrayVariant.length; i++) {
|
||||
assertEquals("" + i, sourceData[i],
|
||||
extractedFromSafeArrayVariant[i].getShort());
|
||||
}
|
||||
Variant av[] = sa.toVariantArray();
|
||||
for (int i = 0; i < av.length; i++) {
|
||||
System.out.println("toVariant=" + av[i]);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testIntSafeArray() {
|
||||
int sourceData[] = new int[] { 100000, 200000, 300000 };
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
saUnderTest.fromIntArray(sourceData);
|
||||
int[] extractedFromSafeArray = saUnderTest.toIntArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
|
||||
}
|
||||
sa.fromDoubleArray(new double[] { 1.5, 2.5, 3.5 });
|
||||
System.out.println("sa double=" + sa);
|
||||
sa.fromFloatArray(new float[] { 1.5F, 2.5F, 3.5F });
|
||||
System.out.println("sa float=" + sa);
|
||||
sa.fromBooleanArray(new boolean[] { true, false, true, false });
|
||||
System.out.println("sa bool=" + sa);
|
||||
av = sa.toVariantArray();
|
||||
for (int i = 0; i < av.length; i++) {
|
||||
System.out.println("toVariant=" + av[i]);
|
||||
}
|
||||
sa.fromCharArray(new char[] { 'a', 'b', 'c', 'd' });
|
||||
System.out.println("sa char=" + sa);
|
||||
sa.fromStringArray(new String[] { "hello", "from", "java", "com" });
|
||||
System.out.println("sa string=" + sa);
|
||||
av = sa.toVariantArray();
|
||||
for (int i = 0; i < av.length; i++) {
|
||||
System.out.println("toVariant=" + av[i]);
|
||||
}
|
||||
sa.fromVariantArray(new Variant[] {
|
||||
new Variant(1), new Variant(2.3), new Variant("hi") });
|
||||
System.out.println("sa variant=" + sa);
|
||||
assertEquals("single get failed: ", sourceData[2], saUnderTest
|
||||
.getInt(2));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testLongSafeArray() {
|
||||
long sourceData[] = new long[] { 2L << 40, 3L << 41, 4L << 42 };
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
saUnderTest.fromLongArray(sourceData);
|
||||
long[] extractedFromSafeArray = saUnderTest.toLongArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
|
||||
}
|
||||
assertEquals("single get failed: ", sourceData[2], saUnderTest
|
||||
.getLong(2));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testDoubleSafeArray() {
|
||||
double sourceData[] = new double[] { 1.5, 2.5, 3.5 };
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
saUnderTest.fromDoubleArray(sourceData);
|
||||
double[] extractedFromSafeArray = saUnderTest.toDoubleArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
|
||||
}
|
||||
assertEquals("single get failed: ", sourceData[2], saUnderTest
|
||||
.getDouble(2));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testFloatSafeArray() {
|
||||
float sourceData[] = new float[] { 1.5F, 2.5F, 3.5F };
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
saUnderTest.fromFloatArray(sourceData);
|
||||
float[] extractedFromSafeArray = saUnderTest.toFloatArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testBooleanSafeArray() {
|
||||
boolean sourceData[] = new boolean[] { true, false, true, false };
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
saUnderTest.fromBooleanArray(sourceData);
|
||||
boolean[] extractedFromSafeArray = saUnderTest.toBooleanArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
|
||||
}
|
||||
assertEquals("single get failed: ", sourceData[2], saUnderTest
|
||||
.getBoolean(2));
|
||||
|
||||
// test conversion
|
||||
Variant extractedFromSafeArrayVariant[] = saUnderTest.toVariantArray();
|
||||
for (int i = 0; i < extractedFromSafeArrayVariant.length; i++) {
|
||||
assertEquals("" + i, sourceData[i],
|
||||
extractedFromSafeArrayVariant[i].getBoolean());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testCharSafeArray() {
|
||||
char sourceData[] = new char[] { 'a', 'b', 'c', 'd' };
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
saUnderTest.fromCharArray(sourceData);
|
||||
char[] extractedFromSafeArray = saUnderTest.toCharArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
|
||||
}
|
||||
assertEquals("single get failed: ", sourceData[2], saUnderTest
|
||||
.getChar(2));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testStringSaveArray() {
|
||||
String sourceData[] = new String[] { "hello", "from", "java", "com" };
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
saUnderTest.fromStringArray(sourceData);
|
||||
String[] extractedFromSafeArray = saUnderTest.toStringArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i], extractedFromSafeArray[i]);
|
||||
}
|
||||
assertEquals("single get failed: ", sourceData[2], saUnderTest
|
||||
.getString(2));
|
||||
|
||||
// test conversion
|
||||
Variant extractedFromSafeArrayVariant[] = saUnderTest.toVariantArray();
|
||||
for (int i = 0; i < extractedFromSafeArrayVariant.length; i++) {
|
||||
assertEquals("" + i, sourceData[i],
|
||||
extractedFromSafeArrayVariant[i].getString());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public void testVariantSafeArray() {
|
||||
Variant sourceData[] = new Variant[] { new Variant(1),
|
||||
new Variant(2.3), new Variant("hi") };
|
||||
|
||||
SafeArray saUnderTest = new SafeArray(Variant.VariantVariant, 3);
|
||||
saUnderTest.fromVariantArray(sourceData);
|
||||
Variant[] extractedFromSafeArray = saUnderTest.toVariantArray();
|
||||
for (int i = 0; i < extractedFromSafeArray.length; i++) {
|
||||
assertEquals("" + i, sourceData[i].toString(),
|
||||
extractedFromSafeArray[i].toString());
|
||||
}
|
||||
assertEquals("single get failed: ", sourceData[2].toString(),
|
||||
saUnderTest.getVariant(2).toString());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* test method that verifies setting of bounds in multi-dimensional arrays
|
||||
*/
|
||||
public void testSafeArrayNumDimensions() {
|
||||
int[] lowerBounds = new int[] { 0, 0, 0 };
|
||||
int[] dimensionSizes = new int[] { 3, 3, 3 };
|
||||
@@ -75,13 +203,29 @@ public class SafeArrayBasicTest extends BaseTestCase {
|
||||
dimensionSizes);
|
||||
|
||||
System.out.println("Num Dimensions = " + sa3x3.getNumDim());
|
||||
for (int i = 1; i <= sa3x3.getNumDim(); i++) {
|
||||
System.out.println("Dimension number = " + i);
|
||||
System.out.println("Lower bound = " + sa3x3.getLBound(i));
|
||||
System.out.println("Upper bound = " + sa3x3.getUBound(i));
|
||||
for (int safeArrayDimension = 1; safeArrayDimension <= sa3x3
|
||||
.getNumDim(); safeArrayDimension++) {
|
||||
int configArrayIndex = safeArrayDimension - 1;
|
||||
assertEquals("unexpected lower bound value ",
|
||||
lowerBounds[configArrayIndex], sa3x3
|
||||
.getLBound(safeArrayDimension));
|
||||
assertEquals("unexpeced upper bound value ",
|
||||
(dimensionSizes[configArrayIndex] - 1)
|
||||
+ lowerBounds[configArrayIndex], sa3x3
|
||||
.getUBound(safeArrayDimension));
|
||||
}
|
||||
}
|
||||
|
||||
int fill = 0;
|
||||
/**
|
||||
* test the set and get method on multi-dimensional arrays
|
||||
*/
|
||||
public void testSafeArrayMultiDimension() {
|
||||
|
||||
int[] lowerBounds = new int[] { 0, 0, 0 };
|
||||
int[] dimensionSizes = new int[] { 3, 3, 3 };
|
||||
|
||||
SafeArray sa3x3 = new SafeArray(Variant.VariantVariant, lowerBounds,
|
||||
dimensionSizes);
|
||||
int[] indices = new int[] { 0, 0, 0 };
|
||||
|
||||
for (int i = 0; i < 3; i++) {
|
||||
@@ -90,10 +234,17 @@ public class SafeArrayBasicTest extends BaseTestCase {
|
||||
indices[1] = j;
|
||||
for (int k = 0; k < 3; k++) {
|
||||
indices[2] = k;
|
||||
|
||||
int fill = 0;
|
||||
fill = i * 100 + j * 10 + k;
|
||||
sa3x3.setInt(indices, fill);
|
||||
System.out.println("sa[" + i + "][" + j + "][" + k + "] = "
|
||||
+ sa3x3.getInt(indices));
|
||||
assertEquals(fill, sa3x3.getInt(indices));
|
||||
|
||||
long fillLong = 0L;
|
||||
// Pick a number bigger than 2^31
|
||||
fillLong = 100000000000000L * fill;
|
||||
sa3x3.setLong(indices, fillLong);
|
||||
assertEquals(fillLong, sa3x3.getLong(indices));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,7 +267,7 @@ public class SafeArrayContents extends BaseTestCase {
|
||||
fail("Failed to catch expected exception");
|
||||
} catch (ComFailException cfe) {
|
||||
// do nothing
|
||||
//cfe.printStackTrace();
|
||||
// cfe.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,9 @@ import com.jacob.com.SafeArray;
|
||||
import com.jacob.com.Variant;
|
||||
import com.jacob.test.BaseTestCase;
|
||||
|
||||
/**
|
||||
* Test class to verify dispatch with SafeArray
|
||||
*/
|
||||
public class SafeArrayDispatchTest extends BaseTestCase {
|
||||
public void testDispatchWithSafeArray() {
|
||||
try {
|
||||
@@ -36,7 +39,7 @@ public class SafeArrayDispatchTest extends BaseTestCase {
|
||||
assertTrue(result.toString().equals("6"));
|
||||
} catch (ComException e) {
|
||||
e.printStackTrace();
|
||||
fail("script failure "+e);
|
||||
fail("script failure " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,191 +3,175 @@ package com.jacob.test.safearray;
|
||||
import com.jacob.activeX.ActiveXComponent;
|
||||
import com.jacob.com.ComThread;
|
||||
import com.jacob.com.Dispatch;
|
||||
import com.jacob.com.JacobObject;
|
||||
import com.jacob.com.JacobReleaseInfo;
|
||||
import com.jacob.com.SafeArray;
|
||||
import com.jacob.com.Variant;
|
||||
import com.jacob.test.BaseTestCase;
|
||||
|
||||
/**
|
||||
* This test program demonstrates a weak in the setString(int[],String) method in SafeArray.
|
||||
* To see the leak:
|
||||
* This test program demonstrates a weak in the setString(int[],String) method
|
||||
* in SafeArray. To see the leak:
|
||||
* <ul>
|
||||
* <li>Bring up the windows task manager and click on the performance tab.
|
||||
* <li>Run the test program
|
||||
* </ul>
|
||||
* You should see the Page File Usage History graph rise at te end of every cycle.
|
||||
* Running the same program with setString(r,c,String) does not show the same symptoms
|
||||
* You should see the Page File Usage History graph rise at te end of every
|
||||
* cycle. Running the same program with setString(r,c,String) does not show the
|
||||
* same symptoms
|
||||
*/
|
||||
public class SafeArrayLeak extends BaseTestCase {
|
||||
public class SafeArrayLeak extends BaseTestCase {
|
||||
|
||||
/**
|
||||
/**
|
||||
* ----------------------------------------------------------------------------------------------------------------------------
|
||||
*
|
||||
* ----------------------------------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
public void testLeakWithSetString() {
|
||||
|
||||
ActiveXComponent xl = null;
|
||||
Dispatch workbooks = null;
|
||||
Dispatch workbook = null;
|
||||
Dispatch workSheets = null;
|
||||
Dispatch sheet = null;
|
||||
Dispatch tabCells = null;
|
||||
SafeArray sa = null;
|
||||
|
||||
//-Dcom.jacob.autogc=true
|
||||
System.out.println("Jacob version: "+JacobObject.getBuildVersion());
|
||||
|
||||
for(int t = 0; t < 10; t++) {
|
||||
// look at a large range of cells
|
||||
ActiveXComponent xl = null;
|
||||
Dispatch workbooks = null;
|
||||
Dispatch workbook = null;
|
||||
Dispatch workSheets = null;
|
||||
Dispatch sheet = null;
|
||||
Dispatch tabCells = null;
|
||||
SafeArray sa = null;
|
||||
|
||||
// -Dcom.jacob.autogc=true
|
||||
System.out.println("Jacob version: " + JacobReleaseInfo.getBuildVersion());
|
||||
|
||||
for (int t = 0; t < 10; t++) {
|
||||
// look at a large range of cells
|
||||
String position = "A7:DM8934";
|
||||
|
||||
try {
|
||||
xl = new ActiveXComponent("Excel.Application");
|
||||
System.out.println("Excel version=" + xl.getProperty("Version"));
|
||||
|
||||
|
||||
try {
|
||||
xl = new ActiveXComponent("Excel.Application");
|
||||
System.out
|
||||
.println("Excel version=" + xl.getProperty("Version"));
|
||||
|
||||
xl.setProperty("Visible", new Variant(false));
|
||||
workbooks = xl.getProperty("Workbooks").toDispatch();
|
||||
|
||||
workbook = Dispatch.get(workbooks,"Add").toDispatch();
|
||||
|
||||
|
||||
workbook = Dispatch.get(workbooks, "Add").toDispatch();
|
||||
|
||||
workSheets = Dispatch.get(workbook, "Worksheets").toDispatch();
|
||||
|
||||
|
||||
sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
|
||||
// grab the whole range specified above.
|
||||
tabCells = Dispatch.invoke(sheet, "Range", Dispatch.Get,
|
||||
new Object[] { position },
|
||||
new int[1]).toDispatch();
|
||||
|
||||
tabCells = Dispatch.invoke(sheet, "Range", Dispatch.Get,
|
||||
new Object[] { position }, new int[1]).toDispatch();
|
||||
|
||||
sa = Dispatch.get(tabCells, "Value").toSafeArray(true);
|
||||
|
||||
|
||||
System.out.println("Ub0=" + sa.getUBound(1)); // nbCol
|
||||
System.out.println("Ub1=" + sa.getUBound(2)); // nbLgn
|
||||
|
||||
|
||||
// number of rows
|
||||
int nbLgn = sa.getUBound(2);
|
||||
// number of columns
|
||||
int nbCol = sa.getUBound(1);
|
||||
|
||||
|
||||
int[] colLgn = new int[] { 0, 0 };
|
||||
|
||||
|
||||
// now set a value on every cell in the range we retrieved
|
||||
for(int i = 1; i <= nbLgn; i++) {
|
||||
for (int i = 1; i <= nbLgn; i++) {
|
||||
colLgn[1] = i;
|
||||
|
||||
for(int j = 1; j <= nbCol; j++) {
|
||||
|
||||
for (int j = 1; j <= nbCol; j++) {
|
||||
colLgn[0] = j;
|
||||
// this one works with out a leak 1.13-M3
|
||||
//sa.setString(j, i, "test");
|
||||
// sa.setString(j, i, "test");
|
||||
// This one leaks with 1.13-M3 and earlier
|
||||
sa.setString(colLgn, "test");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Dispatch.put(tabCells, "Value", sa);
|
||||
|
||||
|
||||
Variant f = new Variant(false);
|
||||
Dispatch.call(workbook, "Close", f);
|
||||
System.out.println("Close");
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
|
||||
if (sa != null) {
|
||||
try {
|
||||
sa.safeRelease();
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
sa = null;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
|
||||
if (sa != null) {
|
||||
try {
|
||||
sa.safeRelease();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
sa = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (tabCells != null) {
|
||||
try {
|
||||
tabCells.safeRelease();
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
tabCells = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (sheet != null) {
|
||||
try {
|
||||
sheet.safeRelease();
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
sheet = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (workSheets != null) {
|
||||
try {
|
||||
workSheets.safeRelease();
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
workSheets = null;
|
||||
}
|
||||
try {
|
||||
tabCells.safeRelease();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
tabCells = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (sheet != null) {
|
||||
try {
|
||||
sheet.safeRelease();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
sheet = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (workSheets != null) {
|
||||
try {
|
||||
workSheets.safeRelease();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
workSheets = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (workbook != null) {
|
||||
try {
|
||||
workbook.safeRelease();
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
workbook = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (workbooks != null) {
|
||||
try {
|
||||
workbooks.safeRelease();
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
workbooks = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (xl != null) {
|
||||
try {
|
||||
xl.invoke("Quit", new Variant[] {});
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
xl.safeRelease();
|
||||
}
|
||||
catch(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
finally {
|
||||
xl = null;
|
||||
}
|
||||
}
|
||||
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
try {
|
||||
workbook.safeRelease();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
workbook = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (workbooks != null) {
|
||||
try {
|
||||
workbooks.safeRelease();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
workbooks = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (xl != null) {
|
||||
try {
|
||||
xl.invoke("Quit", new Variant[] {});
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
try {
|
||||
xl.safeRelease();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
xl = null;
|
||||
}
|
||||
}
|
||||
|
||||
ComThread.Release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,111 +7,83 @@ import com.jacob.test.BaseTestCase;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* May need to run with some command line options (including from inside Eclipse).
|
||||
* Look in the docs area at the Jacob usage document for command line options.
|
||||
* <p>
|
||||
* SF 1085370
|
||||
In my understatnding, an instance of SafeArray java
|
||||
class has a
|
||||
value of a pointer to VARIANT structure that contains a
|
||||
pointer to
|
||||
a SAFEARRAY strucuture.
|
||||
|
||||
On the other hand, we can create a Variant object from
|
||||
the
|
||||
SafeArray object like this:
|
||||
SafeArray sa = ...;
|
||||
Variant val = new Variant(sa);
|
||||
the val object has a pointer to another VARIANT
|
||||
structure that
|
||||
contains a pointer to the same SAFEARRAY structure.
|
||||
|
||||
In this case, the val object has a pointer to another
|
||||
VARIANT that
|
||||
contains a pointer to the same SAFEARRAY like this:
|
||||
|
||||
+-----------+
|
||||
|SafeArray | +------------+
|
||||
| m_pV--->VARIANT(a) |
|
||||
+-----------+ | VT_ARRAY| +---------+
|
||||
| parray---->SAFEARRAY|
|
||||
+------------+ +^--------+
|
||||
|
|
||||
+-----------+ |
|
||||
|Variant | +------------+ |
|
||||
| m_pVariant--->VARIANT(b) | |
|
||||
+-----------+ | VT_ARRAY| |
|
||||
| parray-----+
|
||||
+------------+
|
||||
|
||||
When previous objects are rereased by
|
||||
ComThread.Release(),
|
||||
first the VARIANT(a) is released by VariantClear()
|
||||
function,
|
||||
and second the VARIANT(b) is released by VariantClear()
|
||||
function too.
|
||||
But the SAFEARRAY was already released by the
|
||||
VARIANT(a).
|
||||
|
||||
So, in my enviroment (WinXP + J2SDK 1.4.1) the
|
||||
following java program
|
||||
is sometimes crash with EXCEPTION_ACCESS_VIOLATION.
|
||||
|
||||
|
||||
To solve this problem, it is nessesary to copy the
|
||||
SAFEARRAY like this:
|
||||
|
||||
+-----------+
|
||||
|Variant | +------------+
|
||||
| m_pVariant--->VARIANT(a) |
|
||||
+-----------+ | VT_ARRAY| +---------+
|
||||
| parray---->SAFEARRAY|
|
||||
+------------+ +|--------+
|
||||
|
|
||||
+-----------+ | copySA()
|
||||
|SafeArray | +------------+ |
|
||||
| m_pV--->VARIANT(b) | V
|
||||
+-----------+ | VT_ARRAY| +---------+
|
||||
| parray---->SAFEARRAY|
|
||||
+------------+ +---------+
|
||||
*
|
||||
* May need to run with some command line options (including from inside
|
||||
* Eclipse). Look in the docs area at the Jacob usage document for command line
|
||||
* options.
|
||||
* <p>
|
||||
* May need to run with some command line options (including from inside Eclipse).
|
||||
* Look in the docs area at the Jacob usage document for command line options.
|
||||
* SF 1085370 In my understatnding, an instance of SafeArray java class has a
|
||||
* value of a pointer to VARIANT structure that contains a pointer to a
|
||||
* SAFEARRAY strucuture.
|
||||
*
|
||||
* On the other hand, we can create a Variant object from the SafeArray object
|
||||
* like this: SafeArray sa = ...; Variant val = new Variant(sa); the val object
|
||||
* has a pointer to another VARIANT structure that contains a pointer to the
|
||||
* same SAFEARRAY structure.
|
||||
*
|
||||
* In this case, the val object has a pointer to another VARIANT that contains a
|
||||
* pointer to the same SAFEARRAY like this:
|
||||
*
|
||||
* +-----------+ |SafeArray | +------------+ | m_pV--->VARIANT(a) |
|
||||
* +-----------+ | VT_ARRAY| +---------+ | parray---->SAFEARRAY| +------------+
|
||||
* +^--------+ | +-----------+ | |Variant | +------------+ | |
|
||||
* m_pVariant--->VARIANT(b) | | +-----------+ | VT_ARRAY| | | parray-----+
|
||||
* +------------+
|
||||
*
|
||||
* When previous objects are rereased by ComThread.Release(), first the
|
||||
* VARIANT(a) is released by VariantClear() function, and second the VARIANT(b)
|
||||
* is released by VariantClear() function too. But the SAFEARRAY was already
|
||||
* released by the VARIANT(a).
|
||||
*
|
||||
* So, in my enviroment (WinXP + J2SDK 1.4.1) the following java program is
|
||||
* sometimes crash with EXCEPTION_ACCESS_VIOLATION.
|
||||
*
|
||||
*
|
||||
* To solve this problem, it is nessesary to copy the SAFEARRAY like this:
|
||||
*
|
||||
* +-----------+ |Variant | +------------+ | m_pVariant--->VARIANT(a) |
|
||||
* +-----------+ | VT_ARRAY| +---------+ | parray---->SAFEARRAY| +------------+
|
||||
* +|--------+ | +-----------+ | copySA() |SafeArray | +------------+ | |
|
||||
* m_pV--->VARIANT(b) | V +-----------+ | VT_ARRAY| +---------+ |
|
||||
* parray---->SAFEARRAY| +------------+ +---------+
|
||||
*
|
||||
* <p>
|
||||
* May need to run with some command line options (including from inside
|
||||
* Eclipse). Look in the docs area at the Jacob usage document for command line
|
||||
* options.
|
||||
*/
|
||||
|
||||
public class SafeArrayReleaseTest extends BaseTestCase {
|
||||
final static int MAX = 300;
|
||||
public void testSaveArrayRelease() {
|
||||
int count;
|
||||
System.out.println("Starting test for max = "+MAX);
|
||||
for(count = 1; count<MAX; count++)
|
||||
{
|
||||
int i = 0;
|
||||
try
|
||||
{
|
||||
ComThread.InitMTA();
|
||||
for (i = 0; i < count; i++)
|
||||
{
|
||||
SafeArray a1 = new SafeArray(Variant.VariantVariant, 2);
|
||||
a1.setVariant(0, new Variant("foo"));
|
||||
a1.setVariant(1, new Variant("bar"));
|
||||
Variant v = new Variant(a1);
|
||||
SafeArray a2 = v.toSafeArray(true);
|
||||
if (a2 == null){
|
||||
System.out.println("got null back from toSafeArray()");
|
||||
}
|
||||
}
|
||||
ComThread.Release();
|
||||
System.gc();
|
||||
//System.out.print(".");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
fail("Test fails with i = " + i + " (max = "+MAX+")");
|
||||
}
|
||||
}
|
||||
System.gc();
|
||||
System.out.println("\nTest ends with count = " + count + " (max = "+MAX+")");
|
||||
}
|
||||
final static int MAX = 300;
|
||||
|
||||
/**
|
||||
* verifies the release works on SafeArray
|
||||
*/
|
||||
public void testSaveArrayRelease() {
|
||||
int count;
|
||||
System.out.println("Starting test for max = " + MAX);
|
||||
for (count = 1; count < MAX; count++) {
|
||||
int i = 0;
|
||||
try {
|
||||
ComThread.InitMTA();
|
||||
for (i = 0; i < count; i++) {
|
||||
SafeArray a1 = new SafeArray(Variant.VariantVariant, 2);
|
||||
a1.setVariant(0, new Variant("foo"));
|
||||
a1.setVariant(1, new Variant("bar"));
|
||||
Variant v = new Variant(a1);
|
||||
SafeArray a2 = v.toSafeArray(true);
|
||||
if (a2 == null) {
|
||||
System.out.println("got null back from toSafeArray()");
|
||||
}
|
||||
}
|
||||
ComThread.Release();
|
||||
System.gc();
|
||||
// System.out.print(".");
|
||||
} catch (Exception e) {
|
||||
fail("Test fails with i = " + i + " (max = " + MAX + ")");
|
||||
}
|
||||
}
|
||||
System.gc();
|
||||
System.out.println("\nTest ends with count = " + count + " (max = "
|
||||
+ MAX + ")");
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,26 @@
|
||||
package com.jacob.test.safearray;
|
||||
|
||||
import com.jacob.com.*;
|
||||
import com.jacob.activeX.ActiveXComponent;
|
||||
import com.jacob.com.Dispatch;
|
||||
import com.jacob.com.SafeArray;
|
||||
import com.jacob.com.Variant;
|
||||
import com.jacob.test.BaseTestCase;
|
||||
import com.jacob.activeX.*;
|
||||
|
||||
/**
|
||||
* This does simple tests with SafeArray using Excel as a source
|
||||
* <p>
|
||||
* May need to run with some command line options (including from inside Eclipse).
|
||||
* Look in the docs area at the Jacob usage document for command line options.
|
||||
* May need to run with some command line options (including from inside
|
||||
* Eclipse). Look in the docs area at the Jacob usage document for command line
|
||||
* options.
|
||||
* <p>
|
||||
* This relies on BaseTestCase to provide the root path to the file under test
|
||||
*/
|
||||
public class SafeArrayViaExcel extends BaseTestCase {
|
||||
|
||||
/**
|
||||
* verify safe arrays work with standard applications, Excel in this case
|
||||
*/
|
||||
public void testSafeArrayViaExcel() {
|
||||
// deprecated
|
||||
// System.runFinalizersOnExit(true);
|
||||
|
||||
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
|
||||
try {
|
||||
@@ -26,8 +31,9 @@ public class SafeArrayViaExcel extends BaseTestCase {
|
||||
Dispatch workbook = Dispatch.call(
|
||||
workbooks,
|
||||
"Open",
|
||||
getWindowsFilePathToPackageResource("SafeArrayViaExcel.xls",this.getClass()))
|
||||
.toDispatch();
|
||||
getWindowsFilePathToPackageResource(
|
||||
"SafeArrayViaExcel.xls", this.getClass()))
|
||||
.toDispatch();
|
||||
System.out.println("Opened File - SafeArrayViaExcel.xls\n");
|
||||
Dispatch sheet = Dispatch.get(workbook, "ActiveSheet").toDispatch();
|
||||
cell = Dispatch.invoke(sheet, "Range", Dispatch.Get,
|
||||
@@ -61,7 +67,7 @@ public class SafeArrayViaExcel extends BaseTestCase {
|
||||
System.out.println("Closed File\n");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
fail("Caught Exception "+e);
|
||||
fail("Caught Exception " + e);
|
||||
} finally {
|
||||
xl.invoke("Quit", new Variant[] {});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user