Skip to content

Commit

Permalink
テストを追加中.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Jan 21, 2017
1 parent b11c558 commit 5a27624
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 39 deletions.
14 changes: 8 additions & 6 deletions src/main/java/com/github/kunai/sink/DataSinkHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@
import java.nio.file.spi.FileSystemProvider;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.github.kunai.util.Exceptions;

public class DataSinkHelper {
public static FileSystem buildFileSystem(Path path){
Map<String, String> environment = new HashMap<>();
environment.put("create", "true");
return buildFileSystem(path, environment);
return buildFileSystem(path, environment).get();
}

public static FileSystem buildFileSystem(Path path, Map<String, String> environment){
try {
return FileSystems.newFileSystem(URI.create("jar:file:" + path.toAbsolutePath()), environment);
} catch (IOException e) { e.printStackTrace(); }
return null;
private static Optional<FileSystem> buildFileSystem(Path path, Map<String, String> environment){
return Exceptions.map(path, environment,
(p, map) -> FileSystems.newFileSystem(
URI.create("jar:file:" + p.toAbsolutePath()), map));
}

public static OutputStream newOutputStream(FileSystem system, Path path) throws IOException{
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/com/github/kunai/sink/DirectoryMaker.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
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.LinkOption;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;

import com.github.kunai.util.Exceptions;

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

public static void mkdirs(FileSystem system, Path path){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
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 DataSinkFactoryBuilder {
private List<DataSinkFactory> factories = new ArrayList<>();

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

public DataSinkFactory factory(Path path){
Expand All @@ -29,4 +30,8 @@ public DataSinkFactory factory(Path path){
public DataSink create(Path path){
return factory(path).create(path);
}

private void register(DataSinkFactory factory){
factories.add(factory);
}
}
13 changes: 5 additions & 8 deletions src/main/java/com/github/kunai/source/DirectoryTraverser.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.kunai.source;

import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
Expand All @@ -13,6 +12,8 @@
import java.util.List;
import java.util.Optional;

import com.github.kunai.util.Exceptions;

class DirectoryTraverser {
private static final DirectoryStream.Filter<Path> FILTER = new EveryFileAcceptFilter();

Expand All @@ -34,8 +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 All @@ -53,10 +53,7 @@ private void doTraverse(FileSystemProvider provider, List<Path> list, Path path,
}

private Optional<BasicFileAttributes> getAttributes(FileSystemProvider provider, Path path){
try {
return Optional.of(provider.readAttributes(path, BasicFileAttributes.class));
} catch (IOException e) {
}
return Optional.empty();
return Exceptions.map(provider, path, (p2, path2) ->
provider.readAttributes(path, BasicFileAttributes.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
import com.github.kunai.entries.KunaiException;
import com.github.kunai.source.ClassFileDataSource;
import com.github.kunai.source.DataSource;
import com.github.kunai.util.PathHelper;

class ClassFileDataSourceFactory implements DataSourceFactory{
public ClassFileDataSourceFactory(){
}

@Override
public boolean isTarget(Path path, FileSystem system, BasicFileAttributes attributes){
String name = path.toString();
return name.endsWith(".class")
return PathHelper.endsWith(path, ".class")
&& attributes.isRegularFile();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.nio.file.spi.FileSystemProvider;
Expand All @@ -19,6 +20,10 @@ public DataSourceFactories(){
factories.add(new DirectoryDataSourceFactory());
}

public Optional<DataSourceFactory> find(Path path) throws IOException{
return find(path, FileSystems.getDefault());
}

public Optional<DataSourceFactory> find(Path path, FileSystem system) throws IOException{
return find(path, system, system.provider());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package com.github.kunai.source.factories;

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

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

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

default boolean isTarget(Path path, FileSystem system){
try {
FileSystemProvider provider = system.provider();
BasicFileAttributes attributes = provider.readAttributes(path, BasicFileAttributes.class);
return isTarget(path, system, attributes);
} catch (IOException e) {
}
return false;
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){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import com.github.kunai.entries.KunaiException;
import com.github.kunai.source.DataSource;
import com.github.kunai.source.JarFileDataSource;
import com.github.kunai.util.PathHelper;

class JarFileDataSourceFactory implements DataSourceFactory{
public JarFileDataSourceFactory(){
}

@Override
public boolean isTarget(Path path, FileSystem system, BasicFileAttributes attributes){
String name = path.toString();
return name.endsWith(".jar")
return PathHelper.endsWith(path, ".jar")
&& attributes.isRegularFile();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

import com.github.kunai.source.DataSource;
import com.github.kunai.source.WarFileDataSource;
import com.github.kunai.util.PathHelper;

class WarFileDataSourceFactory extends JarFileDataSourceFactory{
public WarFileDataSourceFactory(){
}

@Override
public boolean isTarget(Path path, FileSystem system, BasicFileAttributes attributes){
String name = path.toString();
return name.endsWith(".war")
return PathHelper.endsWith(path, ".war")
&& attributes.isRegularFile();
}

Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/github/kunai/util/Exceptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.github.kunai.util;

import java.util.Optional;

public class Exceptions {
public static <T> boolean isThrowed(T argument, ThrowableConsumer<T, Exception> consumer){
try{ consumer.apply(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); }
catch(Exception e){ return false; }
return true;
}

public static <T> boolean isThrowedCondition(T argument,
ThrowablePredicate<T, Exception> predicate){
try{ return predicate.test(argument); }
catch(Exception e){ return false; }
}

public static <T, S> boolean isThrowedCondition(T argument1, S argument2,
ThrowableBiPredicate<T, S, Exception> predicate){
try{ return predicate.test(argument1, argument2); }
catch(Exception e){ return false; }
}

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)); }
catch(Exception e){ }
return Optional.empty();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/github/kunai/util/PathHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.kunai.util;

import java.nio.file.Path;

public class PathHelper {
public static boolean endsWith(Path path, String suffix){
String name = path.toString();
return name.endsWith(suffix);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/github/kunai/util/ThrowableBiConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.kunai.util;

public interface ThrowableBiConsumer<S1, S2, E extends Exception> {
void apply(S1 s1, S2 s2) throws E;
}
5 changes: 5 additions & 0 deletions src/main/java/com/github/kunai/util/ThrowableBiFunction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github.kunai.util;

public interface ThrowableBiFunction<A1, A2, R, E extends Exception> {
R apply(A1 argument1, A2 argument) throws E;
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/kunai/util/ThrowableBiPredicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.kunai.util;

@FunctionalInterface
public interface ThrowableBiPredicate<S1, S2, E extends Exception> {
boolean test(S1 s1, S2 s2) throws E;
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/kunai/util/ThrowableConsumer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.kunai.util;

@FunctionalInterface
public interface ThrowableConsumer<S, E extends Exception> {
void apply(S argument) throws E;
}
6 changes: 6 additions & 0 deletions src/main/java/com/github/kunai/util/ThrowablePredicate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.github.kunai.util;

@FunctionalInterface
public interface ThrowablePredicate<S, E extends Exception> {
boolean test(S argument) throws E;
}
2 changes: 2 additions & 0 deletions src/test/java/com/github/kunai/entries/ClassNameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public void testBasic(){
assertThat(name2, is(new Name("com.github.kunai.entries.NameTest")));
assertThat(name2, is(not("com.github.kunai.entries.NameTest")));
assertThat(name2.toString(), is("com.github.kunai.entries.NameTest"));

assertThat(name1, is(not(name2)));
}
}
3 changes: 3 additions & 0 deletions src/test/java/com/github/kunai/entries/NameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ public void setUp(){
@Test
public void testBasic(){
assertThat(name, is(new Name("Haruaki Tamada")));
assertThat(name, is(not(new Name("Nanashi no Gonbe"))));
assertThat(name, is(not("Haruaki Tamada")));
assertThat(name, is(not(new Object())));

assertThat(name.toString(), is("Haruaki Tamada"));
assertThat(name.name(), is("Haruaki Tamada"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.github.kunai.source.factories;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Paths;
import java.util.Optional;

import org.junit.Test;

import com.github.kunai.util.Assert;

public class DataSourceFactoriesTest {
private DataSourceFactories factories = new DataSourceFactories();

@Test
public void testDirectory() throws Exception{
Optional<DataSourceFactory> factory = factories.find(Paths.get("target/classes"), FileSystems.getDefault());

assertThat(factory.isPresent(), is(true));
assertThat(factory.get(), is(instanceOf(DirectoryDataSourceFactory.class)));
}

@Test
public void testJar() throws Exception{
Optional<DataSourceFactory> factory = factories.find(Paths.get("target/test-classes/hello/target/hello-1.0-SNAPSHOT.jar"), FileSystems.getDefault());

assertThat(factory.isPresent(), is(true));
assertThat(factory.get(), is(instanceOf(JarFileDataSourceFactory.class)));
}

@Test
public void testNotExistsJar() throws Exception{
Assert.assertThrows(IOException.class,
() -> factories.find(Paths.get("not/exists/jar")));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.kunai.source.factories;

import org.junit.Test;

public class DefaultDataSourceFactoryTest {

}
14 changes: 14 additions & 0 deletions src/test/java/com/github/kunai/util/Assert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.kunai.util;

import static org.junit.Assert.assertTrue;

public class Assert {
public static <E extends Exception> void assertThrows(Class<E> exceptionClass, ThrowableProcessor<E> processor){
boolean thrown = false;
try { processor.perform(); }
catch (Exception ex) {
thrown = exceptionClass.isInstance(ex);
}
assertTrue(thrown);
}
}
Loading

0 comments on commit 5a27624

Please sign in to comment.