context: using System;
  using System.Collections;
  using NHibernate.Criterion;
  using NHibernate.Transform;
  using NHibernate.Type;
  using NHibernate.Util;
  using NUnit.Framework;
  
  namespace NHibernate.Test.Criteria
  {
  	[TestFixture]
  	public class DetachedCriteriaSerializable : TestCase
  	{
  		protected override string MappingsAssembly
  		{
  			get { return "NHibernate.Test"; }
  		}
  
  		protected override string[] Mappings
  		{
  			get { return new string[] { "Criteria.Enrolment.hbm.xml" }; }
  		}
  
  		private void SerializeAndList(DetachedCriteria dc)
  		{
  			byte[] bytes = SerializationHelper.Serialize(dc);
  
  			DetachedCriteria dcs = (DetachedCriteria)SerializationHelper.Deserialize(bytes);
  
  			using (ISession s = OpenSession())
  			{
  				dcs.GetExecutableCriteria(s).List();
  			}
  		}
  
  		[Test]
  		public void AllCriterionAreSerializable()
  		{
  			NHAssert.InheritedAreMarkedSerializable(typeof(ICriterion));
  		}
  
  		[Test]
  		public void AllProjectionAreSerializable()
  		{
  			NHAssert.InheritedAreMarkedSerializable(typeof(IProjection));
  		}
  
  		[Test]
  		public void AllEmbeddedResultTransformersAreSerializable()
  		{
  			NHAssert.InheritedAreMarkedSerializable(typeof(IResultTransformer));
  		}
  
  		[Test]
  		public void DetachedCriteriaItSelf()
  		{
  			DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
  				.Add(Expression.Eq("Name", "Gavin King"));
  			SerializeAndList(dc);
  		}
  
  		[Test]
  		public void BasicCriterions()
  		{
  			ICriterion c = Expression.Eq("Name", "Gavin King");
  			NHAssert.IsSerializable(c);
  			IDictionary nameValue = new Hashtable(1);
  			nameValue.Add("Name", "Ralph");
  			c = Expression.AllEq(nameValue);
  			NHAssert.IsSerializable(c);
  			c = Expression.Between("Name", "aaaaa", "zzzzz");
  			NHAssert.IsSerializable(c);
  			c = Expression.EqProperty("Name", "Name");
  			NHAssert.IsSerializable(c);
  			c = Expression.Ge("Name", "a");
  			NHAssert.IsSerializable(c);
  			c = Expression.GeProperty("Name", "Name");
  			NHAssert.IsSerializable(c);
  			c = Expression.Gt("Name", "z");
  			NHAssert.IsSerializable(c);
  			c = Expression.GtProperty("Name", "Name");
  			NHAssert.IsSerializable(c);
  			c = Expression.IdEq(1);
  			NHAssert.IsSerializable(c);
  			c = Expression.In("Name", new string[] { "Gavin", "Ralph" });
  			NHAssert.IsSerializable(c);
  			c = Expression.InsensitiveLike("Name", "GAVIN");
  			NHAssert.IsSerializable(c);
  			c = Expression.IsEmpty("Enrolments");
  			NHAssert.IsSerializable(c);
  			c = Expression.IsNotEmpty("Enrolments");
  			NHAssert.IsSerializable(c);
  			c = Expression.IsNotNull("PreferredCourse");
  			NHAssert.IsSerializable(c);
  			c = Expression.IsNull("PreferredCourse");
  			NHAssert.IsSerializable(c);
  			c = Expression.Le("Name", "a");
  			NHAssert.IsSerializable(c);
  			c = Expression.LeProperty("Name", "Name");
  			NHAssert.IsSerializable(c);
  			c = Expression.Lt("Name", "z");
  			NHAssert.IsSerializable(c);
  			c = Expression.LtProperty("Name", "Name");
  			NHAssert.IsSerializable(c);
  			c = Expression.Like("Name", "G%");
  			NHAssert.IsSerializable(c);
  			c = Expression.Not(Expression.Eq("Name", "Ralph"));
  			NHAssert.IsSerializable(c);
  			c = Expression.NotEqProperty("Name", "Name");
  			NHAssert.IsSerializable(c);
  		}
  
  		[Test]
  		public void LikeCriterions()
  		{
  			ICriterion c = Expression.Like("Name", "Gavin", MatchMode.Anywhere);
  			NHAssert.IsSerializable(c);
  			c = Expression.Like("Name", "Gavin", MatchMode.End);
  			NHAssert.IsSerializable(c);
  			c = Expression.Like("Name", "Gavin", MatchMode.Exact);
  			NHAssert.IsSerializable(c);
  			c = Expression.Like("Name", "Gavin", MatchMode.Start);
  			NHAssert.IsSerializable(c);
  		}
  
  		[Test]
  		public void LogicalCriterions()
  		{
  			ICriterion c =
  				Expression.Or(Expression.Eq("Name", "Ralph"), Expression.Eq("Name", "Gavin"));
  			NHAssert.IsSerializable(c);
  			c =
  				Expression.And(Expression.Gt("StudentNumber", 1),
  										  Expression.Lt("StudentNumber", 10));
  			NHAssert.IsSerializable(c);
  		}
  
  		[Test]
  		public void ProjectionsExpressions()
  		{
  			IProjection p = Projections.Avg("StudentNumber");
  			NHAssert.IsSerializable(p);
  			p = Projections.Count("StudentNumber");
  			NHAssert.IsSerializable(p);
  			p = Projections.CountDistinct("StudentNumber");
  			NHAssert.IsSerializable(p);
  			p = Projections.GroupProperty("Name");
  			NHAssert.IsSerializable(p);
  			p = Projections.Id();
  			NHAssert.IsSerializable(p);
  			p = Projections.Max("StudentNumber");
  			NHAssert.IsSerializable(p);
  			p = Projections.Min("StudentNumber");
  			NHAssert.IsSerializable(p);
  			p = Projections.Property("StudentNumber");
  			NHAssert.IsSerializable(p);
  			p = Projections.RowCount();
  			NHAssert.IsSerializable(p);
  			p = Projections.Sum("StudentNumber");
  			NHAssert.IsSerializable(p);
  
  			p = Projections.Alias(Projections.Count("StudentNumber"), "alias");
  			NHAssert.IsSerializable(p);
  			p = Projections.Distinct(Projections.Id());
  			NHAssert.IsSerializable(p);
  			p = Projections.ProjectionList().Add(Projections.Max("StudentNumber"));
  			NHAssert.IsSerializable(p);
  		}
  
  		[Test]
  		public void Junctions()
  		{
  			ICriterion c = Expression.Conjunction()
  				.Add(Expression.Eq("Name", "Ralph"))
  				.Add(Expression.Eq("StudentNumber", "1"));
  			NHAssert.IsSerializable(c);
  			c = Expression.Disjunction()
  				.Add(Expression.Eq("Name", "Ralph"))
  				.Add(Expression.Eq("Name", "Gavin"));
  			NHAssert.IsSerializable(c);
  		}
  
  		[Test]
  		public void SubqueriesExpressions()
  		{
  			DetachedCriteria dc = DetachedCriteria.For(typeof(Student))
  				.Add(Expression.Eq("Name", "Gavin King"));
  			ICriterion c = Subqueries.Eq("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.EqAll("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.Exists(dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.Ge("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.GeAll("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.GeSome("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.Gt("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.GtAll("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.GtSome("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.In("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.Le("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.LeAll("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.LeSome("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  			c = Subqueries.Lt("Gavin King", dc);
  			NHAssert.IsSerializable(c);
  
gt: 			c = Subqueries.LtAll("Gavin King", dc);