libis-tools 1.0.5-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.coveralls.yml +2 -0
  3. data/.gitignore +16 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +40 -0
  6. data/Gemfile +7 -0
  7. data/README.md +202 -0
  8. data/Rakefile +11 -0
  9. data/bin/libis_tool +5 -0
  10. data/lib/libis-tools.rb +1 -0
  11. data/lib/libis/tools.rb +25 -0
  12. data/lib/libis/tools/assert.rb +52 -0
  13. data/lib/libis/tools/checksum.rb +106 -0
  14. data/lib/libis/tools/cli/cli_helper.rb +189 -0
  15. data/lib/libis/tools/cli/reorg.rb +416 -0
  16. data/lib/libis/tools/command.rb +133 -0
  17. data/lib/libis/tools/command_line.rb +23 -0
  18. data/lib/libis/tools/config.rb +147 -0
  19. data/lib/libis/tools/config_file.rb +85 -0
  20. data/lib/libis/tools/csv.rb +38 -0
  21. data/lib/libis/tools/deep_struct.rb +71 -0
  22. data/lib/libis/tools/extend/array.rb +16 -0
  23. data/lib/libis/tools/extend/empty.rb +7 -0
  24. data/lib/libis/tools/extend/hash.rb +147 -0
  25. data/lib/libis/tools/extend/kernel.rb +25 -0
  26. data/lib/libis/tools/extend/ostruct.rb +3 -0
  27. data/lib/libis/tools/extend/roo.rb +91 -0
  28. data/lib/libis/tools/extend/string.rb +94 -0
  29. data/lib/libis/tools/extend/struct.rb +29 -0
  30. data/lib/libis/tools/extend/symbol.rb +8 -0
  31. data/lib/libis/tools/logger.rb +130 -0
  32. data/lib/libis/tools/mets_dnx.rb +61 -0
  33. data/lib/libis/tools/mets_file.rb +504 -0
  34. data/lib/libis/tools/mets_objects.rb +547 -0
  35. data/lib/libis/tools/parameter.rb +372 -0
  36. data/lib/libis/tools/spreadsheet.rb +196 -0
  37. data/lib/libis/tools/temp_file.rb +42 -0
  38. data/lib/libis/tools/thread_safe.rb +31 -0
  39. data/lib/libis/tools/version.rb +5 -0
  40. data/lib/libis/tools/xml_document.rb +583 -0
  41. data/libis-tools.gemspec +55 -0
  42. data/spec/assert_spec.rb +65 -0
  43. data/spec/checksum_spec.rb +68 -0
  44. data/spec/command_spec.rb +90 -0
  45. data/spec/config_file_spec.rb +83 -0
  46. data/spec/config_spec.rb +113 -0
  47. data/spec/csv_spec.rb +159 -0
  48. data/spec/data/test-headers.csv +2 -0
  49. data/spec/data/test-headers.tsv +2 -0
  50. data/spec/data/test-noheaders.csv +1 -0
  51. data/spec/data/test-noheaders.tsv +1 -0
  52. data/spec/data/test.data +9 -0
  53. data/spec/data/test.xlsx +0 -0
  54. data/spec/data/test.xml +8 -0
  55. data/spec/data/test.yml +2 -0
  56. data/spec/data/test_config.yml +15 -0
  57. data/spec/deep_struct_spec.rb +138 -0
  58. data/spec/logger_spec.rb +165 -0
  59. data/spec/mets_file_spec.rb +223 -0
  60. data/spec/parameter_container_spec.rb +152 -0
  61. data/spec/parameter_spec.rb +148 -0
  62. data/spec/spec_helper.rb +29 -0
  63. data/spec/spreadsheet_spec.rb +1820 -0
  64. data/spec/temp_file_spec.rb +76 -0
  65. data/spec/test.xsd +20 -0
  66. data/spec/thread_safe_spec.rb +64 -0
  67. data/spec/xmldocument_spec.rb +421 -0
  68. data/test/test_helper.rb +7 -0
  69. data/test/webservices/test_ca_item_info.rb +59 -0
  70. data/test/webservices/test_ca_search.rb +35 -0
  71. metadata +437 -0
@@ -0,0 +1,76 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/temp_file'
4
+
5
+ describe 'TempFile' do
6
+
7
+ context 'name' do
8
+
9
+ it 'without arguments' do
10
+ fname = Libis::Tools::TempFile.name()
11
+ expect(File.basename(fname)).to match(/^\d{8}_\d+_\w+$/)
12
+ end
13
+
14
+ it 'with prefix' do
15
+ fname = Libis::Tools::TempFile.name('abc')
16
+ expect(File.basename(fname)).to match(/^abc_\d{8}_\d+_\w+$/)
17
+ end
18
+
19
+ it 'with suffix' do
20
+ fname = Libis::Tools::TempFile.name(nil, '.xyz')
21
+ expect(File.basename(fname)).to match(/^\d{8}_\d+_\w+\.xyz$/)
22
+ end
23
+
24
+ it 'with prefix and suffix' do
25
+ fname = Libis::Tools::TempFile.name('abc', '.xyz')
26
+ expect(File.basename(fname)).to match(/^abc_\d{8}_\d+_\w+\.xyz$/)
27
+ end
28
+
29
+ it 'not in temp dir' do
30
+ fname = Libis::Tools::TempFile.name(nil, nil, '/abc/xyz/')
31
+ expect(File.dirname(fname)).to match('/abc/xyz')
32
+ end
33
+
34
+ end
35
+
36
+ context 'file' do
37
+
38
+ it 'created' do
39
+ f = Libis::Tools::TempFile.file
40
+ expect(File.exists?(f.path)).to be_truthy
41
+ f.close
42
+ f.delete
43
+ end
44
+
45
+ it 'is open' do
46
+ f = Libis::Tools::TempFile.file
47
+ expect(f.closed?).to be_falsey
48
+ f.close
49
+ f.delete
50
+ end
51
+
52
+ it 'can be closed' do
53
+ f = Libis::Tools::TempFile.file
54
+ expect(f.closed?).to be_falsey
55
+ f.close
56
+ expect(f.closed?).to be_truthy
57
+ f.delete
58
+ end
59
+
60
+ it 'can be unlinked' do
61
+ f = Libis::Tools::TempFile.file
62
+ f.close
63
+ f.unlink
64
+ expect(File.exists?(f.path)).to be_falsey
65
+ end
66
+
67
+ it 'can be deleted' do
68
+ f = Libis::Tools::TempFile.file
69
+ f.close
70
+ f.delete
71
+ expect(File.exists?(f.path)).to be_falsey
72
+ end
73
+
74
+ end
75
+
76
+ end
@@ -0,0 +1,20 @@
1
+ <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
2
+ <xs:element name="patron">
3
+ <xs:complexType>
4
+ <xs:sequence>
5
+ <xs:element type="xs:string" name="name"/>
6
+ <xs:element name="barcode">
7
+ <xs:complexType>
8
+ <xs:simpleContent>
9
+ <xs:extension base="xs:int">
10
+ <xs:attribute type="xs:string" name="library"/>
11
+ </xs:extension>
12
+ </xs:simpleContent>
13
+ </xs:complexType>
14
+ </xs:element>
15
+ <xs:element type="xs:string" name="access_level"/>
16
+ <xs:element type="xs:string" name="email" maxOccurs="unbounded" minOccurs="0"/>
17
+ </xs:sequence>
18
+ </xs:complexType>
19
+ </xs:element>
20
+ </xs:schema>
@@ -0,0 +1,64 @@
1
+ require_relative 'spec_helper'
2
+ require 'awesome_print'
3
+ require 'libis/tools/thread_safe'
4
+
5
+ describe 'ThreadSafe' do
6
+
7
+ class Foo
8
+ include Libis::Tools::ThreadSafe
9
+
10
+ def self.bar
11
+ self.class_mutex.synchronize {
12
+ @bar ||= get_number
13
+ }
14
+ end
15
+
16
+ def self.get_number
17
+ nr = rand(1000000)
18
+ sleep 1
19
+ nr
20
+ end
21
+ end
22
+
23
+ it 'protects class variable' do
24
+ mutex = Monitor.new
25
+ result = []
26
+ 1000.times.map do
27
+ Thread.new do
28
+ a = Foo.bar
29
+ mutex.synchronize {result << a}
30
+ end
31
+ end.each(&:join)
32
+ result.uniq!
33
+ # ap result
34
+ expect(result.size).to be 1
35
+ end
36
+
37
+ class Bar
38
+
39
+ def self.bar
40
+ @bar ||= get_number
41
+ end
42
+
43
+ def self.get_number
44
+ nr = rand(1000000)
45
+ sleep 1
46
+ nr
47
+ end
48
+ end
49
+
50
+ it 'without not thread safe' do
51
+ mutex = Monitor.new
52
+ result = []
53
+ 1000.times.map do
54
+ Thread.new do
55
+ a = Bar.bar
56
+ mutex.synchronize {result << a}
57
+ end
58
+ end.each(&:join)
59
+ result.uniq!
60
+ # ap result
61
+ expect(result.size).not_to be 1
62
+ end
63
+
64
+ end
@@ -0,0 +1,421 @@
1
+ # encoding: utf-8
2
+ require_relative 'spec_helper'
3
+ require 'libis/tools/xml_document'
4
+ require 'libis/tools/extend/string'
5
+
6
+ require 'rspec/matchers'
7
+ require 'equivalent-xml'
8
+
9
+ describe 'XML Document' do
10
+
11
+ test_file = File.join(File.dirname(__FILE__), 'data', 'test.xml')
12
+ test_xsd = File.join(File.dirname(__FILE__), 'test.xsd')
13
+
14
+ def match_xml(doc1, doc2)
15
+ doc1 = ::Nokogiri::XML(doc1) if doc1.is_a?(String)
16
+ doc2 = ::Nokogiri::XML(doc2) if doc2.is_a?(String)
17
+ # noinspection RubyResolve
18
+ # expect(doc1).to be_equivalent_to(doc2).respecting_element_order
19
+ expect(doc1).to be_equivalent_to(doc2)
20
+ end
21
+
22
+ before :context do
23
+ @xml_template = <<-END.align_left
24
+ <?xml version="1.0" encoding="utf-8"?>
25
+ <patron>
26
+ <name>Harry Potter</name>
27
+ <barcode library="Hogwarts Library">1234567890</barcode>
28
+ <access_level>student</access_level>
29
+ <email>harry.potter@hogwarts.edu</email>
30
+ <email>hpotter@JKRowling.com</email>
31
+ </patron>
32
+ END
33
+ end
34
+
35
+ it 'should create new empty XML document' do
36
+
37
+ xml_doc = ::Libis::Tools::XmlDocument.new
38
+
39
+ expect(xml_doc.document).not_to be_nil
40
+ # noinspection RubyResolve
41
+ expect(xml_doc).not_to be_valid
42
+ # noinspection RubyResolve
43
+ expect(xml_doc).to be_invalid
44
+
45
+ match_xml xml_doc.document, <<-END.align_left
46
+ <?xml version="1.0" encoding="utf-8"?>
47
+ END
48
+
49
+ end
50
+
51
+ it 'should load test file' do
52
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
53
+ # noinspection RubyResolve
54
+ expect(xml_doc).to be_valid
55
+ match_xml xml_doc.document, @xml_template
56
+
57
+ end
58
+
59
+ it 'should parse XML from string' do
60
+ xml_doc = ::Libis::Tools::XmlDocument.parse(<<-END.align_left)
61
+ <patron>
62
+ <name>Harry Potter</name>
63
+ <barcode library="Hogwarts Library">1234567890</barcode>
64
+ <access_level>student</access_level>
65
+ <email>harry.potter@hogwarts.edu</email>
66
+ <email>hpotter@JKRowling.com</email>
67
+ </patron>
68
+ END
69
+
70
+ match_xml xml_doc.document, @xml_template
71
+ end
72
+
73
+ it 'should parse XML from Hash' do
74
+ xml_doc = ::Libis::Tools::XmlDocument.from_hash({patron: {
75
+ name: 'Harry Potter',
76
+ barcode: {
77
+ '@library' => 'Hogwarts Library',
78
+ content!: '1234567890',
79
+ },
80
+ access_level: 'student',
81
+ email: %w'harry.potter@hogwarts.edu hpotter@JKRowling.com'
82
+ }},
83
+ {:key_converter => :none}
84
+ )
85
+
86
+ match_xml xml_doc.document, @xml_template
87
+
88
+ end
89
+
90
+ it 'should validate document against schema' do
91
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
92
+
93
+ expect(xml_doc.validates_against? test_xsd).to be_truthy
94
+ # noinspection RubyResolve
95
+ expect(xml_doc.validate test_xsd).to be_empty
96
+
97
+ end
98
+
99
+ it 'should allow to add a processing instruction' do
100
+ xml_doc = ::Libis::Tools::XmlDocument.parse '<patron/>'
101
+ xml_doc.add_processing_instruction 'xml-stylesheet', 'type="text/xsl" href="style.xsl"'
102
+
103
+ match_xml xml_doc.document, <<-END.align_left
104
+ <?xml version="1.0" encoding="utf-8"?>
105
+ <?xml-stylesheet type="text/xsl" href="style.xsl"?>
106
+ <patron/>
107
+ END
108
+ end
109
+
110
+ it 'should get the root node of the document' do
111
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
112
+ root = xml_doc.root
113
+
114
+ expect(root.name).to eq 'patron'
115
+ match_xml root.document, <<-END.align_left
116
+ <patron>
117
+ <name>Harry Potter</name>
118
+ <barcode library="Hogwarts Library">1234567890</barcode>
119
+ <access_level>student</access_level>
120
+ <email>harry.potter@hogwarts.edu</email>
121
+ <email>hpotter@JKRowling.com</email>
122
+ </patron>
123
+ END
124
+
125
+ end
126
+
127
+ it 'should set the root node of the document' do
128
+ xml_doc = ::Libis::Tools::XmlDocument.new
129
+ patron = ::Nokogiri::XML::Node.new 'patron', xml_doc.document
130
+ xml_doc.root = patron
131
+
132
+ match_xml xml_doc.document, <<-END.align_left
133
+ <?xml version="1.0" encoding="utf-8"?>
134
+ <patron/>
135
+ END
136
+
137
+ end
138
+
139
+ it 'should enable Nokogiri Build syntax' do
140
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
141
+
142
+ xml_doc.build(xml_doc.root) do
143
+ # noinspection RubyResolve
144
+ books do
145
+ book title: 'Quidditch Through the Ages', author: 'Kennilworthy Whisp', due_date: '1992-4-23'
146
+ end
147
+ end
148
+
149
+ match_xml xml_doc.document, <<-END.align_left
150
+ <?xml version="1.0" encoding="utf-8"?>
151
+ <patron>
152
+ <name>Harry Potter</name>
153
+ <barcode library="Hogwarts Library">1234567890</barcode>
154
+ <access_level>student</access_level>
155
+ <email>harry.potter@hogwarts.edu</email>
156
+ <email>hpotter@JKRowling.com</email>
157
+ <books>
158
+ <book title="Quidditch Through the Ages" author="Kennilworthy Whisp" due_date="1992-4-23"/>
159
+ </books>
160
+ </patron>
161
+ END
162
+
163
+ end
164
+
165
+ it 'should enable Nokogiri Build syntax for new document' do
166
+ xml_doc = ::Libis::Tools::XmlDocument.build do
167
+ # noinspection RubyResolve
168
+ patron {
169
+ name 'Harry Potter'
170
+ barcode( '1234567890', library: 'Hogwarts Library')
171
+ access_level 'student'
172
+ email 'harry.potter@hogwarts.edu'
173
+ email 'hpotter@JKRowling.com'
174
+ # noinspection RubyResolve
175
+ books {
176
+ book title: 'Quidditch Through the Ages', author: 'Kennilworthy Whisp', due_date: '1992-4-23'
177
+ }
178
+ }
179
+ end
180
+
181
+ match_xml xml_doc.document, <<-END.align_left
182
+ <?xml version="1.0" encoding="utf-8"?>
183
+ <patron>
184
+ <name>Harry Potter</name>
185
+ <barcode library="Hogwarts Library">1234567890</barcode>
186
+ <access_level>student</access_level>
187
+ <email>harry.potter@hogwarts.edu</email>
188
+ <email>hpotter@JKRowling.com</email>
189
+ <books>
190
+ <book title="Quidditch Through the Ages" author="Kennilworthy Whisp" due_date="1992-4-23"/>
191
+ </books>
192
+ </patron>
193
+ END
194
+
195
+ end
196
+
197
+ it 'should add a new node to the document' do
198
+ xml_doc = ::Libis::Tools::XmlDocument.new
199
+
200
+ xml_doc.add_node :patron
201
+ xml_doc.add_node :name, 'Harry Potter'
202
+ books = xml_doc.add_node :books, namespaces: { jkr: 'http://JKRowling.com' , node_ns: 'jkr' }
203
+ xml_doc.add_node :book, nil, books,
204
+ title: 'Quidditch Through the Ages', author: 'Kennilworthy Whisp', due_date: '1992-4-23',
205
+ namespaces: {node_ns: 'jkr'}
206
+
207
+ match_xml xml_doc.document, <<-END.align_left
208
+ <?xml version="1.0" encoding="utf-8"?>
209
+ <patron>
210
+ <name>Harry Potter</name>
211
+ <jkr:books xmlns:jkr="http://JKRowling.com">
212
+ <jkr:book author="Kennilworthy Whisp" due_date="1992-4-23" title="Quidditch Through the Ages"/>
213
+ </jkr:books>
214
+ </patron>
215
+ END
216
+
217
+ expect(xml_doc.root.name).to eq 'patron'
218
+ expect(xml_doc.root.children.size).to be 2
219
+ expect(xml_doc.root.children[0].name).to eq 'name'
220
+ expect(xml_doc.root.children[0].content).to eq 'Harry Potter'
221
+ expect(xml_doc.root.children[1].name).to eq 'books'
222
+ expect(xml_doc.root.children[1].namespace.prefix).to eq 'jkr'
223
+ expect(xml_doc.root.children[1].namespace.href).to eq 'http://JKRowling.com'
224
+ expect(xml_doc.root.children[1].namespaces.size).to be 1
225
+ expect(xml_doc.root.children[1].namespaces['xmlns:jkr']).to eq 'http://JKRowling.com'
226
+ expect(xml_doc.root.children[1].children.size).to be 1
227
+ expect(xml_doc.root.children[1].children[0].name).to eq 'book'
228
+ expect(xml_doc.root.children[1].children[0].namespace.prefix).to eq 'jkr'
229
+ expect(xml_doc.root.children[1].children[0].namespace.href).to eq 'http://JKRowling.com'
230
+ expect(xml_doc.root.children[1].children[0]['title']).to eq 'Quidditch Through the Ages'
231
+ expect(xml_doc.root.children[1].children[0]['author']).to eq 'Kennilworthy Whisp'
232
+ expect(xml_doc.root.children[1].children[0]['due_date']).to eq '1992-4-23'
233
+
234
+ end
235
+
236
+ it 'should add attributes to a node' do
237
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
238
+
239
+ xml_doc.add_attributes xml_doc.root, id: '123456', status: 'active'
240
+
241
+ match_xml xml_doc.document, <<-END.align_left
242
+ <?xml version="1.0" encoding="utf-8"?>
243
+ <patron status="active" id="123456">
244
+ <name>Harry Potter</name>
245
+ <barcode library="Hogwarts Library">1234567890</barcode>
246
+ <access_level>student</access_level>
247
+ <email>harry.potter@hogwarts.edu</email>
248
+ <email>hpotter@JKRowling.com</email>
249
+ </patron>
250
+ END
251
+
252
+ end
253
+
254
+ it 'should add namespaces to a node' do
255
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
256
+
257
+ xml_doc.add_namespaces xml_doc.root, jkr: 'http://JKRowling.com', node_ns: 'jkr'
258
+ # noinspection RubyResolve
259
+ xml_doc.add_namespaces xml_doc.barcode, nil => 'http://hogwarts.edu'
260
+
261
+ match_xml xml_doc.document, <<-END.align_left
262
+ <?xml version="1.0" encoding="utf-8"?>
263
+ <jkr:patron xmlns:jkr="http://JKRowling.com">
264
+ <name>Harry Potter</name>
265
+ <barcode library="Hogwarts Library" xmlns="http://hogwarts.edu">1234567890</barcode>
266
+ <access_level>student</access_level>
267
+ <email>harry.potter@hogwarts.edu</email>
268
+ <email>hpotter@JKRowling.com</email>
269
+ </jkr:patron>
270
+ END
271
+
272
+ expect(xml_doc.document.root.namespace.prefix).to eq 'jkr'
273
+ expect(xml_doc.document.root.namespace.href).to eq 'http://JKRowling.com'
274
+ expect(xml_doc.document.root.elements[1].namespace.prefix).to be_nil
275
+ expect(xml_doc.document.root.elements[1].namespace.href).to eq 'http://hogwarts.edu'
276
+
277
+ end
278
+
279
+ it 'should search for nodes in the current document root' do
280
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
281
+
282
+ nodes = xml_doc.xpath('//email')
283
+ expect(nodes.size).to be 2
284
+ expect(nodes.map(&:content)).to eq %w'harry.potter@hogwarts.edu hpotter@JKRowling.com'
285
+
286
+ end
287
+
288
+ it 'should check if the XML document contains certain element(s)' do
289
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
290
+
291
+ expect(xml_doc.has_element? 'barcode[@library="Hogwarts Library"]').to be_truthy
292
+
293
+ end
294
+
295
+ it 'should return the content of the first element found' do
296
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
297
+
298
+ expect(xml_doc.value('//email')).to eq 'harry.potter@hogwarts.edu'
299
+
300
+ end
301
+
302
+ it 'should return the content of all elements found' do
303
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
304
+
305
+ expect(xml_doc.values('//email')).to eq %w'harry.potter@hogwarts.edu hpotter@JKRowling.com'
306
+
307
+ end
308
+
309
+ it 'should return the content of the first element in the set of nodes' do
310
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
311
+
312
+ expect(::Libis::Tools::XmlDocument.get_content(xml_doc.xpath('//email'))).to eq 'harry.potter@hogwarts.edu'
313
+ end
314
+
315
+ it 'should Find a node and set its content' do
316
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
317
+
318
+ xml_doc['//access_level'] = 'postgraduate'
319
+
320
+ match_xml xml_doc.document, <<-END.align_left
321
+ <?xml version="1.0" encoding="utf-8"?>
322
+ <patron>
323
+ <name>Harry Potter</name>
324
+ <barcode library="Hogwarts Library">1234567890</barcode>
325
+ <access_level>postgraduate</access_level>
326
+ <email>harry.potter@hogwarts.edu</email>
327
+ <email>hpotter@JKRowling.com</email>
328
+ </patron>
329
+ END
330
+
331
+ end
332
+
333
+ # noinspection RubyResolve
334
+ it 'should allow node access by method name' do
335
+ xml_doc = ::Libis::Tools::XmlDocument.open(test_file)
336
+
337
+ expect(xml_doc.email.content).to eq 'harry.potter@hogwarts.edu'
338
+ expect(xml_doc.barcode 'library').to eq 'Hogwarts Library'
339
+
340
+ xml_doc.access_level = 'postgraduate'
341
+ xml_doc.barcode 'library', 'Hogwarts Dumbledore Library'
342
+ # noinspection RubyResolve
343
+ xml_doc.dates do |dates|
344
+ dates.birth_date '1980-07-31'
345
+ dates.member_since '1991-09-01'
346
+ end
347
+
348
+ match_xml xml_doc.document, <<-END.align_left
349
+ <?xml version="1.0" encoding="utf-8"?>
350
+ <patron>
351
+ <name>Harry Potter</name>
352
+ <barcode library="Hogwarts Dumbledore Library">1234567890</barcode>
353
+ <access_level>postgraduate</access_level>
354
+ <email>harry.potter@hogwarts.edu</email>
355
+ <email>hpotter@JKRowling.com</email>
356
+ <dates>
357
+ <birth_date>1980-07-31</birth_date>
358
+ <member_since>1991-09-01</member_since>
359
+ </dates>
360
+ </patron>
361
+ END
362
+
363
+ end
364
+
365
+ it 'allows to parse xml string, save and reload' do
366
+ xml_doc = ::Libis::Tools::XmlDocument.parse(<<-END.align_left)
367
+ <patron>
368
+ <name>Harry Potter</name>
369
+ <barcode library="Hogwarts Library">1234567890</barcode>
370
+ <access_level>student</access_level>
371
+ <email>harry.potter@hogwarts.edu</email>
372
+ <email>hpotter@JKRowling.com</email>
373
+ </patron>
374
+ END
375
+
376
+ match_xml xml_doc.document, @xml_template
377
+
378
+ xml_doc.save('/tmp/test.xml')
379
+
380
+ xml_doc = ::Libis::Tools::XmlDocument.open('/tmp/test.xml')
381
+
382
+ match_xml xml_doc.document, @xml_template
383
+
384
+ end
385
+
386
+ it 'supports build to create XML document' do
387
+
388
+ xml_doc = ::Libis::Tools::XmlDocument.build do
389
+ # noinspection RubyResolve
390
+ patron {
391
+ name 'Harry Potter'
392
+ barcode( '1234567890', library: 'Hogwarts Library')
393
+ access_level 'student'
394
+ email 'harry.potter@hogwarts.edu'
395
+ email 'hpotter@JKRowling.com'
396
+ }
397
+ end
398
+
399
+ match_xml xml_doc.document, @xml_template
400
+
401
+ end
402
+
403
+ it 'supports different ways to create nodes' do
404
+
405
+ xml_doc = ::Libis::Tools::XmlDocument.new
406
+ xml_doc.add_node :patron
407
+ xml_doc.name = 'Harry Potter'
408
+ # noinspection RubyResolve
409
+ xml_doc.barcode = '1234567890'
410
+ # noinspection RubyResolve
411
+ xml_doc.barcode :library, 'Hogwarts Library'
412
+ # noinspection RubyResolve
413
+ xml_doc.access_level = 'student'
414
+ xml_doc.email = 'harry.potter@hogwarts.edu'
415
+ xml_doc.add_node :email, 'hpotter@JKRowling.com'
416
+
417
+ match_xml xml_doc.document, @xml_template
418
+
419
+ end
420
+
421
+ end