Skip to content

Commit

Permalink
Merge pull request #32 from rorlic/develop
Browse files Browse the repository at this point in the history
Fixed issues [#27.. #31]
  • Loading branch information
xivk committed Feb 17, 2016
2 parents 0f11c8c + 8480192 commit 2370065
Show file tree
Hide file tree
Showing 31 changed files with 955 additions and 50 deletions.
22 changes: 9 additions & 13 deletions GTFS.IO.Desktop/GTFSDirectorySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,24 @@ public GTFSDirectorySource(DirectoryInfo directory, char seperator)
/// <returns></returns>
private void BuildSource()
{
if(_sourceFiles != null)
if (_sourceFiles != null)
{
foreach(var sourceFile in _sourceFiles)
foreach (var sourceFile in _sourceFiles)
{
sourceFile.Dispose();
}
}

var files = _directory.GetFiles("*.txt");
_sourceFiles = new List<IGTFSSourceFile>(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));
}
}

Expand Down
55 changes: 55 additions & 0 deletions GTFS.Test/Exceptions/GTFSExceptionsTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Contains tests for <see cref="GTFSExceptionBase"/>.
/// </summary>
[TestFixture]
// ReSharper disable once InconsistentNaming
public class GTFSExceptionsTests
{
/// <summary>
/// Tests all exceptions derive from our base.
/// </summary>
[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)));
}
}
}
58 changes: 58 additions & 0 deletions GTFS.Test/FeedInfoEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using GTFS.Entities;

namespace GTFS.Test
{
/// <summary>
/// Compares instances of type <see cref="FeedInfo"/> for equality.
/// </summary>
public sealed class FeedInfoEqualityComparer : IEqualityComparer<FeedInfo>
{
/// <summary>
/// Determines whether the specified objects are equal.
/// </summary>
/// <returns>
/// true if the specified objects are equal; otherwise, false.
/// </returns>
/// <param name="x">The first object of type <see cref="FeedInfo"/> to compare.</param>
/// <param name="y">The second object of type <see cref="FeedInfo"/> to compare.</param>
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);
}

/// <summary>
/// Returns a hash code for the specified object.
/// </summary>
/// <returns>
/// A hash code for the specified object.
/// </returns>
/// <param name="obj">The <see cref="T:System.Object"/> for which a hash code is to be returned.</param>
/// <exception cref="T:System.ArgumentNullException">The type of <paramref name="obj"/> is a reference type and <paramref name="obj"/> is null.</exception>
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;
}
}
}
}
100 changes: 100 additions & 0 deletions GTFS.Test/FileNotDisposedTest.cs
Original file line number Diff line number Diff line change
@@ -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<GTFSFeed>();
reader.Read(gtfsDirectorySource);
}
catch (GTFSExceptionBase)
{
// ignore our exceptions
}

var agencyFile = Path.Combine(directoryInfo.FullName, "agency.txt");

Assert.Throws<IOException>(
() =>
{
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<GTFSFeed>();
reader.Read(gtfsDirectorySource);
}
}
catch (GTFSExceptionBase)
{
// ignore our exceptions
}

var agencyFile = Path.Combine(directoryInfo.FullName, "agency.txt");

using (File.OpenWrite(agencyFile))
{
// do nothing
}
}
}
}
66 changes: 66 additions & 0 deletions GTFS.Test/GTFS.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,15 @@
<Compile Include="DB\SQLite\SQLiteGTFSFeedDBTests.cs" />
<Compile Include="DirectorySourceTest.cs" />
<Compile Include="Entities\CalendarTests.cs" />
<Compile Include="Exceptions\GTFSExceptionsTests.cs" />
<Compile Include="FeedInfoEqualityComparer.cs" />
<Compile Include="Filters\GTFSFeedStopsFilterTests.cs" />
<Compile Include="GTFSAssert.cs" />
<Compile Include="GTFSFeedTests.cs" />
<Compile Include="FileNotDisposedTest.cs" />
<Compile Include="ParseFeedTests.cs" />
<Compile Include="ParseAgenciesTests.cs" />
<Compile Include="ParseRoutesTests.cs" />
<Compile Include="ParseEntitiesTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="sample-feed\transfers.txt">
Expand Down Expand Up @@ -123,6 +130,10 @@
<Project>{41b2bada-85ab-4967-8511-16eede2301aa}</Project>
<Name>GTFS.DB.SQLite</Name>
</ProjectReference>
<ProjectReference Include="..\GTFS.IO.Desktop\GTFS.IO.Desktop.csproj">
<Project>{869456E4-50EF-4777-9E77-9A6AC96B406A}</Project>
<Name>GTFS.IO.Desktop</Name>
</ProjectReference>
<ProjectReference Include="..\GTFS.IO\GTFS.IO.csproj">
<Project>{39531d68-74cf-4c1c-9271-a2c874495383}</Project>
<Name>GTFS.IO</Name>
Expand All @@ -138,6 +149,61 @@
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\routes.no_desc.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<None Include="folder-feed\agency.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="folder-feed\routes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="folder-feed\stops.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="folder-feed\stop_times.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="folder-feed\trips.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<None Include="folder-feed\calendar.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\agency_with_id.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\agency_no_id.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\agencies_no_id.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="other-feed\agencies_with_id.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</EmbeddedResource>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading

0 comments on commit 2370065

Please sign in to comment.