Java tutorial
/* * Copyright 2013 Sigurd Randoll <srandoll@digiway.de>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package de.digiway.rapidbreeze.server.model.storage.provider; import de.digiway.rapidbreeze.server.model.storage.AuthenticationStatus; import de.digiway.rapidbreeze.server.model.storage.FileStatus; import de.digiway.rapidbreeze.server.model.storage.UrlStatus; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.commons.io.IOUtils; import org.junit.Test; import org.junit.Before; import static org.junit.Assert.*; import org.junit.Assume; /** * * @author Sigurd Randoll <srandoll@digiway.de> */ public class ShareOnlineBizTest { private ShareOnlineBiz storageDownload = null; private URL url; private static final Logger LOG = Logger.getLogger(ShareOnlineBizTest.class.getName()); @Before public void setup() throws IOException { boolean hasHoster = StorageDownloadTestUtil.hasStorageDownload(ShareOnlineBizTest.class); if (!hasHoster) { LOG.log(Level.WARNING, "Not going to execute test class:{0} because properties are missing", ShareOnlineBizTest.class.getSimpleName()); } Assume.assumeTrue(hasHoster); storageDownload = new ShareOnlineBiz(); storageDownload.authenticate(StorageDownloadTestUtil.getUsername(ShareOnlineBizTest.class), StorageDownloadTestUtil.getPassword(ShareOnlineBizTest.class)); url = new URL(StorageDownloadTestUtil.getTestLink(ShareOnlineBizTest.class)); } @Test public void testAuthenticate() { storageDownload = new ShareOnlineBiz(); assertEquals(AuthenticationStatus.NOT_AUTHENTICATED, storageDownload.getAuthenticationStatus()); storageDownload.authenticate("wrong", "wrong"); assertEquals(AuthenticationStatus.AUTHENTICATION_FAILED, storageDownload.getAuthenticationStatus()); storageDownload.authenticate(StorageDownloadTestUtil.getUsername(ShareOnlineBizTest.class), StorageDownloadTestUtil.getPassword(ShareOnlineBizTest.class)); assertEquals(AuthenticationStatus.AUTHENTICATED, storageDownload.getAuthenticationStatus()); } @Test public void testStart() throws MalformedURLException, IOException { // Test start from beginning: InputStream is = storageDownload.start(url, 0); assertEquals(StorageDownloadTestUtil.getTestContent(ShareOnlineBizTest.class), IOUtils.toString(is)); is.close(); // Test resume: is = storageDownload.start(url, 5); String read = IOUtils.toString(is); String substring = StorageDownloadTestUtil.getTestContent(ShareOnlineBizTest.class).substring(5); assertEquals(substring, read); } @Test public void testCanHandle() throws MalformedURLException { URL good = new URL(StorageDownloadTestUtil.getTestLink(ShareOnlineBizTest.class)); URL bad = new URL("http://www.google.de/here"); assertTrue(storageDownload.canHandle(good)); assertFalse(storageDownload.canHandle(bad)); } @Test public void testGetUrlStatus() throws MalformedURLException { UrlStatus urlStatus = storageDownload.getUrlStatus(url); assertEquals(FileStatus.OK, urlStatus.getFileStatus()); // TODO: Asserts... URL url = new URL("http://www.share-online.biz/dl/NONEXISTING"); urlStatus = storageDownload.getUrlStatus(url); assertEquals(FileStatus.NOT_AVAILABLE, urlStatus.getFileStatus()); } }