View Javadoc
1   /*
2    * Copyright 2007, 2008, 2009 Ange Optimization ApS
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 eu.simuline.octave.io.impl;
17  
18  import java.io.BufferedReader;
19  import java.io.StringReader;
20  
21  import eu.simuline.octave.OctaveEngine;
22  import eu.simuline.octave.OctaveEngineFactory;
23  import eu.simuline.octave.io.OctaveIO;
24  import eu.simuline.octave.type.OctaveObject;
25  import eu.simuline.octave.type.OctaveString;
26  
27  import static org.junit.Assert.assertEquals;
28  
29  import org.junit.Ignore;
30  import org.junit.Test;
31  
32  /**
33   * Test read/write of {@link OctaveString}
34   */
35  public class TestIoOctaveString {
36  
37      /**
38       * Test format of generated octave data
39       */
40      @Test public void testToString() {
41          final OctaveObject string = new OctaveString("tekst");
42          assertEquals("# name: ans\n" + 
43  		     "# type: string\n" + 
44  		     "# elements: 1\n" + 
45  		     "# length: 5\ntekst\n", 
46  		     OctaveIO.toText(string));
47      }
48  
49      /**
50       * Test format of generated octave data
51       */
52      @Test public void testToOctave() {
53          final OctaveObject string = new OctaveString("mytekst");
54          assertEquals("# name: tre\n" + 
55  		     "# type: string\n" + 
56  		     "# elements: 1\n" + 
57  		     "# length: 7\nmytekst\n", 
58  		     OctaveIO.toText("tre", string));
59      }
60  
61      /**
62       * Test that string put into octave is the same as we get back
63       */
64      @Test public void testOctaveRoundtrip() {
65          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
66          roundtrip(octave, new OctaveString("sample text"));
67          roundtrip(octave, new OctaveString(""));
68          roundtrip(octave, new OctaveString("a\nb"));
69          roundtrip(octave, new OctaveString("a\tb"));
70          octave.close();
71      }
72  
73      private static void roundtrip(final OctaveEngine octave, 
74  				  final OctaveObject octaveObject) {
75          final String key = "octave_string";
76          octave.put(key, octaveObject);
77          final OctaveObject octaveObject2 = octave.get(key);
78          assertEquals(octaveObject, octaveObject2);
79      }
80  
81      /**
82       * Test that string read from octave is what we expect
83       */
84      @Test public void testOctaveRead() {
85          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
86          read(octave, "sample text", "sample text");
87          read(octave, "", "");
88          read(octave, "a\\nb", "a\nb");
89          read(octave, "a\\tb", "a\tb");
90          octave.close();
91      }
92  
93      private static void read(final OctaveEngine octave, 
94  			     final String input, 
95  			     final String expected) {
96          final String key = "octave_string";
97          octave.eval(key + " = \"" + input + "\";");
98          final OctaveString output = octave.get(OctaveString.class, key);
99          assertEquals(expected, output.getString());
100     }
101 
102     /**
103      * @throws Exception
104      */
105     @Test public void testWriteRead() throws Exception {
106         final OctaveString string = new OctaveString("TEST");
107 
108         final String text = OctaveIO.toText(string);
109         final BufferedReader bufferedReader = 
110 	    new BufferedReader(new StringReader(text));
111 
112         assertEquals("# name: ans", bufferedReader.readLine());
113         assertEquals(string, OctaveIO.read(bufferedReader));
114         assertEquals(-1, bufferedReader.read()); // Check end of file
115     }
116 
117 }