package com.peterphi.std.guice.common.serviceprops.jaxbref;

import com.peterphi.std.guice.common.serviceprops.composite.GuiceConfig;
import com.peterphi.std.util.jaxb.JAXBSerialiserFactory;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class JAXBResourceFactoryTest
{
	JAXBSerialiserFactory SERIALISER_FACTORY = new JAXBSerialiserFactory(false);


	@Test
	public void testLiteralProperty() throws Exception
	{
		MyType expected = new MyType("test", true);

		GuiceConfig config = new GuiceConfig();
		config.set("some.xml", "<MyType name=\"test\"/>");

		JAXBResourceFactory factory = new JAXBResourceFactory(config, SERIALISER_FACTORY);

		assertEquals(expected, factory.get(MyType.class, "some.xml"));
	}


	/**
	 * Confirm that even if we have what looks like a variable in our XML it doesn't get processed and we get the raw data we
	 * expect in the xml doc
	 *
	 * @throws Exception
	 */
	@Test
	public void testLiteralPropertyWithInternalVariables() throws Exception
	{
		MyType expected = new MyType("${some.custom.ognl}", true);

		GuiceConfig config = new GuiceConfig();
		config.set("some.xml", "<MyType name=\"${some.custom.ognl}\"/>");

		JAXBResourceFactory factory = new JAXBResourceFactory(config, SERIALISER_FACTORY);

		assertEquals(expected, factory.get(MyType.class, "some.xml"));
	}


	/**
	 * N.B. this test wouldn't work if the XML contained something that looked like a variable (because when you include variables
	 * their values get resolved)
	 *
	 * @throws Exception
	 */
	@Test
	public void testVariableReferencingLiteralDocument() throws Exception
	{
		MyType expected = new MyType("test", true);

		GuiceConfig config = new GuiceConfig();
		config.set("some.xml", "${some.xml.incl}");
		config.set("some.xml.incl", "<MyType name=\"test\"/>");

		JAXBResourceFactory factory = new JAXBResourceFactory(config, SERIALISER_FACTORY);

		assertEquals(expected, factory.get(MyType.class, "some.xml"));
	}


	/**
	 * This will almost certainly be translated into a File resource
	 *
	 * @throws Exception
	 */
	@Test
	public void testResourceFromClasspathReference() throws Exception
	{
		MyType expected = new MyType("from-disk", true);

		GuiceConfig config = new GuiceConfig();
		config.set("some.xml", "/jaxb-resource-factory/mytype.xml");

		JAXBResourceFactory factory = new JAXBResourceFactory(config, SERIALISER_FACTORY);

		assertEquals(expected, factory.get(MyType.class, "some.xml"));
	}
}

@@ -252,16 +252,59 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertSame;
 %0A%0Apublic
@@ -769,32 +769,1099 @@
 me.xml%22));%0A%09%7D%0A%0A%0A
+%09@Test%0A%09public void testLiteralPropertyCaching() throws Exception%0A%09%7B%0A%09%09MyType expected = new MyType(%22test%22, true);%0A%0A%09%09GuiceConfig config = new GuiceConfig();%0A%09%09config.set(%22some.xml%22, %22%3CMyType name=%5C%22test%5C%22/%3E%22);%0A%0A%09%09JAXBResourceFactory factory = new JAXBResourceFactory(config, SERIALISER_FACTORY);%0A%0A%09%09final MyType a = factory.get(MyType.class, %22some.xml%22);%0A%09%09final MyType b = factory.get(MyType.class, %22some.xml%22);%0A%0A%09%09assertEquals(expected, a);%0A%09%09assertSame(%22cache should return same JAXB object ref when XML has not changed%22, a, b);%0A%09%7D%0A%0A%0A%09@Test%0A%09public void testChangingLiteralProperty() throws Exception%0A%09%7B%0A%09%09MyType first = new MyType(%22test1%22, true);%0A%09%09MyType second = new MyType(%22test2%22, true);%0A%0A%09%09GuiceConfig config = new GuiceConfig();%0A%09%09config.set(%22some.xml%22, %22%3CMyType name=%5C%22test1%5C%22/%3E%22);%0A%0A%09%09JAXBResourceFactory factory = new JAXBResourceFactory(config, SERIALISER_FACTORY);%0A%0A%09%09assertEquals(first, factory.get(MyType.class, %22some.xml%22));%0A%0A%09%09config.set(%22some.xml%22, %22%3CMyType name=%5C%22test2%5C%22/%3E%22);%0A%0A%09%09assertEquals(second, factory.get(MyType.class, %22some.xml%22));%0A%09%7D%0A%0A%0A
 %09/**%0A%09 * Confirm

Test caching of properties