diff --git a/GTFS.IO.Desktop/GTFSDirectorySource.cs b/GTFS.IO.Desktop/GTFSDirectorySource.cs index 4da78bd..40ee050 100644 --- a/GTFS.IO.Desktop/GTFSDirectorySource.cs +++ b/GTFS.IO.Desktop/GTFSDirectorySource.cs @@ -95,9 +95,9 @@ public GTFSDirectorySource(DirectoryInfo directory, char seperator) /// private void BuildSource() { - if(_sourceFiles != null) + if (_sourceFiles != null) { - foreach(var sourceFile in _sourceFiles) + foreach (var sourceFile in _sourceFiles) { sourceFile.Dispose(); } @@ -105,18 +105,14 @@ private void BuildSource() var files = _directory.GetFiles("*.txt"); _sourceFiles = new List(files.Length); - foreach(var file in files) + + foreach (var file in files) { - if(_customSeperator.HasValue) - { // add source file with custom seperator. - _sourceFiles.Add( - new GTFSSourceFileLines(File.ReadLines(file.FullName), file.Name.Substring(0, file.Name.Length - file.Extension.Length), _customSeperator.Value)); - } - else - { // no custom seperator here! - _sourceFiles.Add( - new GTFSSourceFileLines(File.ReadLines(file.FullName), file.Name.Substring(0, file.Name.Length - file.Extension.Length))); - } + var nameWithoutExtension = Path.GetFileNameWithoutExtension(file.Name); + + _sourceFiles.Add(_customSeperator.HasValue + ? new GTFSSourceFileStream(File.OpenRead(file.FullName), nameWithoutExtension, _customSeperator.Value) + : new GTFSSourceFileStream(File.OpenRead(file.FullName), nameWithoutExtension)); } } diff --git a/GTFS.Test/Exceptions/GTFSExceptionsTests.cs b/GTFS.Test/Exceptions/GTFSExceptionsTests.cs new file mode 100644 index 0000000..5ae304c --- /dev/null +++ b/GTFS.Test/Exceptions/GTFSExceptionsTests.cs @@ -0,0 +1,55 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Ben Abelshausen + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System; +using System.Linq; +using GTFS.Exceptions; +using NUnit.Framework; + +namespace GTFS.Test.Exceptions +{ + /// + /// Contains tests for . + /// + [TestFixture] + // ReSharper disable once InconsistentNaming + public class GTFSExceptionsTests + { + /// + /// Tests all exceptions derive from our base. + /// + [Test] + public void AllCustomExceptionsInheritFromOurBase() + { + // get all our exceptions. + var baseException = typeof(GTFSExceptionBase); + var systemException = typeof(Exception); + var customExceptions = baseException.Assembly + .GetTypes() + .Where(x => x != baseException && systemException.IsAssignableFrom(x)) + .ToArray(); + + // test result. + Assert.IsTrue(customExceptions.All(x => baseException.IsAssignableFrom(x))); + } + } +} \ No newline at end of file diff --git a/GTFS.Test/FeedInfoEqualityComparer.cs b/GTFS.Test/FeedInfoEqualityComparer.cs new file mode 100644 index 0000000..6b43afb --- /dev/null +++ b/GTFS.Test/FeedInfoEqualityComparer.cs @@ -0,0 +1,58 @@ +using System.Collections.Generic; +using GTFS.Entities; + +namespace GTFS.Test +{ + /// + /// Compares instances of type for equality. + /// + public sealed class FeedInfoEqualityComparer : IEqualityComparer + { + /// + /// Determines whether the specified objects are equal. + /// + /// + /// true if the specified objects are equal; otherwise, false. + /// + /// The first object of type to compare. + /// The second object of type to compare. + public bool Equals(FeedInfo x, FeedInfo y) + { + if (ReferenceEquals(x, y)) return true; + if (ReferenceEquals(x, null)) return false; + if (ReferenceEquals(y, null)) return false; + if (x.GetType() != y.GetType()) return false; + + return string.Equals(x.PublisherName, y.PublisherName) && + string.Equals(x.PublisherUrl, y.PublisherUrl) && + string.Equals(x.Lang, y.Lang) && + string.Equals(x.StartDate, y.StartDate) && + string.Equals(x.EndDate, y.EndDate) && + Equals(x.Tag, y.Tag) && + string.Equals(x.Version, y.Version); + } + + /// + /// Returns a hash code for the specified object. + /// + /// + /// A hash code for the specified object. + /// + /// The for which a hash code is to be returned. + /// The type of is a reference type and is null. + public int GetHashCode(FeedInfo obj) + { + unchecked + { + var hashCode = (obj.PublisherName != null ? obj.PublisherName.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (obj.PublisherUrl != null ? obj.PublisherUrl.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (obj.Lang != null ? obj.Lang.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (obj.StartDate != null ? obj.StartDate.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (obj.EndDate != null ? obj.EndDate.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (obj.Tag != null ? obj.Tag.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ (obj.Version != null ? obj.Version.GetHashCode() : 0); + return hashCode; + } + } + } +} \ No newline at end of file diff --git a/GTFS.Test/FileNotDisposedTest.cs b/GTFS.Test/FileNotDisposedTest.cs new file mode 100644 index 0000000..36ad358 --- /dev/null +++ b/GTFS.Test/FileNotDisposedTest.cs @@ -0,0 +1,100 @@ +// The MIT License (MIT) + +// Copyright (c) 2014 Ben Abelshausen + +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: + +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. + +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +using System.IO; +using GTFS.Exceptions; +using GTFS.IO; +using NUnit.Framework; + +namespace GTFS.Test +{ + [TestFixture] + internal class FileNotDisposedTest + { + private static DirectoryInfo _assemblyLocation; + + private static DirectoryInfo AssemblyLocation + { + get + { + // ReSharper disable once AssignNullToNotNullAttribute + return _assemblyLocation ?? (_assemblyLocation = new DirectoryInfo(Path.GetDirectoryName(typeof(FileNotDisposedTest).Assembly.Location))); + } + } + + [Test] + public void NotDisposingDirectorySourceKeepsSourceFilesOpen() + { + var directoryInfo = new DirectoryInfo(Path.Combine(AssemblyLocation.FullName, "folder-feed")); + + try + { + var gtfsDirectorySource = new GTFSDirectorySource(directoryInfo); + var reader = new GTFSReader(); + reader.Read(gtfsDirectorySource); + } + catch (GTFSExceptionBase) + { + // ignore our exceptions + } + + var agencyFile = Path.Combine(directoryInfo.FullName, "agency.txt"); + + Assert.Throws( + () => + { + using (File.OpenWrite(agencyFile)) + { + // do nothing + } + }, + "The process cannot access the file '{0}' because it is being used by another process.", + agencyFile); + } + + [Test] + public void DisposingDirectorySourceClosesSourceFiles() + { + var directoryInfo = new DirectoryInfo(Path.Combine(AssemblyLocation.FullName, "folder-feed")); + + try + { + using (var gtfsDirectorySource = new GTFSDirectorySource(directoryInfo)) + { + var reader = new GTFSReader(); + reader.Read(gtfsDirectorySource); + } + } + catch (GTFSExceptionBase) + { + // ignore our exceptions + } + + var agencyFile = Path.Combine(directoryInfo.FullName, "agency.txt"); + + using (File.OpenWrite(agencyFile)) + { + // do nothing + } + } + } +} \ No newline at end of file diff --git a/GTFS.Test/GTFS.Test.csproj b/GTFS.Test/GTFS.Test.csproj index 0dba328..bb30a73 100644 --- a/GTFS.Test/GTFS.Test.csproj +++ b/GTFS.Test/GTFS.Test.csproj @@ -73,8 +73,15 @@ + + + + + + + @@ -123,6 +130,10 @@ {41b2bada-85ab-4967-8511-16eede2301aa} GTFS.DB.SQLite + + {869456E4-50EF-4777-9E77-9A6AC96B406A} + GTFS.IO.Desktop + {39531d68-74cf-4c1c-9271-a2c874495383} GTFS.IO @@ -138,6 +149,61 @@ + + + PreserveNewest + + + + + Always + + + + + Always + + + + + Always + + + + + Always + + + + + Always + + + + + Always + + + + + PreserveNewest + + + + + PreserveNewest + + + + + PreserveNewest + + + + + PreserveNewest + +