Datei: IntegrationTests/IntegrationTests/NDOObjectIdTests.cs

Last Commit (50beab8)
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.Linq;
25 using System.Data;
26 using System.Reflection;
27 using System.Collections;
28 using NUnit.Framework;
29 using NDO;
30 using NDO.Mapping;
31 using NDOObjectIdTestClasses;
32 using NDOInterfaces;
33 using NDO.Query;
34 using NDO.ProviderFactory;
35
36 namespace NdoUnitTests
37 {
38 ····[TestFixture]
39 ····public class NDOObjectIdTests : NDOTest
40 ····{
41 ········PersistenceManager pm;
42 ········Guid guid;
43
44 ········[SetUp]
45 ········public void Setup()
46 ········{
47 ············pm = PmFactory.NewPersistenceManager();
48 ············pm.IdGenerationEvent += new IdGenerationHandler(OnIdGeneration);
49 ············guid = new Guid(1,2,3,4,5,6,7,8,9,10,11);
50 ········}
51
52 ········[TearDown]
53 ········public void TearDown()
54 ········{············
55 IList l = pm. GetClassExtent( typeof( ObjectOwner) ) ;
56 ············pm.Delete(l);
57 ············pm.Save();
58 l = pm. GetClassExtent( typeof( NDOoidAndHandler) ) ;
59 ············pm.Delete(l);
60 ············pm.Save();
61 l = pm. GetClassExtent( typeof( HintOwner) ) ;
62 ············pm.Delete(l);
63 ············pm.Save();
64 l = pm. GetClassExtent( typeof( ClassWithHint) ) ;
65 ············pm.Delete(l);
66 ············pm.Save();
67 l = pm. GetClassExtent( typeof( DerivedGuid) ) ;
68 ············pm.Delete(l);
69 ············pm.Save();
70 ············pm.Dispose();
71 ········}
72
73 ········[Test]
74 ········public void OidTest()
75 ········{
76 ············NDOoidAndHandler testobj = new NDOoidAndHandler();
77 ············testobj.MyId = guid;
78 ············testobj.Text = "Test";
79 ············pm.MakePersistent(testobj);
80 ············pm.Save();
81 ············pm.UnloadCache();
82 ············IQuery q = new NDOQuery<NDOoidAndHandler>(pm);
83 ············testobj = null;
84 ············testobj = (NDOoidAndHandler) q.ExecuteSingle(true);
85 ············Assert.That(guid ==··testobj.MyId, "Wrong guid");
86 ············Assert.That("Test" ==··testobj.Text, "Wrong text");
87
88 ············testobj.Text = "Neuer Text";
89 ············pm.Save();
90 ············testobj = null;
91 ············testobj = (NDOoidAndHandler) q.ExecuteSingle(true);
92 ············Assert.That(guid ==··testobj.MyId, "Wrong guid");
93 ············Assert.That("Neuer Text" ==··testobj.Text, "Wrong text");
94 ············
95 ············pm.Delete(testobj);
96 ············pm.Save();
97 ········}
98
99 ········[Test]
100 ········public void TestForeignKey()
101 ········{
102 ············ObjectOwner owner = new ObjectOwner();
103 ············pm.MakePersistent(owner);
104 ············owner.Element = new NDOoidAndHandler();
105 ············owner.Element.Text = "Text";
106 ············pm.Save();
107 ············owner = null;
108 ············pm.UnloadCache();
109 ············IQuery q = new NDOQuery<ObjectOwner>(pm);
110 ············owner = (ObjectOwner) q.ExecuteSingle(true);
111 ············Assert.That(owner.Element != null, "No element");
112 ············Assert.That(guid ==··owner.Element.MyId, "Wrong guid");
113 ············Assert.That("Text" ==··owner.Element.Text, "Wrong text");
114 ········}
115
116
117 ········[Test]
118 ········public void TestHintForeignKey()
119 ········{
120 ············HintOwner owner = new HintOwner();
121 ············pm.MakePersistent(owner);
122 ············owner.Element = new ClassWithHint();
123 ············owner.Element.Text = "Text";
124 ············pm.Save();
125 ············owner = null;
126 ············pm.UnloadCache();
127 ············IQuery q = new NDOQuery<HintOwner>(pm);
128 ············owner = (HintOwner) q.ExecuteSingle(true);
129 ············Assert.That(owner.Element != null, "No element");
130 ············Assert.That(guid == (Guid) owner.Element.NDOObjectId.Id[0], "Wrong guid");
131 ············Assert.That("Text" == owner.Element.Text, "Wrong text");
132 ········}
133
134
135 ········[Test]
136 ········public void HintEnhancerTest()
137 ········{
138 ············Class cl = pm.NDOMapping.FindClass(typeof(ClassWithHint));
139 ············IProvider provider = NDOProviderFactory.Instance[((Connection)pm.NDOMapping.Connections.First()).Type];
140 ············Assert.That(cl != null, "Class not found");
141 ············Assert.That(typeof(Guid) ==··((OidColumn)cl.Oid.OidColumns[0]).SystemType, "Wrong type");
142 ············Type t = pm.GetType();
143 ············FieldInfo fi = t.GetField("ds", BindingFlags.NonPublic | BindingFlags.Instance);
144 ············Assert.That(fi != null, "Field 'ds' not found");
145 ············DataSet ds = (DataSet) fi.GetValue(pm);
146 ············Assert.That(ds != null, "DataSet is null");
147 ············DataTable dt = ds.Tables[cl.TableName];
148 ············Assert.That(dt != null, "DataTable is null");
149 ············OidColumn oidColumn = (OidColumn)cl.Oid.OidColumns[0];
150 ············DataColumn dc = dt.Columns[oidColumn.Name];
151 ············Assert.That(dc != null, "DataColumn is null");
152 ············Assert.That(provider != null, "Provider is null");
153 ············if (provider.SupportsNativeGuidType)
154 ················Assert.That(typeof(Guid) ==··dc.DataType, "Wrong column type");
155 ············else
156 ················Assert.That(typeof(string) ==··dc.DataType, "Wrong column type");
157 ········}
158
159
160 ········[Test]
161 ········public void HintTest()
162 ········{
163 ············ClassWithHint testobj = new ClassWithHint();
164 ············testobj.Text = "Test";
165 ············pm.MakePersistent(testobj);
166 ············pm.Save();
167 ············pm.UnloadCache();
168 ············IQuery q = new NDOQuery<ClassWithHint>(pm);
169 ············testobj = null;
170 ············testobj = (ClassWithHint) q.ExecuteSingle(true);
171 ············Assert.That(guid == (Guid) testobj.NDOObjectId.Id[0], "Wrong guid #1");
172 ············Assert.That("Test" ==··testobj.Text, "Wrong text #1");
173
174 ············testobj.Text = "Neuer Text";
175 ············pm.Save();
176 ············testobj = null;
177 ············testobj = (ClassWithHint) q.ExecuteSingle(true);
178 ············Assert.That(guid == (Guid) testobj.NDOObjectId.Id[0], "Wrong guid #2");
179 ············Assert.That("Neuer Text" ==··testobj.Text, "Wrong text #2");
180 ············
181 ············pm.Delete(testobj);
182 ············pm.Save();
183 ········}
184
185 ········[Test]
186 ········public void DerivedGuidTest()
187 ········{
188 ············PersistenceManager pm = PmFactory.NewPersistenceManager();
189 ············DerivedGuid dg = new DerivedGuid();
190 ············dg.Guid = new Guid(5,5,5,5,5,5,5,5,5,5,5);
191 ············dg.Myfield = "Test";
192 ············pm.MakePersistent(dg);
193 ············pm.Save();
194 ············dg = null;
195 ············pm.UnloadCache();
196 ············IQuery q = new NDOQuery<DerivedGuid>(pm);
197 ············dg = (DerivedGuid) q.ExecuteSingle(true);
198 ············Assert.That(new Guid(5,5,5,5,5,5,5,5,5,5,5) == dg.Guid, "Guid wrong");
199 ············Assert.That(new Guid(5,5,5,5,5,5,5,5,5,5,5) == (Guid) dg.NDOObjectId.Id[0], "Oid wrong");
200 ············Assert.That("Test" ==··dg.Myfield, "Text wrong");
201 ············pm.Delete(dg);
202 ············pm.Save();
203 ········}
204
205 ········private void OnIdGeneration(Type t, ObjectId oid)
206 ········{
207 ············if (!t.FullName.StartsWith ("NDOObjectIdTestClasses."))
208 ················return;··// Use the other handler
209 ············Class cl = pm.NDOMapping.FindClass(t);
210 ············Assert.That(cl != null);
211 ············OidColumn oidColumn = (OidColumn)cl.Oid.OidColumns[0];
212 ············if (t == typeof(NDOoidAndHandler) || t == typeof(ClassWithHint))
213 ············{
214 ················if (oidColumn.SystemType == typeof(string))
215 ····················oid.Id[0] = this.guid.ToString();
216 ················else
217 ····················oid.Id[0] = this.guid;
218 ············}
219 ············else
220 ············{
221 ················oid.Id[0] = 1;
222 ············}
223 ········}
224 ····}
225 }
226
New Commit (74c26c3)
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.Linq;
25 using System.Data;
26 using System.Reflection;
27 using System.Collections;
28 using NUnit.Framework;
29 using NDO;
30 using NDO.Mapping;
31 using NDOObjectIdTestClasses;
32 using NDOInterfaces;
33 using NDO.Query;
34 using NDO.ProviderFactory;
35
36 namespace NdoUnitTests
37 {
38 ····[TestFixture]
39 ····public class NDOObjectIdTests : NDOTest
40 ····{
41 ········PersistenceManager pm;
42 ········Guid guid;
43
44 ········[SetUp]
45 ········public void Setup()
46 ········{
47 ············pm = PmFactory.NewPersistenceManager();
48 ············pm.IdGenerationEvent += new IdGenerationHandler(OnIdGeneration);
49 ············guid = new Guid(1,2,3,4,5,6,7,8,9,10,11);
50 ········}
51
52 ········[TearDown]
53 ········public void TearDown()
54 ········{············
55 IList l = pm. NewQuery( typeof( ObjectOwner) , null, true ) . Execute( ) ;
56 ············pm.Delete(l);
57 ············pm.Save();
58 l = pm. NewQuery( typeof( NDOoidAndHandler) , null, true ) . Execute( ) ;
59 ············pm.Delete(l);
60 ············pm.Save();
61 l = pm. NewQuery( typeof( HintOwner) , null, true ) . Execute( ) ;
62 ············pm.Delete(l);
63 ············pm.Save();
64 l = pm. NewQuery( typeof( ClassWithHint) , null, true ) . Execute( ) ;
65 ············pm.Delete(l);
66 ············pm.Save();
67 l = pm. NewQuery( typeof( DerivedGuid) , null, true ) . Execute( ) ;
68 ············pm.Delete(l);
69 ············pm.Save();
70 ············pm.Dispose();
71 ········}
72
73 ········[Test]
74 ········public void OidTest()
75 ········{
76 ············NDOoidAndHandler testobj = new NDOoidAndHandler();
77 ············testobj.MyId = guid;
78 ············testobj.Text = "Test";
79 ············pm.MakePersistent(testobj);
80 ············pm.Save();
81 ············pm.UnloadCache();
82 ············IQuery q = new NDOQuery<NDOoidAndHandler>(pm);
83 ············testobj = null;
84 ············testobj = (NDOoidAndHandler) q.ExecuteSingle(true);
85 ············Assert.That(guid ==··testobj.MyId, "Wrong guid");
86 ············Assert.That("Test" ==··testobj.Text, "Wrong text");
87
88 ············testobj.Text = "Neuer Text";
89 ············pm.Save();
90 ············testobj = null;
91 ············testobj = (NDOoidAndHandler) q.ExecuteSingle(true);
92 ············Assert.That(guid ==··testobj.MyId, "Wrong guid");
93 ············Assert.That("Neuer Text" ==··testobj.Text, "Wrong text");
94 ············
95 ············pm.Delete(testobj);
96 ············pm.Save();
97 ········}
98
99 ········[Test]
100 ········public void TestForeignKey()
101 ········{
102 ············ObjectOwner owner = new ObjectOwner();
103 ············pm.MakePersistent(owner);
104 ············owner.Element = new NDOoidAndHandler();
105 ············owner.Element.Text = "Text";
106 ············pm.Save();
107 ············owner = null;
108 ············pm.UnloadCache();
109 ············IQuery q = new NDOQuery<ObjectOwner>(pm);
110 ············owner = (ObjectOwner) q.ExecuteSingle(true);
111 ············Assert.That(owner.Element != null, "No element");
112 ············Assert.That(guid ==··owner.Element.MyId, "Wrong guid");
113 ············Assert.That("Text" ==··owner.Element.Text, "Wrong text");
114 ········}
115
116
117 ········[Test]
118 ········public void TestHintForeignKey()
119 ········{
120 ············HintOwner owner = new HintOwner();
121 ············pm.MakePersistent(owner);
122 ············owner.Element = new ClassWithHint();
123 ············owner.Element.Text = "Text";
124 ············pm.Save();
125 ············owner = null;
126 ············pm.UnloadCache();
127 ············IQuery q = new NDOQuery<HintOwner>(pm);
128 ············owner = (HintOwner) q.ExecuteSingle(true);
129 ············Assert.That(owner.Element != null, "No element");
130 ············Assert.That(guid == (Guid) owner.Element.NDOObjectId.Id[0], "Wrong guid");
131 ············Assert.That("Text" == owner.Element.Text, "Wrong text");
132 ········}
133
134
135 ········[Test]
136 ········public void HintEnhancerTest()
137 ········{
138 ············Class cl = pm.NDOMapping.FindClass(typeof(ClassWithHint));
139 ············IProvider provider = NDOProviderFactory.Instance[((Connection)pm.NDOMapping.Connections.First()).Type];
140 ············Assert.That(cl != null, "Class not found");
141 ············Assert.That(typeof(Guid) ==··((OidColumn)cl.Oid.OidColumns[0]).SystemType, "Wrong type");
142 ············Type t = pm.GetType();
143 ············FieldInfo fi = t.GetField("ds", BindingFlags.NonPublic | BindingFlags.Instance);
144 ············Assert.That(fi != null, "Field 'ds' not found");
145 ············DataSet ds = (DataSet) fi.GetValue(pm);
146 ············Assert.That(ds != null, "DataSet is null");
147 ············DataTable dt = ds.Tables[cl.TableName];
148 ············Assert.That(dt != null, "DataTable is null");
149 ············OidColumn oidColumn = (OidColumn)cl.Oid.OidColumns[0];
150 ············DataColumn dc = dt.Columns[oidColumn.Name];
151 ············Assert.That(dc != null, "DataColumn is null");
152 ············Assert.That(provider != null, "Provider is null");
153 ············if (provider.SupportsNativeGuidType)
154 ················Assert.That(typeof(Guid) ==··dc.DataType, "Wrong column type");
155 ············else
156 ················Assert.That(typeof(string) ==··dc.DataType, "Wrong column type");
157 ········}
158
159
160 ········[Test]
161 ········public void HintTest()
162 ········{
163 ············ClassWithHint testobj = new ClassWithHint();
164 ············testobj.Text = "Test";
165 ············pm.MakePersistent(testobj);
166 ············pm.Save();
167 ············pm.UnloadCache();
168 ············IQuery q = new NDOQuery<ClassWithHint>(pm);
169 ············testobj = null;
170 ············testobj = (ClassWithHint) q.ExecuteSingle(true);
171 ············Assert.That(guid == (Guid) testobj.NDOObjectId.Id[0], "Wrong guid #1");
172 ············Assert.That("Test" ==··testobj.Text, "Wrong text #1");
173
174 ············testobj.Text = "Neuer Text";
175 ············pm.Save();
176 ············testobj = null;
177 ············testobj = (ClassWithHint) q.ExecuteSingle(true);
178 ············Assert.That(guid == (Guid) testobj.NDOObjectId.Id[0], "Wrong guid #2");
179 ············Assert.That("Neuer Text" ==··testobj.Text, "Wrong text #2");
180 ············
181 ············pm.Delete(testobj);
182 ············pm.Save();
183 ········}
184
185 ········[Test]
186 ········public void DerivedGuidTest()
187 ········{
188 ············PersistenceManager pm = PmFactory.NewPersistenceManager();
189 ············DerivedGuid dg = new DerivedGuid();
190 ············dg.Guid = new Guid(5,5,5,5,5,5,5,5,5,5,5);
191 ············dg.Myfield = "Test";
192 ············pm.MakePersistent(dg);
193 ············pm.Save();
194 ············dg = null;
195 ············pm.UnloadCache();
196 ············IQuery q = new NDOQuery<DerivedGuid>(pm);
197 ············dg = (DerivedGuid) q.ExecuteSingle(true);
198 ············Assert.That(new Guid(5,5,5,5,5,5,5,5,5,5,5) == dg.Guid, "Guid wrong");
199 ············Assert.That(new Guid(5,5,5,5,5,5,5,5,5,5,5) == (Guid) dg.NDOObjectId.Id[0], "Oid wrong");
200 ············Assert.That("Test" ==··dg.Myfield, "Text wrong");
201 ············pm.Delete(dg);
202 ············pm.Save();
203 ········}
204
205 ········private void OnIdGeneration(Type t, ObjectId oid)
206 ········{
207 ············if (!t.FullName.StartsWith ("NDOObjectIdTestClasses."))
208 ················return;··// Use the other handler
209 ············Class cl = pm.NDOMapping.FindClass(t);
210 ············Assert.That(cl != null);
211 ············OidColumn oidColumn = (OidColumn)cl.Oid.OidColumns[0];
212 ············if (t == typeof(NDOoidAndHandler) || t == typeof(ClassWithHint))
213 ············{
214 ················if (oidColumn.SystemType == typeof(string))
215 ····················oid.Id[0] = this.guid.ToString();
216 ················else
217 ····················oid.Id[0] = this.guid;
218 ············}
219 ············else
220 ············{
221 ················oid.Id[0] = 1;
222 ············}
223 ········}
224 ····}
225 }
226