]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/ext/test/org/argeo/cms/tabular/JcrTabularTest.java
Close release cycle
[lgpl/argeo-commons.git] / org.argeo.cms / ext / test / org / argeo / cms / tabular / JcrTabularTest.java
1 package org.argeo.cms.tabular;
2
3 import java.io.InputStreamReader;
4 import java.util.ArrayList;
5 import java.util.List;
6
7 import javax.jcr.Node;
8 import javax.jcr.PropertyType;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.apache.jackrabbit.commons.cnd.CndImporter;
13 import org.argeo.api.tabular.TabularColumn;
14 import org.argeo.api.tabular.TabularRow;
15 import org.argeo.api.tabular.TabularRowIterator;
16 import org.argeo.api.tabular.TabularWriter;
17 import org.argeo.cms.ArgeoTypes;
18 import org.argeo.jackrabbit.unit.AbstractJackrabbitTestCase;
19
20 public class JcrTabularTest extends AbstractJackrabbitTestCase {
21 private final static Log log = LogFactory.getLog(JcrTabularTest.class);
22
23 public void testWriteReadCsv() throws Exception {
24 // session().setNamespacePrefix("argeo", ArgeoNames.ARGEO_NAMESPACE);
25 InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream("/org/argeo/api/ldap.cnd"));
26 CndImporter.registerNodeTypes(reader, session());
27 reader.close();
28 reader = new InputStreamReader(getClass().getResourceAsStream("/org/argeo/cms/argeo.cnd"));
29 CndImporter.registerNodeTypes(reader, session());
30 reader.close();
31 // reader = new InputStreamReader(getClass().getResourceAsStream("/org/argeo/cms/cms.cnd"));
32 // CndImporter.registerNodeTypes(reader, session());
33 // reader.close();
34
35 // write
36 Integer columnCount = 15;
37 Long rowCount = 1000l;
38 String stringValue = "test, \ntest";
39
40 List<TabularColumn> header = new ArrayList<TabularColumn>();
41 for (int i = 0; i < columnCount; i++) {
42 header.add(new TabularColumn("col" + i, PropertyType.STRING));
43 }
44 Node tableNode = session().getRootNode().addNode("table", ArgeoTypes.ARGEO_TABLE);
45 TabularWriter writer = new JcrTabularWriter(tableNode, header, ArgeoTypes.ARGEO_CSV);
46 for (int i = 0; i < rowCount; i++) {
47 List<Object> objs = new ArrayList<Object>();
48 for (int j = 0; j < columnCount; j++) {
49 objs.add(stringValue);
50 }
51 writer.appendRow(objs.toArray());
52 }
53 writer.close();
54 session().save();
55
56 if (log.isDebugEnabled())
57 log.debug("Wrote tabular content " + rowCount + " rows, " + columnCount + " columns");
58 // read
59 TabularRowIterator rowIt = new JcrTabularRowIterator(tableNode);
60 Long count = 0l;
61 while (rowIt.hasNext()) {
62 TabularRow tr = rowIt.next();
63 assertEquals(header.size(), tr.size());
64 count++;
65 }
66 assertEquals(rowCount, count);
67 if (log.isDebugEnabled())
68 log.debug("Read tabular content " + rowCount + " rows, " + columnCount + " columns");
69 }
70 }