Skip to content

Commit

Permalink
単体テストを追加した.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Jan 22, 2017
1 parent 5a27624 commit c8b9440
Show file tree
Hide file tree
Showing 30 changed files with 577 additions and 105 deletions.
25 changes: 17 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@ This tool is to read/store class files from/to directories, jar file, and war fi
[kunai](https://github.com/tamada/kunai) is same tool, however, kunai is for Java 7 or before.
Kunai2 implemented for Java 8 and used streaming API.

## Simply use.

### Reading
## Simple use case.

```java
Path path = Paths.get("target/source");
DataSource source = new DefaultDataSourceFactory().build(path);
source.stream();
// some operation for stream.
// Reading
Path sourcePath = Paths.get("path/of/source/file/or/directory");
DataSourceFactory sourceFactory = new DefaultDataSourceFactory();
try(DataSource source = sourceFactory.build(sourcePath)){
// Storing
Path outputPath = Paths.get("path/of/output/file.jar");
DataSinkFactory sinkFactory = new DefaultDataSinkFactory();
try(DataSink sink = sinkFactory.build(outputPath)){
sink.consume(source);
// above lines means following code.
// source.forEach(entry -> {
// try{ sink.consume(entry); }
// catch(Exception e){ }
// });
}
}
```

### Storing
4 changes: 2 additions & 2 deletions src/main/java/com/github/kunai/Demo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import java.nio.file.Paths;

import com.github.kunai.sink.DataSink;
import com.github.kunai.sink.factories.DataSinkFactoryBuilder;
import com.github.kunai.sink.factories.DefaultDataSinkFactory;
import com.github.kunai.source.DataSource;
import com.github.kunai.source.factories.DefaultDataSourceFactory;

Expand All @@ -14,7 +14,7 @@ public Demo(String[] args) throws Exception{

private void copy(String from, String to) throws Exception{
try(DataSource source = new DefaultDataSourceFactory().build(Paths.get(from));
DataSink sink = new DataSinkFactoryBuilder().create(Paths.get(to))){
DataSink sink = new DefaultDataSinkFactory().create(Paths.get(to))){
copy(source, sink);
}
}
Expand Down
26 changes: 8 additions & 18 deletions src/main/java/com/github/kunai/sink/DirectoryMaker.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
package com.github.kunai.sink;

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;

import com.github.kunai.util.Exceptions;
import com.github.kunai.util.PathHelper;

public class DirectoryMaker {
private static boolean exists(FileSystemProvider provider, Path path){
return Exceptions.isThrowed(provider, path,
(givenProvider, givenPath) -> givenProvider.readAttributes(
givenPath, BasicFileAttributes.class, NOFOLLOW_LINKS));
}

public static void mkdirs(FileSystem system, Path path){
try{
mkdirsImpl(system.provider(), path.getParent());
} catch(IOException e){ }
public static void mkdirs(Path givenPath, FileSystem givenSystem){
Exceptions.isThrowed(givenPath, givenSystem,
(path, system) -> mkdirsImpl(path, system));
}

private static void mkdirsImpl(FileSystemProvider provider, Path path) throws IOException{
if(path == null || exists(provider, path)) return;
mkdirsImpl(provider, path.getParent());
provider.createDirectory(path);
private static void mkdirsImpl(Path path, FileSystem system) throws IOException{
if(path == null || PathHelper.exists(path, system)) return;
mkdirsImpl(path.getParent(), system);
system.provider().createDirectory(path);
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/github/kunai/sink/JarFileDataSink.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void close() throws IOException {
@Override
public void consume(InputStream in, Entry entry) throws KunaiException {
Path outputPath = base.resolve(createPath(entry));
DirectoryMaker.mkdirs(system, outputPath);
DirectoryMaker.mkdirs(outputPath.getParent(), system);
consume(in, outputPath);
}

Expand Down
20 changes: 3 additions & 17 deletions src/main/java/com/github/kunai/sink/factories/DataSinkFactory.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,11 @@
package com.github.kunai.sink.factories;

import java.nio.file.Path;
import java.util.function.Function;
import java.util.function.Predicate;

import com.github.kunai.sink.DataSink;

class DataSinkFactory {
private Predicate<Path> predicate;
private Function<Path, DataSink> function;
public interface DataSinkFactory {
DataSink create(Path path);

public DataSinkFactory(Predicate<Path> predicate, Function<Path, DataSink> function){
this.predicate = predicate;
this.function = function;
}

public boolean test(Path path){
return predicate.test(path);
}

public DataSink create(Path path){
return function.apply(path);
}
boolean isTarget(Path path);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.kunai.sink.factories;

import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;

import com.github.kunai.sink.ClassFileDataSink;
import com.github.kunai.sink.DataSink;
import com.github.kunai.sink.DirectoryDataSink;
import com.github.kunai.sink.JarFileDataSink;
import com.github.kunai.sink.WarFileDataSink;
import com.github.kunai.util.PathHelper;

public class DefaultDataSinkFactory implements DataSinkFactory{
private List<DataSinkFactory> factories = new ArrayList<>();

public DefaultDataSinkFactory(){
register(new GenericDataSinkFactory(path -> PathHelper.endsWith(path, ".jar"), path -> new JarFileDataSink(path)));
register(new GenericDataSinkFactory(path -> PathHelper.endsWith(path, ".war"), path -> new WarFileDataSink(path)));
register(new GenericDataSinkFactory(path -> PathHelper.endsWith(path, ".class"), path -> new ClassFileDataSink(path)));
register(new GenericDataSinkFactory(path -> true, path -> new DirectoryDataSink(path)));
}

public boolean isTarget(Path path){
return path != null;
}

public DataSinkFactory factory(Path path){
return factories.stream()
.filter(factory -> factory.isTarget(path))
.findFirst().get();
}

public DataSink create(Path path){
return factory(path).create(path);
}

private void register(GenericDataSinkFactory factory){
factories.add(factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.github.kunai.sink.factories;

import java.nio.file.Path;
import java.util.function.Function;
import java.util.function.Predicate;

import com.github.kunai.sink.DataSink;

class GenericDataSinkFactory implements DataSinkFactory{
private Predicate<Path> predicate;
private Function<Path, DataSink> function;

public GenericDataSinkFactory(Predicate<Path> predicate, Function<Path, DataSink> function){
this.predicate = predicate;
this.function = function;
}

@Override
public boolean isTarget(Path path){
return predicate.test(path);
}

@Override
public DataSink create(Path path){
return function.apply(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ private List<Path> traverse(FileSystemProvider provider, List<Path> list, Path..
private List<Path> traverse(FileSystemProvider provider, List<Path> list, Path path){
try(DirectoryStream<Path> stream = provider.newDirectoryStream(path, FILTER)){
stream.forEach(p -> traverseDirectory(provider, list, p));
} catch (Exception e) { }
} catch (Exception e){ }
return list;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Optional;

import com.github.kunai.entries.KunaiException;
import com.github.kunai.source.DataSource;
import com.github.kunai.util.Exceptions;
import com.github.kunai.util.PathHelper;

public interface DataSourceFactory {
boolean isTarget(Path path, FileSystem system, BasicFileAttributes attributes);

default boolean isTarget(Path givenPath, FileSystem givenSystem){
return Exceptions.isThrowedCondition(givenPath, givenSystem,
(path, system) -> isTarget(path, system, system.provider()
.readAttributes(path, BasicFileAttributes.class)));
default boolean isTarget(Path path, FileSystem system){
Optional<BasicFileAttributes> attributes = PathHelper.readAttributes(path, system);
return attributes.isPresent() &&
isTarget(path, system, attributes.get());
}

default boolean isTarget(Path path){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ private DataSource build(Optional<DataSourceFactory> source, Path path) throws K

@Override
public boolean isTarget(Path path, FileSystem system, BasicFileAttributes attributes) {
return true;
}

@Override
public boolean isTarget(Path path) {
return true;
return path != null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public boolean isTarget(Path path, FileSystem system, BasicFileAttributes attrib

@Override
public DataSource build(Path path, FileSystem system) throws KunaiException{
if(!isTarget(path, system))
throw new UnsupportedDataSourceException(path + ": not supported.");
return new DirectoryDataSource(path);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ public boolean isTarget(Path path, FileSystem system, BasicFileAttributes attrib

@Override
public DataSource build(Path path, FileSystem system) throws KunaiException{
try{
return buildDataSource(path, system);
}
try{ return buildImpl(path, system); }
catch(IOException e){
throw new UnsupportedDataSourceException(e.getMessage());
}
}

private DataSource buildImpl(Path path, FileSystem system) throws KunaiException, IOException{
if(!isTarget(path, system))
throw new UnsupportedDataSourceException(path + ": not supported");
return buildDataSource(path, system);
}

private DataSource buildDataSource(Path path, FileSystem system) throws IOException{
ClassLoader loader = getClass().getClassLoader();
FileSystem jarSystem = FileSystems.newFileSystem(path, loader);
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/com/github/kunai/util/DirectoryRemover.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.kunai.util;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

public class DirectoryRemover implements FileVisitor<Path> {
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Files.delete(file);
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exception) throws IOException {
if(exception != null) throw exception;
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/github/kunai/util/Exceptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

public class Exceptions {
public static <T> boolean isThrowed(T argument, ThrowableConsumer<T, Exception> consumer){
try{ consumer.apply(argument); }
try{ consumer.accept(argument); }
catch(Exception e){ return false; }
return true;
}

public static <T, S> boolean isThrowed(T argument1, S argument2,
ThrowableBiConsumer<T, S, Exception> consumer){
try{ consumer.apply(argument1, argument2); }
try{ consumer.accept(argument1, argument2); }
catch(Exception e){ return false; }
return true;
}
Expand All @@ -30,7 +30,7 @@ public static <T, S> boolean isThrowedCondition(T argument1, S argument2,

public static <A1, A2, R> Optional<R> map(A1 argument1, A2 argument2,
ThrowableBiFunction<A1, A2, R, Exception> func){
try{ return Optional.of(func.apply(argument1, argument2)); }
try{ return Optional.ofNullable(func.apply(argument1, argument2)); }
catch(Exception e){ }
return Optional.empty();
}
Expand Down
Loading

0 comments on commit c8b9440

Please sign in to comment.