Added version support in RestClient. Revised CallPath to extract version number specified in the path.

Moved version tests in CallPathProviderTest to CallPathTest.
This commit is contained in:
Inderjeet Singh 2010-11-03 16:16:17 +00:00
parent 83539c534c
commit 44edfcfb33
4 changed files with 43 additions and 28 deletions

View File

@ -51,7 +51,9 @@ public class RestClient {
}
private URL getWebServiceUrl(RestCallSpec callSpec) {
String url = config.getServiceBaseUrl() + callSpec.getPath().get();
double version = callSpec.getVersion();
String versionPath = version == -1 ? "" : "/" + version;
String url = config.getServiceBaseUrl() + versionPath + callSpec.getPath().get();
try {
return new URL(url);
} catch (MalformedURLException e) {

View File

@ -23,15 +23,38 @@ package com.google.gson.webservice.definition;
public final class CallPath {
private final String path;
private final double version;
public CallPath(String path) {
this.path = path;
if (path == null) {
this.path = null;
version = -1D;
} else {
int index1 = path.indexOf('/');
int index2 = path.substring(index1+1).indexOf('/');
String versionStr = path.substring(index1+1, index2+1);
String callPathStr = path;
double givenVersion = -1D;
try {
// Skip over the version number from the URL
givenVersion = Double.parseDouble(versionStr);
callPathStr = path.substring(index2+1);
} catch (NumberFormatException e) {
// Assume that version number wasn't specified
}
this.path = callPathStr;
this.version = givenVersion;
}
}
public String get() {
return path;
}
public double getVersion() {
return version;
}
@Override
public int hashCode() {
return path.hashCode();
@ -45,9 +68,13 @@ public final class CallPath {
if (obj == null) {
return false;
}
return getClass() == obj.getClass() && path == ((CallPath)obj).path;
return getClass() == obj.getClass() && equal(path, ((CallPath)obj).path);
}
private static boolean equal(String s1, String s2) {
return s1 == s2 || (s1 != null && s2 != null && s1.equals(s2));
}
@Override
public String toString() {
return path;

View File

@ -13,24 +13,21 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.gson.wsf.inject;
package com.google.gson.webservice.definition;
import junit.framework.TestCase;
/**
* Unit test for {@link CallPathProvider}
*
* @author Inderjeet Singh
*/
public class CallPathProviderTest extends TestCase {
public class CallPathTest extends TestCase {
public void testVersionIsSkipped() {
CallPathProvider provider = new CallPathProvider("/1.0/rest/service1");
assertEquals("/rest/service1", provider.get().get());
CallPath path = new CallPath("/1.0/rest/service1");
assertEquals("/rest/service1", path.get());
assertEquals(1D, path.getVersion());
}
public void testVersionNotPresent() {
CallPathProvider provider = new CallPathProvider("/rest/service1");
assertEquals("/rest/service1", provider.get().get());
CallPath path = new CallPath("/rest/service1");
assertEquals("/rest/service1", path.get());
assertEquals(-1D, path.getVersion());
}
}

View File

@ -35,18 +35,7 @@ public final class CallPathProvider implements Provider<CallPath> {
}
public CallPathProvider(String pathInfo) {
int index1 = pathInfo.indexOf('/');
int index2 = pathInfo.substring(index1+1).indexOf('/');
String versionStr = pathInfo.substring(index1+1, index2+1);
String callPathStr = pathInfo;
try {
// Skip over the version number from the URL
Double.parseDouble(versionStr);
callPathStr = pathInfo.substring(index2+1);
} catch (NumberFormatException e) {
// Assume that version number wasn't specified
}
this.callPath = new CallPath(callPathStr);
this.callPath = new CallPath(pathInfo);
}
@Override