Permalink
Browse files

Unit Test Changes

  • Loading branch information...
1 parent 368aa9e commit e9d54339e3e8bd2e1c2c4e907a0b9fd7063f6151 @stuckless stuckless committed Sep 6, 2016
View
@@ -0,0 +1,5 @@
+root = true
+
+[*.java]
+indent_style = space
+indent_size = 2
View
@@ -120,6 +120,7 @@ sourceSets {
dependencies {
testCompile 'org.testng:testng:6.1.1'
+ testCompile "org.mockito:mockito-core:1.9.5"
compile files(
'third_party/UPnPLib/sbbi-upnplib-1.0.3.jar',
@@ -142,8 +143,12 @@ dependencies {
test {
useTestNG()
reports.html.destination = file("$sageBuildDir/reports/testng")
- systemProperty 'io_test_file', "$sageBuildDir/delete-io-testng."
- systemProperty 'nio_test_file', "$sageBuildDir/delete-nio-testng."
+ systemProperty 'io_test_file', "delete-io-testng."
+ systemProperty 'nio_test_file', "delete-nio-testng."
+ systemProperty 'sage.testing', "true"
+
+ workingDir = file("$sageBuildDir/sagetv_test/");
+ workingDir.mkdirs();
}
/**
@@ -18,12 +18,65 @@
import sage.io.RemoteSageFile;
import sage.io.SageInputStream;
+import java.io.BufferedReader;
+import java.io.Closeable;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+
public class IOUtils
{
private IOUtils()
{
}
+ /**
+ * Used to read a URL contents into String. This operation blocks and the content is a string, so it should
+ * not be used on large files, and should not be used on binary files.
+ *
+ * @param urlStr URL to download
+ * @return String contents of the given URL
+ */
+ public static String getUrlAsString(String urlStr)
+ {
+ StringBuilder sb = new StringBuilder();
+ try
+ {
+ URL url = new URL(urlStr);
+ InputStream is = url.openStream();
+ BufferedReader br = new BufferedReader(new InputStreamReader(is));
+
+ String line;
+ while ( (line = br.readLine()) != null)
+ sb.append(line);
+
+ closeQuietly(br);
+ closeQuietly(is);
+ }
+ catch (Exception e)
+ {
+ if (Sage.DBG)
+ {
+ e.printStackTrace();
+ }
+ }
+ return sb.toString();
+ }
+
+ public static void closeQuietly(Closeable closeable)
+ {
+ if (closeable!=null)
+ {
+ try
+ {
+ closeable.close();
+ } catch (IOException e)
+ {
+ }
+ }
+ }
+
public static String getFileExtension(java.io.File f)
{
String s = f == null ? "" : f.toString();
@@ -0,0 +1,19 @@
+package sage;
+
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.*;
+import static sage.TestUtils.*;
+
+/**
+ * Created by seans on 03/09/16.
+ */
+public class IOUtilsTest
+{
+ @Test
+ public void testGetUrlAsString() throws Exception
+ {
+ String md5 = IOUtils.getUrlAsString(file2url(getTestResource("test-jar-1.0.zip.md5")));
+ assertEquals(md5, "beec1608cf997d3acb42f0ab772b143f");
+ }
+}
@@ -0,0 +1,105 @@
+package sage;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+/**
+ * Created by seans on 04/09/16.
+ */
+public class TestUtils
+{
+ public static File JARs = null;
+ public static File STVs = null;
+
+ private static File projectRoot = null;
+ private static File testResourcesRoot = null;
+ /**
+ * Used for Testing. This will call the normal startup() method with but it will not start a full SageTV server.
+ * It just initializes enough of the SageTV subsystem, such as prefs and resources so that most of the code that
+ * doesn't rely on native parts or a running server can still be tested.
+ *
+ * @throws Throwable
+ */
+ public static void initializeSageTVForTesting() throws Throwable
+ {
+ if (System.getProperty("sage.testing.started")!=null)
+ {
+ // the test stub has been started, so just exit since there is no point
+ // in doing this again during a test run
+ return;
+ }
+ System.setProperty("sage.testing.started", "true");
+
+ Native.LOG_NATIVE_FAILURES=false;
+
+ System.setProperty("java.awt.headless", "true");
+
+ Sage.TESTING=true;
+ Sage.client=false;
+ Sage.EMBEDDED=false;
+ Sage.USE_HIRES_TIME=false;
+
+ // create some important files/dirs for testing
+ JARs = new File("JARs");
+ STVs = new File("STVs");
+ JARs.mkdirs();
+ STVs.mkdirs();
+
+ // create the startup params
+ String mainWnd="0";
+ String stdoutHandle="0";
+ String sysArgs = "-startup"; // can also accept -properties
+ String appName = "sagetv sagetv";
+ Sage.startup(new String[] {mainWnd, stdoutHandle, sysArgs, appName});
+ }
+
+ /**
+ * Returns a resource file relative to the test/resources/ directory
+ * @param name
+ * @return
+ */
+ public static File getTestResource(String name)
+ {
+ if (projectRoot==null) getProjectRoot();
+ return new File(testResourcesRoot, name);
+ }
+
+ /**
+ * Returns the file that is the ROOT of the project files
+ *
+ * @return
+ */
+ public static File getProjectRoot()
+ {
+ if (projectRoot==null)
+ {
+ // TEST Root is buildoutput/sagetv_test/ so we go back 2 dirs to get project root
+ projectRoot = new File("../../");
+ testResourcesRoot = new File(projectRoot, "test/resources/");
+ if (!testResourcesRoot.exists())
+ {
+ // check if CWD is actually the project root (should not be, but sometimes if you run tests in the IDE ie might)
+ projectRoot = new File(".");
+ testResourcesRoot = new File(projectRoot, "test/resources/");
+ if (!testResourcesRoot.exists())
+ {
+ throw new RuntimeException("Unable to determine the Project Root. Current Dir is: " + projectRoot.getAbsolutePath());
+ }
+ }
+ }
+ return projectRoot;
+ }
+
+ public static String file2url(File file)
+ {
+ try
+ {
+ return file.getCanonicalFile().toURI().toURL().toExternalForm();
+ } catch (java.io.IOException e)
+ {
+ e.printStackTrace();
+ }
+ return null;
+ }
+}
@@ -0,0 +1,44 @@
+package sage.plugin;
+
+import org.testng.annotations.Test;
+import sage.Native;
+import sage.TestUtils;
+
+import static org.testng.Assert.*;
+
+/**
+ * Created by seans on 03/09/16.
+ */
+public class PluginWrapperTest
+{
+ @Test
+ public void testGetMD5() throws Exception
+ {
+ // Don't report failed native library loading
+ Native.LOG_NATIVE_FAILURES=false;
+
+ String md5="beec1608cf997d3acb42f0ab772b143f";
+
+ String testUrl = TestUtils.file2url(TestUtils.getTestResource("test-jar-1.0.zip"));
+
+ // verify it will look it up from the base url if the md5 is null
+ verifyMD5(md5, testUrl, null);
+
+ // verify it will look it up from the base url if the md5 is empty
+ verifyMD5(md5, testUrl, " ");
+
+ // verify it can use the http reference to md5
+ verifyMD5(md5, testUrl,
+ testUrl + ".md5");
+
+ // verify that if we give it an actual MD5 it works
+ verifyMD5(md5, "http://doesn.not.exist/file.jar", md5);
+ }
+
+ void verifyMD5(String md5, String jarUrl, String md5Url)
+ {
+ PluginWrapper.Package pack = new PluginWrapper.Package("JAR", jarUrl, md5Url, true);
+ assertEquals(md5Url, pack.getRawMD5());
+ assertEquals(md5, pack.getMD5());
+ }
+}
Binary file not shown.
@@ -0,0 +1 @@
+beec1608cf997d3acb42f0ab772b143f

0 comments on commit e9d5433

Please sign in to comment.