diff --git a/httpchannel-api/src/main/java/com/rogiel/httpchannel/service/AccountDetails.java b/httpchannel-api/src/main/java/com/rogiel/httpchannel/service/AccountDetails.java index ccbfbd5..31551f6 100644 --- a/httpchannel-api/src/main/java/com/rogiel/httpchannel/service/AccountDetails.java +++ b/httpchannel-api/src/main/java/com/rogiel/httpchannel/service/AccountDetails.java @@ -31,8 +31,8 @@ package com.rogiel.httpchannel.service; * quota *
+ * Key: username + */ + private String VALID_USERNAME; + /** + * See src/test/resources/config/megaupload.properties + *
+ * Key: password + */ + private String VALID_PASSWORD; + + private static final String INVALID_USERNAME = "invalid"; + private static final String INVALID_PASSWORD = "abc"; + + @Before + public void setUp() throws Exception { + service = new UptoboxService(); + + final Properties properties = new Properties(); + properties.load(new FileInputStream( + "../src/test/resources/login.properties")); + VALID_USERNAME = properties.getProperty("uptobox.username"); + VALID_PASSWORD = properties.getProperty("uptobox.password"); + } + + @Test + public void testServiceId() { + assertEquals(ServiceID.create("uptobox"), service.getServiceID()); + } + + @Test + public void testValidAuthenticator() throws IOException { + service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD)) + .login(); + } + + @Test(expected = AuthenticationInvalidCredentialException.class) + public void testInvalidAuthenticator() throws IOException { + service.getAuthenticator( + new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login(); + } + + @Test + public void testNonLoguedInUploader() throws IOException { + final Path path = Paths + .get("../src/test/resources/upload-test-file.txt"); + final UploadChannel channel = UploadServices.upload(service, path) + .openChannel(); + final SeekableByteChannel inChannel = Files.newByteChannel(path); + + try { + ChannelUtils.copy(inChannel, channel); + } finally { + inChannel.close(); + channel.close(); + } + + System.out.println(channel.getDownloadLink()); + Assert.assertNotNull(channel.getDownloadLink()); + } + + @Test + public void testLoggedInUploader() throws IOException { + service.getAuthenticator(new Credential(VALID_USERNAME, VALID_PASSWORD)) + .login(); + + final Path path = Paths + .get("../src/test/resources/upload-test-file.txt"); + final UploadChannel channel = UploadServices.upload(service, path) + .openChannel(); + final SeekableByteChannel inChannel = Files.newByteChannel(path); + + try { + ChannelUtils.copy(inChannel, channel); + } finally { + inChannel.close(); + channel.close(); + } + + System.out.println(channel.getDownloadLink()); + Assert.assertNotNull(channel.getDownloadLink()); + } +} diff --git a/httpchannel-util/src/main/java/com/rogiel/httpchannel/util/Filesizes.java b/httpchannel-util/src/main/java/com/rogiel/httpchannel/util/Filesizes.java index fb1bab5..34c004a 100644 --- a/httpchannel-util/src/main/java/com/rogiel/httpchannel/util/Filesizes.java +++ b/httpchannel-util/src/main/java/com/rogiel/httpchannel/util/Filesizes.java @@ -5,18 +5,31 @@ package com.rogiel.httpchannel.util; /** * @author Rogiel - * + * */ public class Filesizes { - public static long kb(long kb) { - return kb * 1024; + public static long kb(double kb) { + return (long) (kb * 1024); } - - public static long mb(long mb) { + + public static long mb(double mb) { return kb(mb) * 1024; } - - public static long gb(long gb) { + + public static long gb(double gb) { return mb(gb) * 1024; } + + public static long auto(double value, String unit) { + unit = unit.toUpperCase().trim().substring(0, 1); + switch (unit) { + case "K": + return kb(value); + case "M": + return mb(value); + case "G": + return gb(value); + } + return 0; + } } diff --git a/httpchannel-util/src/main/java/com/rogiel/httpchannel/util/htmlparser/HTMLPage.java b/httpchannel-util/src/main/java/com/rogiel/httpchannel/util/htmlparser/HTMLPage.java index 4f67f8d..ab8a4c3 100644 --- a/httpchannel-util/src/main/java/com/rogiel/httpchannel/util/htmlparser/HTMLPage.java +++ b/httpchannel-util/src/main/java/com/rogiel/httpchannel/util/htmlparser/HTMLPage.java @@ -75,6 +75,10 @@ public class HTMLPage { return filtered; } + public boolean containsPlain(Pattern pattern) { + return pattern.matcher(asString()).find(); + } + public boolean contains(final Pattern pattern) { return !filter(Node.class, new ContainsFilter(pattern)).isEmpty(); } @@ -90,6 +94,21 @@ public class HTMLPage { .quote(text.toLowerCase())))).isEmpty(); } + public String findPlain(final Pattern pattern, int n) { + final Matcher matcher = pattern.matcher(asString()); + if (matcher.find()) + return matcher.group(n); + return null; + } + + public int findIntPlain(final Pattern pattern, int n) { + return Integer.parseInt(findPlain(pattern, n)); + } + + public double findDoublePlain(final Pattern pattern, int n) { + return Double.parseDouble(findPlain(pattern, n)); + } + public String find(final Pattern pattern, int n) { for (final Node tag : filter(Tag.class, new ContainsFilter(pattern))) { final Matcher matcher = pattern.matcher(tag.getText()); @@ -200,6 +219,10 @@ public class HTMLPage { return ((TextareaTag) getTagByID(id)).getStringText(); } + public String getTextareaValueByName(String name) { + return ((TextareaTag) getTagByName(name)).getStringText(); + } + public Tag getTagByID(final String id) { for (final Tag tag : filter(Tag.class, new IDFilter(id))) { return tag; @@ -257,4 +280,25 @@ public class HTMLPage { return null; } } + + public String asString() { + StringBuffer buff = new StringBuffer(); + for (int i = 0; i < nodes.size(); i++) { + // final String content = nodes.elementAt(i).toPlainTextString() + // .replaceAll("\n", "").replaceAll("\\t", "").trim(); + // if (content.length() > 0) { + // buff.append(" ").append(content); + // } + final String[] lines = nodes.elementAt(i).toPlainTextString() + .split("\n"); + for (final String line : lines) { + final String processed = line.trim(); + if (processed.length() > 0) { + buff.append(line.trim()).append(" "); + } + } + + } + return buff.toString(); + } }