-
Notifications
You must be signed in to change notification settings - Fork 547
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unload root element if Part.GetStream updates the underlying value (#…
- Loading branch information
1 parent
f1fecd3
commit 522f9f3
Showing
2 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using DocumentFormat.OpenXml.Spreadsheet; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using Xunit; | ||
|
||
namespace DocumentFormat.OpenXml.Packaging.Tests; | ||
|
||
public class OpenXmlPartTests | ||
{ | ||
[InlineData(FileAccess.Write)] | ||
[InlineData(FileAccess.ReadWrite)] | ||
[Theory] | ||
public void GetStreamWrite(FileAccess access) | ||
{ | ||
// Arrange | ||
const string expected = """<x:sst xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><x:si><x:t>Test</x:t></x:si></x:sst>"""; | ||
var stream = new MemoryStream(); | ||
{ | ||
using var package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook); | ||
var wb = package.AddWorkbookPart(); | ||
|
||
var part = wb.AddNewPart<SharedStringTablePart>(); | ||
|
||
part.SharedStringTable = new SharedStringTable(); | ||
|
||
using var partStream = part.GetStream(FileMode.Create, access); | ||
|
||
var bytes = Encoding.UTF8.GetBytes(expected); | ||
partStream.Write(bytes, 0, bytes.Length); | ||
} | ||
|
||
// Reopen package | ||
stream.Position = 0; | ||
using var spreadsheet = SpreadsheetDocument.Open(stream, isEditable: false); | ||
|
||
// Assert | ||
Assert.Equal(expected, spreadsheet.WorkbookPart.SharedStringTablePart.RootElement.OuterXml); | ||
} | ||
|
||
[InlineData(FileAccess.Write)] | ||
[InlineData(FileAccess.ReadWrite)] | ||
[Theory] | ||
public void GetStreamWriteNoUpdates(FileAccess access) | ||
{ | ||
// Arrange | ||
const string expected = """<x:sst xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main" />"""; | ||
var stream = new MemoryStream(); | ||
{ | ||
using var package = SpreadsheetDocument.Create(stream, SpreadsheetDocumentType.Workbook); | ||
var wb = package.AddWorkbookPart(); | ||
|
||
var part = wb.AddNewPart<SharedStringTablePart>(); | ||
|
||
part.SharedStringTable = new SharedStringTable(); | ||
|
||
package.Save(); | ||
|
||
using var partStream = part.GetStream(FileMode.Open, access); | ||
} | ||
|
||
// Reopen package | ||
stream.Position = 0; | ||
using var spreadsheet = SpreadsheetDocument.Open(stream, isEditable: false); | ||
|
||
// Assert | ||
Assert.Equal(expected, spreadsheet.WorkbookPart.SharedStringTablePart.RootElement.OuterXml); | ||
} | ||
} |