Skip to content

Commit

Permalink
Move api.io package to styx-common module.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkokar committed Aug 6, 2018
1 parent f3a40cf commit 6564898
Show file tree
Hide file tree
Showing 39 changed files with 62 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;

import static com.google.common.base.Throwables.propagate;

/**
* A {@link com.hotels.styx.api.Resource} implementation for class path resources.
* Uses either a given ClassLoader or a given Class for loading resources.
Expand Down Expand Up @@ -75,7 +73,7 @@ public URL url() {
URL url = this.classLoader.getResource(this.path);

if (url == null) {
throw propagate(new FileNotFoundException(this.path));
throw new RuntimeException(new FileNotFoundException(this.path));
}

return url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.google.common.collect.Iterators;
import com.hotels.styx.api.Resource;
Expand All @@ -24,7 +24,6 @@
import java.util.Iterator;
import java.util.stream.Stream;

import static com.google.common.base.Throwables.propagate;
import static java.util.Spliterator.ORDERED;
import static java.util.Spliterators.spliteratorUnknownSize;
import static java.util.stream.Collectors.toList;
Expand Down Expand Up @@ -54,7 +53,7 @@ public Iterable<Resource> list(String path, String suffix) {
.collect(toList());

} catch (IOException e) {
throw propagate(e);
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

import java.net.URL;
import java.util.Iterator;
import java.util.ServiceLoader;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;


/**
Expand Down Expand Up @@ -49,7 +49,7 @@ public DelegatingResourceIteratorFactory() {
*/
public DelegatingResourceIteratorFactory(ResourceIteratorFactory fallback) {
this.delegates = ServiceLoader.load(ResourceIteratorFactory.class);
this.fallback = checkNotNull(fallback);
this.fallback = requireNonNull(fallback);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

Expand All @@ -24,8 +24,6 @@
import java.net.MalformedURLException;
import java.net.URL;

import static com.google.common.base.Throwables.propagate;

/**
* Resource implementation for {@link File} handles.
*/
Expand Down Expand Up @@ -64,7 +62,7 @@ public URL url() {
try {
return getFile().toURI().toURL();
} catch (MalformedURLException e) {
throw propagate(e);
throw new RuntimeException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

import java.io.File;

import static com.google.common.collect.Lists.newArrayList;
import java.util.ArrayList;

/**
* An index of file system resources.
*/
public class FileResourceIndex implements ResourceIndex {
@Override
public Iterable<Resource> list(String path, String suffix) {
return newArrayList(new FileResourceIterator(new File(path), new File(path), suffix));
ArrayList<Resource> list = new ArrayList<>();
new FileResourceIterator(new File(path), new File(path), suffix).forEachRemaining(list::add);
return list;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;
import com.hotels.styx.api.common.Strings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import java.io.File;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

import static com.hotels.styx.api.io.ClasspathResource.CLASSPATH_SCHEME;
import static com.hotels.styx.common.io.ClasspathResource.CLASSPATH_SCHEME;

/**
* Factory for creating resources (file, classpath) based on prefix.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

Expand All @@ -23,7 +23,7 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.requireNonNull;

/**
* A resource inside a JAR.
Expand All @@ -33,8 +33,8 @@ public class ZipResource implements Resource {
private final ZipEntry jarEntry;

public ZipResource(ZipFile jarFile, ZipEntry jarEntry) {
this.jarFile = checkNotNull(jarFile);
this.jarEntry = checkNotNull(jarEntry);
this.jarFile = requireNonNull(jarFile);
this.jarEntry = requireNonNull(jarEntry);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;
import org.testng.annotations.Test;

import static com.hotels.styx.api.io.ResourcePathMatcher.resourceWithPath;
import static com.hotels.styx.common.io.ResourcePathMatcher.resourceWithPath;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;

Expand All @@ -28,7 +28,7 @@ public void listsByPathAndSuffix() {
ClassLoader classLoader = ClasspathResourceIndexTest.class.getClassLoader();
ClasspathResourceIndex index = new ClasspathResourceIndex(classLoader);

Iterable<Resource> resources = index.list("com/hotels/styx/api/io", ".txt");
Iterable<Resource> resources = index.list("com/hotels/styx/common/io", ".txt");

assertThat(resources, containsInAnyOrder(
resourceWithPath("resource.txt"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
Expand All @@ -22,7 +22,7 @@
import java.net.MalformedURLException;
import java.net.URL;

import static com.hotels.styx.api.io.ResourceContentMatcher.contains;
import static com.hotels.styx.common.io.ResourceContentMatcher.contains;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

Expand All @@ -42,17 +42,17 @@ public void readsValidResourceFromClass(String path) throws MalformedURLExceptio
}

private static void assertThatResourceIsLoadedCorrectly(ClasspathResource resource) throws MalformedURLException {
assertThat(resource.path(), is("com/hotels/styx/api/io/resource.txt"));
assertThat(resource.absolutePath(), is(absolutePath("/com/hotels/styx/api/io/resource.txt")));
assertThat(resource.url(), is(new URL("file:" + absolutePath("/com/hotels/styx/api/io/resource.txt"))));
assertThat(resource.path(), is("com/hotels/styx/common/io/resource.txt"));
assertThat(resource.absolutePath(), is(absolutePath("/com/hotels/styx/common/io/resource.txt")));
assertThat(resource.url(), is(new URL("file:" + absolutePath("/com/hotels/styx/common/io/resource.txt"))));
assertThat(resource, contains("This is an example resource.\nIt has content to use in automated tests."));
}

@DataProvider(name = "validPaths")
private static Object[][] validPaths() {
return new Object[][]{
{"classpath:com/hotels/styx/api/io/resource.txt"},
{"classpath:/com/hotels/styx/api/io/resource.txt"},
{"classpath:com/hotels/styx/common/io/resource.txt"},
{"classpath:/com/hotels/styx/common/io/resource.txt"},
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;
import org.testng.annotations.Test;

import java.io.File;
import java.nio.file.Path;

import static com.hotels.styx.api.io.ResourcePathMatcher.resourceWithPath;
import static com.hotels.styx.common.io.ResourcePathMatcher.resourceWithPath;
import static com.hotels.styx.support.ResourcePaths.fixturesHome;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand All @@ -27,7 +27,7 @@

import static com.google.common.io.Files.createTempDir;
import static com.google.common.io.Files.write;
import static com.hotels.styx.api.io.ResourceContentMatcher.contains;
import static com.hotels.styx.common.io.ResourceContentMatcher.contains;
import static io.netty.util.CharsetUtil.UTF_8;
import static java.io.File.createTempFile;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.google.common.io.CharStreams;
import com.hotels.styx.api.Resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;
import org.testng.annotations.Test;

import static com.hotels.styx.api.io.ResourceContentMatcher.contains;
import static com.hotels.styx.common.io.ResourceContentMatcher.contains;
import static org.hamcrest.MatcherAssert.assertThat;

public class ResourceFactoryTest {
@Test
public void canAcquireClasspathResources() {
Resource resource = ResourceFactory.newResource("classpath:com/hotels/styx/api/io/resource.txt");
Resource resource = ResourceFactory.newResource("classpath:com/hotels/styx/common/io/resource.txt");

assertThat(resource, contains("This is an example resource.\nIt has content to use in automated tests."));
}

@Test
public void canAcquireFileResources() {
String filePath = ResourceFactoryTest.class.getResource("/com/hotels/styx/api/io/resource.txt").getPath();
String filePath = ResourceFactoryTest.class.getResource("/com/hotels/styx/common/io/resource.txt").getPath();

Resource resource = ResourceFactory.newResource(filePath);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.hotels.styx.api.io;
package com.hotels.styx.common.io;

import com.hotels.styx.api.Resource;
import org.hamcrest.Description;
Expand Down
Loading

0 comments on commit 6564898

Please sign in to comment.