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 com.google.gerrit.extensions.annotations.Exports; 18 : import com.google.gerrit.extensions.events.LifecycleListener; 19 : import com.google.gerrit.server.DynamicOptions; 20 : import com.google.gerrit.server.restapi.change.QueryChanges; 21 : import com.google.gerrit.sshd.commands.Query; 22 : import com.google.inject.AbstractModule; 23 : import com.google.inject.Inject; 24 : import com.google.inject.Singleton; 25 : import com.google.inject.internal.UniqueAnnotations; 26 : import java.util.Collections; 27 : import org.kohsuke.args4j.Option; 28 : 29 2 : public class AbstractLifecycleListenersTest extends AbstractDaemonTest { 30 2 : protected static class SimpleModule extends AbstractModule { 31 : @Override 32 : public void configure() { 33 2 : bind(com.google.gerrit.server.DynamicOptions.DynamicBean.class) 34 2 : .annotatedWith(Exports.named(Query.class)) 35 2 : .to(MyClassNameProvider.class); 36 2 : bind(DynamicOptions.DynamicBean.class) 37 2 : .annotatedWith(Exports.named(QueryChanges.class)) 38 2 : .to(MyClassNameProvider.class); 39 2 : } 40 : } 41 : 42 2 : protected static class MyClassNameProvider implements DynamicOptions.ModulesClassNamesProvider { 43 : @Override 44 : public String getClassName() { 45 2 : return "com.google.gerrit.acceptance.AbstractLifecycleListenersTest$MyOptions"; 46 : } 47 : 48 : @Override 49 : public Iterable<String> getModulesClassNames() { 50 2 : return Collections.singleton( 51 : "com.google.gerrit.acceptance.AbstractLifecycleListenersTest$MyOptions$MyOptionsModule"); 52 : } 53 : } 54 : 55 2 : public static class MyOptions implements DynamicOptions.DynamicBean { 56 : @Option(name = "--opt") 57 : public boolean opt; 58 : 59 2 : public static class MyOptionsModule extends AbstractModule { 60 : @Override 61 : protected void configure() { 62 2 : bind(LifecycleListener.class) 63 2 : .annotatedWith(UniqueAnnotations.create()) 64 2 : .to(MyLifecycleListener.class); 65 2 : } 66 : } 67 : } 68 : 69 : protected static class MyLifecycleListener implements LifecycleListener { 70 : protected final InvocationCheck invocationCheck; 71 : 72 : @Inject 73 2 : public MyLifecycleListener(InvocationCheck invocationCheck) { 74 2 : this.invocationCheck = invocationCheck; 75 2 : } 76 : 77 : @Override 78 : public void start() { 79 2 : invocationCheck.setStartInvoked(true); 80 2 : } 81 : 82 : @Override 83 : public void stop() { 84 2 : invocationCheck.setStopInvoked(true); 85 2 : } 86 : } 87 : 88 : @Singleton 89 2 : public static class InvocationCheck { 90 2 : private boolean isStartInvoked = false; 91 2 : private boolean isStopInvoked = false; 92 : 93 : public boolean isStartInvoked() { 94 2 : return isStartInvoked; 95 : } 96 : 97 : public void setStartInvoked(boolean startInvoked) { 98 2 : isStartInvoked = startInvoked; 99 2 : } 100 : 101 : public boolean isStopInvoked() { 102 2 : return isStopInvoked; 103 : } 104 : 105 : public void setStopInvoked(boolean stopInvoked) { 106 2 : isStopInvoked = stopInvoked; 107 2 : } 108 : } 109 : }