Line data Source code
1 : // Copyright (C) 2020 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.acceptance; 16 : 17 : import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; 18 : import static java.nio.charset.StandardCharsets.UTF_8; 19 : 20 : import com.google.common.collect.Lists; 21 : import com.google.gerrit.extensions.annotations.Exports; 22 : import com.google.gerrit.json.OutputFormat; 23 : import com.google.gerrit.server.DynamicOptions; 24 : import com.google.gerrit.sshd.CommandMetaData; 25 : import com.google.gerrit.sshd.CommandModule; 26 : import com.google.gerrit.sshd.SshCommand; 27 : import com.google.gson.reflect.TypeToken; 28 : import com.google.inject.AbstractModule; 29 : import com.google.inject.Inject; 30 : import java.io.BufferedWriter; 31 : import java.io.OutputStream; 32 : import java.io.OutputStreamWriter; 33 : import java.io.PrintWriter; 34 : import java.util.Collections; 35 : import java.util.List; 36 : 37 1 : public class AbstractDynamicOptionsTest extends AbstractDaemonTest { 38 : protected static final String LS_SAMPLES = "ls-samples"; 39 : 40 : protected interface Bean { 41 : void setSamples(List<String> samples); 42 : } 43 : 44 1 : protected static class ListSamples implements Bean, DynamicOptions.BeanReceiver { 45 1 : protected List<String> samples = Collections.emptyList(); 46 : 47 : @Override 48 : public void setSamples(List<String> samples) { 49 1 : this.samples = samples; 50 1 : } 51 : 52 : public void display(OutputStream displayOutputStream) throws Exception { 53 1 : PrintWriter stdout = 54 : new PrintWriter(new BufferedWriter(new OutputStreamWriter(displayOutputStream, UTF_8))); 55 : try { 56 1 : OutputFormat.JSON 57 1 : .newGson() 58 1 : .toJson(samples, new TypeToken<List<String>>() {}.getType(), stdout); 59 1 : stdout.print('\n'); 60 : } finally { 61 1 : stdout.flush(); 62 : } 63 1 : } 64 : 65 : @Override 66 1 : public void setDynamicBean(String plugin, DynamicOptions.DynamicBean dynamicBean) {} 67 : } 68 : 69 : @CommandMetaData(name = LS_SAMPLES, runsAt = MASTER_OR_SLAVE) 70 1 : protected static class ListSamplesCommand extends SshCommand { 71 : @Inject private ListSamples impl; 72 : 73 : @Override 74 : protected void run() throws Exception { 75 1 : impl.display(out); 76 1 : } 77 : 78 : @Override 79 : protected void parseCommandLine(DynamicOptions pluginOptions) throws UnloggedFailure { 80 1 : parseCommandLine(impl, pluginOptions); 81 1 : } 82 : } 83 : 84 1 : public static class PluginOneSshModule extends CommandModule { 85 : @Override 86 : public void configure() { 87 1 : command(LS_SAMPLES).to(ListSamplesCommand.class); 88 1 : } 89 : } 90 : 91 1 : protected static class ListSamplesOptions implements DynamicOptions.BeanParseListener { 92 : @Override 93 : public void onBeanParseStart(String plugin, Object bean) { 94 1 : ((Bean) bean).setSamples(Lists.newArrayList("sample1", "sample2")); 95 1 : } 96 : 97 : @Override 98 1 : public void onBeanParseEnd(String plugin, Object bean) {} 99 : } 100 : 101 1 : protected static class PluginTwoModule extends AbstractModule { 102 : @Override 103 : public void configure() { 104 1 : bind(DynamicOptions.DynamicBean.class) 105 1 : .annotatedWith( 106 1 : Exports.named("com.google.gerrit.acceptance.AbstractDynamicOptionsTest.ListSamples")) 107 1 : .to(ListSamplesOptionsClassNameProvider.class); 108 1 : } 109 : } 110 : 111 1 : protected static class ListSamplesOptionsClassNameProvider 112 : implements DynamicOptions.ClassNameProvider { 113 : @Override 114 : public String getClassName() { 115 1 : return "com.google.gerrit.acceptance.AbstractDynamicOptionsTest$ListSamplesOptions"; 116 : } 117 : } 118 : }