1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-08 05:37:31 +08:00
osu-lazer/osu.Game.Tests/Database/LegacyExporterTest.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

130 lines
4.8 KiB
C#
Raw Normal View History

2023-02-16 21:26:57 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using NUnit.Framework;
2023-02-17 12:30:00 +08:00
using osu.Framework.Extensions.ObjectExtensions;
2023-02-16 21:26:57 +08:00
using osu.Framework.Platform;
using osu.Framework.Testing;
using osu.Game.Database;
namespace osu.Game.Tests.Database
{
2023-02-17 12:39:31 +08:00
[TestFixture]
2023-02-16 21:26:57 +08:00
public class LegacyExporterTest
{
2023-02-17 12:30:00 +08:00
private TestLegacyExporter legacyExporter = null!;
private TemporaryNativeStorage storage = null!;
2023-02-16 21:26:57 +08:00
2023-02-17 12:39:24 +08:00
private const string long_filename =
"some file with super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name super long name";
2023-02-16 21:26:57 +08:00
[SetUp]
2023-02-17 12:30:00 +08:00
public void SetUp()
2023-02-16 21:26:57 +08:00
{
storage = new TemporaryNativeStorage("export-storage");
legacyExporter = new TestLegacyExporter(storage);
}
[Test]
2023-02-17 06:44:45 +08:00
public void ExportFileWithNormalNameTest()
2023-02-16 21:26:57 +08:00
{
2023-02-17 12:30:00 +08:00
var exportStorage = storage.GetStorageForDirectory(@"exports");
2023-02-16 21:26:57 +08:00
2023-02-16 21:42:07 +08:00
const string filename = "normal file name";
2023-02-16 21:26:57 +08:00
var item = new TestPathInfo(filename);
2023-02-17 12:39:24 +08:00
Assert.That(item.Filename.Length, Is.LessThan(TestLegacyExporter.MAX_FILENAME_LENGTH));
2023-02-17 06:46:15 +08:00
exportItemAndAssert(item, exportStorage, filename);
2023-02-17 06:44:45 +08:00
}
[Test]
public void ExportFileWithNormalNameMultipleTimesTest()
{
2023-02-17 12:30:00 +08:00
var exportStorage = storage.GetStorageForDirectory(@"exports");
2023-02-17 06:44:45 +08:00
const string filename = "normal file name";
var item = new TestPathInfo(filename);
2023-02-17 12:39:24 +08:00
Assert.That(item.Filename.Length < TestLegacyExporter.MAX_FILENAME_LENGTH, Is.True);
2023-02-17 06:44:45 +08:00
//Export multiple times
for (int i = 0; i < 10; i++)
{
2023-02-17 06:50:24 +08:00
string expectedFileName = i == 0 ? filename : $"{filename} ({i})";
2023-02-17 06:46:15 +08:00
exportItemAndAssert(item, exportStorage, expectedFileName);
2023-02-17 06:44:45 +08:00
}
}
[Test]
public void ExportFileWithSuperLongNameTest()
{
2023-02-17 12:30:00 +08:00
var exportStorage = storage.GetStorageForDirectory(@"exports");
2023-02-17 06:44:45 +08:00
2023-02-17 12:39:24 +08:00
int expectedLength = TestLegacyExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
string expectedName = long_filename.Remove(expectedLength);
2023-02-17 06:44:45 +08:00
2023-02-17 12:39:24 +08:00
var item = new TestPathInfo(long_filename);
2023-02-17 06:44:45 +08:00
2023-02-17 12:39:24 +08:00
Assert.That(item.Filename.Length > TestLegacyExporter.MAX_FILENAME_LENGTH, Is.True);
2023-02-17 06:46:15 +08:00
exportItemAndAssert(item, exportStorage, expectedName);
2023-02-16 21:26:57 +08:00
}
[Test]
2023-02-17 06:44:45 +08:00
public void ExportFileWithSuperLongNameMultipleTimesTest()
2023-02-16 21:26:57 +08:00
{
2023-02-17 12:30:00 +08:00
var exportStorage = storage.GetStorageForDirectory(@"exports");
2023-02-16 21:26:57 +08:00
2023-02-17 12:39:24 +08:00
int expectedLength = TestLegacyExporter.MAX_FILENAME_LENGTH - (legacyExporter.GetExtension().Length);
string expectedName = long_filename.Remove(expectedLength);
2023-02-16 21:26:57 +08:00
2023-02-17 12:39:24 +08:00
var item = new TestPathInfo(long_filename);
2023-02-16 21:26:57 +08:00
2023-02-17 12:39:24 +08:00
Assert.That(item.Filename.Length > TestLegacyExporter.MAX_FILENAME_LENGTH, Is.True);
2023-02-17 06:44:45 +08:00
//Export multiple times
for (int i = 0; i < 10; i++)
2023-02-17 06:46:15 +08:00
exportItemAndAssert(item, exportStorage, expectedName);
2023-02-17 06:44:45 +08:00
}
2023-02-17 12:30:00 +08:00
private void exportItemAndAssert(IHasNamedFiles item, Storage exportStorage, string expectedName)
2023-02-17 06:44:45 +08:00
{
2023-02-17 12:30:00 +08:00
Assert.DoesNotThrow(() => legacyExporter.Export(item));
Assert.That(exportStorage.Exists($"{expectedName}{legacyExporter.GetExtension()}"), Is.True);
2023-02-16 21:26:57 +08:00
}
[TearDown]
2023-02-17 12:30:00 +08:00
public void TearDown()
2023-02-16 21:26:57 +08:00
{
2023-02-17 12:30:00 +08:00
if (storage.IsNotNull())
storage.Dispose();
2023-02-16 21:26:57 +08:00
}
private class TestPathInfo : IHasNamedFiles
{
2023-02-17 12:30:00 +08:00
public string Filename { get; }
2023-02-16 21:26:57 +08:00
2023-02-17 12:30:00 +08:00
public IEnumerable<INamedFileUsage> Files { get; } = new List<INamedFileUsage>();
2023-02-16 21:26:57 +08:00
2023-02-17 12:30:00 +08:00
public TestPathInfo(string filename)
{
Filename = filename;
}
2023-02-16 21:26:57 +08:00
2023-02-17 12:30:00 +08:00
public override string ToString() => Filename;
2023-02-16 21:26:57 +08:00
}
private class TestLegacyExporter : LegacyExporter<IHasNamedFiles>
{
2023-02-16 21:42:07 +08:00
public TestLegacyExporter(Storage storage)
: base(storage)
{
}
2023-02-16 21:26:57 +08:00
public string GetExtension() => FileExtension;
protected override string FileExtension => ".ots";
}
}
}