From c030c089e74f2325171ee3d8800824afb2c9eee1 Mon Sep 17 00:00:00 2001
From: Julian Hochstetter <julian.hochstetter@mni.thm.de>
Date: Fri, 7 Sep 2012 14:21:12 +0200
Subject: [PATCH] #3827, add test base and first controller tests

---
 .../AbstractSpringContextTestBase.java        | 63 ++++++++++++++++
 src/test/java/de/thm/arsnova/WiringTest.java  | 42 +++++++++++
 .../controller/LoginControllerTest.java       | 72 +++++++++++++++++++
 .../controller/WelcomeControllerTest.java     | 55 ++++++++++++++
 4 files changed, 232 insertions(+)
 create mode 100644 src/test/java/de/thm/arsnova/AbstractSpringContextTestBase.java
 create mode 100644 src/test/java/de/thm/arsnova/WiringTest.java
 create mode 100644 src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
 create mode 100644 src/test/java/de/thm/arsnova/controller/WelcomeControllerTest.java

diff --git a/src/test/java/de/thm/arsnova/AbstractSpringContextTestBase.java b/src/test/java/de/thm/arsnova/AbstractSpringContextTestBase.java
new file mode 100644
index 000000000..5210facc4
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/AbstractSpringContextTestBase.java
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2012 THM webMedia
+ * 
+ * This file is part of ARSnova.
+ *
+ * ARSnova is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova 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, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova;
+
+import static org.junit.Assert.assertNotNull;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+import org.springframework.web.servlet.HandlerAdapter;
+import org.springframework.web.servlet.HandlerExecutionChain;
+import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.HandlerMapping;
+import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter;
+import org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping;
+
+public class AbstractSpringContextTestBase extends AbstractJUnit4SpringContextTests {
+	
+	protected MockHttpServletRequest request;
+	protected MockHttpServletResponse response;
+	
+	protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response)
+            throws Exception {
+        final HandlerMapping handlerMapping = applicationContext.getBean(DefaultAnnotationHandlerMapping.class);
+        final HandlerExecutionChain handler = handlerMapping.getHandler(request);
+        assertNotNull("No handler found for request, check you request mapping", handler);
+
+        final Object controller = handler.getHandler();
+        // if you want to override any injected attributes do it here
+
+        final HandlerInterceptor[] interceptors =
+            handlerMapping.getHandler(request).getInterceptors();
+        for (HandlerInterceptor interceptor : interceptors) {
+            final boolean carryOn = interceptor.preHandle(request, response, controller);
+            if (!carryOn) {
+                return null;
+            }
+        }
+        HandlerAdapter handlerAdapter = applicationContext.getBean(AnnotationMethodHandlerAdapter.class);;
+        final ModelAndView mav = handlerAdapter.handle(request, response, controller);
+        return mav;
+    }
+}
diff --git a/src/test/java/de/thm/arsnova/WiringTest.java b/src/test/java/de/thm/arsnova/WiringTest.java
new file mode 100644
index 000000000..f9e3a8b5a
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/WiringTest.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2012 THM webMedia
+ * 
+ * This file is part of ARSnova.
+ *
+ * ARSnova is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova 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, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova;
+
+
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+
+/**
+ * Unit test to verify Spring context wiring.
+ *
+ */
+@ContextConfiguration(locations="file:src/main/webapp/WEB-INF/arsnova-servlet.xml")
+@RunWith(SpringJUnit4ClassRunner.class)
+public class WiringTest extends AbstractJUnit4SpringContextTests {
+
+    @Test
+    public void testWiring() throws Exception {
+        assertTrue(applicationContext.getBeanDefinitionCount() > 0);
+    }
+}
\ No newline at end of file
diff --git a/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java b/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
new file mode 100644
index 000000000..f4e767044
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/controller/LoginControllerTest.java
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2012 THM webMedia
+ * 
+ * This file is part of ARSnova.
+ *
+ * ARSnova is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova 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, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.controller;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.web.servlet.ModelAndView;
+
+import de.thm.arsnova.AbstractSpringContextTestBase;
+
+@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/arsnova-servlet.xml")
+@RunWith(SpringJUnit4ClassRunner.class)
+public class LoginControllerTest extends AbstractSpringContextTestBase {
+
+	
+	@Before
+	public void setUp() throws Exception {
+		this.request = new MockHttpServletRequest();
+		this.response = new MockHttpServletResponse();
+	}
+	
+	@Test
+	public void testCasLogin() throws Exception {
+		request.setMethod("GET");
+        request.setRequestURI("/doCasLogin");
+
+        final ModelAndView mav = handle(request, response);
+        assertEquals(null, mav.getViewName());
+	}
+	
+	@Test
+	public void testGuestLogin() throws Exception {
+		request.setMethod("GET");
+        request.setRequestURI("/doGuestLogin");
+
+        final ModelAndView mav = handle(request, response);
+        
+        assertTrue(mav.getViewName().startsWith("redirect:/"));
+        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
+        assertEquals(auth.getClass(), UsernamePasswordAuthenticationToken.class);
+	}
+	
+	
+	
+}
diff --git a/src/test/java/de/thm/arsnova/controller/WelcomeControllerTest.java b/src/test/java/de/thm/arsnova/controller/WelcomeControllerTest.java
new file mode 100644
index 000000000..838d4c978
--- /dev/null
+++ b/src/test/java/de/thm/arsnova/controller/WelcomeControllerTest.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2012 THM webMedia
+ * 
+ * This file is part of ARSnova.
+ *
+ * ARSnova is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * ARSnova 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, see <http://www.gnu.org/licenses/>.
+ */
+package de.thm.arsnova.controller;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
+import org.springframework.web.servlet.ModelAndView;
+
+import de.thm.arsnova.AbstractSpringContextTestBase;
+
+@ContextConfiguration(locations = "file:src/main/webapp/WEB-INF/arsnova-servlet.xml")
+@RunWith(SpringJUnit4ClassRunner.class)
+public class WelcomeControllerTest extends AbstractSpringContextTestBase {
+
+	@Before
+	public void setUp() throws Exception {
+		this.request = new MockHttpServletRequest();
+		this.response = new MockHttpServletResponse();
+
+	}
+	
+	@Test
+	public void testIndexPage() throws Exception {
+		request.setMethod("GET");
+        request.setRequestURI("/");
+
+        final ModelAndView mav = handle(request, response);
+        assertEquals("redirect:/index.html", mav.getViewName());
+	}
+	
+	
+}
-- 
GitLab