From c5a4e8c463e9928fa235e3cd7217447f67a71a0b Mon Sep 17 00:00:00 2001 From: kohsuke Date: Wed, 31 Dec 2008 23:58:43 +0000 Subject: [PATCH] Prohibit creating two views with the same name. http://d.hatena.ne.jp/ssogabe/20090101/1230744150 git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@14067 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/model/View.java | 5 ++- test/pom.xml | 2 +- test/src/test/java/hudson/model/ViewTest.java | 33 +++++++++++++++++++ 3 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 test/src/test/java/hudson/model/ViewTest.java diff --git a/core/src/main/java/hudson/model/View.java b/core/src/main/java/hudson/model/View.java index 5ab721a58f..e2c44bc773 100644 --- a/core/src/main/java/hudson/model/View.java +++ b/core/src/main/java/hudson/model/View.java @@ -531,7 +531,10 @@ public abstract class View extends AbstractModelObject implements AccessControll public static View create(StaplerRequest req, StaplerResponse rsp, ViewGroup owner) throws ParseException, FormException, IOException, ServletException { req.setCharacterEncoding("UTF-8"); - checkGoodName(req.getParameter("name")); + String name = req.getParameter("name"); + checkGoodName(name); + if(owner.getView(name)!=null) + throw new FormException(Messages.Hudson_ViewAlreadyExists(name),"name"); // create a view View v = LIST.findByName(req.getParameter("mode")).newInstance(req,req.getSubmittedForm()); diff --git a/test/pom.xml b/test/pom.xml index 9e3cbf38a8..7485595a3b 100644 --- a/test/pom.xml +++ b/test/pom.xml @@ -72,7 +72,7 @@ org.jvnet.hudson htmlunit - 2.2-hudson-8 + 2.2-hudson-9 diff --git a/test/src/test/java/hudson/model/ViewTest.java b/test/src/test/java/hudson/model/ViewTest.java new file mode 100644 index 0000000000..bc1da96904 --- /dev/null +++ b/test/src/test/java/hudson/model/ViewTest.java @@ -0,0 +1,33 @@ +package hudson.model; + +import org.jvnet.hudson.test.Email; +import org.jvnet.hudson.test.HudsonTestCase; +import com.gargoylesoftware.htmlunit.html.HtmlForm; +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; + +/** + * @author Kohsuke Kawaguchi + */ +public class ViewTest extends HudsonTestCase { + /** + * Creating two views with the same name. + */ + @Email("http://d.hatena.ne.jp/ssogabe/20090101/1230744150") + public void testConflictingName() throws Exception { + assertNull(hudson.getView("foo")); + + HtmlForm form = new WebClient().goTo("newView").getFormByName("createView"); + form.getInputByName("name").setValueAttribute("foo"); + form.getRadioButtonsByName("mode").get(0).setChecked(true); + submit(form); + assertNotNull(hudson.getView("foo")); + + // do it again and verify an error + try { + submit(form); + fail("shouldn't be allowed to create two views of the same name."); + } catch (FailingHttpStatusCodeException e) { + assertEquals(400,e.getStatusCode()); + } + } +}