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.juneau.marshaller;
18  
19  import static org.apache.juneau.TestUtils.*;
20  
21  import java.io.*;
22  import java.util.*;
23  
24  import org.apache.juneau.*;
25  import org.apache.juneau.collections.*;
26  import org.apache.juneau.parser.*;
27  import org.junit.jupiter.api.*;
28  
29  class Csv_Test extends TestBase{
30  
31  	@Test void a01_to() throws Exception {
32  		var in1 = "foo";
33  		var in2 = new Object[]{JsonMap.of("a","foo","b","bar")};
34  		var expected1 = "value\nfoo\n";
35  		var expected2 = "a,b\nfoo,bar\n";
36  
37  		assertString(expected1, Csv.of(in1));
38  		assertString(expected1, Csv.of(in1,stringWriter()));
39  		assertString(expected2, Csv.of(in2));
40  		assertString(expected2, Csv.of(in2,stringWriter()));
41  	}
42  
43  	@Test void a02_from() {
44  		var in1 = "'foo'";
45  		var in2 = "{foo:'bar'}";
46  
47  		assertThrowsWithMessage(ParseException.class, "Not implemented.", ()->Csv.to(in1, String.class));
48  		assertThrowsWithMessage(ParseException.class, "Not implemented.", ()->Csv.to(stringReader(in1), String.class));
49  		assertThrowsWithMessage(ParseException.class, "Not implemented.", ()->Csv.to(in2, Map.class, String.class, String.class));
50  		assertThrowsWithMessage(ParseException.class, "Not implemented.", ()->Csv.to(stringReader(in2), Map.class, String.class, String.class));
51  	}
52  	//-----------------------------------------------------------------------------------------------------------------
53  	// Helper methods
54  	//-----------------------------------------------------------------------------------------------------------------
55  
56  	private Writer stringWriter() {
57  		return new StringWriter();
58  	}
59  
60  	private Reader stringReader(String s) {
61  		return new StringReader(s);
62  	}
63  }