This is an automated email from the ASF dual-hosted git repository. guangning pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push: new 37c5e17 [website][pulsar]generate docs for pulsar subcommand: compact-topic (#11230) 37c5e17 is described below commit 37c5e179c6254da8121d483910d37892c278139d Author: Li Li <urfreesp...@gmail.com> AuthorDate: Fri Jul 9 14:38:31 2021 +0800 [website][pulsar]generate docs for pulsar subcommand: compact-topic (#11230) ### Master Issue: #10040 Motivation Support auto generate HTML page for pulsar client cli tool, for example: https://github.com/apache/pulsar/tree/asf-site/content/tools/pulsar-admin ### Modifications generate docs for pulsar subcommand: compact-topic --- .../apache/pulsar/compaction/CompactorTool.java | 11 ++++ .../pulsar/compaction/CompactorToolTest.java | 75 ++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java b/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java index ee2b374..ff01a1d 100644 --- a/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java +++ b/pulsar-broker/src/main/java/org/apache/pulsar/compaction/CompactorTool.java @@ -39,6 +39,7 @@ import org.apache.pulsar.broker.ServiceConfigurationUtils; import org.apache.pulsar.client.api.ClientBuilder; import org.apache.pulsar.client.api.PulsarClient; import org.apache.pulsar.common.configuration.PulsarConfigurationLoader; +import org.apache.pulsar.common.util.CmdGenerateDocs; import org.apache.pulsar.common.util.netty.EventLoopUtil; import org.apache.pulsar.zookeeper.ZooKeeperClientFactory; import org.apache.pulsar.zookeeper.ZookeeperBkClientFactoryImpl; @@ -57,6 +58,9 @@ public class CompactorTool { @Parameter(names = {"-h", "--help"}, description = "Show this help message") private boolean help = false; + + @Parameter(names = {"-g", "--generate-docs"}, description = "Generate docs") + private boolean generateDocs = false; } public static void main(String[] args) throws Exception { @@ -71,6 +75,13 @@ public class CompactorTool { System.exit(-1); } + if (arguments.generateDocs) { + CmdGenerateDocs cmd = new CmdGenerateDocs("pulsar"); + cmd.addCommand("compact-topic", arguments); + cmd.run(null); + System.exit(-1); + } + // init broker config ServiceConfiguration brokerConfig; if (isBlank(arguments.brokerConfigFile)) { diff --git a/pulsar-broker/src/test/java/org/apache/pulsar/compaction/CompactorToolTest.java b/pulsar-broker/src/test/java/org/apache/pulsar/compaction/CompactorToolTest.java new file mode 100644 index 0000000..795cf2b --- /dev/null +++ b/pulsar-broker/src/test/java/org/apache/pulsar/compaction/CompactorToolTest.java @@ -0,0 +1,75 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.pulsar.compaction; + +import static org.testng.Assert.assertTrue; +import com.beust.jcommander.Parameter; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.util.Arrays; +import org.apache.pulsar.common.util.CmdGenerateDocs; +import org.testng.annotations.Test; + +/** + * CompactorTool Tests. + */ +public class CompactorToolTest { + + /** + * Test broker-tool generate docs + * + * @throws Exception + */ + @Test + public void testGenerateDocs() throws Exception { + PrintStream oldStream = System.out; + try { + ByteArrayOutputStream baoStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(baoStream)); + + Class argumentsClass = Class.forName("org.apache.pulsar.compaction.CompactorTool$Arguments"); + + Constructor constructor = argumentsClass.getDeclaredConstructor(); + constructor.setAccessible(true); + Object obj = constructor.newInstance(); + + CmdGenerateDocs cmd = new CmdGenerateDocs("pulsar"); + cmd.addCommand("compact-topic", obj); + cmd.run(null); + + String message = baoStream.toString(); + + Field[] fields = argumentsClass.getDeclaredFields(); + for (Field field : fields) { + boolean fieldHasAnno = field.isAnnotationPresent(Parameter.class); + if (fieldHasAnno) { + Parameter fieldAnno = field.getAnnotation(Parameter.class); + String[] names = fieldAnno.names(); + String nameStr = Arrays.asList(names).toString(); + nameStr = nameStr.substring(1, nameStr.length() - 1); + assertTrue(message.indexOf(nameStr) > 0); + } + } + } finally { + System.setOut(oldStream); + } + } +}