]> git.argeo.org Git - lgpl/argeo-commons.git/blob - cms/tabular/JcrTabularTest.java
Prepare next development cycle
[lgpl/argeo-commons.git] / cms / tabular / JcrTabularTest.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 org.argeo.cms.tabular;
17
18 import java.io.InputStreamReader;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import javax.jcr.Node;
23 import javax.jcr.PropertyType;
24
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.cms.ArgeoTypes;
29 import org.argeo.jackrabbit.unit.AbstractJackrabbitTestCase;
30 import org.argeo.node.tabular.TabularColumn;
31 import org.argeo.node.tabular.TabularRow;
32 import org.argeo.node.tabular.TabularRowIterator;
33 import org.argeo.node.tabular.TabularWriter;
34
35 public class JcrTabularTest extends AbstractJackrabbitTestCase {
36 private final static Log log = LogFactory.getLog(JcrTabularTest.class);
37
38 public void testWriteReadCsv() throws Exception {
39 // session().setNamespacePrefix("argeo", ArgeoNames.ARGEO_NAMESPACE);
40 InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream("/org/argeo/node/node.cnd"));
41 CndImporter.registerNodeTypes(reader, session());
42 reader.close();
43 reader = new InputStreamReader(getClass().getResourceAsStream("/org/argeo/cms/cms.cnd"));
44 CndImporter.registerNodeTypes(reader, session());
45 reader.close();
46
47 // write
48 Integer columnCount = 15;
49 Long rowCount = 1000l;
50 String stringValue = "test, \ntest";
51
52 List<TabularColumn> header = new ArrayList<TabularColumn>();
53 for (int i = 0; i < columnCount; i++) {
54 header.add(new TabularColumn("col" + i, PropertyType.STRING));
55 }
56 Node tableNode = session().getRootNode().addNode("table", ArgeoTypes.ARGEO_TABLE);
57 TabularWriter writer = new JcrTabularWriter(tableNode, header, ArgeoTypes.ARGEO_CSV);
58 for (int i = 0; i < rowCount; i++) {
59 List<Object> objs = new ArrayList<Object>();
60 for (int j = 0; j < columnCount; j++) {
61 objs.add(stringValue);
62 }
63 writer.appendRow(objs.toArray());
64 }
65 writer.close();
66 session().save();
67
68 if (log.isDebugEnabled())
69 log.debug("Wrote tabular content " + rowCount + " rows, " + columnCount + " columns");
70 // read
71 TabularRowIterator rowIt = new JcrTabularRowIterator(tableNode);
72 Long count = 0l;
73 while (rowIt.hasNext()) {
74 TabularRow tr = rowIt.next();
75 assertEquals(header.size(), tr.size());
76 count++;
77 }
78 assertEquals(rowCount, count);
79 if (log.isDebugEnabled())
80 log.debug("Read tabular content " + rowCount + " rows, " + columnCount + " columns");
81 }
82 }