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.extensions.systemstatus.ServerInformation; 20 : import com.google.gerrit.server.DynamicOptions; 21 : import com.google.gerrit.server.config.SitePaths; 22 : import com.google.gerrit.server.util.PluginLogFile; 23 : import com.google.gerrit.server.util.SystemLog; 24 : import com.google.gerrit.sshd.commands.Query; 25 : import com.google.inject.AbstractModule; 26 : import com.google.inject.Inject; 27 : import com.google.inject.Singleton; 28 : import com.google.inject.internal.UniqueAnnotations; 29 : import java.util.Collections; 30 : import org.apache.log4j.AsyncAppender; 31 : import org.apache.log4j.Layout; 32 : import org.apache.log4j.PatternLayout; 33 : import org.eclipse.jgit.lib.Config; 34 : import org.kohsuke.args4j.Option; 35 : 36 1 : public class AbstractPluginLogFileTest extends AbstractDaemonTest { 37 1 : protected static class TestModule extends AbstractModule { 38 : @Override 39 : public void configure() { 40 1 : bind(com.google.gerrit.server.DynamicOptions.DynamicBean.class) 41 1 : .annotatedWith(Exports.named(Query.class)) 42 1 : .to(MyClassNameProvider.class); 43 1 : } 44 : } 45 : 46 1 : protected static class MyClassNameProvider implements DynamicOptions.ModulesClassNamesProvider { 47 : @Override 48 : public String getClassName() { 49 1 : return "com.google.gerrit.acceptance.AbstractPluginLogFileTest$MyOptions"; 50 : } 51 : 52 : @Override 53 : public Iterable<String> getModulesClassNames() { 54 1 : return Collections.singleton( 55 : "com.google.gerrit.acceptance.AbstractPluginLogFileTest$MyOptions$MyOptionsModule"); 56 : } 57 : } 58 : 59 1 : public static class MyOptions implements DynamicOptions.DynamicBean { 60 : @Option(name = "--opt") 61 : public boolean opt; 62 : 63 1 : public static class MyOptionsModule extends AbstractModule { 64 : @Override 65 : protected void configure() { 66 1 : bind(LifecycleListener.class) 67 1 : .annotatedWith(UniqueAnnotations.create()) 68 1 : .to(MyPluginLogFile.class); 69 1 : } 70 : } 71 : } 72 : 73 : protected static class MyPluginLogFile extends PluginLogFile { 74 : protected static final String logName = "test_log"; 75 : 76 : @Inject 77 : public MyPluginLogFile(MySystemLog mySystemLog, ServerInformation serverInfo) { 78 1 : super(mySystemLog, serverInfo, logName, new PatternLayout("[%d] [%t] %m%n")); 79 1 : } 80 : } 81 : 82 : @Singleton 83 : protected static class MySystemLog extends SystemLog { 84 : protected InvocationCounter invocationCounter; 85 : 86 : @Inject 87 : public MySystemLog(SitePaths site, Config config, InvocationCounter invocationCounter) { 88 1 : super(site, config); 89 1 : this.invocationCounter = invocationCounter; 90 1 : } 91 : 92 : @Override 93 : public AsyncAppender createAsyncAppender( 94 : String name, Layout layout, boolean rotate, boolean forPlugin) { 95 1 : invocationCounter.increment(); 96 1 : return super.createAsyncAppender(name, layout, rotate, forPlugin); 97 : } 98 : } 99 : 100 : @Singleton 101 1 : public static class InvocationCounter { 102 1 : private int counter = 0; 103 : 104 : public int getCounter() { 105 1 : return counter; 106 : } 107 : 108 : public synchronized void increment() { 109 1 : counter++; 110 1 : } 111 : } 112 : }