Skip to content

Commit fc1f1de

Browse files
author
Sergey Koshcheyev
committed
NH-499 - filters
SVN: trunk@2438
1 parent f8b2747 commit fc1f1de

21 files changed

+565
-344
lines changed

src/NHibernate.Test/FilterTest/DynamicFilterTest.cs

+87-76
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
namespace NHibernate.Test.FilterTest
1515
{
16-
[TestFixture, Ignore("Filters not yet implemented")]
16+
[TestFixture]
1717
public class DynamicFilterTest : TestCase
1818
{
1919
private ILog log = LogManager.GetLogger(typeof(DynamicFilterTest));
@@ -32,7 +32,9 @@ public void SecondLevelCachedCollectionsFiltering()
3232
ICollectionPersister persister = ((ISessionFactoryImplementor) sessions)
3333
.GetCollectionPersister(typeof(Salesperson).FullName + ".Orders");
3434
Assert.IsTrue(persister.HasCache, "No cache for collection");
35-
object[] cachedData = (object[]) persister.Cache.Cache.Get(testData.steveId);
35+
CacheKey cacheKey =
36+
new CacheKey(testData.steveId, persister.KeyType, persister.Role, (ISessionFactoryImplementor) sessions);
37+
object[] cachedData = (object[]) persister.Cache.Cache.Get(cacheKey);
3638
Assert.IsNotNull(cachedData, "collection was not in cache");
3739

3840
session.Close();
@@ -44,7 +46,7 @@ public void SecondLevelCachedCollectionsFiltering()
4446
.UniqueResult();
4547
Assert.AreEqual(1, sp.Orders.Count, "Filtered-collection not bypassing 2L-cache");
4648

47-
object[] cachedData2 = (object[]) persister.Cache.Cache.Get(testData.steveId);
49+
object[] cachedData2 = (object[]) persister.Cache.Cache.Get(cacheKey);
4850
Assert.IsNotNull(cachedData2, "collection no longer in cache!");
4951
Assert.AreSame(cachedData, cachedData2, "Different cache values!");
5052

@@ -85,7 +87,7 @@ public void CombinedClassAndCollectionFiltersEnabled()
8587
session.Clear();
8688

8789
// test retreival through hql with the collection join fetched
88-
salespersons = session.CreateQuery("select s from Salesperson as s left join fetch s.orders").List();
90+
salespersons = session.CreateQuery("select s from Salesperson as s left join fetch s.Orders").List();
8991
Assert.AreEqual(1, salespersons.Count, "Incorrect salesperson count");
9092
sp = (Salesperson) salespersons[0];
9193
Assert.AreEqual(sp.Orders.Count, 1, "Incorrect order count");
@@ -152,15 +154,16 @@ public void CriteriaQueryFilters()
152154

153155
log.Info("Criteria query against Product...");
154156
IList products = session.CreateCriteria(typeof(Product))
155-
.Add(Expression.Expression.Eq("stockNumber", 124))
157+
.Add(Expression.Expression.Eq("StockNumber", 124))
156158
.List();
157159
Assert.AreEqual(1, products.Count, "Incorrect product count");
158160

159161
session.Close();
160162
testData.Release();
161163
}
162164

163-
public void testGetFilters()
165+
[Test]
166+
public void GetFilters()
164167
{
165168
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
166169
// Get() test
@@ -181,7 +184,8 @@ public void testGetFilters()
181184
testData.Release();
182185
}
183186

184-
public void testOneToManyFilters()
187+
[Test]
188+
public void OneToManyFilters()
185189
{
186190
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
187191
// one-to-many loading tests
@@ -245,49 +249,50 @@ public void ManyToManyFilterOnCriteria()
245249
testData.Release();
246250
}
247251

248-
[Test, Ignore("Statistics not implemented")]
252+
[Test]
249253
public void ManyToManyFilterOnLoad()
250254
{
251-
//TestData testData = new TestData(this);
252-
//testData.Prepare();
255+
TestData testData = new TestData(this);
256+
testData.Prepare();
253257

254-
//ISession session = OpenSession();
255-
//session.EnableFilter("effectiveDate").SetParameter("asOfDate", DateTime.Today);
258+
ISession session = OpenSession();
259+
session.EnableFilter("effectiveDate").SetParameter("asOfDate", DateTime.Today);
256260

257-
//Product prod = (Product) session.Get(typeof(Product), testData.prod1Id);
261+
Product prod = (Product) session.Get(typeof(Product), testData.prod1Id);
258262

259-
//long initLoadCount = sessions.getStatistics().getCollectionLoadCount();
260-
//long initFetchCount = sessions.getStatistics().getCollectionFetchCount();
263+
//long initLoadCount = sessions.Statistics.CollectionLoadCount;
264+
//long initFetchCount = sessions.Statistics.CollectionFetchCount;
261265

262-
//// should already have been initialized...
263-
//int size = prod.Categories.Count;
264-
//Assert.AreEqual(1, size, "Incorrect filtered collection count");
266+
// should already have been initialized...
267+
Assert.IsTrue(NHibernateUtil.IsInitialized(prod.Categories));
268+
int size = prod.Categories.Count;
269+
Assert.AreEqual(1, size, "Incorrect filtered collection count");
265270

266-
//long currLoadCount = sessions.getStatistics().getCollectionLoadCount();
267-
//long currFetchCount = sessions.getStatistics().getCollectionFetchCount();
271+
//long currLoadCount = sessions.Statistics.CollectionLoadCount;
272+
//long currFetchCount = sessions.Statistics.CollectionFetchCount;
268273

269274
//Assert.IsTrue(
270275
// (initLoadCount == currLoadCount) && (initFetchCount == currFetchCount),
271276
// "Load with join fetch of many-to-many did not trigger join fetch"
272-
273277
// );
274278

275-
//// make sure we did not get back a collection of proxies
276-
//long initEntityLoadCount = sessions.getStatistics().getEntityLoadCount();
279+
// make sure we did not get back a collection of proxies
280+
//long initEntityLoadCount = sessions.Statistics.EntityLoadCount;
277281

278-
//foreach (Category cat in prod.Categories)
279-
//{
280-
// Console.WriteLine(" ===> " + cat.Name);
281-
//}
282-
//long currEntityLoadCount = sessions.getStatistics().getEntityLoadCount();
282+
foreach (Category cat in prod.Categories)
283+
{
284+
Assert.IsTrue(NHibernateUtil.IsInitialized(cat), "Load with join fetch of many-to-many did not trigger *complete* join fetch");
285+
//Console.WriteLine(" ===> " + cat.Name);
286+
}
287+
//long currEntityLoadCount = sessions.Statistics.EntityLoadCount;
283288

284289
//Assert.IsTrue(
285290
// (initEntityLoadCount == currEntityLoadCount),
286291
// "Load with join fetch of many-to-many did not trigger *complete* join fetch"
287292
// );
288293

289-
//session.Close();
290-
//testData.Release();
294+
session.Close();
295+
testData.Release();
291296
}
292297

293298
[Test]
@@ -322,7 +327,7 @@ public void ManyToManyFilterOnQuery()
322327
ISession session = OpenSession();
323328
session.EnableFilter("effectiveDate").SetParameter("asOfDate", DateTime.Today);
324329

325-
IList result = session.CreateQuery("from Product p inner join fetch p.categories").List();
330+
IList result = session.CreateQuery("from Product p inner join fetch p.Categories").List();
326331
Assert.IsTrue(result.Count > 0, "No products returned from HQL many-to-many filter case");
327332

328333
Product prod = (Product) result[0];
@@ -334,92 +339,98 @@ public void ManyToManyFilterOnQuery()
334339
testData.Release();
335340
}
336341

337-
[Test, Ignore("Statistics not implemented")]
342+
[Test]
338343
public void ManyToManyBase()
339344
{
340-
//TestData testData = new TestData(this);
341-
//testData.Prepare();
345+
TestData testData = new TestData(this);
346+
testData.Prepare();
342347

343-
//ISession session = OpenSession();
348+
ISession session = OpenSession();
344349

345-
//Product prod = ( Product ) session.Get( typeof(Product), testData.prod1Id );
350+
Product prod = ( Product ) session.Get( typeof(Product), testData.prod1Id );
346351

347-
//long initLoadCount = sessions.getStatistics().getCollectionLoadCount();
348-
//long initFetchCount = sessions.getStatistics().getCollectionFetchCount();
352+
// TODO H3: Statistics
353+
//long initLoadCount = sessions.Statistics.CollectionLoadCount;
354+
//long initFetchCount = sessions.Statistics.CollectionFetchCount;
349355

350-
//// should already have been initialized...
351-
//int size = prod.Categories.Count;
352-
//Assert.AreEqual(2, size, "Incorrect non-filtered collection count" );
356+
// should already have been initialized...
357+
Assert.IsTrue(NHibernateUtil.IsInitialized(prod.Categories), "Load with join fetch of many-to-many did not trigger join fetch");
358+
int size = prod.Categories.Count;
359+
Assert.AreEqual(2, size, "Incorrect non-filtered collection count" );
353360

354-
//long currLoadCount = sessions.getStatistics().getCollectionLoadCount();
355-
//long currFetchCount = sessions.getStatistics().getCollectionFetchCount();
361+
//long currLoadCount = sessions.Statistics.CollectionLoadCount;
362+
//long currFetchCount = sessions.Statistics.CollectionFetchCount;
356363

357364
//Assert.IsTrue(
358365
// ( initLoadCount == currLoadCount ) && ( initFetchCount == currFetchCount ),
359366
// "Load with join fetch of many-to-many did not trigger join fetch"
360367
//);
361368

362-
//// make sure we did not get back a collection of proxies
363-
//long initEntityLoadCount = sessions.getStatistics().getEntityLoadCount();
364-
//foreach (Category cat in prod.Categories)
365-
//{
366-
// Console.WriteLine(" ===> " + cat.Name);
367-
//}
368-
//long currEntityLoadCount = sessions.getStatistics().getEntityLoadCount();
369+
// make sure we did not get back a collection of proxies
370+
// TODO H3: statistics
371+
//long initEntityLoadCount = sessions.Statistics.EntityLoadCount;
372+
foreach (Category cat in prod.Categories)
373+
{
374+
Assert.IsTrue(NHibernateUtil.IsInitialized(cat), "Load with join fetch of many-to-many did not trigger *complete* join fetch");
375+
//Console.WriteLine(" ===> " + cat.Name);
376+
}
377+
//long currEntityLoadCount = sessions.Statistics.EntityLoadCount;
369378

370379
//Assert.IsTrue(
371380
// ( initEntityLoadCount == currEntityLoadCount ),
372381
// "Load with join fetch of many-to-many did not trigger *complete* join fetch"
373382
//);
374383

375-
//session.Close();
376-
//testData.Release();
384+
session.Close();
385+
testData.Release();
377386
}
378387

379-
[Test, Ignore("Statistics not implemented")]
388+
[Test]
380389
public void ManyToManyBaseThruCriteria()
381390
{
382-
//TestData testData = new TestData(this);
383-
//testData.Prepare();
391+
TestData testData = new TestData(this);
392+
testData.Prepare();
384393

385-
//ISession session = OpenSession();
394+
ISession session = OpenSession();
386395

387-
//IList result = session.CreateCriteria(typeof(Product))
388-
// .Add(Expression.Expression.Eq("id", testData.prod1Id))
389-
// .List();
396+
IList result = session.CreateCriteria(typeof(Product))
397+
.Add(Expression.Expression.Eq("id", testData.prod1Id))
398+
.List();
390399

391-
//Product prod = (Product) result[0];
400+
Product prod = (Product) result[0];
392401

393-
//long initLoadCount = sessions.getStatistics().getCollectionLoadCount();
394-
//long initFetchCount = sessions.getStatistics().getCollectionFetchCount();
402+
//long initLoadCount = sessions.Statistics.CollectionLoadCount;
403+
//long initFetchCount = sessions.Statistics.CollectionFetchCount;
395404

396-
//// should already have been initialized...
397-
//int size = prod.Categories.Count;
398-
//Assert.AreEqual(2, size, "Incorrect non-filtered collection count");
405+
// should already have been initialized...
406+
Assert.IsTrue(NHibernateUtil.IsInitialized(prod.Categories), "Load with join fetch of many-to-many did not trigger join fetch");
407+
int size = prod.Categories.Count;
408+
Assert.AreEqual(2, size, "Incorrect non-filtered collection count");
399409

400-
//long currLoadCount = sessions.getStatistics().getCollectionLoadCount();
401-
//long currFetchCount = sessions.getStatistics().getCollectionFetchCount();
410+
//long currLoadCount = sessions.Statistics.CollectionLoadCount;
411+
//long currFetchCount = sessions.Statistics.CollectionFetchCount;
402412

403413
//Assert.IsTrue(
404414
// (initLoadCount == currLoadCount) && (initFetchCount == currFetchCount),
405415
// "Load with join fetch of many-to-many did not trigger join fetch"
406416
// );
407417

408-
//// make sure we did not get back a collection of proxies
409-
//long initEntityLoadCount = sessions.getStatistics().getEntityLoadCount();
410-
//foreach (Category cat in prod.Categories)
411-
//{
412-
// Console.WriteLine(" ===> " + cat.Name);
413-
//}
414-
//long currEntityLoadCount = sessions.getStatistics().getEntityLoadCount();
418+
// make sure we did not get back a collection of proxies
419+
//long initEntityLoadCount = sessions.Statistics.EntityLoadCount;
420+
foreach (Category cat in prod.Categories)
421+
{
422+
Assert.IsTrue(NHibernateUtil.IsInitialized(cat), "Load with join fetch of many-to-many did not trigger *complete* join fetch");
423+
//Console.WriteLine(" ===> " + cat.Name);
424+
}
425+
//long currEntityLoadCount = sessions.Statistics.EntityLoadCount;
415426

416427
//Assert.IsTrue(
417428
// (initEntityLoadCount == currEntityLoadCount),
418429
// "Load with join fetch of many-to-many did not trigger *complete* join fetch"
419430
// );
420431

421-
//session.Close();
422-
//testData.Release();
432+
session.Close();
433+
testData.Release();
423434
}
424435

425436
protected override string MappingsAssembly

src/NHibernate.Test/NHSpecificTest/SetFixture.cs

+11
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,17 @@ public ISessionFactoryImplementor Factory
347347
get { return null; }
348348
}
349349

350+
351+
public bool IsAffectedByEnabledFilters(ISessionImplementor session)
352+
{
353+
return false;
354+
}
355+
356+
public bool HasManyToManyOrdering
357+
{
358+
get { return false; }
359+
}
360+
350361
#endregion
351362
}
352363

src/NHibernate/Cache/QueryKey.cs

+2-58
Original file line numberDiff line numberDiff line change
@@ -46,62 +46,6 @@ public QueryKey( SqlString queryString, QueryParameters queryParameters, ISet fi
4646
this.hashCode = ComputeHashCode();
4747
}
4848

49-
private static bool DictionariesAreEqual( IDictionary a, IDictionary b )
50-
{
51-
if (Equals(a, b))
52-
{
53-
return true;
54-
}
55-
56-
if( a == null || b == null )
57-
{
58-
return false;
59-
}
60-
61-
if( a.Count != b.Count )
62-
{
63-
return false;
64-
}
65-
66-
foreach( object key in a.Keys )
67-
{
68-
if( !object.Equals( a[ key ], b[ key ] ) )
69-
{
70-
return false;
71-
}
72-
}
73-
74-
return true;
75-
}
76-
77-
private static bool SetsAreEqual( ISet a, ISet b )
78-
{
79-
if (Equals(a, b))
80-
{
81-
return true;
82-
}
83-
84-
if( a == null || b == null )
85-
{
86-
return false;
87-
}
88-
89-
if( a.Count != b.Count )
90-
{
91-
return false;
92-
}
93-
94-
foreach (object obj in a)
95-
{
96-
if (!b.Contains(obj))
97-
{
98-
return false;
99-
}
100-
}
101-
102-
return true;
103-
}
104-
10549
public override bool Equals( object other )
10650
{
10751
QueryKey that = ( QueryKey ) other;
@@ -151,12 +95,12 @@ public override bool Equals( object other )
15195
}
15296
}
15397

154-
if( !DictionariesAreEqual( namedParameters, that.namedParameters ) )
98+
if( !CollectionHelper.DictionaryEquals( namedParameters, that.namedParameters ) )
15599
{
156100
return false;
157101
}
158102

159-
if (!SetsAreEqual(filters, that.filters))
103+
if (!CollectionHelper.SetEquals(filters, that.filters))
160104
{
161105
return false;
162106
}

0 commit comments

Comments
 (0)