Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions src/Xamarin.Android.Tools.Bytecode/ClassPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,31 +233,38 @@ void FixupParametersFromDocs (XElement api)
}
}

JavaDocletType GetDocletType (string path)
public JavaDocletType GetDocletType (string path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be made static, and moved to AndroidDocScraper.

{
var kind = JavaDocletType.DroidDoc;
char [] buf = new char [500];

string packagesHtml = Path.Combine (path, "packages.html");
if (File.Exists (packagesHtml) && File.ReadAllText (packagesHtml).Contains ("<body class=\"gc-documentation develop reference api "))
kind = JavaDocletType.DroidDoc2;
using (var reader = File.OpenText (Path.Combine (path, "index.html")))
reader.ReadBlock (buf, 0, buf.Length);
string rawHTML = new string (buf);
if (rawHTML.Contains ("Generated by javadoc (build 1.6"))
kind = JavaDocletType.Java6;
else if (rawHTML.Contains ("Generated by javadoc (version 1.7"))
kind = JavaDocletType.Java7;
else if (rawHTML.Contains ("Generated by javadoc (1.8"))
kind = JavaDocletType.Java8;

string indexHtml = Path.Combine (path, "index.html");
if (File.Exists (indexHtml)) {
using (var reader = File.OpenText (indexHtml))
reader.ReadBlock (buf, 0, buf.Length);
string rawHTML = new string (buf);
if (rawHTML.Contains ("Generated by javadoc (build 1.6"))
kind = JavaDocletType.Java6;
else if (rawHTML.Contains ("Generated by javadoc (version 1.7"))
kind = JavaDocletType.Java7;
else if (rawHTML.Contains ("Generated by javadoc (1.8"))
kind = JavaDocletType.Java8;
}

// Check to see if it's an api.xml formatted doc
string rawXML = null;
using (var reader = File.OpenText (path)) {
int len = reader.ReadBlock (buf, 0, buf.Length);
rawXML = new string (buf, 0, len);
if (File.Exists (path)) {
string rawXML = null;
using (var reader = File.OpenText (path)) {
int len = reader.ReadBlock (buf, 0, buf.Length);
rawXML = new string (buf, 0, len);
}
if (rawXML.Contains ("<api>") && rawXML.Contains ("<package"))
kind = JavaDocletType._ApiXml;
}
if (rawXML.Contains ("<api>") && rawXML.Contains ("<package"))
kind = JavaDocletType._ApiXml;

return kind;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Xamarin.Android.Tools.Bytecode/Tests/ClassFileFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ protected static string LoadString (string resource)
return r.ReadToEnd ();
}

protected static string LoadToTempFile (string resource)
{
var tempFilePath = Path.GetTempFileName ();

using (var w = File.Create (tempFilePath))
using (var s = Assembly.GetExecutingAssembly ().GetManifestResourceStream (resource))
s.CopyTo (w);

return tempFilePath;
}

protected static void AssertXmlDeclaration (string classResource, string xmlResource, string documentationPath = null, JavaDocletType? javaDocletType = null)
{
var classPathBuilder = new ClassPath () {
Expand Down Expand Up @@ -67,6 +78,15 @@ protected static void AssertXmlDeclaration (string[] classResources, string xmlR

Assert.AreEqual (expected, actual.ToString ());
}

protected static void AssertDocletType (string path, JavaDocletType docletType)
{
var classPathBuilder = new ClassPath ();

var actual = classPathBuilder.GetDocletType(path);

Assert.AreEqual (docletType, actual);
}
}
}

59 changes: 44 additions & 15 deletions src/Xamarin.Android.Tools.Bytecode/Tests/ParameterFixupTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,20 @@ public void XmlDeclaration_FixedUpFromDocumentation()
}

[Test]
public void XmlDeclaration_FixedUpFromApiXmlDocumentation()
public void XmlDeclaration_FixedUpFromApiXmlDocumentation ()
{
string tempFile = null;

try
{
tempFile = Path.GetTempFileName();
File.WriteAllText(tempFile, LoadString("ParameterFixupApiXmlDocs.xml"));
try {
tempFile = LoadToTempFile ("ParameterFixupApiXmlDocs.xml");

AssertXmlDeclaration("Collection.class", "ParameterFixupFromDocs.xml", tempFile, Bytecode.JavaDocletType._ApiXml);
}
catch (Exception ex)
{
try
{
if (File.Exists(tempFile))
File.Delete(tempFile);
AssertXmlDeclaration ("Collection.class", "ParameterFixupFromDocs.xml", tempFile);
} finally {
try {
if (File.Exists (tempFile))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be in the catch block, this should be in a finally block, so that we always delete tempFile. (Looks like this was a bug in the original code.)

File.Delete (tempFile);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't need to be wrapped in try/catch.

}
catch { }

Assert.Fail("An unexpected exception was thrown : {0}", ex);
}
}

Expand All @@ -63,6 +56,42 @@ public void XmlDeclaration_DoesNotThrowAnExceptionIfDocumentationNotFound ()
Assert.Fail ("An unexpected exception was thrown : {0}", ex);
}
}

[Test]
public void DocletType_ShouldDetectApiXml ()
{
string tempFile = null;

try {
tempFile = LoadToTempFile ("ParameterFixupApiXmlDocs.xml");

AssertDocletType (tempFile, Bytecode.JavaDocletType._ApiXml);
} finally {
try {
if (File.Exists (tempFile))
File.Delete (tempFile);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't need to wrap this in try/catch.

}
catch { }
}
}

[Test]
public void DocletType_ShouldDetectDroidDocs ()
{
var androidSdkPath = Environment.GetEnvironmentVariable ("ANDROID_SDK_PATH");
if (string.IsNullOrEmpty (androidSdkPath)) {
Assert.Ignore("The `ANDROID_SDK_PATH` environment variable isn't set; " +
"cannot test importing parameter names from HTML. Skipping...");
return;
}

var droidDocsPath = Path.Combine (androidSdkPath, "docs", "reference");

if (!Directory.Exists (droidDocsPath))
Assert.Fail("The Android SDK Documentation path `{0}` was not found.", droidDocsPath);

AssertDocletType (droidDocsPath, Bytecode.JavaDocletType.DroidDoc2);
}
}
}