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 ;
using osu.Framework.Platform ;
using osu.Framework.Testing ;
using osu.Game.Database ;
namespace osu.Game.Tests.Database
{
public class LegacyExporterTest
{
private TestLegacyExporter ? legacyExporter ;
private TemporaryNativeStorage ? storage ;
[SetUp]
public void SetupLegacyExporter ( )
{
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
{
var exportStorage = storage ? . GetStorageForDirectory ( @"exports" ) ;
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 ) ;
Assert . That ( item . FileName . Length < TestLegacyExporter . GetMaxPath ( ) , Is . True ) ;
2023-02-17 06:46:15 +08:00
exportItemAndAssert ( item , exportStorage , filename ) ;
2023-02-17 06:44:45 +08:00
}
[Test]
public void ExportFileWithNormalNameMultipleTimesTest ( )
{
var exportStorage = storage ? . GetStorageForDirectory ( @"exports" ) ;
const string filename = "normal file name" ;
var item = new TestPathInfo ( filename ) ;
Assert . That ( item . FileName . Length < TestLegacyExporter . GetMaxPath ( ) , Is . True ) ;
//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 ( )
{
var exportStorage = storage ? . GetStorageForDirectory ( @"exports" ) ;
const string fullname = "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" ;
int expectedLength = TestLegacyExporter . GetMaxPath ( ) - ( legacyExporter ? . GetExtension ( ) . Length ? ? 0 ) ;
string expectedName = fullname . Remove ( expectedLength ) ;
var item = new TestPathInfo ( fullname ) ;
Assert . That ( item . FileName . Length > TestLegacyExporter . GetMaxPath ( ) , 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
{
var exportStorage = storage ? . GetStorageForDirectory ( @"exports" ) ;
2023-02-16 21:42:07 +08:00
const string fullname = "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
2023-02-17 06:44:45 +08:00
int expectedLength = TestLegacyExporter . GetMaxPath ( ) - ( legacyExporter ? . GetExtension ( ) . Length ? ? 0 ) ;
string expectedName = fullname . Remove ( expectedLength ) ;
2023-02-16 21:26:57 +08:00
var item = new TestPathInfo ( fullname ) ;
Assert . That ( item . FileName . Length > TestLegacyExporter . GetMaxPath ( ) , 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 06:46:15 +08:00
private void exportItemAndAssert ( IHasNamedFiles item , Storage ? exportStorage , string expectedName )
2023-02-17 06:44:45 +08:00
{
2023-02-16 21:26:57 +08:00
Assert . DoesNotThrow ( ( ) = > legacyExporter ? . Export ( item ) ) ;
Assert . That ( exportStorage ? . Exists ( $"{expectedName}{legacyExporter?.GetExtension()}" ) , Is . True ) ;
}
[TearDown]
public void CleanupAfterTest ( )
{
storage ? . Dispose ( ) ;
}
private class TestPathInfo : IHasNamedFiles
{
2023-02-16 21:42:07 +08:00
public string FileName { get ; set ; }
2023-02-16 21:26:57 +08:00
public TestPathInfo ( string fileName ) = > FileName = fileName ;
public IEnumerable < INamedFileUsage > Files { get ; set ; } = new List < INamedFileUsage > ( ) ;
public override string ToString ( ) = > FileName ;
}
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 static int GetMaxPath ( ) = > MAX_PATH ;
public string GetExtension ( ) = > FileExtension ;
protected override string FileExtension = > ".ots" ;
}
}
}