-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a test to verify our assumptions about equality / hashCode / seri…
…alization for EclipseJdtFormatterStep.
- Loading branch information
Showing
1 changed file
with
64 additions
and
0 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
lib-extra/src/test/java/com/diffplug/spotless/extra/java/EclipseJdtEqualityTest.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 2024 DiffPlug | ||
* | ||
* 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.diffplug.spotless.extra.java; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.ObjectOutputStream; | ||
import java.io.Serializable; | ||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import com.diffplug.spotless.FormatterStep; | ||
import com.diffplug.spotless.ResourceHarness; | ||
import com.diffplug.spotless.TestProvisioner; | ||
import com.diffplug.spotless.ThrowingEx; | ||
|
||
public class EclipseJdtEqualityTest extends ResourceHarness { | ||
@Test | ||
public void test() throws Exception { | ||
var settings1 = setFile("subfolder1/formatter.xml").toResource("java/eclipse/formatter.xml"); | ||
var settings2 = setFile("subfolder2/formatter.xml").toResource("java/eclipse/formatter.xml"); | ||
var step1 = withSettingsFile(settings1); | ||
var step2 = withSettingsFile(settings2); | ||
|
||
Assertions.assertTrue(step1.equals(step2)); | ||
Assertions.assertTrue(step1.hashCode() == step2.hashCode()); | ||
|
||
var serialized1 = toBytes(step1); | ||
var serialized2 = toBytes(step2); | ||
Assertions.assertFalse(serialized1.equals(serialized2)); | ||
} | ||
|
||
private static FormatterStep withSettingsFile(File settingsFile) { | ||
var builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral()); | ||
builder.setPreferences(List.of(settingsFile)); | ||
return builder.build(); | ||
} | ||
|
||
private static byte[] toBytes(Serializable obj) { | ||
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); | ||
try (ObjectOutputStream objectOutput = new ObjectOutputStream(byteOutput)) { | ||
objectOutput.writeObject(obj); | ||
} catch (IOException e) { | ||
throw ThrowingEx.asRuntime(e); | ||
} | ||
return byteOutput.toByteArray(); | ||
} | ||
} |