mirror of https://github.com/apache/jclouds.git
Issue 695: Added URISource and Function to get it. Added URISource to BaseResource
This commit is contained in:
parent
5dc2f65636
commit
84b5cfc8dc
|
@ -18,6 +18,8 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.tmrk.enterprisecloud.domain.internal;
|
package org.jclouds.tmrk.enterprisecloud.domain.internal;
|
||||||
|
|
||||||
|
import org.jclouds.tmrk.enterprisecloud.functions.URISource;
|
||||||
|
|
||||||
import javax.xml.bind.annotation.XmlAttribute;
|
import javax.xml.bind.annotation.XmlAttribute;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
@ -30,7 +32,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class BaseResource<T extends BaseResource<T>> {
|
public class BaseResource<T extends BaseResource<T>> implements URISource {
|
||||||
|
|
||||||
public static <T extends BaseResource<T>> Builder<T> builder() {
|
public static <T extends BaseResource<T>> Builder<T> builder() {
|
||||||
return new Builder<T>();
|
return new Builder<T>();
|
||||||
|
@ -106,6 +108,13 @@ public class BaseResource<T extends BaseResource<T>> {
|
||||||
return href;
|
return href;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see #getHref
|
||||||
|
*/
|
||||||
|
public URI getURI() {
|
||||||
|
return getHref();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean equals(Object o) {
|
public boolean equals(Object o) {
|
||||||
if (this == o) return true;
|
if (this == o) return true;
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
package org.jclouds.tmrk.enterprisecloud.functions;
|
||||||
|
|
||||||
|
import com.google.common.base.Function;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static com.google.common.base.Preconditions.checkNotNull;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Defines the URI Source interface and a function that uses it to obtain the URI
|
||||||
|
* @author Jason King
|
||||||
|
*/
|
||||||
|
public interface URISource {
|
||||||
|
|
||||||
|
URI getURI();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a URI Source to a URI
|
||||||
|
*/
|
||||||
|
public class GetURI implements Function<Object, URI> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expects to be called with an object implementing the URISource interface
|
||||||
|
* Calls the getURI method to obtain the URI.
|
||||||
|
* @param source a URISource
|
||||||
|
* @return the URI
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public URI apply(Object source) {
|
||||||
|
checkNotNull(source,"source");
|
||||||
|
URISource uriSource = URISource.class.cast(source);
|
||||||
|
return uriSource.getURI();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds 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.jclouds.tmrk.enterprisecloud.domain.internal;
|
||||||
|
|
||||||
|
import org.jclouds.tmrk.enterprisecloud.domain.NamedResource;
|
||||||
|
import org.jclouds.tmrk.enterprisecloud.functions.URISource;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.net.URISyntaxException;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jason King
|
||||||
|
*/
|
||||||
|
@Test(groups = "unit", testName = "BaseResourceTest")
|
||||||
|
public class NamedResourceTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests getURI on BaseResource via NamedResource subclass
|
||||||
|
*/
|
||||||
|
public void testGetURI() throws URISyntaxException {
|
||||||
|
URI uri = URI.create("/dev/null");
|
||||||
|
URISource source = NamedResource.builder().href(uri).build();
|
||||||
|
assertEquals(uri,source.getURI());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,62 @@
|
||||||
|
/**
|
||||||
|
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||||
|
* contributor license agreements. See the NOTICE file
|
||||||
|
* distributed with this work for additional information
|
||||||
|
* regarding copyright ownership. jclouds 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.jclouds.tmrk.enterprisecloud.functions;
|
||||||
|
|
||||||
|
import org.testng.annotations.BeforeMethod;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Jason King
|
||||||
|
*/
|
||||||
|
@Test(groups = "unit", testName = "GetURITest")
|
||||||
|
public class GetURITest {
|
||||||
|
|
||||||
|
private URISource.GetURI function;
|
||||||
|
|
||||||
|
@BeforeMethod
|
||||||
|
public void setUp() {
|
||||||
|
function = new URISource.GetURI();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testApply() {
|
||||||
|
URI expected = URI.create("/dev/null");
|
||||||
|
URISource source = new TestURISource(expected);
|
||||||
|
URI result = function.apply(source);
|
||||||
|
assertEquals(result,expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class TestURISource implements URISource {
|
||||||
|
|
||||||
|
private URI expected;
|
||||||
|
|
||||||
|
public TestURISource(URI expected) {
|
||||||
|
this.expected = expected;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URI getURI() {
|
||||||
|
return expected;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue