File: ProviderServicesFactoryTests.cs

package info (click to toggle)
mono 3.2.8%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 527,964 kB
  • ctags: 623,327
  • sloc: cs: 3,938,236; xml: 1,891,753; ansic: 418,737; java: 59,920; sh: 15,754; makefile: 11,067; sql: 7,956; perl: 2,279; cpp: 1,380; yacc: 1,203; python: 594; asm: 422; sed: 16; php: 1
file content (187 lines) | stat: -rw-r--r-- 8,061 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity.Config
{
    using System.Data.Common;
    using System.Data.Entity.Core.Common;
    using System.Data.Entity.Core.Common.CommandTrees;
    using System.Data.Entity.Resources;
    using System.Data.Entity.SqlServer;
    using System.Data.Entity.SqlServerCompact;
    using System.Data.Entity.Utilities;
    using System.Data.SqlClient;
    using Xunit;

    public class ProviderServicesFactoryTests
    {
        public class GetInstanceByConvention
        {
            [Fact]
            public void GetInstanceByConvention_returns_SqlProviderServices_type_for_SqlClient_invariant_name()
            {
                Assert.Same(
                    SqlProviderServices.Instance,
                    new ProviderServicesFactory().GetInstanceByConvention("System.Data.SqlClient"));
            }

            [Fact]
            public void GetInstanceByConvention_returns_SqlCeProviderServices_type_for_Sql_Compact_invariant_name()
            {
                Assert.Equal(
                    SqlCeProviderServices.Instance,
                    new ProviderServicesFactory().GetInstanceByConvention("System.Data.SqlServerCe.4.0"));
            }

            [Fact]
            public void GetInstanceByConvention_throws_for_unknown_invariant_name()
            {
                Assert.Equal(
                    Strings.EF6Providers_NoProviderFound("Don't.Come.Around.Here.No.More"),
                    Assert.Throws<InvalidOperationException>(
                        () => new ProviderServicesFactory().GetInstanceByConvention("Don't.Come.Around.Here.No.More")).Message);
            }
        }

        public class GetDbProviderServices
        {
            [Fact]
            public void GetDbProviderServices_uses_public_Instance_property_if_available()
            {
                Assert.Same(
                    FakeProviderWithPublicProperty.Instance,
                    new ProviderServicesFactory().GetInstance(
                        typeof(FakeProviderWithPublicProperty).AssemblyQualifiedName, "Learning.To.Fly"));
            }

            [Fact]
            public void GetDbProviderServices_uses_public_Instance_field_if_available()
            {
                Assert.Same(
                    FakeProviderWithPublicField.Instance,
                    new ProviderServicesFactory().GetInstance(
                        typeof(FakeProviderWithPublicField).AssemblyQualifiedName, "I.Wanna.Hold.Your.Hand"));
            }

            [Fact]
            public void GetDbProviderServices_uses_non_public_Instance_property_if_available()
            {
                Assert.IsType<FakeProviderWithNonPublicProperty>(
                    new ProviderServicesFactory().GetInstance(
                        typeof(FakeProviderWithNonPublicProperty).AssemblyQualifiedName, "Stairway.To.Heaven"));
            }

            [Fact]
            public void GetDbProviderServices_uses_non_public_Instance_field_if_available()
            {
                Assert.IsType<FakeProviderWithNonPublicField>(
                    new ProviderServicesFactory().GetInstance(
                        typeof(FakeProviderWithNonPublicField).AssemblyQualifiedName, "Does.Anybody.Remember.Laughter?"));
            }

            [Fact]
            public void GetDbProviderServices_throws_if_provider_type_cannot_be_loaded()
            {
                Assert.Equal(
                    Strings.EF6Providers_ProviderTypeMissing("Killer.Queen.ProviderServices, Sheer.Heart.Attack", "Killer.Queen"),
                    Assert.Throws<InvalidOperationException>(
                        () => new ProviderServicesFactory().GetInstance(
                            "Killer.Queen.ProviderServices, Sheer.Heart.Attack", "Killer.Queen")).Message);
            }

            [Fact]
            public void GetDbProviderServices_throws_if_there_is_no_Instance_field_or_property()
            {
                Assert.Equal(
                    Strings.EF6Providers_InstanceMissing(typeof(FakeProviderWithNoInstance).AssemblyQualifiedName),
                    Assert.Throws<InvalidOperationException>(
                        () => new ProviderServicesFactory().GetInstance(
                            typeof(FakeProviderWithNoInstance).AssemblyQualifiedName, "One.Headlight")).Message);
            }

            [Fact]
            public void GetDbProviderServices_throws_if_Instance_member_returns_null()
            {
                Assert.Equal(
                    Strings.EF6Providers_NotDbProviderServices(typeof(FakeProviderWithNullInstance).AssemblyQualifiedName),
                    Assert.Throws<InvalidOperationException>(
                        () => new ProviderServicesFactory().GetInstance(
                            typeof(FakeProviderWithNullInstance).AssemblyQualifiedName, "Another.One.Bites.The.Dust")).Message);
            }

            [Fact]
            public void GetDbProviderServices_throws_if_Instance_member_is_not_a_DbProviderServices_instance()
            {
                Assert.Equal(
                    Strings.EF6Providers_NotDbProviderServices(typeof(FakeProviderWithBadInstance).AssemblyQualifiedName),
                    Assert.Throws<InvalidOperationException>(
                        () => new ProviderServicesFactory().GetInstance(
                            typeof(FakeProviderWithBadInstance).AssemblyQualifiedName, "Everlong")).Message);
            }
        }

        public class FakeProviderWithBadInstance : FakeProviderBase
        {
            private static readonly Random Instance = new Random();
        }

        public class FakeProviderWithNullInstance : FakeProviderBase
        {
#pragma warning disable 169 // Used through Reflection
            private static readonly FakeProviderWithNullInstance Instance;
#pragma warning restore 169
        }

        public class FakeProviderWithNoInstance : FakeProviderBase
        {
        }

        public class FakeProviderWithPublicField : FakeProviderBase
        {
            public static readonly FakeProviderWithPublicField Instance = new FakeProviderWithPublicField();
        }

        public class FakeProviderWithPublicProperty : FakeProviderBase
        {
            private static readonly FakeProviderWithPublicProperty Singleton = new FakeProviderWithPublicProperty();

            public static FakeProviderWithPublicProperty Instance
            {
                get { return Singleton; }
            }
        }

        public class FakeProviderWithNonPublicField : FakeProviderBase
        {
            private static readonly FakeProviderWithNonPublicField Instance = new FakeProviderWithNonPublicField();
        }

        public class FakeProviderWithNonPublicProperty : FakeProviderBase
        {
            private static readonly FakeProviderWithNonPublicProperty Singleton = new FakeProviderWithNonPublicProperty();

            private static FakeProviderWithNonPublicProperty Instance
            {
                get { return Singleton; }
            }
        }

        public class FakeProviderBase : DbProviderServices
        {
            protected override DbCommandDefinition CreateDbCommandDefinition(
                DbProviderManifest providerManifest, DbCommandTree commandTree)
            {
                throw new NotImplementedException();
            }

            protected override string GetDbProviderManifestToken(DbConnection connection)
            {
                throw new NotImplementedException();
            }

            protected override DbProviderManifest GetDbProviderManifest(string manifestToken)
            {
                throw new NotImplementedException();
            }
        }
    }
}