// Copyright (c) 2003-2014, Jodd Team (jodd.org). All Rights Reserved.

package jodd.madvoc.component;

import jodd.madvoc.result.ActionResult;
import jodd.madvoc.result.ServletRedirectResult;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

public class ResultsManagerTest {

	@Test
	public void testDuplicateResults1() {
		ResultsManager resultsManager = new ResultsManager() {
			@Override
			protected void initializeResult(ActionResult result) {
			}
		};

		resultsManager.register(new ServletRedirectResult());
		resultsManager.register(new ServletRedirectResult());
		resultsManager.register(new MyRedirect1());

		assertNull(resultsManager.lookup(ServletRedirectResult.class));
		assertEquals(MyRedirect1.class, resultsManager.lookup("redirect").getClass());

		resultsManager.register(new MyRedirect2());
		assertEquals(MyRedirect2.class, resultsManager.lookup("redirect").getClass());

		assertEquals(1, resultsManager.allResults.size());
	}

	@Test
	public void testDuplicateResults2() {
		ResultsManager resultsManager = new ResultsManager() {
			@Override
			protected void initializeResult(ActionResult result) {
			}
		};

		resultsManager.register(new MyRedirect1());
		resultsManager.register(new MyRedirect1());
		resultsManager.register(new ServletRedirectResult());

		assertNotNull(resultsManager.lookup(ServletRedirectResult.class));
		assertEquals(ServletRedirectResult.class, resultsManager.lookup("redirect").getClass());

		resultsManager.register(new MyRedirect2());
		assertEquals(MyRedirect2.class, resultsManager.lookup("redirect").getClass());

		assertEquals(1, resultsManager.allResults.size());
	}

	public static class MyRedirect1 extends ServletRedirectResult {
	}
	public static class MyRedirect2 extends ServletRedirectResult {
	}
}
@@ -259,54 +259,8 @@
 ls;%0A
-import static org.junit.Assert.assertNotNull;%0A
 impo
@@ -504,32 +504,296 @@
 t result) %7B%0A%09%09%09%7D
+%0A%0A%09%09%09@Override%0A%09%09%09protected boolean resultMayReplaceExistingOne(Class%3C? extends ActionResult%3E actionResultClass) %7B%0A%09%09%09%09if (actionResultClass.getName().contains(%22Test%22)) %7B%0A%09%09%09%09%09return true;%0A%09%09%09%09%7D%0A%09%09%09%09return super.resultMayReplaceExistingOne(actionResultClass);%0A%09%09%09%7D
 %0A%09%09%7D;%0A%0A%09%09results
@@ -830,32 +830,39 @@
 directResult());
+%09// new
 %0A%09%09resultsManage
@@ -897,24 +897,34 @@
 ctResult());
+%09// ignore
 %0A%09%09resultsMa
@@ -953,24 +953,38 @@
 edirect1());
+%09%09%09%09// replace
 %0A%0A%09%09assertNu
@@ -1158,32 +1158,46 @@
  MyRedirect2());
+%09%09%09%09// replace
 %0A%09%09assertEquals(
@@ -1495,24 +1495,287 @@
 ult) %7B%0A%09%09%09%7D%0A
+%09%09%09@Override%0A%09%09%09protected boolean resultMayReplaceExistingOne(Class%3C? extends ActionResult%3E actionResultClass) %7B%0A%09%09%09%09if (actionResultClass.getName().contains(%22Test%22)) %7B%0A%09%09%09%09%09return true;%0A%09%09%09%09%7D%0A%09%09%09%09return super.resultMayReplaceExistingOne(actionResultClass);%0A%09%09%09%7D%0A
 %09%09%7D;%0A%0A%09%09resu
@@ -1805,32 +1805,47 @@
  MyRedirect1());
+%09%09%09%09// register
 %0A%09%09resultsManage
@@ -1874,16 +1874,29 @@
 ect1());
+%09%09%09%09// ignore
 %0A%09%09resul
@@ -1939,24 +1939,34 @@
 ctResult());
+%09// ignore
 %0A%0A%09%09assertNo
@@ -1968,11 +1968,8 @@
 ertN
-otN
 ull(
@@ -2036,31 +2036,26 @@
 tEquals(
-Servlet
+My
 Redirect
 Result.c
@@ -2042,30 +2042,25 @@
 s(MyRedirect
-Result
+1
 .class, resu
@@ -2148,16 +2148,29 @@
 ect2());
+%09%09%09%09// ignore
 %0A%09%09asser

Rollback of changed functionality, just remove exceptions