File: EnumOCMappingTests.cs

package info (click to toggle)
mono-reference-assemblies 3.12.1%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 604,240 kB
  • ctags: 625,505
  • sloc: cs: 3,967,741; xml: 2,793,081; ansic: 418,042; java: 60,435; sh: 14,833; makefile: 11,576; sql: 7,956; perl: 1,467; cpp: 1,446; yacc: 1,203; python: 598; asm: 422; sed: 16; php: 1
file content (910 lines) | stat: -rw-r--r-- 38,560 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Data.Entity.MetadataMapping
{
    using System.Data.Entity.Core;
    using System.Data.Entity.Core.Mapping;
    using System.Data.Entity.Core.Metadata.Edm;
    using System.Data.Entity.Resources;
    using System.Linq;
    using System.Reflection;
    using System.Xml.Linq;
    using Xunit;

    public class EnumOCMappingTests : FunctionalTestBase
    {
        private static XDocument EnumCsdl()
        {
            return XDocument.Load(
                Assembly
                    .GetExecutingAssembly()
                    .GetManifestResourceStream("System.Data.Entity.MetadataMapping.Enum.csdl"));
        }

        #region convention loader (POCO)

        [Fact]
        public void Verify_simple_enum_mapping_POCO()
        {
            Verify_simple_enum_mapping(true);
        }

        [Fact]
        public void Complex_type_with_eumm_property_is_mapped_correctly_POCO()
        {
            Complex_type_with_enum_property_is_mapped_correctly(true);
        }

        [Fact]
        public void Enums_with_members_with_same_values_are_mapped_even_if_order_is_different_POCO()
        {
            Enums_with_members_with_same_values_are_mapped_even_if_order_is_different(true);
        }

        [Fact]
        public void Nullability_of_enum_properties_ignored_for_mapping_POCO()
        {
            Nullability_of_enum_properties_ignored_for_mapping(true);
        }

        [Fact]
        public void Can_map_enum_type_with_no_members_POCO()
        {
            Can_map_enum_type_with_no_members(true);
        }

        [Fact]
        public void Cannot_map_OSpace_enum_type_with_unsupported_underlying_type_POCO()
        {
            var exception =
                Assert.Throws<MetadataException>(
                    () => Cannot_map_OSpace_enum_type_with_unsupported_underlying_type(true));

            Assert.Contains(
                Strings.Validator_OSpace_Convention_MissingOSpaceType("MessageModel.MessageType"),
                exception.Message);

            Assert.Contains(
                Strings.Validator_UnsupportedEnumUnderlyingType("System.UInt32"),
                exception.Message);
        }

        [Fact]
        public void Cannot_map_enum_types_if_names_are_different_POCO()
        {
            Assert.Contains(
                Strings.Validator_OSpace_Convention_MissingOSpaceType("MessageModel.PaymentMethod"),
                Assert.Throws<MetadataException>(
                    () => Cannot_map_enum_types_if_names_are_different(true)).Message);
        }

        [Fact]
        public void Cannot_map_enum_types_if_underlying_types_dont_match_POCO()
        {
            var exception = Assert.Throws<MetadataException>(
                () => Cannot_map_enum_types_if_underlying_types_dont_match(true));

            Assert.Contains(
                Strings.Validator_OSpace_Convention_MissingOSpaceType("MessageModel.MessageType"),
                exception.Message);

            Assert.Contains(
                Strings.Validator_OSpace_Convention_NonMatchingUnderlyingTypes,
                exception.Message);
        }

        [Fact]
        public void Cannot_map_OSpace_enum_type_with_fewer_members_than_CSpace_enum_type_POCO()
        {
            var exception = Assert.Throws<MetadataException>(
                () => Cannot_map_OSpace_enum_type_with_fewer_members_than_CSpace_enum_type(true));

            Assert.Contains(
                Strings.Validator_OSpace_Convention_MissingOSpaceType("MessageModel.MessageType"),
                exception.Message);

            Assert.Contains(
                Strings.Mapping_Enum_OCMapping_MemberMismatch("MessageModel.MessageType", "Ground", 2, "MessageModel.MessageType"),
                exception.Message);
        }

        [Fact]
        public void Cannot_map_OSpace_enum_type_whose_member_name_does_not_match_CSpace_enum_type_member_name_POCO()
        {
            var exception = Assert.Throws<MetadataException>(
                () =>
                Cannot_map_OSpace_enum_type_whose_member_name_does_not_match_CSpace_enum_type_member_name(true));

            Assert.Contains(
                Strings.Validator_OSpace_Convention_MissingOSpaceType("MessageModel.MessageType"),
                exception.Message);

            Assert.Contains(
                Strings.Mapping_Enum_OCMapping_MemberMismatch("MessageModel.MessageType", "Ground", 2, "MessageModel.MessageType"),
                exception.Message);
        }

        [Fact]
        public void Can_map_OSpace_enum_type_that_has_more_members_than_CSPace_enum_type_if_members_match_POCO()
        {
            Can_map_OSpace_enum_type_that_has_more_members_than_CSPace_enum_type_if_members_match(true);
        }

        [Fact]
        public void Can_map_CSpace_enum_type_with_no_enum_members_POCO()
        {
            Can_map_CSpace_enum_type_with_no_enum_members(true);
        }

        [Fact]
        public void Cannot_map_if_OSpace_enum_type_member_value_does_not_match_CSpace_enum_type_member_value_POCO()
        {
            var exception = Assert.Throws<MetadataException>(
                () =>
                Cannot_map_if_OSpace_enum_type_member_value_does_not_match_CSpace_enum_type_member_value(true));

            Assert.Contains(
                Strings.Validator_OSpace_Convention_MissingOSpaceType("MessageModel.MessageType"),
                exception.Message);

            Assert.Contains(
                Strings.Mapping_Enum_OCMapping_MemberMismatch("MessageModel.MessageType", "Ground", 2, "MessageModel.MessageType"),
                exception.Message);
        }

        [Fact]
        public void Verify_OSpace_enum_type_is_not_mapped_to_CSpace_entity_type_with_same_name_POCO()
        {
            Assert.Equal(
                Strings.Mapping_Object_InvalidType("Model.MessageType"),
                Assert.Throws<InvalidOperationException>(
                    () => OSpace_enum_type_and_CSpace_entity_type_have_the_same_name(true)).Message);
        }

        [Fact]
        public void Verify_OSpace_entity_type_is_not_mapped_to_CSpace_enum_type_with_same_name_POCO()
        {
            Assert.Equal(
                Strings.Mapping_Object_InvalidType("Model.MessageType"),
                Assert.Throws<InvalidOperationException>(
                    () => OSpace_entity_type_and_CSpace_enum_type_have_the_same_name(true)).Message);
        }

        // POCO specific cases

        [Fact]
        public void Correct_CSpace_enum_type_is_mapped_if_multiple_OSpace_enum_types_exist_but_only_one_matches()
        {
            var additionalEnumType = XDocument.Parse(
                @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""MessageModel1"">
    <EnumType Name=""MessageType"" IsFlags=""false"" />
</Schema>");

            var workspace =
                CreateMetadataWorkspace(
                    EnumCsdl(),
                    BuildAssembly(true, EnumCsdl(), additionalEnumType),
                    true);

            Assert.Equal(
                "MessageModel.MessageType:MessageModel.MessageType",
                workspace.GetMap("MessageModel.MessageType", DataSpace.OSpace, DataSpace.OCSpace).Identity);

            Assert.Equal(
                "MessageModel.Message:MessageModel.Message",
                workspace.GetMap("MessageModel.Message", DataSpace.OSpace, DataSpace.OCSpace).Identity);
        }

        [Fact]
        public void Mapping_fails_for_multiple_OSpace_enum_types_matching_the_same_CSpace_enum_type_POCO()
        {
            var additionalMatchingEnumType = XDocument.Parse(
                @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""MessageModel1"">
  <EnumType Name=""MessageType"" IsFlags=""false"">
    <Member Name=""Express"" />
    <Member Name=""Priority"" />
    <Member Name=""Ground"" />
    <Member Name=""Air"" />
  </EnumType>
</Schema>");

            var assembly = BuildAssembly(true, EnumCsdl(), additionalMatchingEnumType);
            Assert.Contains(
                Strings.Validator_OSpace_Convention_AmbiguousClrType(
                    "MessageType",
                    "MessageModel.MessageType",
                    "MessageModel1.MessageType"),
                Assert.Throws<MetadataException>(
                    () => CreateMetadataWorkspace(EnumCsdl(), assembly, true)).Message);
        }

        [Fact]
        public void Cannot_create_workspace_if_OSpace_enum_property_does_not_have_getter()
        {
            var oSpaceCsdl = EnumCsdl();
            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Property")
                .Single(p => (string)p.Attribute("Name") == "MessageType")
                .SetAttributeValue("{MappingTestExtension}SuppressGetter", true);

            var assembly = BuildAssembly(true, oSpaceCsdl);

            Assert.Contains(
                Strings.Validator_OSpace_Convention_ScalarPropertyMissginGetterOrSetter(
                    "MessageType",
                    "MessageModel.Message",
                    assembly.FullName),
                Assert.Throws<MetadataException>(
                    () => CreateMetadataWorkspace(EnumCsdl(), assembly, true)).Message);
        }

        [Fact]
        public void Cannot_create_workspace_if_OSpace_enum_property_does_not_have_setter()
        {
            var oSpaceCsdl = EnumCsdl();
            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Property")
                .Single(p => (string)p.Attribute("Name") == "MessageType")
                .SetAttributeValue("{MappingTestExtension}SuppressSetter", true);

            var assembly = BuildAssembly(true, oSpaceCsdl);

            Assert.Contains(
                Strings.Validator_OSpace_Convention_ScalarPropertyMissginGetterOrSetter(
                    "MessageType",
                    "MessageModel.Message",
                    assembly.FullName),
                Assert.Throws<MetadataException>(
                    () => CreateMetadataWorkspace(EnumCsdl(), assembly, true)).Message);
        }

        [Fact]
        public void Can_load_entity_with_property_of_enum_type_from_different_assembly()
        {
            const bool isPOCO = true;

            var enumTypeCsdl = XDocument.Parse(
                @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""EnumModel"">
  <EnumType Name=""Enum"" IsFlags=""false"" />
</Schema>");

            var entityTypeCsdl = XDocument.Parse(
                @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""EnumModel"">
  <EntityContainer Name=""EnumModelContainer"">
    <EntitySet Name=""Entity"" EntityType=""EnumModel.Entity"" />
  </EntityContainer>
  <EntityType Name=""Entity"">
    <Key>
      <PropertyRef Name=""Id"" />
    </Key>
    <Property Name=""Id"" Nullable=""false"" Type=""Int32"" />
    <Property Name=""EnumProperty"" Nullable=""false"" Type=""EnumModel.Enum"" />
  </EntityType>
</Schema>");

            var assemblyWithEnumType = BuildAssembly(isPOCO, enumTypeCsdl);
            var assemblyWithEntityType = BuildAssembly(isPOCO, entityTypeCsdl);

            EdmItemCollection edmItemCollection;
            using (var enumTypeReader = enumTypeCsdl.CreateReader())
            {
                using (var entityTypeReader = entityTypeCsdl.CreateReader())
                {
                    edmItemCollection =
                        new EdmItemCollection(
                            new[] { enumTypeReader, entityTypeReader });
                }
            }

            var objectItemCollection = new ObjectItemCollection();
            objectItemCollection.LoadFromAssembly(assemblyWithEnumType, edmItemCollection);
            objectItemCollection.LoadFromAssembly(assemblyWithEntityType, edmItemCollection);

            var workspace = new MetadataWorkspace(
                () => edmItemCollection,
                () => null,
                () => null,
                () => objectItemCollection);

            Assert.Equal(
                "EnumModel.Entity:EnumModel.Entity",
                workspace.GetMap("EnumModel.Entity", DataSpace.OSpace, DataSpace.OCSpace).Identity);
        }

        #endregion

        #region attribute loader (non-POCO)

        [Fact]
        public void Verify_simple_enum_mapping_non_POCO()
        {
            Verify_simple_enum_mapping(false);
        }

        [Fact]
        public void Complex_type_with_enum_property_is_mapped_correctly_NonPOCO()
        {
            Complex_type_with_enum_property_is_mapped_correctly(false);
        }

        [Fact]
        public void Enums_with_members_with_same_values_are_mapped_even_if_order_is_different_NonPOCO()
        {
            Enums_with_members_with_same_values_are_mapped_even_if_order_is_different(false);
        }

        [Fact]
        public void Nullability_of_enum_properties_ignored_for_mapping_NonPOCO()
        {
            Nullability_of_enum_properties_ignored_for_mapping(false);
        }

        [Fact]
        public void Can_map_enum_type_with_no_members_NonPOCO()
        {
            Can_map_enum_type_with_no_members(false);
        }

        [Fact]
        public void Cannot_map_OSpace_enum_type_with_unsupported_underlying_NonPOCO()
        {
            var exception =
                Assert.Throws<MetadataException>(
                    () => Cannot_map_OSpace_enum_type_with_unsupported_underlying_type(false));

            Assert.Contains(
                Strings.Validator_OSpace_ScalarPropertyNotPrimitive(
                    "TypeOfMessage",
                    "MessageModel.MessageTypeLookUp",
                    "MessageModel.MessageType"),
                exception.Message);

            Assert.Contains(
                Strings.Validator_UnsupportedEnumUnderlyingType("System.UInt32"),
                exception.Message);
        }

        [Fact]
        public void Cannot_map_enum_types_if_names_are_different_NonPOCO()
        {
            Assert.Equal(
                Strings.Mapping_Object_InvalidType("MessageModel.ShippingType"),
                Assert.Throws<InvalidOperationException>(
                    () => Cannot_map_enum_types_if_names_are_different(false)).Message);
        }

        [Fact]
        public void OSpaceEnumUnderlyingTypeDoesNotMatchCSpaceEnumUnderlyingTypeName_NonPOCO()
        {
            Assert.Contains(
                Strings.Mapping_Enum_OCMapping_UnderlyingTypesMismatch(
                    "Int32", "MessageModel.MessageType", "Int64", "MessageModel.MessageType"),
                Assert.Throws<MappingException>(
                    () => Cannot_map_enum_types_if_underlying_types_dont_match(false)).Message);
        }

        [Fact]
        public void Cannot_map_OSpace_enum_type_with_fewer_members_than_CSpace_enum_type_NonPOCO()
        {
            Assert.Equal(
                Strings.Mapping_Enum_OCMapping_MemberMismatch("MessageModel.MessageType", "Ground", 2, "MessageModel.MessageType"),
                Assert.Throws<MappingException>(
                    () => Cannot_map_OSpace_enum_type_with_fewer_members_than_CSpace_enum_type(false)).Message);
        }

        [Fact]
        public void OSpaceEnumTypeMemberNameDoesNotMatchCSpaceEnumTypeMemberName_NonPOCO()
        {
            Assert.Equal(
                Strings.Mapping_Enum_OCMapping_MemberMismatch("MessageModel.MessageType", "Ground", 2, "MessageModel.MessageType"),
                Assert.Throws<MappingException>(
                    () =>
                    Cannot_map_OSpace_enum_type_whose_member_name_does_not_match_CSpace_enum_type_member_name(false))
                      .Message);
        }

        [Fact]
        public void Can_map_OSpace_enum_type_that_has_more_members_than_CSPace_enum_type_if_members_match_NonPOCO()
        {
            Can_map_OSpace_enum_type_that_has_more_members_than_CSPace_enum_type_if_members_match(false);
        }

        [Fact]
        public void Can_map_CSpace_enum_type_with_no_enum_members_NonPOCO()
        {
            Can_map_CSpace_enum_type_with_no_enum_members(false);
        }

        [Fact]
        public void Cannot_map_if_OSpace_enum_type_member_value_does_not_match_CSpace_enum_type_member_value_NonPOCO()
        {
            Assert.Equal(
                Strings.Mapping_Enum_OCMapping_MemberMismatch("MessageModel.MessageType", "Ground", 2, "MessageModel.MessageType"),
                Assert.Throws<MappingException>(
                    () =>
                    Cannot_map_if_OSpace_enum_type_member_value_does_not_match_CSpace_enum_type_member_value(false)).Message);
        }

        [Fact]
        public void Cannot_map_OSpace_enum_type_to_CSpace_entity_type_with_the_same_name_NonPOCO()
        {
            Assert.Equal(
                Strings.Mapping_EnumTypeMappingToNonEnumType("Model.MessageType", "Model.MessageType"),
                Assert.Throws<MappingException>(
                    () => OSpace_enum_type_and_CSpace_entity_type_have_the_same_name(false)).Message);
        }

        [Fact]
        public void Cannot_map_OSpace_entity_type_to_CSpace_enum_type_with_the_same_name_NonPOCO()
        {
            Assert.Equal(
                Strings.Mapping_EnumTypeMappingToNonEnumType("Model.MessageType", "Model.MessageType"),
                Assert.Throws<MappingException>(
                    () => OSpace_entity_type_and_CSpace_enum_type_have_the_same_name(false)).Message);
        }

        // non-POCO specific cases

        [Fact]
        public void EnumTypeVerifiedWhenLoadingEntityWithPropertyOfThisEnumType_NonPOCO()
        {
            var oSpaceCsdl = EnumCsdl();

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Member")
                .Single(m => (string)m.Attribute("Name") == "Ground" && (string)m.Parent.Attribute("Name") == "MessageType")
                .SetAttributeValue("Value", "64");

            var workspace = PrepareModel(oSpaceCsdl, EnumCsdl(), false);

            Assert.Equal(
                Strings.Mapping_Enum_OCMapping_MemberMismatch(
                    "MessageModel.MessageType",
                    "Ground",
                    "2",
                    "MessageModel.MessageType"),
                Assert.Throws<MappingException>(
                    () =>
                    GetMappedType(oSpaceCsdl, EnumCsdl(), "MessageModel.Message", false)).Message);
        }

        [Fact]
        public void Can_use_EdmEnumType_attribute_to_map_OSpace_enum_type_to_CSpace_enum_type_with_different_name_NonPOCO()
        {
            var cSpaceCsdl = EnumCsdl();
            cSpaceCsdl
                .Element("{http://schemas.microsoft.com/ado/2009/11/edm}Schema")
                .SetAttributeValue("Namespace", "MessageModelModified");

            foreach (var attribute in cSpaceCsdl
                .Descendants()
                .Attributes()
                .Where(a => ((string)a).StartsWith("MessageModel.")))
            {
                attribute.SetValue(((string)attribute).Replace("MessageModel.", "MessageModelModified."));
            }

            cSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(e => (string)e.Attribute("Name") == "MessageType")
                .SetAttributeValue("Name", "NewMessageType");

            foreach (var propertyElement in cSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Property")
                .Where(p => (string)p.Attribute("Type") == "MessageModelModified.MessageType"))
            {
                propertyElement.SetAttributeValue("Type", "MessageModelModified.NewMessageType");
            }

            var oSpaceCsdl = EnumCsdl();

            foreach (var typeElement in oSpaceCsdl
                .Element("{http://schemas.microsoft.com/ado/2009/11/edm}Schema")
                .Elements()
                .Where(e => new[] { "EntityType", "ComplexType", "EnumType" }.Contains(e.Name.LocalName)))
            {
                if ((string)typeElement.Attribute("Name") == "MessageType")
                {
                    typeElement.SetAttributeValue("{MappingTestExtension}OSpaceTypeName", "NewMessageType");
                }
                typeElement.SetAttributeValue("{MappingTestExtension}OSpaceTypeNamespace", "MessageModelModified");
            }

            var workspace = PrepareModel(oSpaceCsdl, cSpaceCsdl, false);

            Assert.Equal(
                "MessageModel.Message:MessageModelModified.Message",
                workspace.GetMap("MessageModel.Message", DataSpace.OSpace, DataSpace.OCSpace).Identity);

            Assert.Equal(
                "MessageModel.MessageType:MessageModelModified.NewMessageType",
                workspace.GetMap("MessageModel.MessageType", DataSpace.OSpace, DataSpace.OCSpace).Identity);
        }

        [Fact]
        public void Cannot_use_OSpace_enum_type_as_property_type_if_it_does_not_have_EdmTypeAttribute()
        {
            var oSpaceCsdl = EnumCsdl();

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(p => (string)p.Attribute("Name") == "MessageType")
                .SetAttributeValue("{MappingTestExtension}SuppressEdmTypeAttribute", true);

            var exception =
                Assert.Throws<MetadataException>(() => PrepareModel(oSpaceCsdl, EnumCsdl(), false));

            Assert.Contains(
                Strings.Validator_OSpace_ScalarPropertyNotPrimitive(
                    "MessageType",
                    "MessageModel.Message",
                    "MessageModel.MessageType"),
                exception.Message);

            Assert.Contains(
                Strings.Validator_OSpace_ScalarPropertyNotPrimitive(
                    "TypeOfMessage",
                    "MessageModel.MessageTypeLookUp",
                    "MessageModel.MessageType"),
                exception.Message);
        }

        [Fact]
        public void Cannot_have_2_OSpace_enum_types_mapped_to_single_CSpace_enum_type()
        {
            var additionalMatchingEnumType = EnumCsdl();
            additionalMatchingEnumType
                .Element("{http://schemas.microsoft.com/ado/2009/11/edm}Schema")
                .Elements()
                .Where(e => (string)e.Attribute("Name") != "MessageType")
                .Remove();

            additionalMatchingEnumType
                .Element("{http://schemas.microsoft.com/ado/2009/11/edm}Schema")
                .SetAttributeValue("Namespace", "MessageModelAddendum");

            var enumTypeElement =
                additionalMatchingEnumType.
                    Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                                          .Single();

            enumTypeElement.SetAttributeValue("{MappingTestExtension}OSpaceTypeName", "MessageType");
            enumTypeElement.SetAttributeValue("{MappingTestExtension}OSpaceTypeNamespace", "MessageModel");

            Assert.Equal(
                Strings.Mapping_CannotMapCLRTypeMultipleTimes("MessageModel.MessageType"),
                Assert.Throws<MappingException>(
                    () =>
                    CreateMetadataWorkspace(
                        EnumCsdl(),
                        BuildAssembly(false, EnumCsdl(), additionalMatchingEnumType),
                        false)).Message);
        }

        #endregion

        private static void Verify_simple_enum_mapping(bool isPOCO)
        {
            var workspace = PrepareModel( /* oSpaceCsdl */ EnumCsdl(), /* cSpaceCsdl */ EnumCsdl(), isPOCO);

            Assert.Equal(
                "MessageModel.MessageType:MessageModel.MessageType",
                workspace.GetMap("MessageModel.MessageType", DataSpace.OSpace, DataSpace.OCSpace).Identity);

            Assert.Equal(
                "MessageModel.Message:MessageModel.Message",
                workspace.GetMap("MessageModel.Message", DataSpace.OSpace, DataSpace.OCSpace).Identity);
        }

        private static void Complex_type_with_enum_property_is_mapped_correctly(bool isPOCO)
        {
            var complexType = XElement.Parse(
                @"<ComplexType Name=""Complex"" xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"">
  <Property Name=""MessageType"" Type=""MessageModel.MessageType"" Nullable=""false""/>
</ComplexType>");

            var csdl = EnumCsdl();

            csdl
                .Element("{http://schemas.microsoft.com/ado/2009/11/edm}Schema")
                .Add(complexType);

            csdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EntityType")
                .Single(e => (string)e.Attribute("Name") == "Message")
                .Add(
                    new XElement(
                        "{http://schemas.microsoft.com/ado/2009/11/edm}Property",
                        new XAttribute("Name", "ComplexProperty"),
                        new XAttribute("Type", "MessageModel.Complex"),
                        new XAttribute("Nullable", "false")));

            var workspace = PrepareModel( /* oSpaceCsdl */ csdl, /* cSpaceCsdl */ csdl, isPOCO);

            Assert.Equal(
                "MessageModel.MessageType:MessageModel.MessageType",
                workspace.GetMap("MessageModel.MessageType", DataSpace.OSpace, DataSpace.OCSpace).Identity);

            Assert.Equal(
                "MessageModel.Message:MessageModel.Message",
                workspace.GetMap("MessageModel.Message", DataSpace.OSpace, DataSpace.OCSpace).Identity);

            Assert.Equal(
                "MessageModel.Complex:MessageModel.Complex",
                workspace.GetMap("MessageModel.Complex", DataSpace.OSpace, DataSpace.OCSpace).Identity);
        }

        private static void Enums_with_members_with_same_values_are_mapped_even_if_order_is_different(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();
            var oSpaceEnumType = oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(e => (string)e.Attribute("Name") == "ContentsType");

            oSpaceEnumType.Add(
                new XElement(
                    "{http://schemas.microsoft.com/ado/2009/11/edm}Member",
                    new XAttribute("Name", "LegacyType"),
                    new XAttribute("Value", 0)));

            var cSpaceCsdl = new XDocument(oSpaceCsdl);
            var cSpaceEnumType = cSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(e => (string)e.Attribute("Name") == "ContentsType");

            var sortedMemberElements = cSpaceEnumType.Elements().OrderByDescending(e => (string)e.Attribute("Name")).ToArray();

            cSpaceEnumType.Elements().Remove();
            cSpaceEnumType.Add(sortedMemberElements);

            Assert.Equal(
                "MessageModel.ContentsType:MessageModel.ContentsType",
                GetMappedType(oSpaceCsdl, cSpaceCsdl, "MessageModel.ContentsType", isPOCO).Identity);
        }

        private static void Nullability_of_enum_properties_ignored_for_mapping(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();
            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Property")
                .Single(e => (string)e.Attribute("Name") == "MessageType" && (string)e.Parent.Attribute("Name") == "Message")
                .SetAttributeValue("Nullable", true);

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Property")
                .Single(e => (string)e.Attribute("Name") == "ContentsType")
                .SetAttributeValue("Nullable", false);

            var cSpaceCsdl = EnumCsdl();
            cSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Property")
                .Single(e => (string)e.Attribute("Name") == "MessageType" && (string)e.Parent.Attribute("Name") == "Message")
                .SetAttributeValue("Nullable", false);

            cSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Property")
                .Single(e => (string)e.Attribute("Name") == "ContentsType")
                .SetAttributeValue("Nullable", true);

            Assert.Equal(
                "MessageModel.Message:MessageModel.Message",
                GetMappedType(oSpaceCsdl, cSpaceCsdl, "MessageModel.Message", isPOCO).Identity);
        }

        private static void Can_map_enum_type_with_no_members(bool isPOCO)
        {
            var enumCsdl = EnumCsdl();
            enumCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Member")
                .Remove();

            Assert.Equal(
                "MessageModel.Message:MessageModel.Message",
                GetMappedType(enumCsdl, enumCsdl, "MessageModel.Message", isPOCO).Identity);
        }

        private static void Cannot_map_OSpace_enum_type_with_unsupported_underlying_type(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();
            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(p => (string)p.Attribute("Name") == "MessageType")
                .SetAttributeValue("UnderlyingType", "UInt32");

            PrepareModel(oSpaceCsdl, EnumCsdl(), isPOCO);
        }

        private static void Cannot_map_enum_types_if_names_are_different(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(e => (string)e.Attribute("Name") == "PaymentMethod")
                .SetAttributeValue("Name", "ShippingType");

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Property")
                .Single(p => (string)p.Attribute("Name") == "PaymentMethod")
                .SetAttributeValue("Type", "MessageModel.ShippingType");

            GetMappedType(oSpaceCsdl, EnumCsdl(), "MessageModel.ShippingType", isPOCO);
        }

        private static void Cannot_map_enum_types_if_underlying_types_dont_match(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(e => (string)e.Attribute("Name") == "MessageType")
                .SetAttributeValue("UnderlyingType", "Int64");

            GetMappedType(oSpaceCsdl, EnumCsdl(), "MessageModel.MessageType", isPOCO);
        }

        private static void Cannot_map_OSpace_enum_type_with_fewer_members_than_CSpace_enum_type(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Member")
                .Where(m => (string)m.Attribute("Name") == "Ground" && (string)m.Parent.Attribute("Name") == "MessageType")
                .Remove();

            GetMappedType(oSpaceCsdl, EnumCsdl(), "MessageModel.MessageType", isPOCO);
        }

        private static void Cannot_map_OSpace_enum_type_whose_member_name_does_not_match_CSpace_enum_type_member_name(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Member")
                .Single(m => (string)m.Attribute("Name") == "Ground" && (string)m.Parent.Attribute("Name") == "MessageType")
                .SetAttributeValue("Name", "Ground1");

            GetMappedType(oSpaceCsdl, EnumCsdl(), "MessageModel.MessageType", isPOCO);
        }

        private static void Can_map_OSpace_enum_type_that_has_more_members_than_CSPace_enum_type_if_members_match(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();

            var enumType = oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(e => (string)e.Attribute("Name") == "MessageType");

            enumType.Add(
                new XElement(
                    "{http://schemas.microsoft.com/ado/2009/11/edm}Member",
                    new XAttribute("Name", "LegacyType"),
                    new XAttribute("Value", 0)));

            Assert.Equal(
                "MessageModel.Message:MessageModel.Message",
                GetMappedType(oSpaceCsdl, EnumCsdl(), "MessageModel.Message", isPOCO).Identity);
        }

        private static void Can_map_CSpace_enum_type_with_no_enum_members(bool isPOCO)
        {
            var cSpaceCsdl = EnumCsdl();

            cSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}EnumType")
                .Single(e => (string)e.Attribute("Name") == "MessageType")
                .Elements()
                .Remove();

            Assert.Equal(
                "MessageModel.Message:MessageModel.Message",
                GetMappedType(EnumCsdl(), cSpaceCsdl, "MessageModel.Message", isPOCO).Identity);
        }

        private static void Cannot_map_if_OSpace_enum_type_member_value_does_not_match_CSpace_enum_type_member_value(bool isPOCO)
        {
            var oSpaceCsdl = EnumCsdl();

            oSpaceCsdl
                .Descendants("{http://schemas.microsoft.com/ado/2009/11/edm}Member")
                .Single(m => (string)m.Attribute("Name") == "Ground" && (string)m.Parent.Attribute("Name") == "MessageType")
                .SetAttributeValue("Value", "64");

            GetMappedType(oSpaceCsdl, EnumCsdl(), "MessageModel.MessageType", isPOCO);
        }

        private static void OSpace_enum_type_and_CSpace_entity_type_have_the_same_name(bool isPOCO)
        {
            var oSpaceCsdl = XDocument.Parse(
                @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""Model"">
  <EnumType Name=""MessageType"" IsFlags=""false"" />
</Schema>");

            var cSpaceCsdl = XDocument.Parse(
                @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""Model"">
  <EntityType Name=""MessageType"">
    <Key>
      <PropertyRef Name=""Id"" />
    </Key>
    <Property Name=""Id"" Nullable=""false"" Type=""Int32"" />
  </EntityType>
</Schema>");

            GetMappedType(oSpaceCsdl, cSpaceCsdl, "Model.MessageType", isPOCO);
        }

        private static void OSpace_entity_type_and_CSpace_enum_type_have_the_same_name(bool isPOCO)
        {
            var oSpaceCsdl = XDocument.Parse(
                @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""Model"">
  <EntityType Name=""MessageType"">
    <Key>
      <PropertyRef Name=""Id"" />
    </Key>
    <Property Name=""Id"" Nullable=""false"" Type=""Int32"" />
  </EntityType>
</Schema>");

            var cSpaceCsdl = XDocument.Parse(
                @"<Schema xmlns=""http://schemas.microsoft.com/ado/2009/11/edm"" Namespace=""Model"">
  <EnumType Name=""MessageType"" IsFlags=""false"" />
</Schema>");

            GetMappedType(oSpaceCsdl, cSpaceCsdl, "Model.MessageType", isPOCO);
        }

        private static Map GetMappedType(XDocument oSpaceCsdl, XDocument cSpaceCsdl, string oSpaceTypeName, bool isPOCO)
        {
            var workspace = PrepareModel(oSpaceCsdl, cSpaceCsdl, isPOCO);
            return workspace.GetMap(oSpaceTypeName, DataSpace.OSpace, DataSpace.OCSpace);
        }

        private static Assembly BuildAssembly(bool isPOCO, params XDocument[] oSpaceCsdl)
        {
            return
                new CsdlToClrAssemblyConverter(isPOCO, oSpaceCsdl)
                    .BuildAssembly(Guid.NewGuid().ToString());
        }

        private static MetadataWorkspace PrepareModel(XDocument oSpaceCsdl, XDocument cSpaceCsdl, bool isPOCO)
        {
            return CreateMetadataWorkspace(
                cSpaceCsdl,
                BuildAssembly(isPOCO, oSpaceCsdl),
                isPOCO);
        }

        private static MetadataWorkspace CreateMetadataWorkspace(XDocument cSpaceCsdl, Assembly assembly, bool isPOCO)
        {

            EdmItemCollection edmItemCollection;
            using (var csdlReader = cSpaceCsdl.CreateReader())
            {
                edmItemCollection = new EdmItemCollection(new[] { csdlReader });
            }

            // assembly can actually be an AssemblyBuilder. The following line ensures that we are 
            // using the actual assembly otherwise an Assert in ObjectItemAttributeAssemblyLoader.LoadType
            // will fire.
            assembly = assembly.GetTypes().First().Assembly;
            var objectItemCollection = new ObjectItemCollection();

            if (isPOCO)
            {
                objectItemCollection.LoadFromAssembly(assembly, edmItemCollection);
            }
            else
            {
                objectItemCollection.LoadFromAssembly(assembly);
            }

            var workspace = new MetadataWorkspace(
                () => edmItemCollection,
                () => null,
                () => null,
                () => objectItemCollection);

            return workspace;
        }
    }
}