+ * Key: username + */ + private String VALID_USERNAME; + /** + * See src/test/resources/config/hotfile.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 { + // MegaUploadServiceConfiguration.class; + service = new HotFileService( + ServiceConfigurationHelper + .defaultConfiguration(HotFileServiceConfiguration.class)); + + final Properties properties = new Properties(); + properties.load(new FileInputStream( + "src/test/resources/config/hotfile.properties")); + VALID_USERNAME = properties.getProperty("username"); + VALID_PASSWORD = properties.getProperty("password"); + } + + @Test + public void testServiceId() { + System.out.println("Service: " + service.toString()); + assertEquals("hotfile", service.getId()); + } + + @Test + public void testValidAuthenticator() throws IOException { + Assert.assertTrue(((AuthenticationService) service).getAuthenticator( + new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); + } + + @Test + public void testInvalidAuthenticator() throws IOException { + Assert.assertFalse(((AuthenticationService) service).getAuthenticator( + new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login(null)); + } + + @Test + public void testNonLoguedInUploader() throws IOException { + assertTrue( + "This service does not have the capability UploadCapability.FREE_UPLOAD", + ((UploadService) service).getUploadCapabilities().has( + UploaderCapability.NON_PREMIUM_ACCOUNT_UPLOAD)); + final UploadChannel channel = ((UploadService) service).getUploader( + "Upload by httpchannel").upload(new UploadListener() { + @Override + public long getFilesize() { + return new File("simulado_2010_1_res_all.zip").length(); + } + + @Override + public String getFilename() { + return "simulado_2010_1_res_all.zip"; + } + }); + + final FileChannel fileChannel = new FileInputStream( + "simulado_2010_1_res_all.zip").getChannel(); + + copy(fileChannel, channel); + channel.close(); + + System.out.println(channel.getDownloadLink()); + Assert.assertNotNull(channel.getDownloadLink()); + } + + @Test + public void testLoguedInUploader() throws IOException { + assertTrue( + "This service does not have the capability UploadCapability.PREMIUM_UPLOAD", + ((UploadService) service).getUploadCapabilities().has( + UploaderCapability.PREMIUM_ACCOUNT_UPLOAD)); + + Assert.assertTrue(((AuthenticationService) service).getAuthenticator( + new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); + + final UploadChannel channel = ((UploadService) service).getUploader( + "Upload by httpchannel").upload(new UploadListener() { + @Override + public long getFilesize() { + return new File("simulado_2010_1_res_all.zip").length(); + } + + @Override + public String getFilename() { + return "simulado_2010_1_res_all.zip"; + } + }); + + final FileChannel fileChannel = new FileInputStream( + "simulado_2010_1_res_all.zip").getChannel(); + + copy(fileChannel, channel); + channel.close(); + + System.out.println(channel.getDownloadLink()); + Assert.assertNotNull(channel.getDownloadLink()); + } + + @Test + public void testDownloader() throws IOException, MalformedURLException { + final DownloadChannel channel = ((DownloadService) service) + .getDownloader( + new URL( + "http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.html")) + .download(new DownloadListener() { + @Override + public boolean timer(long time, TimerWaitReason reason) { + System.out.println("Waiting " + time + " in " + reason); + // if (reason == TimerWaitReason.DOWNLOAD_TIMER) + // return true; + return true; + } + + @Override + public String captcha(Captcha captcha) { + return null; + } + }); + final ByteArrayOutputStream bout = new ByteArrayOutputStream(); + IOUtils.copy(Channels.newInputStream(channel), bout); + System.out.println(bout.size()); + } + + @Test + public void testLoggedInDownloader() throws IOException, + MalformedURLException { + Assert.assertTrue(((AuthenticationService) service).getAuthenticator( + new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); + + final DownloadChannel channel = ((DownloadService) service) + .getDownloader(new URL("http://hotfile.com/dl/129251605/9b4faf2/simulado_2010_1_res_all.zip.html")) + .download(new DownloadListener() { + @Override + public boolean timer(long time, TimerWaitReason reason) { + System.out.println("Waiting " + time + " in " + reason); + if (reason == TimerWaitReason.DOWNLOAD_TIMER) + return true; + return false; + } + + @Override + public String captcha(Captcha captcha) { + // TODO Auto-generated method stub + return null; + } + }); + + final ByteArrayOutputStream bout = new ByteArrayOutputStream(); + IOUtils.copy(Channels.newInputStream(channel), bout); + System.out.println(bout.size()); + } + + public static void copy(ReadableByteChannel in, WritableByteChannel out) + throws IOException { + // First, we need a buffer to hold blocks of copied bytes. + ByteBuffer buffer = ByteBuffer.allocateDirect(32 * 1024); + + // Now loop until no more bytes to read and the buffer is empty + while (in.read(buffer) != -1 || buffer.position() > 0) { + // The read() call leaves the buffer in "fill mode". To prepare + // to write bytes from the bufferwe have to put it in "drain mode" + // by flipping it: setting limit to position and position to zero + buffer.flip(); + + // Now write some or all of the bytes out to the output channel + out.write(buffer); + + // Compact the buffer by discarding bytes that were written, + // and shifting any remaining bytes. This method also + // prepares the buffer for the next call to read() by setting the + // position to the limit and the limit to the buffer capacity. + buffer.compact(); + } + } +} diff --git a/src/test/java/com/rogiel/httpchannel/service/impl/MegaUploadServiceTest.java b/src/test/java/com/rogiel/httpchannel/service/impl/MegaUploadServiceTest.java index 2185fb4..5e62630 100644 --- a/src/test/java/com/rogiel/httpchannel/service/impl/MegaUploadServiceTest.java +++ b/src/test/java/com/rogiel/httpchannel/service/impl/MegaUploadServiceTest.java @@ -18,7 +18,6 @@ package com.rogiel.httpchannel.service.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.ByteArrayOutputStream; import java.io.File; @@ -37,7 +36,6 @@ import org.junit.Before; import org.junit.Test; import com.rogiel.httpchannel.service.AuthenticationService; -import com.rogiel.httpchannel.service.AuthenticatorListener; import com.rogiel.httpchannel.service.Credential; import com.rogiel.httpchannel.service.DownloadChannel; import com.rogiel.httpchannel.service.DownloadListener; @@ -86,58 +84,20 @@ public class MegaUploadServiceTest { @Test public void testServiceId() { - System.out.println("Service: "+service.toString()); + System.out.println("Service: " + service.toString()); assertEquals("megaupload", service.getId()); } @Test public void testValidAuthenticator() throws IOException { - ((AuthenticationService) service).getAuthenticator( - new Credential(VALID_USERNAME, VALID_PASSWORD)).login( - new AuthenticatorListener() { - @Override - public void invalidCredentials(Credential credential) { - fail("This login attemp should've been successful."); - } - - @Override - public void loginSuccessful(Credential credential) { - } - - @Override - public void logout(Credential credential) { - } - - @Override - public void exception(IOException e) throws IOException { - throw e; - } - }); + Assert.assertTrue(((AuthenticationService) service).getAuthenticator( + new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); } @Test public void testInvalidAuthenticator() throws IOException { - ((AuthenticationService) service).getAuthenticator( - new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login( - new AuthenticatorListener() { - @Override - public void invalidCredentials(Credential credential) { - } - - @Override - public void loginSuccessful(Credential credential) { - fail("This login attemp should't have been successful."); - } - - @Override - public void logout(Credential credential) { - } - - @Override - public void exception(IOException e) throws IOException { - throw e; - } - }); + Assert.assertFalse(((AuthenticationService) service).getAuthenticator( + new Credential(INVALID_USERNAME, INVALID_PASSWORD)).login(null)); } @Test @@ -184,28 +144,8 @@ public class MegaUploadServiceTest { ((UploadService) service).getUploadCapabilities().has( UploaderCapability.PREMIUM_ACCOUNT_UPLOAD)); - ((AuthenticationService) service).getAuthenticator( - new Credential(VALID_USERNAME, VALID_PASSWORD)).login( - new AuthenticatorListener() { - @Override - public void invalidCredentials(Credential credential) { - fail("Invalid credentials"); - } - - @Override - public void loginSuccessful(Credential credential) { - } - - @Override - public void logout(Credential credential) { - } - - @Override - public void exception(IOException e) throws IOException { - // TODO Auto-generated method stub - - } - }); + Assert.assertTrue(((AuthenticationService) service).getAuthenticator( + new Credential(VALID_USERNAME, VALID_PASSWORD)).login(null)); final UploadChannel channel = ((UploadService) service).getUploader( "Upload by httpchannel").upload(new UploadListener() { diff --git a/src/test/resources/log4j.properties b/src/test/resources/log4j.properties new file mode 100644 index 0000000..29ae93a --- /dev/null +++ b/src/test/resources/log4j.properties @@ -0,0 +1,9 @@ +log4j.rootLogger=INFO, stdout + +log4j.appender.stdout=org.apache.log4j.ConsoleAppender +log4j.appender.stdout.layout=org.apache.log4j.PatternLayout +log4j.appender.stdout.layout.ConversionPattern=%5p [%c] %m%n + +log4j.logger.org.apache.http.impl.conn=DEBUG +log4j.logger.org.apache.http.impl.client=DEBUG +log4j.logger.org.apache.http.client=DEBUG \ No newline at end of file