|
|
@@ -6,33 +6,25 @@
|
|
|
*/
|
|
|
package net.ranides.assira.io.uri.impl;
|
|
|
|
|
|
+import static net.ranides.assira.io.uri.URIFlags.*;
|
|
|
+import static net.ranides.assira.junit.NewAssert.*;
|
|
|
+
|
|
|
import java.io.IOException;
|
|
|
import java.net.URI;
|
|
|
import java.net.URISyntaxException;
|
|
|
import java.nio.charset.Charset;
|
|
|
-import java.util.Map;
|
|
|
-import java.util.Optional;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
|
|
|
import net.ranides.assira.collection.maps.GenericHashMap;
|
|
|
-import net.ranides.assira.collection.maps.GenericMap;
|
|
|
-import net.ranides.assira.collection.maps.HashMap;
|
|
|
-import net.ranides.assira.collection.query.CQuery;
|
|
|
-import net.ranides.assira.collection.query.CQueryBuilder;
|
|
|
+import net.ranides.assira.credentials.BasicUserStore;
|
|
|
import net.ranides.assira.credentials.UserProperty;
|
|
|
-import net.ranides.assira.credentials.UserStore;
|
|
|
+import net.ranides.assira.io.uri.URIFlags;
|
|
|
import net.ranides.assira.io.uri.URIHandle;
|
|
|
-import net.ranides.assira.io.uri.URIHandler;
|
|
|
import net.ranides.assira.io.uri.URIResolver;
|
|
|
-import net.ranides.assira.io.uri.URIUtils;
|
|
|
-import net.ranides.assira.reflection.util.ReflectUtils;
|
|
|
-import net.ranides.assira.text.IOStrings;
|
|
|
-import static net.ranides.assira.junit.NewAssert.*;
|
|
|
import net.ranides.assira.text.Charsets;
|
|
|
-
|
|
|
+import net.ranides.assira.text.IOStrings;
|
|
|
import net.ranides.assira.trace.ExceptionUtils;
|
|
|
-import org.junit.Test;
|
|
|
-
|
|
|
-import javax.naming.spi.Resolver;
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
@@ -44,32 +36,102 @@ public class CFTPHandlerTest {
|
|
|
|
|
|
private static final String TEXT_2 = "infov1";
|
|
|
|
|
|
+ private final URIResolver re = new URIResolver()
|
|
|
+ .register(CFTPHandler.class)
|
|
|
+ .users(new BasicUserStore().add(new GenericHashMap<String>(){{
|
|
|
+ put(UserProperty.ACCOUNT, "user");
|
|
|
+ put(UserProperty.LOGIN, "f12357_assira_ftp_test");
|
|
|
+ put(UserProperty.PASSWORD, "TTYKYbg25ijGxlnYBGfN");
|
|
|
+ }}));
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testFlags() throws URISyntaxException, IOException {
|
|
|
+ URIHandle handle1 = re.resolve("ftp://user@ftp.ranides.net/");
|
|
|
+ URIHandle handle2 = re.resolve("ftp://user@ftp.ranides.net/dir/data");
|
|
|
+ URIHandle handle3 = re.resolve("ftp://user@ftp.ranides.net/dir/other/content.txt");
|
|
|
+ URIHandle handle4 = re.resolve("ftp://user@ftp.ranides.net/nop");
|
|
|
+
|
|
|
+ assertEquals(URIFlags.collect(EXISTS, DIR), handle1.flags());
|
|
|
+ assertEquals(URIFlags.collect(EXISTS, DIR), handle2.flags());
|
|
|
+ assertEquals(URIFlags.collect(EXISTS, FILE, BINARY, WRITABLE, READABLE, SIZE, SEEK), handle3.flags());
|
|
|
+ assertEquals(URIFlags.collect(), handle4.flags());
|
|
|
+
|
|
|
+ handle1.close();
|
|
|
+ handle2.close();
|
|
|
+ handle3.close();
|
|
|
+ handle4.close();
|
|
|
+ }
|
|
|
|
|
|
@Test
|
|
|
public void testScan() throws URISyntaxException, IOException {
|
|
|
- TStore users = new TStore();
|
|
|
- URIResolver re = new URIResolver().register(CFTPHandler.class).users(users);
|
|
|
|
|
|
- users.add("user", "f12357_assira_ftp_test", "TTYKYbg25ijGxlnYBGfN");
|
|
|
+ String[] exp1 = {
|
|
|
+ "ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/dir/data",
|
|
|
+ "ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/dir/empty",
|
|
|
+ "ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/dir/file.bin",
|
|
|
+ "ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/dir/first.txt",
|
|
|
+ "ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/dir/other",
|
|
|
+ "ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/dir/sec.log"
|
|
|
+ };
|
|
|
+
|
|
|
+ String[] exp2 = {
|
|
|
+ "ftp://user@ftp.ranides.net/.ftpquota",
|
|
|
+ "ftp://user@ftp.ranides.net/dir",
|
|
|
+ "ftp://user@ftp.ranides.net/info.txt",
|
|
|
+ "ftp://user@ftp.ranides.net/readme.txt"
|
|
|
+ };
|
|
|
+
|
|
|
+ String[] exp3 = {
|
|
|
+ "ftp://user@ftp.ranides.net/dir/data",
|
|
|
+ "ftp://user@ftp.ranides.net/dir/empty",
|
|
|
+ "ftp://user@ftp.ranides.net/dir/file.bin",
|
|
|
+ "ftp://user@ftp.ranides.net/dir/first.txt",
|
|
|
+ "ftp://user@ftp.ranides.net/dir/other",
|
|
|
+ "ftp://user@ftp.ranides.net/dir/sec.log"
|
|
|
+ };
|
|
|
+
|
|
|
+ String[] exp4 = {
|
|
|
+ "ftp://user@ftp.ranides.net/dir/other/content.txt",
|
|
|
+ "ftp://user@ftp.ranides.net/dir/other/list.txt",
|
|
|
+ };
|
|
|
|
|
|
URI u1 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/dir");
|
|
|
- URI u2 = new URI("ftp://user@ftp.ranides.net/dir");
|
|
|
- URI u3 = new URI("ftp://user@ftp.ranides.net/");
|
|
|
- try(URIHandle handle1 = re.resolve(u1)) {
|
|
|
- System.out.println(handle1.scan().map(e -> asName(e)).list());
|
|
|
+ URI u2 = new URI("ftp://user@ftp.ranides.net");
|
|
|
+ URI u3 = new URI("ftp://user@ftp.ranides.net/dir");
|
|
|
+ URI u4 = new URI("ftp://user@ftp.ranides.net/dir/other");
|
|
|
+
|
|
|
+
|
|
|
+ try(URIHandle handle = re.resolve(u1)) {
|
|
|
+ assertEquals("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net", handle.parent().uri().toString());
|
|
|
+ assertArrayEquals(exp1, handle.scan().map(e -> asName(e)).sort().array(String[]::new));
|
|
|
}
|
|
|
- try(URIHandle handle1 = re.resolve(u2)) {
|
|
|
- System.out.println(handle1.scan().map(e -> asName(e)).list());
|
|
|
+
|
|
|
+ try(URIHandle handle = re.resolve(u2)) {
|
|
|
+ assertEquals("ftp://user@ftp.ranides.net", handle.parent().uri().toString());
|
|
|
+ assertArrayEquals(exp2, handle.scan().map(e -> asName(e)).sort().array(String[]::new));
|
|
|
}
|
|
|
|
|
|
- try(URIHandle handle1 = re.resolve(u3)) {
|
|
|
- System.out.println(handle1.scan().map(e -> asName(e)).list());
|
|
|
+ try(URIHandle handle = re.resolve(u3)) {
|
|
|
+ assertEquals("ftp://user@ftp.ranides.net", handle.parent().uri().toString());
|
|
|
+ assertEquals("ftp://user@ftp.ranides.net", handle.parent().parent().uri().toString());
|
|
|
+ assertArrayEquals(exp3, handle.scan().map(e -> asName(e)).sort().array(String[]::new));
|
|
|
}
|
|
|
+
|
|
|
+ try(URIHandle handle = re.resolve(u4)) {
|
|
|
+ assertEquals("ftp://user@ftp.ranides.net/dir", handle.parent().uri().toString());
|
|
|
+ assertEquals("ftp://user@ftp.ranides.net", handle.parent().parent().uri().toString());
|
|
|
+ assertArrayEquals(exp4, handle.scan().map(e -> asName(e)).sort().array(String[]::new));
|
|
|
+ }
|
|
|
+
|
|
|
+ try(URIHandle handle = re.resolve(u2)) {
|
|
|
+ assertEquals(TEXT_1, IOStrings.read(handle.scan().list().get(3).reader()));
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private static String asName(URIHandle handle) {
|
|
|
try {
|
|
|
-// return handle.path().toString();
|
|
|
return handle.uri().toString();
|
|
|
} catch (IOException e) {
|
|
|
throw ExceptionUtils.rethrow(e);
|
|
|
@@ -78,16 +140,13 @@ public class CFTPHandlerTest {
|
|
|
|
|
|
@Test
|
|
|
public void testUser() throws URISyntaxException, IOException {
|
|
|
- URIHandler resolver = new CFTPHandler();
|
|
|
+ URI u1 = new URI("ftp://user@ftp.ranides.net/readme.txt");
|
|
|
|
|
|
- URI u1 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/readme.txt");
|
|
|
-
|
|
|
- URIHandle handle1 = resolver.resolve(u1);
|
|
|
+ URIHandle handle1 = re.resolve(u1);
|
|
|
assertEquals(TEXT_1, IOStrings.read(handle1.reader()));
|
|
|
-// handle1.close();
|
|
|
-
|
|
|
- URI u2 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/info.txt");
|
|
|
- URIHandle handle2 = resolver.resolve(u2);
|
|
|
+
|
|
|
+ URI u2 = new URI("ftp://user@ftp.ranides.net/info.txt");
|
|
|
+ URIHandle handle2 = re.resolve(u2);
|
|
|
assertEquals(TEXT_2, IOStrings.read(handle2.reader()));
|
|
|
|
|
|
handle1.close();
|
|
|
@@ -96,19 +155,18 @@ public class CFTPHandlerTest {
|
|
|
|
|
|
@Test
|
|
|
public void testCopy() throws URISyntaxException, IOException {
|
|
|
- URIHandler resolver = new CFTPHandler();
|
|
|
-
|
|
|
- URI u1 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/readme.txt");
|
|
|
- URI u2 = new URI("ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/out-1.txt");
|
|
|
- String s3 = "ftp://f12357_assira_ftp_test:TTYKYbg25ijGxlnYBGfN@ftp.ranides.net/out-2.txt?charset=windows-1250";
|
|
|
- try (URIHandle target = resolver.resolve(u2)) {
|
|
|
+
|
|
|
+ URI u1 = new URI("ftp://user@ftp.ranides.net/readme.txt");
|
|
|
+ URI u2 = new URI("ftp://user@ftp.ranides.net/out-1.txt");
|
|
|
+ String s3 = "ftp://user@ftp.ranides.net/out-2.txt?charset=windows-1250";
|
|
|
+ try (URIHandle target = re.resolve(u2)) {
|
|
|
|
|
|
assertFalse(target.exists());
|
|
|
|
|
|
- try (URIHandle source = resolver.resolve(u1)) {
|
|
|
+ try (URIHandle source = re.resolve(u1)) {
|
|
|
source.copy(target);
|
|
|
}
|
|
|
- try (URIHandle source = resolver.resolve(u1)) {
|
|
|
+ try (URIHandle source = re.resolve(u1)) {
|
|
|
source.copy(s3);
|
|
|
}
|
|
|
|
|
|
@@ -118,7 +176,7 @@ public class CFTPHandlerTest {
|
|
|
target.delete();
|
|
|
assertFalse(target.exists());
|
|
|
|
|
|
- try(URIHandle u3 = resolver.resolve(new URI(s3))) {
|
|
|
+ try(URIHandle u3 = re.resolve(new URI(s3))) {
|
|
|
assertNotEquals(TEXT_1, IOStrings.read(u3.reader()));
|
|
|
assertEquals(cast(TEXT_1, Charsets.UTF8 ,Charsets.PL_WINDOWS), IOStrings.read(u3.reader()));
|
|
|
u3.delete();
|
|
|
@@ -131,28 +189,4 @@ public class CFTPHandlerTest {
|
|
|
return new String(text.getBytes(source), target);
|
|
|
}
|
|
|
|
|
|
- private static final class TStore implements UserStore {
|
|
|
-
|
|
|
- private Map<String, GenericMap<String>> users = new HashMap<>();
|
|
|
-
|
|
|
- @Override
|
|
|
- public CQuery<GenericMap<String>> match(URI uri) {
|
|
|
- return URIUtils.getLogin(uri).map(this::find).orElse(CQueryBuilder.empty());
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public CQuery<GenericMap<String>> find(String user) {
|
|
|
- GenericMap<String> map = users.get(user);
|
|
|
- return map!=null ? CQueryBuilder.of(map) : CQueryBuilder.empty();
|
|
|
- }
|
|
|
-
|
|
|
- public void add(String id, String login, String pass) {
|
|
|
- GenericMap<String> map = new GenericHashMap<>();
|
|
|
- map.put(UserProperty.LOGIN, login);
|
|
|
- map.put(UserProperty.PASSWORD, pass);
|
|
|
- users.put(id, map);
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
-
|
|
|
}
|