2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org
.argeo
.jcr
.tabular
;
19 import java
.io
.InputStreamReader
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import javax
.jcr
.Node
;
24 import javax
.jcr
.PropertyType
;
26 import org
.apache
.commons
.logging
.Log
;
27 import org
.apache
.commons
.logging
.LogFactory
;
28 import org
.apache
.jackrabbit
.commons
.cnd
.CndImporter
;
29 import org
.argeo
.jcr
.AbstractInternalJackrabbitTestCase
;
30 import org
.argeo
.jcr
.ArgeoNames
;
31 import org
.argeo
.jcr
.ArgeoTypes
;
32 import org
.argeo
.util
.tabular
.TabularColumn
;
33 import org
.argeo
.util
.tabular
.TabularRow
;
34 import org
.argeo
.util
.tabular
.TabularRowIterator
;
35 import org
.argeo
.util
.tabular
.TabularWriter
;
37 public class JcrTabularTest
extends AbstractInternalJackrabbitTestCase
{
38 private final static Log log
= LogFactory
.getLog(JcrTabularTest
.class);
40 public void testWriteReadCsv() throws Exception
{
41 session().setNamespacePrefix("argeo", ArgeoNames
.ARGEO_NAMESPACE
);
42 InputStreamReader reader
= new InputStreamReader(getClass()
43 .getResourceAsStream("/org/argeo/jcr/argeo.cnd"));
44 CndImporter
.registerNodeTypes(reader
, session());
48 Integer columnCount
= 15;
49 Long rowCount
= 1000l;
50 String stringValue
= "test, \ntest";
52 List
<TabularColumn
> header
= new ArrayList
<TabularColumn
>();
53 for (int i
= 0; i
< columnCount
; i
++) {
54 header
.add(new TabularColumn("col" + i
, PropertyType
.STRING
));
56 Node tableNode
= session().getRootNode().addNode("table",
57 ArgeoTypes
.ARGEO_TABLE
);
58 TabularWriter writer
= new JcrTabularWriter(tableNode
, header
,
59 ArgeoTypes
.ARGEO_CSV
);
60 for (int i
= 0; i
< rowCount
; i
++) {
61 List
<Object
> objs
= new ArrayList
<Object
>();
62 for (int j
= 0; j
< columnCount
; j
++) {
63 objs
.add(stringValue
);
65 writer
.appendRow(objs
.toArray());
70 if (log
.isDebugEnabled())
71 log
.debug("Wrote tabular content " + rowCount
+ " rows, "
72 + columnCount
+ " columns");
74 TabularRowIterator rowIt
= new JcrTabularRowIterator(tableNode
);
76 while (rowIt
.hasNext()) {
77 TabularRow tr
= rowIt
.next();
78 assertEquals(header
.size(), tr
.size());
81 assertEquals(rowCount
, count
);
82 if (log
.isDebugEnabled())
83 log
.debug("Read tabular content " + rowCount
+ " rows, "
84 + columnCount
+ " columns");