View Javadoc
1   /*
2    * Copyright 2009, 2012 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;
17  
18  import java.io.File;
19  import java.util.Collection;
20  import java.util.Map;
21  
22  import static org.junit.Assert.assertEquals;
23  import static org.junit.Assert.assertTrue;
24  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.assertNotNull;
26  
27  //import org.junit.Ignore;
28  import org.junit.Test;
29  
30  import eu.simuline.octave.OctaveEngine.PackageDesc;
31  import eu.simuline.octave.OctaveEngine.NameDesc;
32  import eu.simuline.octave.type.Octave;
33  
34  /**
35   * Test meta info like version (both octave and javaoctave bridge), 
36   * vendor, packages installed. 
37   */
38  public class TestMetaInfo {
39  
40      //private final static String PKG_QUATERNION = "quaternion";
41      private final static String PKG_INTERVAL = "interval";
42      private final static String CLS_INTERVAL = "infsup";
43  
44      /**
45       * Test getVersion
46       */
47      @Test public void testVersion() {
48          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
49          octave.getOctaveVersion();
50      }
51  
52      /**
53       * Test that the version is a version we know. 
54       * If this test fails the fix will usually be 
55       * to add the new version to the Set of known versions.
56       */
57      @Test public void testKnownVersion() {
58          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
59          assertTrue("Version '" + octave.getOctaveVersion() + "' is not known", 
60          	octave.isOctaveVersionAllowed());
61          assertEquals("Wrong octave version",
62          	"6.2.0",
63          	octave.getOctaveVersion());
64      }
65      
66      // TBD: reactivate 
67      // Currently, this does not work with failsafe plugin 
68      @Test public void testOctaveJavaVersion() {
69          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
70          octave.getOctaveInJavaVersion();
71  //        assertEquals("wrong version of octavejava", 
72  //        	"0.7-SNAPSHOT",
73  //        	octave.getOctaveInJavaVersion());
74       }
75      
76      // TBD: reactivate 
77      // Currently, this does not work with failsafe plugin 
78      @Test public void testMeta() {
79          final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
80          octave.getVendor();
81  //        assertEquals("Wrong vendor", 
82  //        	"1.0",
83  //        	octave.getVendor());
84      }
85  
86      @Test public void testPackageInstalled() {
87  	final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
88  	Collection<String> names = octave.getNamesOfPackagesInstalled();
89  	assertTrue(names.contains(PKG_INTERVAL));
90      }
91  
92      @Test public void testPackageLoading() {
93  	final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
94  	Map<String, PackageDesc> name2pkg = octave.getPackagesInstalled();
95  	assertTrue(name2pkg.keySet().contains(PKG_INTERVAL));
96  	assertTrue(!name2pkg.get(PKG_INTERVAL).isLoaded);
97  	octave.eval(String.format("pkg('load', '%s')", PKG_INTERVAL));
98  	name2pkg = octave.getPackagesInstalled();
99  	assertTrue( name2pkg.get(PKG_INTERVAL).isLoaded);
100 	octave.eval(String.format("pkg('unload', '%s')", PKG_INTERVAL));
101 	name2pkg = octave.getPackagesInstalled();
102 	assertTrue(!name2pkg.get(PKG_INTERVAL).isLoaded);
103     }
104 
105     /**
106      * Test that the list of variables is empty. 
107      */
108     @Test public void testListVarsEmpty() {
109         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
110         final Collection<String> collection = octave.getVarNames();
111         assertEquals(collection.toString(), 0, collection.size());
112         octave.close();
113     }
114 
115     /**
116      * Test that the list of variables has 1 or 2 entries. 
117      * **** better: test concrete collection of variables. 
118      */
119     @Test public void testListVarsOneTwo() {
120         final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
121 
122         octave.eval("my_var = 42;");
123         final Collection<String> collection1 = octave.getVarNames();
124         assertEquals(collection1.toString(), 1, collection1.size());
125 
126         octave.eval("1 + 2;");
127         octave.eval("my_other_var = 42;");
128         final Collection<String> collection2 = octave.getVarNames();
129         assertEquals(collection2.toString(), 2, collection2.size());
130 
131         octave.close();
132     }
133 
134     @Test public void testOnOS() {
135 	final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
136 	assertEquals(File.separator, octave.getFilesep());
137     }
138 
139     /**
140      * Tests {@link OctaveEngine#getInstHomeDir()}, {@link OctaveEngine#getOctaveVersion()} 
141      * and {@link OctaveEngine#getDescForName(String)} at the same time.
142      */
143     @Test public void testBasedOnWhich() {
144 	final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
145 
146 	// name of a variable 
147 	String name = "someVar";
148 	octave.put(name, Octave.scalar(1));
149 	OctaveEngine.NameDesc desc = octave.getDescForName(name);
150  	assertEquals(NameDesc.Category.Variable, desc.category);
151  	assertEquals(name, desc.name);
152  	assertNull(desc.type);
153  	assertNull(desc.file);
154 
155  	// TBD: add cases: no variable and no file but type
156  	// Problem: i dont know in which case this occurs. 
157 
158 	// name not of a variable, and type and c-file  	
159 	name = "cos";
160  	desc = octave.getDescForName(name);
161  	assertEquals(NameDesc.Category.TypedDefInFile, desc.category);
162  	assertEquals(name, desc.name);
163 	assertEquals("built-in function", desc.type);
164 
165 	File fileCmp = new File("libinterp", "corefcn");
166 	fileCmp = new File(fileCmp, "mappers.cc");
167 	assertEquals(fileCmp, desc.file);
168 
169 	// name not of a variable, and type and m-file  	
170  	name = "pkg";
171  	desc = octave.getDescForName(name);
172  	assertEquals(NameDesc.Category.TypedDefInFile, desc.category);
173  	assertEquals(name, desc.name);
174 	assertEquals("function", desc.type);
175 
176 	fileCmp = new File(octave.getInstHomeDir(), "share");
177 	fileCmp = new File(fileCmp, "octave");
178 	fileCmp = new File(fileCmp, octave.getOctaveVersion());
179 	fileCmp = new File(fileCmp, "m");
180 	fileCmp = new File(fileCmp, "pkg");
181 	fileCmp = new File(fileCmp, "pkg.m");
182 	assertEquals(fileCmp, desc.file);
183 	assertTrue(desc.file.exists());
184 
185 	// name not of a variable, and type and m-file in package, loaded and not loaded
186 	PackageDesc pkgDesc = octave.getPackagesInstalled().get(PKG_INTERVAL);
187 	assertNotNull(pkgDesc);
188 	assertTrue(!pkgDesc.isLoaded);
189 	octave.eval(String.format("pkg('load', '%s')", PKG_INTERVAL));
190 	assertTrue(octave.getPackagesInstalled().get(PKG_INTERVAL).isLoaded);
191 	name = String.format("@%s/abs", CLS_INTERVAL);
192  	desc = octave.getDescForName(name);
193  	assertEquals(NameDesc.Category.TypedDefInFile, desc.category);
194  	assertEquals(name, desc.name);
195 	assertEquals("function", desc.type);
196 	fileCmp = new File(pkgDesc.dir, "@"+CLS_INTERVAL);
197 	fileCmp = new File(fileCmp, "abs.m");
198  	assertEquals(fileCmp, desc.file);
199 	assertTrue(desc.file.exists());
200 	octave.eval(String.format("pkg('unload', '%s')", PKG_INTERVAL));
201 	assertTrue(!octave.getPackagesInstalled().get(PKG_INTERVAL).isLoaded);
202  	desc = octave.getDescForName(name);
203  	assertEquals(NameDesc.Category.Unknown, desc.category);
204 	assertEquals(name, desc.name);
205 	assertNull(desc.type);
206 	assertNull(desc.file);
207 
208 
209 	// name not of a variable, no type but existing and m-file  	
210  	name = octave.getDescForName("pkg").file.toString();
211  	desc = octave.getDescForName(name);
212  	assertEquals(NameDesc.Category.FileEx, desc.category);
213  	assertEquals(name, desc.name);
214 	assertNull(desc.type);
215 	assertEquals(new File(name), desc.file);
216 	assertTrue(desc.file.exists());
217 	assertTrue(desc.file.isFile());
218 
219 	// name not of a variable, no type but existing file 
220 	name = octave.getDescForName("pkg").file.getParent();
221 	desc = octave.getDescForName(name);
222  	assertEquals(NameDesc.Category.FileEx, desc.category);
223  	assertEquals(name, desc.name);
224 	assertNull(desc.type);
225 	assertEquals(new File(name), desc.file);
226 	assertTrue(desc.file.exists());
227 	assertTrue(desc.file.isDirectory());
228 	
229 	// unknown name 
230 	name = "foo";
231 	desc = octave.getDescForName(name);
232  	assertEquals(NameDesc.Category.Unknown, desc.category);
233  	assertEquals(name, desc.name);
234 	assertNull(desc.type);
235 	assertNull(desc.file);
236     }
237 
238     /**
239      * Tests on the java directory.
240      */
241     @Test public void testJavaDir() {
242  	final OctaveEngine octave = new OctaveEngineFactory().getScriptEngine();
243  	String name = "javaaddpath";
244  	OctaveEngine.NameDesc desc = octave.getDescForName(name);
245  	assertEquals(name, desc.name);
246  	assertEquals("function", desc.type);
247  	assertEquals(octave.getJavaHomeDir(), desc.file.getParentFile());
248    }
249 
250 }