Datei: UnitTestGenerator/TestGenerator/MappingGenerator.cs

Last Commit (0bec001)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Diagnostics;
25 using System.Collections;
26 using System.IO;
27 using NDO.Mapping;
28 using System.Linq;
29 using NDO.ProviderFactory;
30
31 namespace TestGenerator
32 {
33 ····/// <summary>
34 ····/// Summary for ClassGenerator.
35 ····/// </summary>
36 ····public class MappingGenerator
37 ····{
38 ········ArrayList relInfos;
39 ········string fileName;
40 ········NDO.Mapping.NDOMapping mapping;
41
42 ········public MappingGenerator(ArrayList relInfos)
43 ········{
44 ············this.relInfos = relInfos;
45 fileName = Path. Combine( AppDomain. CurrentDomain. BaseDirectory, @". . \. . \. . \PersistentClasses\NDOMapping. xml") ;
46 ········}
47
48 ········void CheckRelation(Test test, RelInfo ri)
49 ········{
50 ············if (!ri.HasTable)
51 ················return;
52 ············Class clMapping = mapping.FindClass("RelationTestClasses." + test.OwnClass.Name);
53 ············
54 ············if (clMapping == null)
55 ················return;
56 ············NDO.Mapping.Relation rel = (NDO.Mapping.Relation) clMapping.Relations.FirstOrDefault();
57 ············Debug.Assert(rel != null, "Relation mapping not found");
58 ············if (rel == null)
59 ················throw new Exception("Cant find relation 0 of" + test.OwnClass.Name);
60
61 ············Class derivedMapping;
62 ············NDO.Mapping.Relation derivedRel = null;
63
64 ············if (ri.OwnPoly)
65 ············{
66 ················derivedMapping = mapping.FindClass("RelationTestClasses." + test.OwnDerivedClass.Name);
67 ················if (derivedMapping == null)
68 ····················return;
69 ················derivedRel = derivedMapping.Relations.FirstOrDefault();
70 ················if (rel == null)
71 ····················throw new Exception("Cant find relation 0 of" + test.OwnDerivedClass.Name);
72 ············}
73
74 ············if (rel.MappingTable == null || ri.OwnPoly && derivedRel.MappingTable == null)
75 ············{
76 ················string tableName = null;
77 ················if (test.OwnClass.Name.CompareTo(test.OtherClass.Name) < 0)
78 ····················tableName = "rel" + test.OwnClass.Name + test.OtherClass.Name;
79 ················else
80 ····················tableName = "rel" + test.OtherClass.Name + test.OwnClass.Name;
81 ················ForeignKeyColumn fkColumn = rel.NewForeignKeyColumn();
82 ················fkColumn.Name = "ID" + test.OwnClass.Name;
83 ················if (ri.OwnPoly)
84 ····················rel.ForeignKeyTypeColumnName = "TC" + test.OwnClass.Name;
85 ················else
86 ····················rel.ForeignKeyTypeColumnName = null;
87 ················rel.MappingTable = new MappingTable(rel);
88 ················rel.MappingTable.TableName = tableName;
89 ················fkColumn = rel.MappingTable.NewForeignKeyColumn();
90 ················fkColumn.Name = "ID" + test.OtherClass.Name;
91 ················if (ri.OtherPoly)
92 ····················rel.MappingTable.ChildForeignKeyTypeColumnName = "TC" + test.OtherClass.Name;
93 ················else
94 ····················rel.MappingTable.ChildForeignKeyTypeColumnName = null;
95 ················rel.MappingTable.ConnectionId = clMapping.ConnectionId;
96
97 ················if (ri.OwnPoly)
98 ················{
99 ····················ForeignKeyColumn dfkColumn = (ForeignKeyColumn) derivedRel.ForeignKeyColumns.FirstOrDefault();
100 ····················dfkColumn.Name = ((ForeignKeyColumn) rel.ForeignKeyColumns.FirstOrDefault()).Name;
101 ····················derivedRel.ForeignKeyTypeColumnName = rel.ForeignKeyTypeColumnName;
102 ····················derivedRel.MappingTable = rel.MappingTable;
103 ················}
104 ············}
105 ········}
106
107
108 ········public void Generate()
109 ········{
110 ············this.mapping = new NDOMapping(fileName, NDOProviderFactory.Instance);
111 ············foreach(RelInfo ri in relInfos)
112 ············{
113 ················Test test = new Test(ri, "RelationTestClasses");
114 ················CheckRelation(test, ri);
115 ················if (ri.IsBi)
116 ················{
117 ····················RelInfo reverseInfo = ri.GetReverse();
118 ····················Test reverseTest = test.GetReverse();
119 ····················CheckRelation(reverseTest, reverseInfo);
120 ················}
121 ············}
122 ············this.mapping.Save();
123 ········}
124 ····}
125 }
New Commit (6d63e12)
1 //
2 // Copyright (c) 2002-2016 Mirko Matytschak
3 // (www.netdataobjects.de)
4 //
5 // Author: Mirko Matytschak
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
8 // documentation files (the "Software"), to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
10 // Software, and to permit persons to whom the Software is furnished to do so, subject to the following
11 // conditions:
12
13 // The above copyright notice and this permission notice shall be included in all copies or substantial portions
14 // of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
17 // TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
19 // CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21
22
23 using System;
24 using System.Diagnostics;
25 using System.Collections;
26 using System.IO;
27 using NDO.Mapping;
28 using System.Linq;
29 using NDO.ProviderFactory;
30
31 namespace TestGenerator
32 {
33 ····/// <summary>
34 ····/// Summary for ClassGenerator.
35 ····/// </summary>
36 ····public class MappingGenerator
37 ····{
38 ········ArrayList relInfos;
39 ········string fileName;
40 ········NDO.Mapping.NDOMapping mapping;
41
42 ········public MappingGenerator(ArrayList relInfos)
43 ········{
44 ············this.relInfos = relInfos;
45 fileName = Path. Combine( AppDomain. CurrentDomain. BaseDirectory, @". . \. . \. . \. . \PersistentClasses\NDOMapping. xml") ;
46 ········}
47
48 ········void CheckRelation(Test test, RelInfo ri)
49 ········{
50 ············if (!ri.HasTable)
51 ················return;
52 ············Class clMapping = mapping.FindClass("RelationTestClasses." + test.OwnClass.Name);
53 ············
54 ············if (clMapping == null)
55 ················return;
56 ············NDO.Mapping.Relation rel = (NDO.Mapping.Relation) clMapping.Relations.FirstOrDefault();
57 ············Debug.Assert(rel != null, "Relation mapping not found");
58 ············if (rel == null)
59 ················throw new Exception("Cant find relation 0 of" + test.OwnClass.Name);
60
61 ············Class derivedMapping;
62 ············NDO.Mapping.Relation derivedRel = null;
63
64 ············if (ri.OwnPoly)
65 ············{
66 ················derivedMapping = mapping.FindClass("RelationTestClasses." + test.OwnDerivedClass.Name);
67 ················if (derivedMapping == null)
68 ····················return;
69 ················derivedRel = derivedMapping.Relations.FirstOrDefault();
70 ················if (rel == null)
71 ····················throw new Exception("Cant find relation 0 of" + test.OwnDerivedClass.Name);
72 ············}
73
74 ············if (rel.MappingTable == null || ri.OwnPoly && derivedRel.MappingTable == null)
75 ············{
76 ················string tableName = null;
77 ················if (test.OwnClass.Name.CompareTo(test.OtherClass.Name) < 0)
78 ····················tableName = "rel" + test.OwnClass.Name + test.OtherClass.Name;
79 ················else
80 ····················tableName = "rel" + test.OtherClass.Name + test.OwnClass.Name;
81 ················ForeignKeyColumn fkColumn = rel.NewForeignKeyColumn();
82 ················fkColumn.Name = "ID" + test.OwnClass.Name;
83 ················if (ri.OwnPoly)
84 ····················rel.ForeignKeyTypeColumnName = "TC" + test.OwnClass.Name;
85 ················else
86 ····················rel.ForeignKeyTypeColumnName = null;
87 ················rel.MappingTable = new MappingTable(rel);
88 ················rel.MappingTable.TableName = tableName;
89 ················fkColumn = rel.MappingTable.NewForeignKeyColumn();
90 ················fkColumn.Name = "ID" + test.OtherClass.Name;
91 ················if (ri.OtherPoly)
92 ····················rel.MappingTable.ChildForeignKeyTypeColumnName = "TC" + test.OtherClass.Name;
93 ················else
94 ····················rel.MappingTable.ChildForeignKeyTypeColumnName = null;
95 ················rel.MappingTable.ConnectionId = clMapping.ConnectionId;
96
97 ················if (ri.OwnPoly)
98 ················{
99 ····················ForeignKeyColumn dfkColumn = (ForeignKeyColumn) derivedRel.ForeignKeyColumns.FirstOrDefault();
100 ····················dfkColumn.Name = ((ForeignKeyColumn) rel.ForeignKeyColumns.FirstOrDefault()).Name;
101 ····················derivedRel.ForeignKeyTypeColumnName = rel.ForeignKeyTypeColumnName;
102 ····················derivedRel.MappingTable = rel.MappingTable;
103 ················}
104 ············}
105 ········}
106
107
108 ········public void Generate()
109 ········{
110 ············this.mapping = new NDOMapping(fileName, NDOProviderFactory.Instance);
111 ············foreach(RelInfo ri in relInfos)
112 ············{
113 ················Test test = new Test(ri, "RelationTestClasses");
114 ················CheckRelation(test, ri);
115 ················if (ri.IsBi)
116 ················{
117 ····················RelInfo reverseInfo = ri.GetReverse();
118 ····················Test reverseTest = test.GetReverse();
119 ····················CheckRelation(reverseTest, reverseInfo);
120 ················}
121 ············}
122 ············this.mapping.Save();
123 ········}
124 ····}
125 }