Skip to content

Commit

Permalink
poi: Adjust toString() of ExternalNameRecord
Browse files Browse the repository at this point in the history
  • Loading branch information
antony-liu committed Mar 15, 2024
1 parent 5177907 commit d719437
Show file tree
Hide file tree
Showing 8 changed files with 614 additions and 681 deletions.
4 changes: 2 additions & 2 deletions main/HSSF/Record/ExternalNameRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ public override String ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append("[EXTERNALNAME]\n");
sb.Append(" .options = ").Append(field_1_option_flag).Append("\n");
sb.Append(" .options = ").Append(field_1_option_flag).Append("\n");
sb.Append(" .ix = ").Append(field_2_ixals).Append("\n");
sb.Append(" .name = ").Append(field_4_name).Append("\n");
if (field_5_name_definition != null)
Expand All @@ -317,7 +317,7 @@ public override String ToString()
for (int i = 0; i < ptgs.Length; i++)
{
Ptg ptg = ptgs[i];
sb.Append(ptg.ToString()).Append(ptg.RVAType).Append("\n");
sb.Append(" .namedef = ").Append(ptg.ToString()).Append(ptg.RVAType).Append("\n");
}
}
sb.Append("[/EXTERNALNAME]\n");
Expand Down
16 changes: 7 additions & 9 deletions main/HSSF/Util/GUID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,15 @@ public long D4
{
get{
//return _d4;
byte[] buf;

using (MemoryStream ms = new MemoryStream(8))
byte[] result = new byte[sizeof(long) / sizeof(byte)];
long l = _d4;
for (int i = result.Length - 1; i >= 0; i--)
{
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(_d4);
buf = ms.ToArray();
bw.Close();
result[i] = (byte)(l & 0xFF);
l >>= 8;
}
Array.Reverse(buf);
return new LittleEndianByteArrayInputStream(buf).ReadLong();

return LittleEndian.GetLong(result, 0);
}
}

Expand Down
13 changes: 2 additions & 11 deletions testcases/main/HSSF/Record/TestBOFRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,8 @@ public class TestBOFRecord
[Test]
public void TestBOFRecord1()
{
Stream is1 = HSSFTestDataSamples.OpenSampleFileStream("bug_42794.xls");

// This used to throw an error before
try
{
new HSSFWorkbook(is1);
}
catch (IndexOutOfRangeException)
{
throw new AssertionException("Identified bug 42794");
}
// This used to throw an error before - #42794
HSSFTestDataSamples.OpenSampleFileStream("bug_42794.xls").Close();
}
}

Expand Down
69 changes: 7 additions & 62 deletions testcases/main/HSSF/Record/TestRecordFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,8 @@ public void TestMixedContinue()

//Serialize and verify that the Serialized data is1 the same as the original
MemoryStream out1 = new MemoryStream();
for (IEnumerator it = records.GetEnumerator(); it.MoveNext();)
foreach (Record rec in records)
{
Record rec = (Record)it.Current;
byte[] serialdata = rec.Serialize();
out1.Write(serialdata, 0, serialdata.Length);
}
Expand All @@ -228,7 +227,7 @@ public void TestNonZeroPadding_bug46987()
Record[] recs = {
new BOFRecord(),
new WriteAccessRecord(), // need *something* between BOF and EOF
EOFRecord.instance,
EOFRecord.instance,
BOFRecord.CreateSheetBOF(),
EOFRecord.instance,
};
Expand All @@ -250,71 +249,17 @@ public void TestNonZeroPadding_bug46987()


POIFSFileSystem fs = new POIFSFileSystem();
Stream is1;
fs.CreateDocument(new MemoryStream(baos.ToArray()), "dummy");
is1 = fs.Root.CreatePOIFSDocumentReader("dummy");

fs.CreateDocument(new ByteArrayInputStream(baos.ToArray()), "dummy");
InputStream is1 = fs.Root.CreateDocumentInputStream("dummy");

List<Record> outRecs;
try
{
outRecs = RecordFactory.CreateRecords(is1);
}
catch (Exception e)
{
if (e.Message.Equals("Buffer underrun - requested 512 bytes but 192 was available"))
{
throw new AssertionException("Identified bug 46987");
}
throw;
}
List<Record> outRecs = RecordFactory.CreateRecords(is1);
// Buffer underrun - requested 512 bytes but 192 was available
Assert.AreEqual(5, outRecs.Count);
fs.Close();
}
[Test]
//public void TestNonZeroPadding_bug46987()
public void TestNPOIBug6177()
{
//Record[] recs = {
// new BOFRecord(),
// EOFRecord.instance,
// BOFRecord.CreateSheetBOF(),
// EOFRecord.instance,
//};
//MemoryStream baos = new MemoryStream();
//for (int i = 0; i < recs.Length; i++)
//{
// baos.Write(recs[i].Serialize(),0, recs[i].RecordSize);
//}
////simulate the bad padding at the end of the workbook stream in attachment 23483 of bug 46987
//baos.WriteByte(0x00);
//baos.WriteByte(0x11);
//baos.WriteByte(0x00);
//baos.WriteByte(0x02);
//for (int i = 0; i < 192; i++)
//{
// baos.WriteByte(0x00);
//}


//POIFSFileSystem fs = new POIFSFileSystem();
//Stream is1;
//fs.CreateDocument(new MemoryStream(baos.ToArray()), "dummy");
//is1 = fs.Root.CreatePOIFSDocumentReader("dummy");

//List<Record> outRecs;
//try
//{
// outRecs = RecordFactory.CreateRecords(is1);
//}
//catch (Exception e)
//{
// if (e.Message.Equals("Buffer underrun - requested 512 bytes but 192 was available"))
// {
// throw new AssertionException("Identified bug 46987");
// }
// throw e;
//}
//Assert.AreEqual(4, outRecs.Count);
string sampleFileName = "FW 8.6 Table Relationship2.xls";
HSSFTestDataSamples.OpenSampleWorkbook(sampleFileName);
}
Expand Down
35 changes: 15 additions & 20 deletions testcases/main/HSSF/UserModel/TestFormulaEvaluatorDocs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,15 @@ namespace TestCases.HSSF.UserModel
[TestFixture]
public class TestFormulaEvaluatorDocs
{

/**
* http://poi.apache.org/hssf/eval.html#EvaluateAll
*/
[Test]
public void TestEvaluateAll()
{
HSSFWorkbook wb = new HSSFWorkbook();
NPOI.SS.UserModel.ISheet s1 = wb.CreateSheet();
NPOI.SS.UserModel.ISheet s2 = wb.CreateSheet();
ISheet s1 = wb.CreateSheet();
ISheet s2 = wb.CreateSheet();
wb.SetSheetName(0, "S1");
wb.SetSheetName(1, "S2");

Expand Down Expand Up @@ -74,7 +73,7 @@ public void TestEvaluateAll()
// uses EvaluateFormulaCell()
for (int sheetNum = 0; sheetNum < wb.NumberOfSheets; sheetNum++)
{
NPOI.SS.UserModel.ISheet sheet = wb.GetSheetAt(sheetNum);
ISheet sheet = wb.GetSheetAt(sheetNum);
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);

for (IEnumerator rit = sheet.GetRowEnumerator(); rit.MoveNext(); )
Expand All @@ -84,12 +83,12 @@ public void TestEvaluateAll()
for (IEnumerator cit = r.GetEnumerator(); cit.MoveNext(); )
{
ICell c = (ICell)cit.Current;
if (c.CellType == NPOI.SS.UserModel.CellType.Formula)
if (c.CellType == CellType.Formula)
{
evaluator.EvaluateFormulaCell(c);

// For Testing - all should be numeric
Assert.AreEqual(NPOI.SS.UserModel.CellType.Numeric, evaluator.EvaluateFormulaCell(c));
Assert.AreEqual(CellType.Numeric, evaluator.EvaluateFormulaCell(c));
}
}
}
Expand All @@ -98,32 +97,28 @@ public void TestEvaluateAll()
// Check now as expected
Assert.AreEqual(55.7, wb.GetSheetAt(0).GetRow(0).GetCell(2).NumericCellValue, 0);
Assert.AreEqual("SUM(A1:B1)", wb.GetSheetAt(0).GetRow(0).GetCell(2).CellFormula);
Assert.AreEqual(NPOI.SS.UserModel.CellType.Formula, wb.GetSheetAt(0).GetRow(0).GetCell(2).CellType);
Assert.AreEqual(CellType.Formula, wb.GetSheetAt(0).GetRow(0).GetCell(2).CellType);

Assert.AreEqual(-4.6, wb.GetSheetAt(0).GetRow(1).GetCell(2).NumericCellValue, 0);
Assert.AreEqual("SUM(A2:B2)", wb.GetSheetAt(0).GetRow(1).GetCell(2).CellFormula);
Assert.AreEqual(NPOI.SS.UserModel.CellType.Formula, wb.GetSheetAt(0).GetRow(1).GetCell(2).CellType);
Assert.AreEqual(CellType.Formula, wb.GetSheetAt(0).GetRow(1).GetCell(2).CellType);

Assert.AreEqual(22.3, wb.GetSheetAt(1).GetRow(0).GetCell(0).NumericCellValue, 0);
Assert.AreEqual("'S1'!A1", wb.GetSheetAt(1).GetRow(0).GetCell(0).CellFormula);
Assert.AreEqual(NPOI.SS.UserModel.CellType.Formula, wb.GetSheetAt(1).GetRow(0).GetCell(0).CellType);
Assert.AreEqual(CellType.Formula, wb.GetSheetAt(1).GetRow(0).GetCell(0).CellType);


// Now do the alternate call, which zaps the formulas
// uses EvaluateInCell()
for (int sheetNum = 0; sheetNum < wb.NumberOfSheets; sheetNum++)
foreach (ISheet sheet in wb)
{
NPOI.SS.UserModel.ISheet sheet = wb.GetSheetAt(sheetNum);
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);

for (IEnumerator rit = sheet.GetRowEnumerator(); rit.MoveNext(); )
foreach (IRow r in sheet)
{
IRow r = (IRow)rit.Current;

for (IEnumerator cit = r.GetEnumerator(); cit.MoveNext(); )
foreach (ICell c in r)
{
ICell c = (ICell)cit.Current;
if (c.CellType == NPOI.SS.UserModel.CellType.Formula)
if (c.CellType == CellType.Formula)
{
evaluator.EvaluateInCell(c);
}
Expand All @@ -132,13 +127,13 @@ public void TestEvaluateAll()
}

Assert.AreEqual(55.7, wb.GetSheetAt(0).GetRow(0).GetCell(2).NumericCellValue, 0);
Assert.AreEqual(NPOI.SS.UserModel.CellType.Numeric, wb.GetSheetAt(0).GetRow(0).GetCell(2).CellType);
Assert.AreEqual(CellType.Numeric, wb.GetSheetAt(0).GetRow(0).GetCell(2).CellType);

Assert.AreEqual(-4.6, wb.GetSheetAt(0).GetRow(1).GetCell(2).NumericCellValue, 0);
Assert.AreEqual(NPOI.SS.UserModel.CellType.Numeric, wb.GetSheetAt(0).GetRow(1).GetCell(2).CellType);
Assert.AreEqual(CellType.Numeric, wb.GetSheetAt(0).GetRow(1).GetCell(2).CellType);

Assert.AreEqual(22.3, wb.GetSheetAt(1).GetRow(0).GetCell(0).NumericCellValue, 0);
Assert.AreEqual(NPOI.SS.UserModel.CellType.Numeric, wb.GetSheetAt(1).GetRow(0).GetCell(0).CellType);
Assert.AreEqual(CellType.Numeric, wb.GetSheetAt(1).GetRow(0).GetCell(0).CellType);
}
}
}
17 changes: 12 additions & 5 deletions testcases/main/POIFS/FileSystem/TestEmptyDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ public void TestSingleEmptyDocument()

MemoryStream output = new MemoryStream();
fs.WriteFileSystem(output);
byte[] temp = output.ToArray();
Assert.IsNotNull(new POIFSFileSystem(new MemoryStream(temp)));

new POIFSFileSystem(new ByteArrayInputStream(output.ToArray())).Close();
fs.Close();
}

[Test]
Expand All @@ -109,7 +110,8 @@ public void TestSingleEmptyDocumentEvent()

MemoryStream output = new MemoryStream();
fs.WriteFileSystem(output);
Assert.IsNotNull(new POIFSFileSystem(new MemoryStream(output.ToArray())));
new POIFSFileSystem(new ByteArrayInputStream(output.ToArray())).Close();
fs.Close();
}
[Test]
public void TestEmptyDocumentWithFriend()
Expand All @@ -121,7 +123,8 @@ public void TestEmptyDocumentWithFriend()

MemoryStream output = new MemoryStream();
fs.WriteFileSystem(output);
Assert.IsNotNull(new POIFSFileSystem(new MemoryStream(output.ToArray())));
new POIFSFileSystem(new ByteArrayInputStream(output.ToArray())).Close();
fs.Close();
}

[Test]
Expand All @@ -134,7 +137,8 @@ public void TestEmptyDocumentEventWithFriend()

MemoryStream output = new MemoryStream();
fs.WriteFileSystem(output);
Assert.IsNotNull(new POIFSFileSystem(new MemoryStream(output.ToArray())));
new POIFSFileSystem(new ByteArrayInputStream(output.ToArray())).Close();
fs.Close();

}
[Test]
Expand All @@ -147,6 +151,7 @@ public void TestEmptyDocumentBug11744()
fs.CreateDocument(new MemoryStream(TestData), "NotEmpty");
MemoryStream output = new MemoryStream();
fs.WriteFileSystem(output);
fs.Close();

// This line caused the error.
fs = new POIFSFileSystem(new MemoryStream(output.ToArray()));
Expand All @@ -162,6 +167,8 @@ public void TestEmptyDocumentBug11744()
Assert.AreEqual(TestData.Length, entry.Size, "Expected size was wrong");
Assert.IsTrue(
Arrays.Equals(TestData,actualReadbackData), "Expected different data Read from stream");

fs.Close();
}
}
}
Loading

0 comments on commit d719437

Please sign in to comment.