// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. namespace Microsoft.DbContextPackage.Utilities { using System; using System.Collections.Generic; using System.Data.Mapping; using System.IO; using System.Linq; using System.Text; using Microsoft.DbContextPackage.Extensions; using Microsoft.DbContextPackage.Resources; using Xunit; public class EdmxUtilityTests { [Fact] public void GetMappingCollection_returns_mapping() { var edmxContents = @" "; StorageMappingItemCollection mappingCollection; using (var edmx = new TempFile(edmxContents)) { mappingCollection = new EdmxUtility(edmx.FileName) .GetMappingCollection(); } Assert.True(mappingCollection.Contains("DatabaseEntities")); } [Fact] public void GetMappingCollection_returns_mapping_for_v2_schema() { var edmxContents = @" "; StorageMappingItemCollection mappingCollection; using (var edmx = new TempFile(edmxContents)) { mappingCollection = new EdmxUtility(edmx.FileName) .GetMappingCollection(); } Assert.True(mappingCollection.Contains("DatabaseEntities")); } [Fact] public void GetMappingCollection_throws_on_schema_errors() { var edmxContents = @" "; using (var edmx = new TempFile(edmxContents)) { var ex = Assert.Throws( () => new EdmxUtility(edmx.FileName).GetMappingCollection()); Assert.Equal( Strings.EdmSchemaError( Path.GetFileName(edmx.FileName), "ConceptualModels"), ex.Message); } } private sealed class TempFile : IDisposable { private readonly string _fileName; private bool _disposed; public TempFile(string contents) { _fileName = Path.GetTempFileName(); File.WriteAllText(_fileName, contents); } ~TempFile() { Dispose(false); } public string FileName { get { HandleDisposed(); return _fileName; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!_disposed) { if (!string.IsNullOrWhiteSpace(_fileName)) { File.Delete(_fileName); } _disposed = true; } } private void HandleDisposed() { if (_disposed) { throw new ObjectDisposedException(null); } } } } }