Skip to content

Commit

Permalink
Generate documentation of PulsarProxy automatically (apache#11421)
Browse files Browse the repository at this point in the history
Master Issue: apache#10041

### Motivation
The config object has already been annotated.
We just need to add it to the auto-generated list
  • Loading branch information
315157973 authored Jul 22, 2021
1 parent 2370643 commit 3e34ef1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* 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.proxy.util;

import com.beust.jcommander.Parameters;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.broker.BaseGenerateDocumentation;
import org.apache.pulsar.proxy.server.ProxyConfiguration;

@Data
@Parameters(commandDescription = "Generate documentation automatically.")
@Slf4j
public class CmdGenerateDocumentation extends BaseGenerateDocumentation {

@Override
public String generateDocumentByClassName(String className) throws Exception {
StringBuilder sb = new StringBuilder();
if (ProxyConfiguration.class.getName().equals(className)) {
return generateDocByFieldContext(className, "Pulsar proxy", sb);
}
return "Class [" + className + "] not found";
}

public static void main(String[] args) throws Exception {
CmdGenerateDocumentation generateDocumentation = new CmdGenerateDocumentation();
generateDocumentation.run(args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* 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.proxy.util;

import static org.testng.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.Field;
import org.apache.pulsar.common.configuration.FieldContext;
import org.testng.annotations.Test;

public class CmdTest {

@Test
public void cmdParserProxyConfigurationTest() throws Exception {
String value = generateDoc("org.apache.pulsar.proxy.server.ProxyConfiguration");
assertTrue(value.contains("Pulsar proxy"));
}

private String generateDoc(String clazz) throws Exception {
PrintStream oldStream = System.out;
try (ByteArrayOutputStream baoStream = new ByteArrayOutputStream(2048);
PrintStream cacheStream = new PrintStream(baoStream);) {
System.setOut(cacheStream);
CmdGenerateDocumentation.main(("-c " + clazz).split(" "));
String message = baoStream.toString();
Class cls = Class.forName(clazz);
Field[] fields = cls.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
FieldContext fieldContext = field.getAnnotation(FieldContext.class);
if (fieldContext == null) {
continue;
}
assertTrue(message.indexOf(field.getName()) > 0);
}
return message;
} finally {
System.setOut(oldStream);
}
}
}

0 comments on commit 3e34ef1

Please sign in to comment.