2 * Copyright (C) 2007-2012 Argeo GmbH
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.
16 package org
.argeo
.jcr
.tabular
;
18 import java
.io
.InputStreamReader
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
22 import javax
.jcr
.Node
;
23 import javax
.jcr
.PropertyType
;
25 import org
.apache
.commons
.logging
.Log
;
26 import org
.apache
.commons
.logging
.LogFactory
;
27 import org
.apache
.jackrabbit
.commons
.cnd
.CndImporter
;
28 import org
.argeo
.jcr
.AbstractInternalJackrabbitTestCase
;
29 import org
.argeo
.jcr
.ArgeoNames
;
30 import org
.argeo
.jcr
.ArgeoTypes
;
31 import org
.argeo
.util
.tabular
.TabularColumn
;
32 import org
.argeo
.util
.tabular
.TabularRow
;
33 import org
.argeo
.util
.tabular
.TabularRowIterator
;
34 import org
.argeo
.util
.tabular
.TabularWriter
;
36 public class JcrTabularTest
extends AbstractInternalJackrabbitTestCase
{
37 private final static Log log
= LogFactory
.getLog(JcrTabularTest
.class);
39 public void testWriteReadCsv() throws Exception
{
40 session().setNamespacePrefix("argeo", ArgeoNames
.ARGEO_NAMESPACE
);
41 InputStreamReader reader
= new InputStreamReader(getClass()
42 .getResourceAsStream("/org/argeo/jcr/argeo.cnd"));
43 CndImporter
.registerNodeTypes(reader
, session());
47 Integer columnCount
= 15;
48 Long rowCount
= 1000l;
49 String stringValue
= "test, \ntest";
51 List
<TabularColumn
> header
= new ArrayList
<TabularColumn
>();
52 for (int i
= 0; i
< columnCount
; i
++) {
53 header
.add(new TabularColumn("col" + i
, PropertyType
.STRING
));
55 Node tableNode
= session().getRootNode().addNode("table",
56 ArgeoTypes
.ARGEO_TABLE
);
57 TabularWriter writer
= new JcrTabularWriter(tableNode
, header
,
58 ArgeoTypes
.ARGEO_CSV
);
59 for (int i
= 0; i
< rowCount
; i
++) {
60 List
<Object
> objs
= new ArrayList
<Object
>();
61 for (int j
= 0; j
< columnCount
; j
++) {
62 objs
.add(stringValue
);
64 writer
.appendRow(objs
.toArray());
69 if (log
.isDebugEnabled())
70 log
.debug("Wrote tabular content " + rowCount
+ " rows, "
71 + columnCount
+ " columns");
73 TabularRowIterator rowIt
= new JcrTabularRowIterator(tableNode
);
75 while (rowIt
.hasNext()) {
76 TabularRow tr
= rowIt
.next();
77 assertEquals(header
.size(), tr
.size());
80 assertEquals(rowCount
, count
);
81 if (log
.isDebugEnabled())
82 log
.debug("Read tabular content " + rowCount
+ " rows, "
83 + columnCount
+ " columns");