Merge branch 'stable-2.12'

* stable-2.12:
  Add a test for Asciidoctor document titles / sections
  Add correct license for asciidoctor
  Make the basicTextFormattingWorks() test more strict
  Add tests to check that empty input renders to empty output
  AsciidoctorFormatterTest: Fix order of expected and actual results
  AsciidoctorFormatterTest: Do not pass baseDir to AsciidoctorFormatter()
  AsciidoctorFormatter: Simplify code by using the Asciidoctor >= 1.5 API
  Upgrade asciidoctor and its jruby dependency
  Add a unit test for the AsciidoctorFormatter
  Upgrade asciidoctor and its jruby dependency
  MarkdownFormatterTest: Fix order of expected and actual results
  MarkdownFormatterTest: Make a few member variables local ones
  xdocs: Add a unit test for the MarkdownFormatter

Change-Id: I99578f15906a3fa7a75d9ea500d502f1948b8720
diff --git a/BUCK b/BUCK
index 2bd5f04..46cc0f6 100644
--- a/BUCK
+++ b/BUCK
@@ -38,3 +38,12 @@
   deps = [':x-docs__plugin'],
 )
 
+java_test(
+  name = 'x-docs_tests',
+  srcs = glob(['src/test/java/**/*.java']),
+  labels = ['xdocs'],
+  source_under_test = [':x-docs__plugin'],
+  deps = GERRIT_PLUGIN_API + GERRIT_TESTS + [
+    ':x-docs__plugin',
+  ],
+)
diff --git a/lib/asciidoctor/BUCK b/lib/asciidoctor/BUCK
index 701af29..0b23fdf 100644
--- a/lib/asciidoctor/BUCK
+++ b/lib/asciidoctor/BUCK
@@ -1,5 +1,6 @@
 include_defs('//bucklets/maven_jar.bucklet')
 
+define_license('asciidoctor')
 define_license('jruby')
 
 java_library(
@@ -13,16 +14,16 @@
 
 maven_jar(
   name = 'asciidoctor',
-  id = 'org.asciidoctor:asciidoctorj:1.5.1',
-  sha1 = 'f31c95e557ee185c935fd3764e83b01c81a3b41f',
-  license = 'Apache2.0',
+  id = 'org.asciidoctor:asciidoctorj:1.5.4.1',
+  sha1 = 'f7ddfb2bbed2f8da3f9ad0d1a5514f04b4274a5a',
+  license = 'asciidoctor',
   attach_source = False,
 )
 
 maven_jar(
   name = 'jruby',
-  id = 'org.jruby:jruby-complete:1.7.16',
-  sha1 = '8ef6c848cc5491c5f22ddad4532c68fe778421f6',
+  id = 'org.jruby:jruby-complete:1.7.25',
+  sha1 = '8eb234259ec88edc05eedab05655f458a84bfcab',
   license = 'jruby',
   local_license = True,
   attach_source = False,
diff --git a/lib/asciidoctor/LICENSE-asciidoctor b/lib/asciidoctor/LICENSE-asciidoctor
new file mode 100644
index 0000000..d7e3a20
--- /dev/null
+++ b/lib/asciidoctor/LICENSE-asciidoctor
@@ -0,0 +1,21 @@
+The MIT License
+
+Copyright (C) 2012-2016 Dan Allen, Ryan Waldron and the Asciidoctor Project
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
diff --git a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
index caa31c4..2da5f69 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/xdocs/formatter/AsciidoctorFormatter.java
@@ -16,27 +16,18 @@
 
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_ALLOW_HTML;
 import static com.googlesource.gerrit.plugins.xdocs.XDocGlobalConfig.KEY_INCLUDE_TOC;
-import static java.nio.charset.StandardCharsets.UTF_8;
 
-import com.google.common.io.ByteStreams;
-import com.google.gerrit.common.TimeUtil;
-import com.google.gerrit.extensions.annotations.PluginData;
 import com.google.inject.Inject;
 import com.google.inject.Singleton;
 
 import com.googlesource.gerrit.plugins.xdocs.ConfigSection;
 
 import org.asciidoctor.Asciidoctor;
-import org.asciidoctor.Attributes;
 import org.asciidoctor.AttributesBuilder;
-import org.asciidoctor.Options;
 import org.asciidoctor.OptionsBuilder;
 import org.asciidoctor.SafeMode;
 
 import java.io.BufferedReader;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.StringReader;
@@ -50,15 +41,14 @@
   private static final String DOCTYPE = "article";
   private static final String ERUBY = "erb";
 
-  private final File baseDir;
   private final Properties attributes;
   private final FormatterUtil util;
   private final Formatters formatters;
 
   @Inject
-  public AsciidoctorFormatter(@PluginData File baseDir,
-      FormatterUtil formatterUtil, Formatters formatters) throws IOException {
-    this.baseDir = baseDir;
+  public AsciidoctorFormatter(
+      FormatterUtil formatterUtil,
+      Formatters formatters) throws IOException {
     this.attributes = readAttributes();
     this.util = formatterUtil;
     this.formatters = formatters;
@@ -73,25 +63,9 @@
 
     ConfigSection projectCfg =
         formatters.getFormatterConfig(NAME, projectName);
-    // asciidoctor ignores all attributes if no output file is specified,
-    // this is why we must specify an output file and then read its content
-    File tmpDir = new File(baseDir, "tmp");
-    tmpDir.mkdirs();
-    File tmpFile = File.createTempFile("asciidoctor-", null, tmpDir);
-    try {
-      Asciidoctor.Factory.create(AsciidoctorFormatter.class.getClassLoader())
-          .render(raw, createOptions(projectCfg, abbrRev, tmpFile));
-      try (FileInputStream input = new FileInputStream(tmpFile)) {
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        ByteStreams.copy(input, out);
-        String html = out.toString(UTF_8.name());
-        return util.applyCss(html, NAME, projectName);
-      }
-    } finally {
-      if (!tmpFile.delete()) {
-        tmpFile.deleteOnExit();
-      }
-    }
+    String html = Asciidoctor.Factory.create(AsciidoctorFormatter.class.getClassLoader())
+        .convert(raw, createOptions(projectCfg, abbrRev));
+    return util.applyCss(html, NAME, projectName);
   }
 
   private String suppressHtml(String raw) throws IOException {
@@ -112,19 +86,17 @@
     }
   }
 
-  private Options createOptions(ConfigSection cfg, String revision, File out) {
+  private OptionsBuilder createOptions(ConfigSection cfg, String revision) {
     return OptionsBuilder.options()
         .backend(BACKEND)
         .docType(DOCTYPE)
         .eruby(ERUBY)
         .safe(SafeMode.SECURE)
         .attributes(getAttributes(cfg, revision))
-        .mkDirs(true)
-        .toFile(out)
-        .get();
+        .mkDirs(true);
   }
 
-  private Attributes getAttributes(ConfigSection cfg, String revision) {
+  private AttributesBuilder getAttributes(ConfigSection cfg, String revision) {
     AttributesBuilder ab = AttributesBuilder.attributes()
         .tableOfContents(cfg.getBoolean(KEY_INCLUDE_TOC, true))
         .sourceHighlighter("prettify");
@@ -133,7 +105,7 @@
     }
     ab.attribute("last-update-label!");
     ab.attribute("revnumber", revision);
-    return ab.get();
+    return ab;
   }
 
   private static Properties readAttributes() throws IOException {
diff --git a/src/main/resources/Documentation/about.md b/src/main/resources/Documentation/about.md
index 2749ff4..6be38e2 100644
--- a/src/main/resources/Documentation/about.md
+++ b/src/main/resources/Documentation/about.md
@@ -59,7 +59,7 @@
       <a href="http://www.methods.co.nz/asciidoc/userguide.html">Aciidoc</a>
     </td>
     <td>
-      asciidoctorj: <a href="../../../Documentation/licenses.html#Apache2_0">Apache2.0</a><br/>
+      asciidoctorj: <a href="licenses.html#MIT-asciidoctor">MIT License</a><br/>
       jruby: <a href="licenses.html#EPL1_0">Eclipse Public License 1.0</a>
     </td>
     <td>
diff --git a/src/main/resources/Documentation/licenses.md b/src/main/resources/Documentation/licenses.md
index 4e7b72c..cdb02a8 100644
--- a/src/main/resources/Documentation/licenses.md
+++ b/src/main/resources/Documentation/licenses.md
@@ -94,3 +94,32 @@
 
 This Agreement is governed by the laws of the State of New York and the intellectual property laws of the United States of America. No party to this Agreement will bring a legal action under this Agreement more than one year after the cause of action arose. Each party waives its rights to a jury trial in any resulting litigation.
 ````
+
+<a id="MIT-asciidoctor">
+MIT License of Asciidoctor
+--------------------------
+
+````
+The MIT License
+
+Copyright (C) 2012-2016 Dan Allen, Ryan Waldron and the Asciidoctor Project
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+````
+
diff --git a/src/test/java/com/googlesource/gerrit/plugins/x-docs/AsciidoctorFormatterTest.java b/src/test/java/com/googlesource/gerrit/plugins/x-docs/AsciidoctorFormatterTest.java
new file mode 100644
index 0000000..236eeb4
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/x-docs/AsciidoctorFormatterTest.java
@@ -0,0 +1,93 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// 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 com.googlesource.gerrit.plugins.xdocs.formatter;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertEquals;
+
+import com.googlesource.gerrit.plugins.xdocs.ConfigSection;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.lang.StringUtils;
+import org.easymock.IAnswer;
+import org.junit.Before;
+import org.junit.Test;
+
+public class AsciidoctorFormatterTest {
+
+  private ConfigSection cfg;
+  private AsciidoctorFormatter formatter;
+
+  @Before
+  public void setUp() throws IOException {
+    FormatterUtil util = createNiceMock(FormatterUtil.class);
+
+    // To simplify things, make applyCss() a no-op and return the HTML code as-is.
+    expect(util.applyCss(anyString(), anyString(), anyString())).andAnswer(
+      new IAnswer<String>() {
+        @Override
+        public String answer() throws Throwable {
+          // The first argument is the HTML code.
+          return (String) getCurrentArguments()[0];
+        }
+      }
+    );
+
+    replay(util);
+
+    cfg = createNiceMock(ConfigSection.class);
+
+    // Do not expect any behavior from the ConfigSection itself.
+    replay(cfg);
+
+    Formatters formatters = createNiceMock(Formatters.class);
+
+    // Avoid a NPE by just returning the ConfigSection mock object.
+    expect(formatters.getFormatterConfig(anyString(), anyString())).andReturn(cfg);
+
+    replay(formatters);
+
+    formatter = new AsciidoctorFormatter(util, formatters);
+  }
+
+  @Test
+  public void emptyInputRendersNothing() throws IOException {
+    assertEquals(StringUtils.EMPTY, formatter.format(null, null, null, null, cfg, StringUtils.EMPTY));
+  }
+
+  @Test
+  public void basicTextFormattingWorks() throws IOException {
+    String raw = "_italic_ *bold* `monospace`";
+    String formatted = "<div class=\"paragraph\">\n<p><em>italic</em> <strong>bold</strong> <code>monospace</code></p>\n</div>";
+    assertEquals(formatted, formatter.format(null, null, null, null, cfg, raw));
+  }
+
+  @Test
+  public void documentTitleIsNotRenderedAsPartOfSections() throws IOException {
+    String raw = "= Document Title (Level 0)\n\n== Level 1 Section Title\n\n=== Level 2 Section Title";
+    String formatted =
+      "<div class=\"sect1\">\n" +
+      "<h2 id=\"_level_1_section_title\">Level 1 Section Title</h2>\n" +
+      "<div class=\"sectionbody\">\n" +
+      "<div class=\"sect2\">\n" +
+      "<h3 id=\"_level_2_section_title\">Level 2 Section Title</h3>\n\n" +
+      "</div>\n" +
+      "</div>\n" +
+      "</div>";
+    assertEquals(formatted, formatter.format(null, null, null, null, cfg, raw));
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/x-docs/MarkdownFormatterTest.java b/src/test/java/com/googlesource/gerrit/plugins/x-docs/MarkdownFormatterTest.java
new file mode 100644
index 0000000..8294f30
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/x-docs/MarkdownFormatterTest.java
@@ -0,0 +1,71 @@
+// Copyright (C) 2016 The Android Open Source Project
+//
+// 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 com.googlesource.gerrit.plugins.xdocs.formatter;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertEquals;
+
+import com.googlesource.gerrit.plugins.xdocs.ConfigSection;
+
+import java.io.IOException;
+
+import org.apache.commons.lang.StringUtils;
+import org.junit.Before;
+import org.junit.Test;
+
+public class MarkdownFormatterTest {
+
+  private static final String PROLOG = "<html><head><style type=\"text/css\">\n\n</style></head><body>\n";
+  private static final String EPILOG = "\n</body></html>";
+
+  private ConfigSection cfg;
+  private MarkdownFormatter formatter;
+
+  @Before
+  public void setUp() throws IOException {
+    FormatterUtil util = createNiceMock(FormatterUtil.class);
+
+    // For easier result comparison, avoid the internal MarkdownFormatter to apply the default CSS.
+    expect(util.getInheritedCss(anyString(), anyString(), anyString(), anyString())).andReturn("");
+
+    replay(util);
+
+    cfg = createNiceMock(ConfigSection.class);
+
+    // Do not expect any behavior from the ConfigSection itself.
+    replay(cfg);
+
+    Formatters formatters = createNiceMock(Formatters.class);
+
+    // Avoid a NPE by just returning the ConfigSection mock object.
+    expect(formatters.getFormatterConfig(anyString(), anyString())).andReturn(cfg);
+
+    replay(formatters);
+
+    formatter = new MarkdownFormatter(util, formatters);
+  }
+
+  @Test
+  public void emptyInputRendersNothing() throws IOException {
+    assertEquals(PROLOG + EPILOG, formatter.format(null, null, null, null, cfg, StringUtils.EMPTY));
+  }
+
+  @Test
+  public void basicTextFormattingWorks() throws IOException {
+    String raw = "*italic* **bold** `monospace`";
+    String formatted = PROLOG + "<p><em>italic</em> <strong>bold</strong> <code>monospace</code></p>" + EPILOG;
+    assertEquals(formatted, formatter.format(null, null, null, null, cfg, raw));
+  }
+}