-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
avoid multiple charAt calls for injectedLogSite equals()
RELNOTES=n/a PiperOrigin-RevId: 700422386
- Loading branch information
1 parent
fc6e93b
commit cb3989a
Showing
3 changed files
with
73 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
api/src/test/java/com/google/common/flogger/LogSiteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright (C) 2024 The Flogger Authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.common.flogger; | ||
|
||
import static com.google.common.truth.Truth.assertThat; | ||
|
||
import com.google.common.testing.EqualsTester; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public final class LogSiteTest { | ||
|
||
@Test | ||
public void injectedLogSite_internalVsExternalName() { | ||
String className = "com.google.common.flogger.LogSiteTest"; | ||
String internalClassName = "com/google/common/flogger/LogSiteTest"; | ||
|
||
// Using the same class, but represented by the internal vs external name, should | ||
// be considered equal. | ||
new EqualsTester() | ||
.addEqualityGroup(logSite(className), logSite(internalClassName)) | ||
.addEqualityGroup(logSite(className + "2"), logSite(internalClassName + "2")) | ||
.addEqualityGroup(logSite(className + "3"), logSite(internalClassName + "3")) | ||
.testEquals(); | ||
|
||
// The public "className()" method will be the same | ||
assertThat(logSite(className).getClassName()).isEqualTo(className); | ||
assertThat(logSite(internalClassName).getClassName()).isEqualTo(className); | ||
} | ||
|
||
// This technically passes, but is not what we want, since the botched name isn't a real class | ||
// identifier. The complexity of tracking the separator is not worth it, however. | ||
@Test | ||
public void injectedLogSite_invalidClassName_stillValid() { | ||
String className = "com.google.common.flogger.LogSiteTest"; | ||
String internalClassName = "com/google/common/flogger/LogSiteTest"; | ||
String botchedClassName = "com.google/common.flogger/LogSiteTest"; | ||
|
||
new EqualsTester() | ||
.addEqualityGroup(logSite(className), logSite(internalClassName), logSite(botchedClassName)) | ||
.testEquals(); | ||
} | ||
|
||
@SuppressWarnings("deprecation") // Intentionally calling injectedLogSite to test it. | ||
private static LogSite logSite(String className) { | ||
return LogSite.injectedLogSite(className, "someMethod", 42, "MyFile.java"); | ||
} | ||
} |