View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.csv.issues;
18  
19  import static org.junit.jupiter.api.Assertions.assertTrue;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.csv.CSVFormat;
24  import org.apache.commons.csv.CSVPrinter;
25  import org.junit.jupiter.api.Test;
26  
27  public class JiraCsv154Test {
28  
29      @Test
30      public void testJiraCsv154_withCommentMarker() throws IOException {
31          final String comment = "This is a header comment";
32          // @formatter:off
33          final CSVFormat format = CSVFormat.EXCEL.builder()
34              .setHeader("H1", "H2")
35              .setCommentMarker('#')
36              .setHeaderComments(comment)
37              .build();
38          // @formatter:on
39          final StringBuilder out = new StringBuilder();
40          try (final CSVPrinter printer = format.print(out)) {
41              printer.print("A");
42              printer.print("B");
43          }
44          final String s = out.toString();
45          assertTrue(s.contains(comment), s);
46      }
47  
48      @Test
49      public void testJiraCsv154_withHeaderComments() throws IOException {
50          final String comment = "This is a header comment";
51          // @formatter:off
52          final CSVFormat format = CSVFormat.EXCEL.builder()
53              .setHeader("H1", "H2")
54              .setHeaderComments(comment)
55              .setCommentMarker('#')
56              .build();
57          // @formatter:on
58          final StringBuilder out = new StringBuilder();
59          try (final CSVPrinter printer = format.print(out)) {
60              printer.print("A");
61              printer.print("B");
62          }
63          final String s = out.toString();
64          assertTrue(s.contains(comment), s);
65      }
66  
67  }