test_Properties.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- Python -*-
3 
4 #
5 # @file test_Properties.py
6 # @brief test for Property list class (derived from Java Properties)
7 # @date $Date: $
8 # @author Shinji Kurihara
9 #
10 # Copyright (C) 2006
11 # Task-intelligence Research Group,
12 # Intelligent Systems Research Institute,
13 # National Institute of
14 # Advanced Industrial Science and Technology (AIST), Japan
15 # All rights reserved.
16 #
17 
18 import sys
19 sys.path.insert(1,"../")
20 
21 import unittest
22 
23 from Properties import *
24 import OpenRTM_aist
25 
26 
27 configsample_spec = ["implementation_id", "ConfigSample",
28  "type_name", "ConfigSample",
29  "description", "Configuration example component",
30  "version", "1.0",
31  "vendor", "Shinji Kurihara, AIST",
32  "category", "example",
33  "activity_type", "DataFlowComponent",
34  "max_instance", "10",
35  "language", "C++",
36  "lang_type", "compile",
37  # Configuration variables
38  "conf.default.int_param0", "0",
39  "conf.default.int_param1", "1",
40  "conf.default.double_param0", "0.11",
41  "conf.default.double_param1", "9.9",
42  "conf.default.str_param0", "hoge",
43  "conf.default.str_param1", "dara",
44  "conf.default.vector_param0", "0.0,1.0,2.0,3.0,4.0",
45  ""]
46 
47 class TestProperties(unittest.TestCase) :
48 
49  def setUp(self):
50 
51  self.prop = Properties(defaults_str=configsample_spec)
52 
53  def tearDown(self):
54  OpenRTM_aist.Manager.instance().shutdownManager()
55  return
56 
57  def test_getName(self):
58  self.assertEqual(self.prop.getName(), "", "Result failed.")
59 
60 
61  def test_getVlue(self):
62  self.assertEqual(self.prop.getValue(), "", "Result failed.")
63 
64 
66  self.assertEqual(self.prop.getDefaultValue(), "", "Result failed.")
67 
68 
69 
70  def test_getLeaf(self):
71  #print self.prop.getLeaf()[0].getName()
72  #print self.prop.getLeaf()[0].getValue()
73 
74  self.assertEqual(self.prop.getLeaf()[0].getName(), "implementation_id", "Result failed.")
75  self.assertEqual(self.prop.getLeaf()[1].getName(), "type_name", "Result failed.")
76  self.assertEqual(self.prop.getLeaf()[2].getName(), "description", "Result failed.")
77  self.assertEqual(self.prop.getLeaf()[3].getName(), "version", "Result failed.")
78  self.assertEqual(self.prop.getLeaf()[4].getName(), "vendor", "Result failed.")
79  self.assertEqual(self.prop.getLeaf()[5].getName(), "category", "Result failed.")
80  self.assertEqual(self.prop.getLeaf()[6].getName(), "activity_type", "Result failed.")
81  self.assertEqual(self.prop.getLeaf()[7].getName(), "max_instance", "Result failed.")
82  self.assertEqual(self.prop.getLeaf()[8].getName(), "language", "Result failed.")
83  self.assertEqual(self.prop.getLeaf()[9].getName(), "lang_type", "Result failed.")
84  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getLeaf()[0].getName(), "int_param0", "Result failed.")
85  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getLeaf()[1].getName(), "int_param1", "Result failed.")
86  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getLeaf()[2].getName(), "double_param0", "Result failed.")
87  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getLeaf()[3].getName(), "double_param1", "Result failed.")
88  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getLeaf()[4].getName(), "str_param0", "Result failed.")
89  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getLeaf()[5].getName(), "str_param1", "Result failed.")
90  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getLeaf()[6].getName(), "vector_param0", "Result failed.")
91 
92  # Failed Patern
93  # self.assertEqual(self.prop.getLeaf()[10].getName(), "conf.default.int_param0", "Result failed.")
94 
95 
96  def test_getRoot(self):
97  self.assertEqual(self.prop.getRoot(), None, "Result failed.")
98  self.assertEqual(self.prop.getLeaf()[10].getRoot().getRoot(), None, "Result failed.")
99  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getRoot().getName(), "conf", "Result failed.")
100  self.assertEqual(self.prop.getLeaf()[10].getLeaf()[0].getLeaf()[0].getRoot().getName(), "default", "Result failed.")
101 
102 
103  def test_getProperty(self):
104  self.assertEqual(self.prop.getProperty("implementation_id"), "ConfigSample", "Result failed.")
105  self.assertEqual(self.prop.getProperty("type_name"), "ConfigSample", "Result failed.")
106  self.assertEqual(self.prop.getProperty("description"), "Configuration example component","Result failed.")
107  self.assertEqual(self.prop.getProperty("version"), "1.0","Result failed.")
108  self.assertEqual(self.prop.getProperty("vendor"),"Shinji Kurihara, AIST", "Result failed.")
109  self.assertEqual(self.prop.getProperty("category"), "example","Result failed.")
110  self.assertEqual(self.prop.getProperty("activity_type"), "DataFlowComponent", "Result failed.")
111  self.assertEqual(self.prop.getProperty("max_instance"), "10", "Result failed.")
112  self.assertEqual(self.prop.getProperty("language"), "C++", "Result failed.")
113  self.assertEqual(self.prop.getProperty("lang_type"), "compile", "Result failed.")
114  self.assertEqual(self.prop.getProperty("conf.default.int_param0"), "0", "Result failed.")
115  self.assertEqual(self.prop.getProperty("conf.default.int_param1"), "1", "Result failed.")
116  self.assertEqual(self.prop.getProperty("conf.default.double_param0"), "0.11", "Result failed.")
117  self.assertEqual(self.prop.getProperty("conf.default.double_param1"), "9.9", "Result failed.")
118  self.assertEqual(self.prop.getProperty("conf.default.str_param0"), "hoge", "Result failed.")
119  self.assertEqual(self.prop.getProperty("conf.default.str_param1"), "dara", "Result failed.")
120  self.assertEqual(self.prop.getProperty("conf.default.vector_param0"), "0.0,1.0,2.0,3.0,4.0","Result failed.")
121 
122  # Failed Pattern
123  # self.assertEqual(self.prop.getProperty("int_param0"), "0", "Result failed.")
124  # self.assertEqual(self.prop.getProperty("int_param1"), "1", "Result failed.")
125  # self.assertEqual(self.prop.getProperty("double_param0"), "0.11", "Result failed.")
126  # self.assertEqual(self.prop.getProperty("double_param1"), "9.9", "Result failed.")
127  # self.assertEqual(self.prop.getProperty("str_param0"), "hoge", "Result failed.")
128  # self.assertEqual(self.prop.getProperty("str_param1"), "dara", "Result failed.")
129  # self.assertEqual(self.prop.getProperty("vector_param0"), "0.0,1.0,2.0,3.0,4.0","Result failed.")
130 
131 
132  def test_getDefault(self):
133  self.assertEqual(self.prop.getDefault("implementation_id"), "ConfigSample", "Result failed.")
134  self.assertEqual(self.prop.getDefault("type_name"), "ConfigSample", "Result failed.")
135  self.assertEqual(self.prop.getDefault("description"), "Configuration example component","Result failed.")
136  self.assertEqual(self.prop.getDefault("version"), "1.0","Result failed.")
137  self.assertEqual(self.prop.getDefault("vendor"),"Shinji Kurihara, AIST", "Result failed.")
138  self.assertEqual(self.prop.getDefault("category"), "example","Result failed.")
139  self.assertEqual(self.prop.getDefault("activity_type"), "DataFlowComponent", "Result failed.")
140  self.assertEqual(self.prop.getDefault("max_instance"), "10", "Result failed.")
141  self.assertEqual(self.prop.getDefault("language"), "C++", "Result failed.")
142  self.assertEqual(self.prop.getDefault("lang_type"), "compile", "Result failed.")
143  self.assertEqual(self.prop.getDefault("conf.default.int_param0"), "0", "Result failed.")
144  self.assertEqual(self.prop.getDefault("conf.default.int_param1"), "1", "Result failed.")
145  self.assertEqual(self.prop.getDefault("conf.default.double_param0"), "0.11", "Result failed.")
146  self.assertEqual(self.prop.getDefault("conf.default.double_param1"), "9.9", "Result failed.")
147  self.assertEqual(self.prop.getDefault("conf.default.str_param0"), "hoge", "Result failed.")
148  self.assertEqual(self.prop.getDefault("conf.default.str_param1"), "dara", "Result failed.")
149  self.assertEqual(self.prop.getDefault("conf.default.vector_param0"), "0.0,1.0,2.0,3.0,4.0","Result failed.")
150 
151 
152  def test_setProperty(self):
153  self.prop.setProperty("implementation_id", "ConfigSample_test")
154  self.prop.setProperty("type_name", "ConfigSample_test")
155  self.prop.setProperty("description", "Configuration example component test")
156  self.prop.setProperty("version", "2.0")
157  self.prop.setProperty("vendor","Shinji Kurihara")
158  self.prop.setProperty("category", "example_test")
159  self.prop.setProperty("activity_type", "DataFlowComponent")
160  self.prop.setProperty("max_instance", "1")
161  self.prop.setProperty("language", "C++")
162  self.prop.setProperty("lang_type", "compile")
163  self.prop.setProperty("conf.default.int_param0", "111")
164  self.prop.setProperty("conf.default.int_param1", "222")
165  self.prop.setProperty("conf.default.double_param0", "2.22")
166  self.prop.setProperty("conf.default.double_param1", "9.99")
167  self.prop.setProperty("conf.default.str_param0", "hogehoge")
168  self.prop.setProperty("conf.default.str_param1", "daradara")
169  self.prop.setProperty("conf.default.vector_param0", "0.0,1.1,2.2,3.3,4.4")
170 
171  self.assertEqual(self.prop.getProperty("implementation_id"), "ConfigSample_test", "Result failed.")
172  self.assertEqual(self.prop.getProperty("type_name"), "ConfigSample_test", "Result failed.")
173  self.assertEqual(self.prop.getProperty("description"), "Configuration example component test","Result failed.")
174  self.assertEqual(self.prop.getProperty("version"), "2.0","Result failed.")
175  self.assertEqual(self.prop.getProperty("vendor"),"Shinji Kurihara", "Result failed.")
176  self.assertEqual(self.prop.getProperty("category"), "example_test","Result failed.")
177  self.assertEqual(self.prop.getProperty("activity_type"), "DataFlowComponent", "Result failed.")
178  self.assertEqual(self.prop.getProperty("max_instance"), "1", "Result failed.")
179  self.assertEqual(self.prop.getProperty("language"), "C++", "Result failed.")
180  self.assertEqual(self.prop.getProperty("lang_type"), "compile", "Result failed.")
181  self.assertEqual(self.prop.getProperty("conf.default.int_param0"), "111", "Result failed.")
182  self.assertEqual(self.prop.getProperty("conf.default.int_param1"), "222", "Result failed.")
183  self.assertEqual(self.prop.getProperty("conf.default.double_param0"), "2.22", "Result failed.")
184  self.assertEqual(self.prop.getProperty("conf.default.double_param1"), "9.99", "Result failed.")
185  self.assertEqual(self.prop.getProperty("conf.default.str_param0"), "hogehoge", "Result failed.")
186  self.assertEqual(self.prop.getProperty("conf.default.str_param1"), "daradara", "Result failed.")
187  self.assertEqual(self.prop.getProperty("conf.default.vector_param0"), "0.0,1.1,2.2,3.3,4.4","Result failed.")
188 
189 
190  def test_setDefault(self):
191  self.prop.setDefault("implementation_id", "ConfigSample")
192  self.prop.setDefault("type_name", "ConfigSample")
193  self.prop.setDefault("description", "Configuration example component")
194  self.prop.setDefault("version", "1.0")
195  self.prop.setDefault("vendor","Shinji Kurihara")
196  self.prop.setDefault("category", "example")
197  self.prop.setDefault("activity_type", "DataFlowComponent")
198  self.prop.setDefault("max_instance", "10")
199  self.prop.setDefault("language", "C++")
200  self.prop.setDefault("lang_type", "compile")
201  self.prop.setDefault("conf.default.int_param0", "0")
202  self.prop.setDefault("conf.default.int_param1", "1")
203  self.prop.setDefault("conf.default.double_param0", "0.11")
204  self.prop.setDefault("conf.default.double_param1", "9.9")
205  self.prop.setDefault("conf.default.str_param0", "hoge")
206  self.prop.setDefault("conf.default.str_param1", "dara")
207  self.prop.setDefault("conf.default.vector_param0", "0.0,1.0,2.0,3.0,4.0")
208 
209  self.assertEqual(self.prop.getDefault("implementation_id"), "ConfigSample", "Result failed.")
210  self.assertEqual(self.prop.getDefault("type_name"), "ConfigSample", "Result failed.")
211  self.assertEqual(self.prop.getDefault("description"), "Configuration example component","Result failed.")
212  self.assertEqual(self.prop.getDefault("version"), "1.0","Result failed.")
213  self.assertEqual(self.prop.getDefault("vendor"),"Shinji Kurihara", "Result failed.")
214  self.assertEqual(self.prop.getDefault("category"), "example","Result failed.")
215  self.assertEqual(self.prop.getDefault("activity_type"), "DataFlowComponent", "Result failed.")
216  self.assertEqual(self.prop.getDefault("max_instance"), "10", "Result failed.")
217  self.assertEqual(self.prop.getDefault("language"), "C++", "Result failed.")
218  self.assertEqual(self.prop.getDefault("lang_type"), "compile", "Result failed.")
219  self.assertEqual(self.prop.getDefault("conf.default.int_param0"), "0", "Result failed.")
220  self.assertEqual(self.prop.getDefault("conf.default.int_param1"), "1", "Result failed.")
221  self.assertEqual(self.prop.getDefault("conf.default.double_param0"), "0.11", "Result failed.")
222  self.assertEqual(self.prop.getDefault("conf.default.double_param1"), "9.9", "Result failed.")
223  self.assertEqual(self.prop.getDefault("conf.default.str_param0"), "hoge", "Result failed.")
224  self.assertEqual(self.prop.getDefault("conf.default.str_param1"), "dara", "Result failed.")
225  self.assertEqual(self.prop.getDefault("conf.default.vector_param0"), "0.0,1.0,2.0,3.0,4.0","Result failed.")
226 
227 
228  def test_setDefaults(self):
229  self.prop.setDefaults(configsample_spec)
230 
231  self.assertEqual(self.prop.getDefault("implementation_id"), "ConfigSample", "Result failed.")
232  self.assertEqual(self.prop.getDefault("type_name"), "ConfigSample", "Result failed.")
233  self.assertEqual(self.prop.getDefault("description"), "Configuration example component","Result failed.")
234  self.assertEqual(self.prop.getDefault("version"), "1.0","Result failed.")
235  self.assertEqual(self.prop.getDefault("vendor"),"Shinji Kurihara, AIST", "Result failed.")
236  self.assertEqual(self.prop.getDefault("category"), "example","Result failed.")
237  self.assertEqual(self.prop.getDefault("activity_type"), "DataFlowComponent", "Result failed.")
238  self.assertEqual(self.prop.getDefault("max_instance"), "10", "Result failed.")
239  self.assertEqual(self.prop.getDefault("language"), "C++", "Result failed.")
240  self.assertEqual(self.prop.getDefault("lang_type"), "compile", "Result failed.")
241  self.assertEqual(self.prop.getDefault("conf.default.int_param0"), "0", "Result failed.")
242  self.assertEqual(self.prop.getDefault("conf.default.int_param1"), "1", "Result failed.")
243  self.assertEqual(self.prop.getDefault("conf.default.double_param0"), "0.11", "Result failed.")
244  self.assertEqual(self.prop.getDefault("conf.default.double_param1"), "9.9", "Result failed.")
245  self.assertEqual(self.prop.getDefault("conf.default.str_param0"), "hoge", "Result failed.")
246  self.assertEqual(self.prop.getDefault("conf.default.str_param1"), "dara", "Result failed.")
247  self.assertEqual(self.prop.getDefault("conf.default.vector_param0"), "0.0,1.0,2.0,3.0,4.0","Result failed.")
248 
249 
250  def test_list(self):
251  #self.prop.list(sys.stdout)
252  pass
253 
254 
255  def test_load(self):
256  fp = file("configsample.conf","r")
257  self.prop.load(fp)
258  self.assertEqual(self.prop.getProperty("conf.mode0.int_param0"), "12345", "Result failed.")
259  self.assertEqual(self.prop.getProperty("conf.mode0.int_param1"), "98765", "Result failed.")
260  self.assertEqual(self.prop.getProperty("conf.mode0.double_param0"), "3.141592653589793238462643383279")
261  self.assertEqual(self.prop.getProperty("conf.mode0.double_param1"), "2.718281828459045235360287471352", "Result failed.")
262  self.assertEqual(self.prop.getProperty("conf.mode0.str_param0"), "bar", "Result failed.")
263  self.assertEqual(self.prop.getProperty("conf.mode0.str_param1"), "foo", "Result failed.")
264  self.assertEqual(self.prop.getProperty("conf.mode0.vector_param0"), "0.0,0.1,0.2,0.3,0.4","Result failed.")
265 
266 
267  def test_save(self):
268  fp = file("test_save.data","w")
269  self.prop.save(fp,"test Properties")
270 
271 
272  def test_store(self):
273  fp = file("test_store.data","w")
274  self.prop.store(fp,"test Properties")
275 
276 
278  keys = self.prop.propertyNames()
279  self.assertEqual(keys[0],"implementation_id", "Result failed.")
280  self.assertEqual(keys[1],"type_name", "Result failed.")
281  self.assertEqual(keys[2],"description","Result failed.")
282  self.assertEqual(keys[3],"version","Result failed.")
283  self.assertEqual(keys[4],"vendor", "Result failed.")
284  self.assertEqual(keys[5],"category","Result failed.")
285  self.assertEqual(keys[6],"activity_type", "Result failed.")
286  self.assertEqual(keys[7],"max_instance", "Result failed.")
287  self.assertEqual(keys[8],"language", "Result failed.")
288  self.assertEqual(keys[9],"lang_type", "Result failed.")
289  self.assertEqual(keys[10],"conf.default.int_param0", "Result failed.")
290  self.assertEqual(keys[11],"conf.default.int_param1", "Result failed.")
291  self.assertEqual(keys[12],"conf.default.double_param0", "Result failed.")
292  self.assertEqual(keys[13],"conf.default.double_param1", "Result failed.")
293  self.assertEqual(keys[14],"conf.default.str_param0", "Result failed.")
294  self.assertEqual(keys[15],"conf.default.str_param1", "Result failed.")
295  self.assertEqual(keys[16],"conf.default.vector_param0","Result failed.")
296 
297 
298  def test_size(self):
299  self.assertEqual(self.prop.size(),17,"Result failed.")
300 
301 
302  def test_findNode(self):
303  self.assertEqual(self.prop.findNode("implementation_id").getDefaultValue(), "ConfigSample", "Result failed.")
304 
305  def test_getNode(self):
306  self.assertEqual(self.prop.getNode("implementation_id").getDefaultValue(), "ConfigSample", "Result failed.")
307  self.assertEqual(self.prop.getNode("type_name").getDefaultValue(), "ConfigSample", "Result failed.")
308  self.assertEqual(self.prop.getNode("description").getDefaultValue(), "Configuration example component","Result failed.")
309  self.assertEqual(self.prop.getNode("version").getDefaultValue(), "1.0","Result failed.")
310  self.assertEqual(self.prop.getNode("vendor").getDefaultValue(),"Shinji Kurihara, AIST", "Result failed.")
311  self.assertEqual(self.prop.getNode("category").getDefaultValue(), "example","Result failed.")
312  self.assertEqual(self.prop.getNode("activity_type").getDefaultValue(), "DataFlowComponent", "Result failed.")
313  self.assertEqual(self.prop.getNode("max_instance").getDefaultValue(), "10", "Result failed.")
314  self.assertEqual(self.prop.getNode("language").getDefaultValue(), "C++", "Result failed.")
315  self.assertEqual(self.prop.getNode("lang_type").getDefaultValue(), "compile", "Result failed.")
316  self.assertEqual(self.prop.getNode("conf.default.int_param0").getDefaultValue(), "0", "Result failed.")
317  self.assertEqual(self.prop.getNode("conf.default.int_param1").getDefaultValue(), "1", "Result failed.")
318  self.assertEqual(self.prop.getNode("conf.default.double_param0").getDefaultValue(), "0.11", "Result failed.")
319  self.assertEqual(self.prop.getNode("conf.default.double_param1").getDefaultValue(), "9.9", "Result failed.")
320  self.assertEqual(self.prop.getNode("conf.default.str_param0").getDefaultValue(), "hoge", "Result failed.")
321  self.assertEqual(self.prop.getNode("conf.default.str_param1").getDefaultValue(), "dara", "Result failed.")
322  self.assertEqual(self.prop.getNode("conf.default.vector_param0").getDefaultValue(), "0.0,1.0,2.0,3.0,4.0","Result failed.")
323 
324 
325  def test_createNode(self):
326  self.assertEqual(self.prop.createNode("conf.default.int_param.0"),True, "Result failed.")
327  self.assertEqual(self.prop.createNode("conf.add.int_param1"),True, "Result failed.")
328  self.assertEqual(len(self.prop.getNode("conf.default").leaf), 8, "Result failed.")
329  self.assertEqual(len(self.prop.getNode("conf.add").leaf), 1, "Result failed.")
330 
331 
332  def test_removeNode(self):
333  node = self.prop.getNode("conf.default")
334  node.removeNode("int_param0")
335  self.assertEqual( len(self.prop.getNode("conf.default").leaf),6, "Result failed.")
336 
337  node = self.prop.getNode("conf")
338  node.removeNode("default")
339  self.assertEqual( len(self.prop.getNode("conf").leaf),0, "Result failed.")
340 
341 
342  def test_hasKey(self):
343  node = self.prop.getNode("conf")
344  self.assertEqual(len(node.hasKey("default").leaf),7, "Result failed.")
345 
346  self.assertEqual(self.prop.hasKey("default"),None, "Result failed.")
347 
348 
349  def test_clear(self):
350  self.prop.clear()
351  self.assertEqual(self.prop.getProperty("implementation_id"), "", "Result failed.")
352 
353  # Failed Pattern
354  # self.assertEqual(self.prop.getProperty("implementation_id"), "ConfigSample", "Result failed.")
355 
356 
358  key=[]
359  val=[]
360  self.prop.splitKeyValue("key:value",key,val)
361  self.assertEqual(key[0],"key")
362  self.assertEqual(val[0],"value")
363 
364 
365  def test_split(self):
366  val=[]
367  self.prop.split("test.split,hoge",".",val)
368 
369  self.assertEqual(val, ["test","split,hoge"])
370 
371 
373  self.prop.mergeProperties(self.prop)
374  self.assertEqual(self.prop.getProperty("implementation_id"), "ConfigSample", "Result failed.")
375 
376 
377  def test_dump(self):
378  print self.prop
379 
380 
381 
382 ############### test #################
383 if __name__ == '__main__':
384  unittest.main()


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Thu Jun 6 2019 19:11:35