Skip to content

Commit 37439af

Browse files
authored
Verify our assumptions about equality / hashCode / serialization for EclipseJdtFormatterStep (#2193)
2 parents f70156b + 53a2c48 commit 37439af

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2024 DiffPlug
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+
package com.diffplug.spotless.extra.java;
17+
18+
import java.io.ByteArrayOutputStream;
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.ObjectOutputStream;
22+
import java.io.Serializable;
23+
import java.util.List;
24+
25+
import org.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.Test;
27+
28+
import com.diffplug.spotless.FormatterStep;
29+
import com.diffplug.spotless.ResourceHarness;
30+
import com.diffplug.spotless.TestProvisioner;
31+
import com.diffplug.spotless.ThrowingEx;
32+
33+
public class EclipseJdtEqualityTest extends ResourceHarness {
34+
@Test
35+
public void test() throws Exception {
36+
var settings1 = setFile("subfolder1/formatter.xml").toResource("java/eclipse/formatter.xml");
37+
var settings2 = setFile("subfolder2/formatter.xml").toResource("java/eclipse/formatter.xml");
38+
var step1 = withSettingsFile(settings1);
39+
var step2 = withSettingsFile(settings2);
40+
41+
Assertions.assertTrue(step1.equals(step2));
42+
Assertions.assertTrue(step1.hashCode() == step2.hashCode());
43+
44+
var serialized1 = toBytes(step1);
45+
var serialized2 = toBytes(step2);
46+
Assertions.assertFalse(serialized1.equals(serialized2));
47+
}
48+
49+
private static FormatterStep withSettingsFile(File settingsFile) {
50+
var builder = EclipseJdtFormatterStep.createBuilder(TestProvisioner.mavenCentral());
51+
builder.setPreferences(List.of(settingsFile));
52+
return builder.build();
53+
}
54+
55+
private static byte[] toBytes(Serializable obj) {
56+
ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
57+
try (ObjectOutputStream objectOutput = new ObjectOutputStream(byteOutput)) {
58+
objectOutput.writeObject(obj);
59+
} catch (IOException e) {
60+
throw ThrowingEx.asRuntime(e);
61+
}
62+
return byteOutput.toByteArray();
63+
}
64+
}

0 commit comments

Comments
 (0)