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