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