Capture variables could be different in subresource methods

Fixes [WINK-344].

Thanks to Stefan Witte for reporting the issue.

git-svn-id: https://svn.apache.org/repos/asf/incubator/wink/trunk@1091468 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java b/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java
index d6f4d0c..85e854d 100644
--- a/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java
+++ b/wink-server/src/main/java/org/apache/wink/server/internal/handlers/FindResourceMethodHandler.java
@@ -159,7 +159,6 @@
         SearchResult result = context.getAttribute(SearchResult.class);
         ResourceInstance resource = result.getResource();
         SubResourceRecord subResourceRecord = subResourceInstance.getRecord();
-        UriTemplateMatcher matcher = subResourceInstance.getMatcher();
         String pattern = subResourceRecord.getTemplateProcessor().getPatternString();
         // dispatch to one of the sub-resource methods.
         SubResourceInstance method = null;
@@ -173,6 +172,7 @@
             return;
         }
 
+        UriTemplateMatcher matcher = method.getMatcher();
         saveFoundMethod(result, matcher, method, context);
 
         // continue the chain to invoke the method
diff --git a/wink-server/src/test/java/org/apache/wink/server/internal/DifferentCaptureVariablesTest.java b/wink-server/src/test/java/org/apache/wink/server/internal/DifferentCaptureVariablesTest.java
new file mode 100644
index 0000000..eb2d403
--- /dev/null
+++ b/wink-server/src/test/java/org/apache/wink/server/internal/DifferentCaptureVariablesTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *  
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.wink.server.internal;
+
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+
+import org.apache.wink.server.internal.servlet.MockServletInvocationTest;
+import org.apache.wink.test.mock.MockRequestConstructor;
+import org.springframework.mock.web.MockHttpServletResponse;
+
+/**
+ * Even if the regular expression used in a sub-resource method is the same
+ * (which it must be according to JAX-RS specification), the capture variable
+ * names can be different in 2 different HTTP methods. Need to use the proper
+ * capture variable names.
+ */
+public class DifferentCaptureVariablesTest extends MockServletInvocationTest {
+
+    @Override
+    protected Class<?>[] getClasses() {
+        return new Class[] {TestResource.class, TestResource2.class};
+    }
+
+    @Path("/test")
+    public static class TestResource {
+
+        @GET
+        @Path("{id1}")
+        public String getMethod(@PathParam("id1") String a) {
+            return a;
+        }
+
+        @POST
+        public String doSomething() {
+            return null;
+        }
+
+        @DELETE
+        @Path("{id3}")
+        public String deleteMethod(@PathParam("id3") String id) {
+            return id;
+        }
+    }
+
+    @Path("/test2")
+    public static class TestResource2 {
+
+        @GET
+        @Path("{id1}")
+        public String getMethod(@PathParam("id1") String id) {
+            return id;
+        }
+
+        @POST
+        @Path("{id2}/a")
+        public String doSomething(@PathParam("id2") String id) {
+            return id;
+        }
+
+        @DELETE
+        @Path("{id3}")
+        public String deleteMethod(@PathParam("id3") String id) {
+            return id;
+        }
+    }
+
+    public void testDifferentCaptureVariableNames() throws Exception {
+        MockHttpServletResponse resp =
+            invoke(MockRequestConstructor.constructMockRequest("GET", "/test/1234", "*/*"));
+        assertEquals("", "1234", resp.getContentAsString());
+
+        resp = invoke(MockRequestConstructor.constructMockRequest("DELETE", "/test/5678", "*/*"));
+        assertEquals("", "5678", resp.getContentAsString());
+    }
+
+    public void testDifferentCaptureVariableNames2() throws Exception {
+        MockHttpServletResponse resp =
+            invoke(MockRequestConstructor.constructMockRequest("DELETE", "/test2/1234", "*/*"));
+        assertEquals("", "1234", resp.getContentAsString());
+
+        resp =
+            invoke(MockRequestConstructor.constructMockRequest("POST", "/test2/9012/a", "*/*"));
+        assertEquals("", "9012", resp.getContentAsString());
+        
+        resp = invoke(MockRequestConstructor.constructMockRequest("GET", "/test/5678", "*/*"));
+        assertEquals("", "5678", resp.getContentAsString());
+    }
+
+}