|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 the original author or 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 | + * https://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 io.spring.javaformat.formatter; |
| 18 | + |
| 19 | +import java.io.ByteArrayOutputStream; |
| 20 | +import java.io.File; |
| 21 | +import java.io.IOException; |
| 22 | +import java.nio.charset.Charset; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.nio.file.Files; |
| 25 | +import java.nio.file.StandardCopyOption; |
| 26 | + |
| 27 | +import org.eclipse.text.edits.TextEdit; |
| 28 | +import org.junit.jupiter.api.BeforeEach; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | +import org.junit.jupiter.api.io.TempDir; |
| 31 | + |
| 32 | +import static org.assertj.core.api.Assertions.assertThat; |
| 33 | + |
| 34 | +/** |
| 35 | + * Tests for {@link StreamsEdit}. |
| 36 | + * |
| 37 | + * @author Phillip Webb |
| 38 | + */ |
| 39 | +public class StreamsEditTests { |
| 40 | + |
| 41 | + private static final Charset UTF_8 = StandardCharsets.UTF_8; |
| 42 | + |
| 43 | + @TempDir |
| 44 | + public File temp; |
| 45 | + |
| 46 | + private File source; |
| 47 | + |
| 48 | + private File expected; |
| 49 | + |
| 50 | + private TextEdit textEdit; |
| 51 | + |
| 52 | + private StreamsEdit streamsEdit; |
| 53 | + |
| 54 | + @BeforeEach |
| 55 | + void setup() throws IOException { |
| 56 | + this.source = new File(this.temp, "source.txt"); |
| 57 | + this.expected = new File(this.temp, "expected.txt"); |
| 58 | + Files.copy(new File("src/test/resources/source/javadoc-top.txt").toPath(), this.source.toPath(), |
| 59 | + StandardCopyOption.REPLACE_EXISTING); |
| 60 | + Files.copy(new File("src/test/resources/expected/javadoc-top.txt").toPath(), this.expected.toPath(), |
| 61 | + StandardCopyOption.REPLACE_EXISTING); |
| 62 | + String content = read(this.source); |
| 63 | + this.textEdit = new Formatter().format(content); |
| 64 | + this.streamsEdit = new StreamsEdit(content, this.textEdit); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + void hasEditsWhenHasEditsReturnsTrue() throws Exception { |
| 69 | + assertThat(this.streamsEdit.hasEdits()).isTrue(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void hasEditsWhenHasNoEditsReturnsFalse() throws Exception { |
| 74 | + String content = read(this.expected); |
| 75 | + this.textEdit = new Formatter().format(content); |
| 76 | + this.streamsEdit = new StreamsEdit(content, this.textEdit); |
| 77 | + assertThat(this.streamsEdit.hasEdits()).isFalse(); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + void writeToOutputStreamWritesContent() throws Exception { |
| 82 | + String expected = read(this.expected); |
| 83 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 84 | + this.streamsEdit.writeTo(outputStream); |
| 85 | + assertThat(outputStream.toByteArray()).isEqualTo(expected.getBytes(UTF_8)); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void writeToOutputStreamWithCharsetWritesContent() throws IOException { |
| 90 | + String expected = read(this.expected); |
| 91 | + ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
| 92 | + this.streamsEdit.writeTo(outputStream, UTF_8); |
| 93 | + assertThat(outputStream.toByteArray()).isEqualTo(expected.getBytes(UTF_8)); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + void writeToAppendableWritesContent() throws IOException { |
| 98 | + String expected = read(this.expected); |
| 99 | + StringBuilder output = new StringBuilder(); |
| 100 | + this.streamsEdit.writeTo(output); |
| 101 | + assertThat(output.toString()).isEqualTo(expected); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + void getFormattedContentReturnsFormattedContent() throws Exception { |
| 106 | + String expected = read(this.expected); |
| 107 | + assertThat(this.streamsEdit.getFormattedContent()).isEqualTo(expected); |
| 108 | + } |
| 109 | + |
| 110 | + private String read(File file) throws IOException { |
| 111 | + return new String(Files.readAllBytes(file.toPath()), UTF_8); |
| 112 | + } |
| 113 | + |
| 114 | +} |
0 commit comments