Repository: cxf Updated Branches: refs/heads/master 762b011a8 -> 10db3b5ce
[CXF-6215] Adding a test Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/10db3b5c Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/10db3b5c Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/10db3b5c Branch: refs/heads/master Commit: 10db3b5ce912ad4bda2662764dde584d179e8e83 Parents: 762b011 Author: Sergey Beryozkin <[email protected]> Authored: Thu Jan 22 17:59:31 2015 +0000 Committer: Sergey Beryozkin <[email protected]> Committed: Thu Jan 22 17:59:31 2015 +0000 ---------------------------------------------------------------------- .../model/OperationResourceInfoComparator.java | 39 ++++++++++--- .../apache/cxf/jaxrs/DefaultMethodResource.java | 44 +++++++++++++++ .../cxf/jaxrs/SelectMethodCandidatesTest.java | 59 ++++++++++++++++++++ 3 files changed, 134 insertions(+), 8 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/10db3b5c/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfoComparator.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfoComparator.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfoComparator.java index 74073eb..5def5a2 100644 --- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfoComparator.java +++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/model/OperationResourceInfoComparator.java @@ -27,6 +27,7 @@ import javax.ws.rs.HttpMethod; import javax.ws.rs.core.MediaType; import org.apache.cxf.endpoint.Endpoint; +import org.apache.cxf.jaxrs.ext.DefaultMethod; import org.apache.cxf.jaxrs.ext.ResourceComparator; import org.apache.cxf.jaxrs.utils.JAXRSUtils; import org.apache.cxf.message.Message; @@ -63,28 +64,32 @@ public class OperationResourceInfoComparator implements Comparator<OperationReso } public int compare(OperationResourceInfo e1, OperationResourceInfo e2) { - + if (e1 == e2) { + return 0; + } if (rc != null) { int result = rc.compare(e1, e2, message); if (result != 0) { return result; } } + String e1HttpMethod = e1.getHttpMethod(); + String e2HttpMethod = e2.getHttpMethod(); + int result = 0; if (!getMethod && HttpMethod.HEAD.equals(httpMethod)) { - if (HttpMethod.HEAD.equals(e1.getHttpMethod())) { - return -1; - } else if (HttpMethod.HEAD.equals(e2.getHttpMethod())) { - return 1; + result = compareWithHead(e1HttpMethod, e2HttpMethod); + if (result != 0) { + return result; } } - int result = URITemplate.compareTemplates( + result = URITemplate.compareTemplates( e1.getURITemplate(), e2.getURITemplate()); - if (result == 0 && (e1.getHttpMethod() != null && e2.getHttpMethod() == null - || e1.getHttpMethod() == null && e2.getHttpMethod() != null)) { + if (result == 0 && (e1HttpMethod != null && e2HttpMethod == null + || e1HttpMethod == null && e2HttpMethod != null)) { // resource method takes precedence over a subresource locator return e1.getHttpMethod() != null ? -1 : 1; } @@ -103,7 +108,25 @@ public class OperationResourceInfoComparator implements Comparator<OperationReso acceptTypes); } + if (result == 0 && e1HttpMethod != null && e2HttpMethod != null) { + boolean e1IsDefault = DefaultMethod.class.getSimpleName().equals(e1HttpMethod); + boolean e2IsDefault = DefaultMethod.class.getSimpleName().equals(e2HttpMethod); + if (e1IsDefault && !e2IsDefault) { + result = 1; + } else if (!e1IsDefault && e2IsDefault) { + result = -1; + } + } + return result; } + private static int compareWithHead(String e1HttpMethod, String e2HttpMethod) { + if (HttpMethod.HEAD.equals(e1HttpMethod)) { + return -1; + } else if (HttpMethod.HEAD.equals(e2HttpMethod)) { + return 1; + } + return 0; + } } http://git-wip-us.apache.org/repos/asf/cxf/blob/10db3b5c/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/DefaultMethodResource.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/DefaultMethodResource.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/DefaultMethodResource.java new file mode 100644 index 0000000..156c8e2 --- /dev/null +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/DefaultMethodResource.java @@ -0,0 +1,44 @@ +/** + * 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.cxf.jaxrs; + +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.core.Response; + +import org.apache.cxf.jaxrs.ext.DefaultMethod; + +@Path("service") +public class DefaultMethodResource { + + @Path("all") + @DefaultMethod + public Response all() { + return null; + } + @Path("all") + @GET + public Response getAll() { + return null; + } + @GET + public Response get() { + return null; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/10db3b5c/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java ---------------------------------------------------------------------- diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java index d51cd83..c873454 100644 --- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java +++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/SelectMethodCandidatesTest.java @@ -217,6 +217,65 @@ public class SelectMethodCandidatesTest extends Assert { } @Test + public void testDefaultMethod() throws Exception { + JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean(); + sf.setResourceClasses(DefaultMethodResource.class); + sf.create(); + List<ClassResourceInfo> resources = ((JAXRSServiceImpl)sf.getService()).getClassResourceInfos(); + String contentType = "text/xml"; + String acceptContentTypes = "text/xml"; + + Message m = new MessageImpl(); + m.put(Message.CONTENT_TYPE, contentType); + Exchange ex = new ExchangeImpl(); + ex.setInMessage(m); + m.setExchange(ex); + Endpoint e = EasyMock.createMock(Endpoint.class); + e.isEmpty(); + EasyMock.expectLastCall().andReturn(true).anyTimes(); + e.size(); + EasyMock.expectLastCall().andReturn(0).anyTimes(); + e.getEndpointInfo(); + EasyMock.expectLastCall().andReturn(null).anyTimes(); + e.get(ServerProviderFactory.class.getName()); + EasyMock.expectLastCall().andReturn(ServerProviderFactory.getInstance()).times(3); + e.get("org.apache.cxf.jaxrs.comparator"); + EasyMock.expectLastCall().andReturn(null); + EasyMock.replay(e); + ex.put(Endpoint.class, e); + + MetadataMap<String, String> values = new MetadataMap<String, String>(); + OperationResourceInfo ori = findTargetResourceClass(resources, m, + "/service/all", + "PUT", + values, + contentType, + sortMediaTypes(acceptContentTypes)); + assertNotNull(ori); + assertEquals("resourceMethod needs to be selected", "all", + ori.getMethodToInvoke().getName()); + values.clear(); + ori = findTargetResourceClass(resources, m, + "/service/all", + "GET", + values, + contentType, + sortMediaTypes(acceptContentTypes)); + assertNotNull(ori); + assertEquals("resourceMethod needs to be selected", "getAll", + ori.getMethodToInvoke().getName()); + ori = findTargetResourceClass(resources, m, + "/service", + "GET", + values, + contentType, + sortMediaTypes(acceptContentTypes)); + assertNotNull(ori); + assertEquals("resourceMethod needs to be selected", "get", + ori.getMethodToInvoke().getName()); + } + + @Test public void testConsumesResource1() throws Exception { doTestConsumesResource(ConsumesResource1.class, "text/xml", "m2"); }
