1 /* 2 * Copyright (C) 2013 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. 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 distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.caliper.config; 16 17 import static com.google.common.base.Charsets.UTF_8; 18 import static java.util.logging.Level.INFO; 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertTrue; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import com.google.caliper.model.Run; 25 import com.google.common.io.Files; 26 27 import org.joda.time.Instant; 28 import org.joda.time.format.ISODateTimeFormat; 29 import org.junit.Before; 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.rules.TemporaryFolder; 33 import org.junit.runner.RunWith; 34 import org.mockito.ArgumentCaptor; 35 import org.mockito.Captor; 36 import org.mockito.Mock; 37 import org.mockito.runners.MockitoJUnitRunner; 38 39 import java.io.File; 40 import java.io.IOException; 41 import java.util.UUID; 42 import java.util.logging.FileHandler; 43 import java.util.logging.Handler; 44 import java.util.logging.LogManager; 45 import java.util.logging.LogRecord; 46 import java.util.logging.Logger; 47 import java.util.logging.SimpleFormatter; 48 49 /** 50 * Tests {@link LoggingConfigLoader}. 51 */ 52 53 @RunWith(MockitoJUnitRunner.class) 54 public class LoggingConfigLoaderTest { 55 @Rule public TemporaryFolder folder = new TemporaryFolder(); 56 57 @Mock LogManager logManager; 58 @Mock Logger logger; 59 @Captor ArgumentCaptor<Handler> handlerCaptor; 60 61 private LoggingConfigLoader loader; 62 private UUID runId = UUID.randomUUID(); 63 private Instant startTime = new Instant(); 64 private File caliperDirectory; 65 setUp()66 @Before public void setUp() throws IOException { 67 this.caliperDirectory = folder.newFolder(); 68 this.loader = new LoggingConfigLoader(caliperDirectory, logManager, new Run.Builder(runId) 69 .label("fake run") 70 .startTime(startTime) 71 .build()); 72 } 73 testLoadDefaultLogConfiguration()74 @Test public void testLoadDefaultLogConfiguration() 75 throws SecurityException, IOException { 76 when(logManager.getLogger("")).thenReturn(logger); 77 loader.maybeLoadDefaultLogConfiguration(logManager); 78 verify(logManager).reset(); 79 verify(logger).addHandler(handlerCaptor.capture()); 80 FileHandler fileHandler = (FileHandler) handlerCaptor.getValue(); 81 assertEquals(UTF_8.name(), fileHandler.getEncoding()); 82 assertTrue(fileHandler.getFormatter() instanceof SimpleFormatter); 83 fileHandler.publish(new LogRecord(INFO, "some message")); 84 File logFile = new File(new File(caliperDirectory, "log"), 85 ISODateTimeFormat.basicDateTimeNoMillis().print(startTime) + "." + runId + ".log"); 86 assertTrue(logFile.isFile()); 87 assertTrue(Files.toString(logFile, UTF_8).contains("some message")); 88 } 89 } 90