Skip to content

Commit

Permalink
Merge pull request #4 from tamada/plainfile
Browse files Browse the repository at this point in the history
単なるファイルを扱う PlainFileDataSource を追加した.
  • Loading branch information
tamada committed Feb 17, 2017
2 parents c45dbfe + 3a29b47 commit 5bcf687
Show file tree
Hide file tree
Showing 15 changed files with 204 additions and 68 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/github/kunai/entries/Entry.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ default boolean isClass(){
return name.endsWith(".class");
}

boolean endsWith(String suffix);

boolean isName(String name);

default URI loadFrom(){
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/github/kunai/entries/PathEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ public InputStream openStream() throws IOException{
return source.openStream(path);
}

public boolean isName(Name name){
return path.endsWith(name.toString());
@Override
public boolean endsWith(String suffix){
return path.toString()
.endsWith(suffix);
}

@Override
public boolean isName(String name){
return isName(new Name(name));
return path.endsWith(name);
}

@Override
public String toString(){
return String.format("%s <%s>", loadFrom(), isClass()? className(): path);
return String.format("%s <%s>", loadFrom(), isClass() && className() != null? className(): path());
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/github/kunai/source/AbstractDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ int getStartIndex(String name){
String parseClassName(String name){
int start = getStartIndex(name);
int last = getLastIndex(name, ".class");
return parseClassName(name, start, last);
return trimName(name, start, last);
}

private String parseClassName(String name, int start, int last){
if(start > 0 && start < last)
String trimName(String name, int start, int last){
if(start >= 0 && start < last)
return name.substring(start, last);
return name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@ public Stream<Entry> stream() {
public ClassName parseClassName(Path targetPath){
Path path = basePath.relativize(targetPath);
String name = path.toString();
return parse(name, getLastIndex(name, ".class"));
return new ClassName(super.parseClassName(name));
}

private ClassName parse(String name, int lastIndex){
if(lastIndex > 0)
name = name.substring(0, lastIndex);
return new ClassName(name);
}

@Override
public void close(){
// do nothing
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/github/kunai/source/PlainFileDataSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.kunai.source;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Stream;

import com.github.kunai.entries.ClassName;
import com.github.kunai.entries.Entry;
import com.github.kunai.entries.PathEntry;

public class PlainFileDataSource extends AbstractDataSource implements PathResolver{
private Path path;

public PlainFileDataSource(Path path){
this.path = path;
}

@Override
public Stream<Entry> stream() {
return Stream.of(new PathEntry(path, this));
}

@Override
public void close() throws IOException {
// do nothing.
}

@Override
public InputStream openStream(Path path) throws IOException {
return Files.newInputStream(path);
}

@Override
public ClassName parseClassName(Path path) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public DataSourceFactories(){
factories.add(new WarFileDataSourceFactory());
factories.add(new ClassFileDataSourceFactory());
factories.add(new DirectoryDataSourceFactory());
factories.add(new PlainFileDataSourceFactory());
}

public Optional<DataSourceFactory> find(Path path) throws IOException{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.kunai.source.factories;

import java.nio.file.FileSystem;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;

import com.github.kunai.entries.KunaiException;
import com.github.kunai.source.DataSource;
import com.github.kunai.source.PlainFileDataSource;

class PlainFileDataSourceFactory implements DataSourceFactory{
public PlainFileDataSourceFactory(){
}

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

@Override
public DataSource build(Path path, FileSystem system) throws KunaiException{
return new PlainFileDataSource(path);
}
}
18 changes: 0 additions & 18 deletions src/main/java/com/github/kunai/translators/NullTranslator.java

This file was deleted.

14 changes: 0 additions & 14 deletions src/main/java/com/github/kunai/translators/Translator.java

This file was deleted.

23 changes: 23 additions & 0 deletions src/test/java/com/github/kunai/source/AbstractDataSourceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.kunai.source;

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

import java.nio.file.Paths;

import org.junit.Test;

public class AbstractDataSourceTest{
@Test
public void testBasic() throws Exception{
try(PlainFileDataSource source = new PlainFileDataSource(Paths.get("target/test-classes/dummy.class/emptyfile"))){
assertThat(source.getStartIndex("/hoge.class"), is(1));
assertThat(source.getStartIndex("hoge.class"), is(0));
assertThat(source.trimName("/hoge.class", 1, 5), is("hoge"));
assertThat(source.trimName("aaa", -1, 1), is("aaa"));
assertThat(source.trimName("aaa", 2, 1), is("aaa"));
assertThat(source.trimName("aaa", 0, 1), is("a"));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public void testDataSource() throws Exception{

assertThat(factory.isTarget(path), is(true));

DataSource source = factory.build(path);

Entry[] entries = source.stream().toArray(count -> new Entry[count]);
assertThat(entries.length, is(1));
assertThat(entries[0].isName("sample/hello/HelloWorld.class"), is(true));
assertThat(entries[0].isClass(), is(true));
assertThat(entries[0].className(), is(new ClassName("sample.hello.HelloWorld")));
try(DataSource source = factory.build(path)){
Entry[] entries = source.stream().toArray(count -> new Entry[count]);
assertThat(entries.length, is(1));
assertThat(entries[0].isName("sample/hello/HelloWorld.class"), is(true));
assertThat(entries[0].isClass(), is(true));
assertThat(entries[0].className(), is(new ClassName("sample.hello.HelloWorld")));
}
}
}
28 changes: 14 additions & 14 deletions src/test/java/com/github/kunai/source/DirectoryDataSourceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ public void testDataSource() throws Exception{

assertThat(factory.isTarget(path), is(true));

DataSource source = factory.build(path);

Entry[] entries = source.stream()
.sorted(new EntryComparator())
.toArray(count -> new Entry[count]);
assertThat(entries.length, is(2));

assertThat(entries[0].isName("sample/hello/HelloWorld.class"), is(true));
assertThat(entries[0].isClass(), is(true));
assertThat(entries[0].className(), is(new ClassName("sample.hello.HelloWorld")));

assertThat(entries[1].isName("sample/hello/Launcher.class"), is(true));
assertThat(entries[1].isClass(), is(true));
assertThat(entries[1].className(), is(new ClassName("sample.hello.Launcher")));
try(DataSource source = factory.build(path)){
Entry[] entries = source.stream()
.sorted(new EntryComparator())
.toArray(count -> new Entry[count]);
assertThat(entries.length, is(2));

assertThat(entries[0].isName("sample/hello/HelloWorld.class"), is(true));
assertThat(entries[0].isClass(), is(true));
assertThat(entries[0].className(), is(new ClassName("sample.hello.HelloWorld")));

assertThat(entries[1].isName("sample/hello/Launcher.class"), is(true));
assertThat(entries[1].isClass(), is(true));
assertThat(entries[1].className(), is(new ClassName("sample.hello.Launcher")));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.kunai.source.factories;

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

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

import org.junit.Before;
import org.junit.Test;

import com.github.kunai.entries.ClassName;
import com.github.kunai.entries.Entry;
import com.github.kunai.source.DataSource;

public class ClassFileDataSourceFactoryTest {
private Path path;
private Path dummyPath;
private ClassFileDataSourceFactory factory = new ClassFileDataSourceFactory();

@Before
public void setUp(){
path = Paths.get("target/test-classes/hello/target/classes/sample/hello/HelloWorld.class");
dummyPath = Paths.get("target/test-classes/dummy.class");
}

@Test
public void testBasic() throws Exception{
assertThat(factory.isTarget(path), is(true));
assertThat(factory.isTarget(dummyPath), is(false));

DataSource source = factory.build(new File(path.toString()));
Entry[] entries = source.stream().toArray(size -> new Entry[size]);

assertThat(entries.length, is(1));
assertThat(entries[0].className(), is(new ClassName("sample.hello.HelloWorld")));
assertThat(entries[0].isClass(), is(true));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.kunai.source.factories;

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

import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.kunai.source.factories;

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

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

import org.junit.Before;
import org.junit.Test;

import com.github.kunai.entries.Entry;
import com.github.kunai.source.DataSource;

public class PlainFileDataSourceFactoryTest {
private Path path;
private Path dummyPath;
private DataSourceFactory factory = new PlainFileDataSourceFactory();

@Before
public void setUp(){
path = Paths.get("target/test-classes/hello/target/classes/sample/hello/HelloWorld.class");
dummyPath = Paths.get("target/test-classes/dummy.class");
}

@Test
public void testBasic() throws Exception{
assertThat(factory.isTarget(path), is(true));
assertThat(factory.isTarget(dummyPath), is(false));

DataSource source = factory.build(new File(path.toString()));
Entry[] entries = source.stream().toArray(size -> new Entry[size]);

assertThat(entries.length, is(1));
assertThat(entries[0].endsWith(".class"), is(true));
assertThat(entries[0].loadFrom(), is(path.toUri()));
assertThat(entries[0].className(), is(nullValue()));
assertThat(entries[0].isClass(), is(true));
assertThat(entries[0].toString(), is(String.format("%s <%s>", path.toUri(), path)));
}
}

0 comments on commit 5bcf687

Please sign in to comment.