Exemplo n.º 1
0
        public void GetRenamedTrackFiles_GivenMultipleArtistsAndAlbums_ShouldReturnInOrderByArtistAndAlbum()
        {
            // Arrange.
            var artist1Album1Track1 = Substitute.For <ITrackFile>();
            var artist1Album1Track2 = Substitute.For <ITrackFile>();
            var artist1Album2Track1 = Substitute.For <ITrackFile>();
            var artist1Album2Track2 = Substitute.For <ITrackFile>();
            var artist2Album1Track1 = Substitute.For <ITrackFile>();
            var artist2Album1Track2 = Substitute.For <ITrackFile>();

            artist1Album1Track1.Artist.Returns("Artist1");
            artist1Album1Track2.Artist.Returns("Artist1");
            artist1Album2Track1.Artist.Returns("Artist1");
            artist1Album2Track2.Artist.Returns("Artist1");
            artist2Album1Track1.Artist.Returns("Artist2");
            artist2Album1Track2.Artist.Returns("Artist2");

            artist1Album1Track1.Album.Returns("1");
            artist1Album1Track2.Album.Returns("1");
            artist1Album2Track1.Album.Returns("2");
            artist1Album2Track2.Album.Returns("2");
            artist2Album1Track1.Album.Returns("3");
            artist2Album1Track2.Album.Returns("3");

            var trackFiles = new List <ITrackFile>
            {
                artist2Album1Track2,
                artist2Album1Track1,
                artist1Album2Track2,
                artist1Album2Track1,
                artist1Album1Track2,
                artist1Album1Track1
            };

            // Act.
            var renamedFiles = TrackFileRenamer.GetRenamedTrackFiles(
                trackFiles,
                99,
                99);

            // Assert.
            Assert.AreEqual("Artist1_1_01", renamedFiles.ElementAt(0).NewFilename);
            Assert.AreEqual("Artist1_1_02", renamedFiles.ElementAt(1).NewFilename);
            Assert.AreEqual("Artist1_2_01", renamedFiles.ElementAt(2).NewFilename);
            Assert.AreEqual("Artist1_2_02", renamedFiles.ElementAt(3).NewFilename);
            Assert.AreEqual("Artist2_3_01", renamedFiles.ElementAt(4).NewFilename);
            Assert.AreEqual("Artist2_3_02", renamedFiles.ElementAt(5).NewFilename);
        }
Exemplo n.º 2
0
        public void GetRenamedTrackFiles_GivenLongArtistAndAlbumNames_ShouldTruncate()
        {
            // Arrange.
            var artist1Album1Track1 = Substitute.For <ITrackFile>();
            var artist1Album1Track2 = Substitute.For <ITrackFile>();
            var artist1Album2Track1 = Substitute.For <ITrackFile>();
            var artist1Album2Track2 = Substitute.For <ITrackFile>();
            var artist2Album1Track1 = Substitute.For <ITrackFile>();
            var artist2Album1Track2 = Substitute.For <ITrackFile>();

            artist1Album1Track1.Artist.Returns("Some Artist1");
            artist1Album1Track2.Artist.Returns("Some Artist1");
            artist1Album2Track1.Artist.Returns("Some Artist1");
            artist1Album2Track2.Artist.Returns("Some Artist1");
            artist2Album1Track1.Artist.Returns("Another Artist2");
            artist2Album1Track2.Artist.Returns("Another Artist2");

            artist1Album1Track1.Album.Returns("Some Album");
            artist1Album1Track2.Album.Returns("Some Album");
            artist1Album2Track1.Album.Returns("Another Album");
            artist1Album2Track2.Album.Returns("Another Album");
            artist2Album1Track1.Album.Returns("Random Album 3");
            artist2Album1Track2.Album.Returns("Random Album 3");

            var trackFiles = new List <ITrackFile>
            {
                artist2Album1Track2,
                artist2Album1Track1,
                artist1Album2Track2,
                artist1Album2Track1,
                artist1Album1Track2,
                artist1Album1Track1
            };

            // Act.
            var renamedFiles = TrackFileRenamer.GetRenamedTrackFiles(trackFiles);

            // Assert.
            Assert.AreEqual("Anothe_Random_01", renamedFiles.ElementAt(0).NewFilename);
            Assert.AreEqual("Anothe_Random_02", renamedFiles.ElementAt(1).NewFilename);
            Assert.AreEqual("SomeAr_Anothe_01", renamedFiles.ElementAt(2).NewFilename);
            Assert.AreEqual("SomeAr_Anothe_02", renamedFiles.ElementAt(3).NewFilename);
            Assert.AreEqual("SomeAr_SomeAl_01", renamedFiles.ElementAt(4).NewFilename);
            Assert.AreEqual("SomeAr_SomeAl_02", renamedFiles.ElementAt(5).NewFilename);
        }
Exemplo n.º 3
0
        public void GetRenamedTrackFiles_GivenOneTrackFile_ShouldReturnRenamedTrackFile()
        {
            // Arrange.
            var trackFile = Substitute.For <ITrackFile>();

            trackFile.Artist.Returns("Some Artist");
            trackFile.Album.Returns("Some Album");
            trackFile.TrackNumber.Returns(1);

            // Act.
            var renamedFiles = TrackFileRenamer.GetRenamedTrackFiles(
                new List <ITrackFile> {
                trackFile
            },
                99,
                99);

            // Assert.
            Assert.AreEqual(1, renamedFiles.Count());
            Assert.AreEqual("SomeArtist_SomeAlbum_01", renamedFiles.First().NewFilename);
        }
Exemplo n.º 4
0
        public void GetRenamedTrackFiles_GivenMultipleArtists_ShouldReturnInOrder()
        {
            // Arrange.
            var trackFile1 = Substitute.For <ITrackFile>();

            trackFile1.Artist.Returns("Some Artist");
            trackFile1.TrackNumber.Returns(1);

            var trackFiles = new List <ITrackFile>
            {
                Substitute.For <ITrackFile>(),
                Substitute.For <ITrackFile>(),
                Substitute.For <ITrackFile>(),
                Substitute.For <ITrackFile>()
            };

            trackFiles[0].Artist.Returns("Some Artist 2");
            trackFiles[1].Artist.Returns("Some Artist 1");
            trackFiles[2].Artist.Returns("Some Artist 1");
            trackFiles[3].Artist.Returns("Some Artist 2");

            trackFiles[0].Album.Returns("Some Album");
            trackFiles[1].Album.Returns("Some Album");
            trackFiles[2].Album.Returns("Some Album");
            trackFiles[3].Album.Returns("Some Album");

            // Act.
            var renamedFiles = TrackFileRenamer.GetRenamedTrackFiles(
                trackFiles,
                99,
                99);

            // Assert.
            Assert.AreEqual("SomeArtist1_SomeAlbum_01", renamedFiles.ElementAt(0).NewFilename);
            Assert.AreEqual("SomeArtist1_SomeAlbum_02", renamedFiles.ElementAt(1).NewFilename);
            Assert.AreEqual("SomeArtist2_SomeAlbum_01", renamedFiles.ElementAt(2).NewFilename);
            Assert.AreEqual("SomeArtist2_SomeAlbum_02", renamedFiles.ElementAt(3).NewFilename);
        }
Exemplo n.º 5
0
        //---------------------------------------------------------------------------------------------

        private void uiScanMusicFolder_Click(object sender, EventArgs e)
        {
            var files          = Directory.GetFiles(uiMusicFolder.Text, "*.mp3", SearchOption.AllDirectories);
            var tracks         = new List <ITrackFile>();
            var fileSystem     = new FileSystem();
            var writeToConsole = new WriteToConsoleDelegate(WriteToConsole);

#if true
            var filesToRename = new ConcurrentStack <string>(files);

            BackgroundWorker[] backgroundWorkers = new BackgroundWorker[8];
            for (var i = 0; i < backgroundWorkers.Length; i++)
            {
                backgroundWorkers[i] = new BackgroundWorker
                {
                    WorkerSupportsCancellation = true
                };
            }

            var tracksListLock = new object();

            while (!filesToRename.IsEmpty)
            {
                for (var i = 0; i < backgroundWorkers.Length; i++)
                {
                    if (backgroundWorkers[i].IsBusy && !backgroundWorkers[i].CancellationPending)
                    {
                        continue;
                    }

                    backgroundWorkers[i] = new BackgroundWorker
                    {
                        WorkerSupportsCancellation = true
                    };

                    var worker = backgroundWorkers[i];

                    worker.DoWork += (sender1, e1) =>
                    {
                        var filename = string.Empty;

                        try
                        {
                            if (!filesToRename.TryPop(out filename))
                            {
                                return;
                            }

                            var filePropertyRetriever = new FileExtendedPropertyRetriever(filename);

                            lock (tracksListLock)
                            {
                                tracks.Add(new TrackFile(filename, fileSystem, filePropertyRetriever));
                            }
                        }
                        catch (Exception ex)
                        {
                            writeToConsole.Invoke($"{filename} : {ex.Message}");
                            Console.WriteLine(ex);
                        }
                    };

                    worker.RunWorkerAsync();
                }

                Application.DoEvents();
            }

            while (backgroundWorkers.Any(bw => bw.IsBusy))
            {
                Thread.Sleep(1000);
                Application.DoEvents();
            }
#else
            foreach (var filename in files)
            {
                var filePropertyRetriever = new FileExtendedPropertyRetriever(filename);

                tracks.Add(new TrackFile(filename, fileSystem, filePropertyRetriever));
            }
#endif
            var renamedTracks = TrackFileRenamer.GetRenamedTrackFiles(tracks);

            uiFiles.Items.Clear();

            foreach (var track in renamedTracks)
            {
                uiFiles.Items.Add(track);
            }

            writeToConsole.Invoke("Scanning complete.");
        }