This is an automated email from the ASF dual-hosted git repository. slachiewicz pushed a commit to branch MINDEXER-124 in repository https://gitbox.apache.org/repos/asf/maven-indexer.git
commit 4113b925eeb94d9360a5be21b98b0d680479fa43 Author: sixcorners <sixcorn...@gmail.com> AuthorDate: Sun Feb 2 09:38:15 2020 -0600 [MINDEXER-124] Add ResourceHandler implementations --- indexer-reader/pom.xml | 5 ++ .../index/reader/resource/BufferedResource.java | 47 +++++++++++++ .../reader/resource/BufferedResourceHandler.java | 48 ++++++++++++++ .../reader/resource/BufferedWritableResource.java | 53 +++++++++++++++ .../resource/BufferedWritableResourceHandler.java | 49 ++++++++++++++ .../reader/resource/PathWritableResource.java | 58 ++++++++++++++++ .../resource/PathWritableResourceHandler.java | 45 +++++++++++++ .../index/reader/resource/UriResourceHandler.java | 45 +++++++++++++ .../maven/index/reader/resource/UrlResource.java | 48 ++++++++++++++ .../resource/BufferedResourceHandlerTest.java | 77 ++++++++++++++++++++++ .../BufferedWritableResourceHandlerTest.java | 71 ++++++++++++++++++++ .../resource/BufferedWritableResourceTest.java | 40 +++++++++++ .../resource/PathWritableResourceHandlerTest.java | 53 +++++++++++++++ .../reader/resource/UriResourceHandlerTest.java | 47 +++++++++++++ 14 files changed, 686 insertions(+) diff --git a/indexer-reader/pom.xml b/indexer-reader/pom.xml index 11f1f5b..6818397 100644 --- a/indexer-reader/pom.xml +++ b/indexer-reader/pom.xml @@ -44,6 +44,11 @@ under the License. <scope>test</scope> </dependency> <dependency> + <groupId>org.jmock</groupId> + <artifactId>jmock</artifactId> + <scope>test</scope> + </dependency> + <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <scope>test</scope> diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedResource.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedResource.java new file mode 100644 index 0000000..fa51ef4 --- /dev/null +++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedResource.java @@ -0,0 +1,47 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Objects; +import org.apache.maven.index.reader.ResourceHandler.Resource; + +/** + * Wraps {@link Resource}s so that they return {@link BufferedInputStream}s. + */ +public class BufferedResource implements Resource { + private final Resource resource; + + public BufferedResource(Resource resource) { + Objects.requireNonNull(resource, "resource cannot be null"); + this.resource = resource; + } + + @Override + public InputStream read() throws IOException { + InputStream in = resource.read(); + if (in == null) { + return null; + } + return new BufferedInputStream(in); + } +} diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedResourceHandler.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedResourceHandler.java new file mode 100644 index 0000000..b50c6bf --- /dev/null +++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedResourceHandler.java @@ -0,0 +1,48 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.util.Objects; +import org.apache.maven.index.reader.ResourceHandler; + +/** + * Wraps {@link ResourceHandler}s so that they return {@link Resource}s that return {@link + * BufferedInputStream}s. + */ +public class BufferedResourceHandler implements ResourceHandler { + private final ResourceHandler resourceHandler; + + public BufferedResourceHandler(ResourceHandler resourceHandler) { + Objects.requireNonNull(resourceHandler, "resourceHandler cannot be null"); + this.resourceHandler = resourceHandler; + } + + @Override + public Resource locate(String name) throws IOException { + return new BufferedResource(resourceHandler.locate(name)); + } + + @Override + public void close() throws IOException { + resourceHandler.close(); + } +} diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedWritableResource.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedWritableResource.java new file mode 100644 index 0000000..54d9cdf --- /dev/null +++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedWritableResource.java @@ -0,0 +1,53 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import org.apache.maven.index.reader.WritableResourceHandler.WritableResource; + +/** + * Wraps {@link WritableResource}s so that they return {@link BufferedInputStream}s and {@link + * BufferedOutputStream}s. + */ +public class BufferedWritableResource extends BufferedResource implements WritableResource { + private final WritableResource resource; + + public BufferedWritableResource(WritableResource resource) { + super(resource); + this.resource = resource; + } + + @Override + public OutputStream write() throws IOException { + OutputStream out = resource.write(); + if (out == null) { + return null; + } + return new BufferedOutputStream(out); + } + + @Override + public void close() throws IOException { + resource.close(); + } +} diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedWritableResourceHandler.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedWritableResourceHandler.java new file mode 100644 index 0000000..00a1062 --- /dev/null +++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/BufferedWritableResourceHandler.java @@ -0,0 +1,49 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.IOException; +import java.util.Objects; +import org.apache.maven.index.reader.WritableResourceHandler; + +/** + * Wraps {@link WritableResourceHandler}s so that they return {@link WritableResource}s that return + * {@link BufferedInputStream}s and {@link BufferedOutputStream}s. + */ +public class BufferedWritableResourceHandler implements WritableResourceHandler { + private final WritableResourceHandler writableResourceHandler; + + public BufferedWritableResourceHandler(WritableResourceHandler writableResourceHandler) { + Objects.requireNonNull(writableResourceHandler, "writableResourceHandler cannot be null"); + this.writableResourceHandler = writableResourceHandler; + } + + @Override + public WritableResource locate(String name) throws IOException { + return new BufferedWritableResource(writableResourceHandler.locate(name)); + } + + @Override + public void close() throws IOException { + writableResourceHandler.close(); + } +} diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/PathWritableResource.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/PathWritableResource.java new file mode 100644 index 0000000..2105b38 --- /dev/null +++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/PathWritableResource.java @@ -0,0 +1,58 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import java.nio.file.NoSuchFileException; +import java.nio.file.Path; +import java.util.Objects; +import org.apache.maven.index.reader.WritableResourceHandler.WritableResource; + +/** + * A {@link WritableResource} that represents a {@link Path}. + */ +public class PathWritableResource implements WritableResource { + private final Path path; + + public PathWritableResource(Path path) { + Objects.requireNonNull(path, "path cannot be null"); + this.path = path; + } + + @Override + public OutputStream write() throws IOException { + return Files.newOutputStream(path); + } + + @Override + public InputStream read() throws IOException { + try { + return Files.newInputStream(path); + } catch (NoSuchFileException e) { + return null; + } + } + + @Override + public void close() throws IOException {} +} diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/PathWritableResourceHandler.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/PathWritableResourceHandler.java new file mode 100644 index 0000000..82dd957 --- /dev/null +++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/PathWritableResourceHandler.java @@ -0,0 +1,45 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Objects; +import org.apache.maven.index.reader.WritableResourceHandler; + +/** + * A {@link WritableResourceHandler} that represents the base of a {@link Path} hierarchy. + */ +public class PathWritableResourceHandler implements WritableResourceHandler { + private final Path path; + + public PathWritableResourceHandler(Path path) { + Objects.requireNonNull(path, "path cannot be null"); + this.path = path; + } + + @Override + public WritableResource locate(String name) { + return new PathWritableResource(path.resolve(name)); + } + + @Override + public void close() throws IOException {} +} diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/UriResourceHandler.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/UriResourceHandler.java new file mode 100644 index 0000000..304dab1 --- /dev/null +++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/UriResourceHandler.java @@ -0,0 +1,45 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.IOException; +import java.net.URI; +import java.util.Objects; +import org.apache.maven.index.reader.ResourceHandler; + +/** + * A {@link ResourceHandler} that represents the base of a {@link URI} hierarchy. + */ +public class UriResourceHandler implements ResourceHandler { + private final URI uri; + + public UriResourceHandler(URI uri) { + Objects.requireNonNull(uri, "uri cannot be null"); + this.uri = uri; + } + + @Override + public Resource locate(String name) throws IOException { + return new UrlResource(uri.resolve(name).toURL()); + } + + @Override + public void close() throws IOException {} +} diff --git a/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/UrlResource.java b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/UrlResource.java new file mode 100644 index 0000000..388f417 --- /dev/null +++ b/indexer-reader/src/main/java/org/apache/maven/index/reader/resource/UrlResource.java @@ -0,0 +1,48 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Objects; +import org.apache.maven.index.reader.ResourceHandler.Resource; + +/** + * A {@link Resource} that represents a {@link URL}. + */ +public class UrlResource implements Resource { + private final URL url; + + public UrlResource(URL url) { + Objects.requireNonNull(url, "url cannot be null"); + this.url = url; + } + + @Override + public InputStream read() throws IOException { + try { + return url.openStream(); + } catch (FileNotFoundException e) { + return null; + } + } +} diff --git a/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedResourceHandlerTest.java b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedResourceHandlerTest.java new file mode 100644 index 0000000..61ec7eb --- /dev/null +++ b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedResourceHandlerTest.java @@ -0,0 +1,77 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import org.apache.maven.index.reader.ResourceHandler; +import org.apache.maven.index.reader.ResourceHandler.Resource; +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.junit.Test; + +public class BufferedResourceHandlerTest { + private Mockery context = new Mockery(); + + @Test + public void locate() throws IOException { + final Resource resource = context.mock(Resource.class); + final ResourceHandler resourceHandler = context.mock(ResourceHandler.class); + context.checking(new Expectations() {{ + oneOf(resource).read(); + will(returnValue(new ByteArrayInputStream(new byte[]{'a'}))); + oneOf(resourceHandler).locate("test.txt"); + will(returnValue(resource)); + }}); + InputStream in = new BufferedResourceHandler(resourceHandler).locate("test.txt").read(); + assertTrue(in instanceof BufferedInputStream); + assertEquals('a', in.read()); + context.assertIsSatisfied(); + } + + @Test + public void locateNull() throws IOException { + final Resource resource = context.mock(Resource.class); + final ResourceHandler resourceHandler = context.mock(ResourceHandler.class); + context.checking(new Expectations() {{ + oneOf(resource).read(); + oneOf(resourceHandler).locate("test.txt"); + will(returnValue(resource)); + }}); + assertNull(new BufferedResourceHandler(resourceHandler).locate("test.txt").read()); + context.assertIsSatisfied(); + } + + @Test + public void close() throws IOException { + final ResourceHandler resourceHandler = context.mock(ResourceHandler.class); + context.checking(new Expectations() {{ + oneOf(resourceHandler).close(); + }}); + new BufferedResourceHandler(resourceHandler).close(); + context.assertIsSatisfied(); + } +} diff --git a/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedWritableResourceHandlerTest.java b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedWritableResourceHandlerTest.java new file mode 100644 index 0000000..85e8fd4 --- /dev/null +++ b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedWritableResourceHandlerTest.java @@ -0,0 +1,71 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +import java.io.BufferedOutputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import org.apache.maven.index.reader.WritableResourceHandler; +import org.apache.maven.index.reader.WritableResourceHandler.WritableResource; +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.junit.Test; + +public class BufferedWritableResourceHandlerTest { + private Mockery context = new Mockery(); + + @Test + public void locate() throws IOException { + final WritableResource writableResource = context.mock(WritableResource.class); + final WritableResourceHandler writableResourceHandler = context + .mock(WritableResourceHandler.class); + final ByteArrayOutputStream baos = new ByteArrayOutputStream(); + context.checking(new Expectations() {{ + oneOf(writableResource).write(); + will(returnValue(baos)); + oneOf(writableResourceHandler).locate("test.txt"); + will(returnValue(writableResource)); + }}); + OutputStream out = new BufferedWritableResourceHandler(writableResourceHandler) + .locate("test.txt").write(); + assertTrue(out instanceof BufferedOutputStream); + assertArrayEquals(new byte[]{}, baos.toByteArray()); + out.write('a'); + assertArrayEquals(new byte[]{}, baos.toByteArray()); + out.flush(); + assertArrayEquals(new byte[]{'a'}, baos.toByteArray()); + context.assertIsSatisfied(); + } + + @Test + public void close() throws IOException { + final WritableResourceHandler writableResourceHandler = context + .mock(WritableResourceHandler.class); + context.checking(new Expectations() {{ + oneOf(writableResourceHandler).close(); + }}); + new BufferedWritableResourceHandler(writableResourceHandler).close(); + context.assertIsSatisfied(); + } +} diff --git a/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedWritableResourceTest.java b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedWritableResourceTest.java new file mode 100644 index 0000000..0a9b522 --- /dev/null +++ b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/BufferedWritableResourceTest.java @@ -0,0 +1,40 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import java.io.IOException; +import org.apache.maven.index.reader.WritableResourceHandler.WritableResource; +import org.jmock.Expectations; +import org.jmock.Mockery; +import org.junit.Test; + +public class BufferedWritableResourceTest { + private Mockery context = new Mockery(); + + @Test + public void close() throws IOException { + final WritableResource resourceHandler = context.mock(WritableResource.class); + context.checking(new Expectations() {{ + oneOf(resourceHandler).close(); + }}); + new BufferedWritableResource(resourceHandler).close(); + context.assertIsSatisfied(); + } +} diff --git a/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/PathWritableResourceHandlerTest.java b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/PathWritableResourceHandlerTest.java new file mode 100644 index 0000000..c259c9a --- /dev/null +++ b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/PathWritableResourceHandlerTest.java @@ -0,0 +1,53 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.Files; +import org.apache.maven.index.reader.WritableResourceHandler.WritableResource; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class PathWritableResourceHandlerTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void locate() throws IOException { + WritableResource test = new PathWritableResourceHandler(folder.getRoot().toPath()) + .locate("test.txt"); + assertNull(test.read()); + try (OutputStream out = test.write()) { + out.write('a'); + } + try (InputStream in = test.read()) { + assertEquals('a', in.read()); + } + assertArrayEquals(new byte[]{'a'}, + Files.readAllBytes(folder.getRoot().toPath().resolve("test.txt"))); + } +} diff --git a/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/UriResourceHandlerTest.java b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/UriResourceHandlerTest.java new file mode 100644 index 0000000..31a7653 --- /dev/null +++ b/indexer-reader/src/test/java/org/apache/maven/index/reader/resource/UriResourceHandlerTest.java @@ -0,0 +1,47 @@ +package org.apache.maven.index.reader.resource; + +/* + * 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. + */ + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import org.apache.maven.index.reader.ResourceHandler.Resource; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +public class UriResourceHandlerTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void locate() throws IOException { + Resource test = new UriResourceHandler(folder.getRoot().toURI()) + .locate("test.txt"); + assertNull(test.read()); + Files.write(folder.getRoot().toPath().resolve("test.txt"), new byte[]{'a'}); + try (InputStream in = test.read()) { + assertEquals('a', in.read()); + } + } +}