/* Copyright (C) 2004 - 2005  db4objects Inc.  http://www.db4o.com

This file is part of the db4o open source object database.

db4o is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */
using System;
using System.IO;
using System.Reflection;
using com.db4o;
using com.db4o.inside.query;
using com.db4o.query;
using com.db4o.ext;

namespace com.db4o.test.nativequeries
{
	public class Author
	{
		private int _id;
		private string _name;

		public int Id
		{
			get { return _id; }
		}

		public string Name
		{
			get { return _name; }
		}

		public Author(int id, string name)
		{
			_id = id;
			_name = name;
		}
	}

	public class MultipleAssemblySupportTestCase
	{	
		public class InnerAuthor : Author
		{
			public InnerAuthor(int id, string name) : base(id, name)
			{
			}
		}

		public MultipleAssemblySupportTestCase()
		{
		}

		public void store()
		{            
			Tester.store(new Author(1, "Kurt Vonnegut"));
			Tester.store(new Author(2, "Kilgore Trout"));
			Tester.store(new InnerAuthor(3, "Joao Saramago"));
			Tester.store(new InnerAuthor(4, "Douglas Adams"));
		}

		public void testPredicateAccessingTopLevelType()
		{
			string predicateCode = @"
using com.db4o.query;
using com.db4o.test.nativequeries;

public class AuthorNamePredicate : Predicate
{
	public bool Match(Author candidate)
	{
		return candidate.Name == ""Kilgore Trout"";
	}
}
";
			AssertPredicate(2, predicateCode, "AuthorNamePredicate");
		}

		public void testPredicateAccessingNestedType()
		{
			string predicateCode = @"
using com.db4o.query;
using com.db4o.test.nativequeries;

public class InnerAuthorNamePredicate : Predicate
{
	public bool Match(MultipleAssemblySupportTestCase.InnerAuthor candidate)
	{
		return candidate.Name == ""Joao Saramago"" && candidate.Id > 1;
	}
}
";
			AssertPredicate(3, predicateCode, "InnerAuthorNamePredicate");
		}

		private void AssertPredicate(int expectedId, string predicateCode, string predicateTypeName)
		{
            // use a prefix to avoid conflicts between SOLO and C/S tests run
            string assemblyNamePrefix = Tester.isClientServer() ? "CS" : "";
			Assembly assembly = EmitAssemblyAndLoad(assemblyNamePrefix + predicateTypeName + ".dll", predicateCode);
	
			Predicate predicate = (Predicate)Activator.CreateInstance(assembly.GetType(predicateTypeName));
	
			ExtObjectContainer container = Tester.objectContainer();
            container.configure().optimizeNativeQueries(true);

			NativeQueryHandler handler = GetNativeQueryHandler(container);
            handler.QueryExecution += new QueryExecutionHandler(OnQueryExecution);
			try
			{
				ObjectSet os = container.query(predicate);
				Tester.ensureEquals(1, os.size());
				Tester.ensureEquals(expectedId, ((Author)os.next()).Id);
			}
			finally
			{
                handler.QueryExecution -= new QueryExecutionHandler(OnQueryExecution);
			}
		}
		
		private static NativeQueryHandler GetNativeQueryHandler(ObjectContainer container)
		{
			return ((YapStream)container).getNativeQueryHandler();
		}

		private static Assembly EmitAssemblyAndLoad(string assemblyName, string code)
		{	
			string assemblyFile = Path.Combine(Path.GetTempPath(), assemblyName);
			CompilationServices.EmitAssembly(assemblyFile, code);
			return System.Reflection.Assembly.LoadFrom(assemblyFile);
		}

		private void OnQueryExecution(object sender, QueryExecutionEventArgs args)
		{
			Tester.ensureEquals(QueryExecutionKind.DynamicallyOptimized, args.ExecutionKind);
		}
	}
}
