1 /* 2 * Copyright (C) 2020 The Dagger Authors. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package dagger.internal.codegen.bindinggraphvalidation; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static dagger.internal.codegen.bindinggraphvalidation.PackageNameCompressor.LEGEND_FOOTER; 21 import static dagger.internal.codegen.bindinggraphvalidation.PackageNameCompressor.LEGEND_HEADER; 22 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.junit.runners.JUnit4; 26 27 /** Tests for {@link PackageNameCompressor}. */ 28 @RunWith(JUnit4.class) 29 public class PackageNameCompressorTest { 30 @Test testSimple()31 public void testSimple() { 32 String input = "Something is wrong with foo.bar.baz.Foo class!"; 33 String expectedOutput = "Something is wrong with Foo class!" 34 + LEGEND_HEADER 35 + "Foo: foo.bar.baz.Foo\n" 36 + LEGEND_FOOTER; 37 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(expectedOutput); 38 } 39 40 @Test testSameSimpleNames()41 public void testSameSimpleNames() { 42 String input = "Something is wrong with foo.bar.baz.Foo and foo.bar.qux.Foo class!"; 43 String expectedOutput = "Something is wrong with baz.Foo and qux.Foo class!" 44 + LEGEND_HEADER 45 + "baz.Foo: foo.bar.baz.Foo\n" 46 + "qux.Foo: foo.bar.qux.Foo\n" 47 + LEGEND_FOOTER; 48 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(expectedOutput); 49 } 50 51 @Test testMethodNames()52 public void testMethodNames() { 53 String input = "Something is wrong with foo.bar.baz.Foo.provideFoo()!"; 54 String expectedOutput = "Something is wrong with Foo.provideFoo()!" 55 + LEGEND_HEADER 56 + "Foo: foo.bar.baz.Foo\n" 57 + LEGEND_FOOTER; 58 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(expectedOutput); 59 } 60 61 @Test testMultipleLevelsOfConflicts()62 public void testMultipleLevelsOfConflicts() { 63 String input = "Something is wrong with z.a.b.c.Foo, z.b.b.c.Foo, z.a.b.d.Foo class!"; 64 String expectedOutput = "Something is wrong with a.b.c.Foo, b.b.c.Foo, d.Foo class!" 65 + LEGEND_HEADER 66 + "a.b.c.Foo: z.a.b.c.Foo\n" 67 + "b.b.c.Foo: z.b.b.c.Foo\n" 68 + "d.Foo: z.a.b.d.Foo\n" 69 + LEGEND_FOOTER; 70 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(expectedOutput); 71 } 72 73 // In some sense, we're really just compressing the outer class since the legend is going to 74 // only refer to the outer class. 75 @Test testInnerClassesKeepOuterClassNameToo()76 public void testInnerClassesKeepOuterClassNameToo() { 77 String input = "Something is wrong with foo.bar.baz.Foo.Bar.Baz class!"; 78 String expectedOutput = "Something is wrong with Foo.Bar.Baz class!" 79 + LEGEND_HEADER 80 + "Foo: foo.bar.baz.Foo\n" 81 + LEGEND_FOOTER; 82 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(expectedOutput); 83 } 84 85 // If relying on conflicts by inserting into the map, an extra conflict on c.Foo may result in 86 // uneven renaming because when the first two conflict on c.Foo they may make room for the next 87 // conflict to just take over what had previously been a conflict. Make sure that this unevenness 88 // doesn't happen. 89 @Test testThreeMultiLevelConflicts()90 public void testThreeMultiLevelConflicts() { 91 String input = "Something is wrong with z.a.c.Foo, z.b.c.Foo, and z.c.c.Foo class!"; 92 String expectedOutput = "Something is wrong with a.c.Foo, b.c.Foo, and c.c.Foo class!" 93 + LEGEND_HEADER 94 + "a.c.Foo: z.a.c.Foo\n" 95 + "b.c.Foo: z.b.c.Foo\n" 96 + "c.c.Foo: z.c.c.Foo\n" 97 + LEGEND_FOOTER; 98 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(expectedOutput); 99 } 100 101 @Test testDoesNotCompressSubstringsOfClasses()102 public void testDoesNotCompressSubstringsOfClasses() { 103 // This shouldn't try to compress the "ar.Foo" in "Bar.Foo" 104 String input = "Something is wrong with Bar.Foo class!"; 105 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(input); 106 } 107 108 @Test testNoClassNamesDoNotPutInLegend()109 public void testNoClassNamesDoNotPutInLegend() { 110 String input = "Something is wrong with something!"; 111 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(input); 112 } 113 114 @Test testFullConflictsDoNotPutInLegend()115 public void testFullConflictsDoNotPutInLegend() { 116 String input = "Something is wrong with foo.Foo and bar.Foo class!"; 117 // No shortening can be done without loss of clarity so do not modify this and add no legend. 118 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(input); 119 } 120 121 @Test testLegendDoesNotIncludeJavaLang()122 public void testLegendDoesNotIncludeJavaLang() { 123 String input = "Something is wrong with java.lang.Set, java.lang.a.Foo," 124 + " and java.lang.b.Foo class!"; 125 String expectedOutput = "Something is wrong with Set, a.Foo, and b.Foo class!" 126 + LEGEND_HEADER 127 + "a.Foo: java.lang.a.Foo\n" 128 + "b.Foo: java.lang.b.Foo\n" 129 + LEGEND_FOOTER; 130 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(expectedOutput); 131 } 132 133 @Test testOnlyExcludedPrefixesDoesNotPutInLegend()134 public void testOnlyExcludedPrefixesDoesNotPutInLegend() { 135 String input = "Something is wrong with java.lang.Set class!"; 136 String expectedOutput = "Something is wrong with Set class!"; 137 assertThat(PackageNameCompressor.compressPackagesInMessage(input)).isEqualTo(expectedOutput); 138 } 139 } 140