|
| 1 | +/* |
| 2 | + * Copyright 2021 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.googlejavaformat.java; |
| 16 | + |
| 17 | +import static com.google.common.truth.Truth.assertThat; |
| 18 | + |
| 19 | +import java.io.PrintWriter; |
| 20 | +import java.io.StringWriter; |
| 21 | +import java.util.ServiceLoader; |
| 22 | +import java.util.spi.ToolProvider; |
| 23 | +import org.junit.Test; |
| 24 | +import org.junit.runner.RunWith; |
| 25 | +import org.junit.runners.JUnit4; |
| 26 | + |
| 27 | +/** Tests for {@link GoogleJavaFormatToolProvider}. */ |
| 28 | +@RunWith(JUnit4.class) |
| 29 | +public class GoogleJavaFormatToolProviderTest { |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testUsageOutputAfterLoadingViaToolName() { |
| 33 | + String name = "google-java-format"; |
| 34 | + |
| 35 | + ToolProvider format = |
| 36 | + ServiceLoader.load(ToolProvider.class).stream() |
| 37 | + .map(ServiceLoader.Provider::get) |
| 38 | + .filter(provider -> provider.name().equals(name)) |
| 39 | + .findAny() |
| 40 | + .orElseThrow(() -> new AssertionError("Tool not found by name: " + name)); |
| 41 | + |
| 42 | + StringWriter out = new StringWriter(); |
| 43 | + StringWriter err = new StringWriter(); |
| 44 | + |
| 45 | + int result = format.run(new PrintWriter(out, true), new PrintWriter(err, true), "--help"); |
| 46 | + |
| 47 | + assertThat(result).isEqualTo(0); |
| 48 | + |
| 49 | + String usage = err.toString(); |
| 50 | + |
| 51 | + // Check that doc links are included. |
| 52 | + assertThat(usage).contains("https://github.com/google/google-java-format"); |
| 53 | + assertThat(usage).contains("Usage: google-java-format"); |
| 54 | + } |
| 55 | +} |
0 commit comments