]> git.argeo.org Git - lgpl/argeo-commons.git/blob - server/runtime/org.argeo.server.jcr/src/test/java/org/argeo/jcr/tabular/JcrTabularTest.java
Improve JCR authorizations
[lgpl/argeo-commons.git] / server / runtime / org.argeo.server.jcr / src / test / java / org / argeo / jcr / tabular / JcrTabularTest.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.jcr.tabular;
18
19 import java.io.InputStreamReader;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import javax.jcr.Node;
24 import javax.jcr.PropertyType;
25
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;
36
37 public class JcrTabularTest extends AbstractInternalJackrabbitTestCase {
38 private final static Log log = LogFactory.getLog(JcrTabularTest.class);
39
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());
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",
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);
64 }
65 writer.appendRow(objs.toArray());
66 }
67 writer.close();
68 session().save();
69
70 if (log.isDebugEnabled())
71 log.debug("Wrote tabular content " + rowCount + " rows, "
72 + columnCount + " columns");
73 // read
74 TabularRowIterator rowIt = new JcrTabularRowIterator(tableNode);
75 Long count = 0l;
76 while (rowIt.hasNext()) {
77 TabularRow tr = rowIt.next();
78 assertEquals(header.size(), tr.size());
79 count++;
80 }
81 assertEquals(rowCount, count);
82 if (log.isDebugEnabled())
83 log.debug("Read tabular content " + rowCount + " rows, "
84 + columnCount + " columns");
85 }
86 }