/*
 */

package com.googlecode.objectify.test;

import java.util.logging.Logger;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.AlsoLoad;
import com.googlecode.objectify.annotation.Cache;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.test.util.TestBase;
import com.googlecode.objectify.test.util.TestObjectify;

/**
 * More tests of using the @AlsoLoad annotation
 * 
 * @author Jeff Schnitzer <jeff@infohazard.org>
 */
public class AlsoLoadMoreTests extends TestBase
{
	/** */
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(AlsoLoadMoreTests.class.getName());
	
	/** */
	public static final String TEST_VALUE = "blah";
	
	/** */
	@Entity
	@Cache
	static class MethodOverridesField
	{
		@Id Long id;
		String foo;
		String bar;
		public void set(@AlsoLoad("foo") String overrides)
		{
			this.bar = overrides;
		}
	}
	
	/** Should not be registerable */
	@Entity
	static class ConflictingFields
	{
		@Id Long id;
		String foo;
		@AlsoLoad("foo") String bar;
	}
	
	/** Should not be registerable */
	@Entity
	static class ConflictingMethods
	{
		@Id Long id;
		public void set1(@AlsoLoad("foo") String foo1) {}
		public void set2(@AlsoLoad("foo") String foo2) {}
	}
	
	/**
	 * Add an entry to the database that should never come back from null queries.
	 */
	@BeforeMethod
	public void setUp()
	{
		super.setUp();
		
		this.fact.register(MethodOverridesField.class);
	}

	/** */
	@Test
	public void testMethodOverridingField() throws Exception
	{
		TestObjectify ofy = this.fact.begin();
		
		com.google.appengine.api.datastore.Entity ent = new com.google.appengine.api.datastore.Entity(Key.getKind(MethodOverridesField.class));
		ent.setProperty("foo", TEST_VALUE);
		ds().put(ent);
		
		Key<MethodOverridesField> key = Key.create(ent.getKey());
		MethodOverridesField fetched = ofy.load().entity(key).get();
		
		assert fetched.foo == null;
		assert fetched.bar.equals(TEST_VALUE);
	}
	
	/** */
	@Test
	public void testNotRegisterable() throws Exception
	{
		try
		{
			this.fact.register(ConflictingFields.class);
			assert false;
		}
		catch (IllegalStateException ex) {}
		
		try
		{
			this.fact.register(ConflictingMethods.class);
			assert false;
		}
		catch (IllegalStateException ex) {}
	}
}
@@ -1089,374 +1089,8 @@
 %0A%09%0D%0A
-%09/** Should not be registerable */%0D%0A%09@Entity%0D%0A%09static class ConflictingFields%0D%0A%09%7B%0D%0A%09%09@Id Long id;%0D%0A%09%09String foo;%0D%0A%09%09@AlsoLoad(%22foo%22) String bar;%0D%0A%09%7D%0D%0A%09%0D%0A%09/** Should not be registerable */%0D%0A%09@Entity%0D%0A%09static class ConflictingMethods%0D%0A%09%7B%0D%0A%09%09@Id Long id;%0D%0A%09%09public void set1(@AlsoLoad(%22foo%22) String foo1) %7B%7D%0D%0A%09%09public void set2(@AlsoLoad(%22foo%22) String foo2) %7B%7D%0D%0A%09%7D%0D%0A%09%0D%0A
 %09/**
@@ -1835,337 +1835,5 @@
 %09%7D%0D%0A
-%09%0D%0A%09/** */%0D%0A%09@Test%0D%0A%09public void testNotRegisterable() throws Exception%0D%0A%09%7B%0D%0A%09%09try%0D%0A%09%09%7B%0D%0A%09%09%09this.fact.register(ConflictingFields.class);%0D%0A%09%09%09assert false;%0D%0A%09%09%7D%0D%0A%09%09catch (IllegalStateException ex) %7B%7D%0D%0A%09%09%0D%0A%09%09try%0D%0A%09%09%7B%0D%0A%09%09%09this.fact.register(ConflictingMethods.class);%0D%0A%09%09%09assert false;%0D%0A%09%09%7D%0D%0A%09%09catch (IllegalStateException ex) %7B%7D%0D%0A%09%7D%0D%0A
 %7D

Test change: You are allowed to register conflicting fields, they work now.