package org.jboss.soa.modeshape.qa.tests; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.Map; import java.util.ServiceLoader; import java.util.Set; import javax.jcr.Binary; import javax.jcr.Node; import javax.jcr.Repository; import javax.jcr.RepositoryFactory; import javax.jcr.Session; import org.jboss.soa.modeshape.ModeshapeClient; import org.testng.Assert; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; @Test(enabled = true) public class CNDFileSequencerTest { protected static final String DEFAULT_REPOSITORY = "eds"; protected static final String DEFAULT_WORKSPACE = "default"; protected static final String FILES_DIRECTORY = "files"; private ModeshapeClient client; private Map parentProperties; private Map qeNodeProperties; private Map propertyDefinition; private Set multiValues; private Session sess; @DataProvider(name = "repository-types") Object[][] getRepositoryTypes() { return new Object [][] { {DEFAULT_REPOSITORY,DEFAULT_WORKSPACE}/* {"test-inmemory","default"} , {"test-jpa", "default"}*/ }; } @DataProvider(name = "repos") Object[][] repositories() { return new Object [][] { {DEFAULT_REPOSITORY,DEFAULT_WORKSPACE}, {"test-jpa", "default"} }; } @BeforeMethod public void beforeMethod() throws Exception { initProperties(); } private Repository getRepository(String name) throws Exception { Repository repository = null; Map parameters = new HashMap(); parameters.put("org.modeshape.jcr.URL", "jndi:jcr/local?repositoryName="+ name); for (RepositoryFactory aFactory : ServiceLoader.load(RepositoryFactory.class)) { repository = aFactory.getRepository(parameters); if (repository != null) return repository; } return repository; } private Session getSession(Repository repository, String workspace) throws Exception { return repository.login(workspace); } @Test(enabled = true, dataProvider = "repository-types") public void testCNDFileSequencerSession(String repository, String workspace) throws Exception { System.out.println("Retrieving repository: " + repository + " with workspace " + workspace); sess = getSession(getRepository(repository), workspace); Node root = sess.getRootNode(); Node files = null; Node testCnd = null; Node content = null; System.out.println("Retrieving /files nodes.."); if (root.hasNode("files")) { files = root.getNode("files"); testCnd = files.addNode("test.cnd", "nt:file"); content = testCnd.addNode("jcr:content", "nt:resource"); Binary b = sess.getValueFactory().createBinary(getClass().getResourceAsStream("/test.cnd")); content.setProperty("jcr:data", b); content.setProperty("jcr:mimeType", "text/plain"); sess.save(); root = sess.getRootNode(); System.out.println("has /files/test.cnd: "+ root.hasNode("files/test.cnd")); System.out.println("has /sequenced: " + root.hasNode("sequenced")); System.out.println("has /sequenced/cnd: " + root.hasNode("sequenced/cnd")); System.out.println("has /sequenced/cnd/test.cnd: " + root.hasNode("sequenced/cnd/test.cnd")); Node qeNode = root.getNode("sequenced/cnd/test.cnd/qe:qeNode"); assertNodeProperties(qeNode, qeNodeProperties); Node qeParentNode = root.getNode("sequenced/cnd/test.cnd/qe:qeParentNode"); assertNodeProperties(qeParentNode, parentProperties); } } private void assertNodeExists(String relPath, boolean expected) throws Exception { Assert.assertEquals(client.hasNode(relPath), expected); } private void assertNodeProperties(Node node, Map properties) throws Exception { String value = null; Iterator> it = properties.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = it.next(); if (node.getProperty(entry.getKey()).isMultiple()) { value = node.getProperty(entry.getKey()).getValues()[0].getString(); } else { value = node.getProperty(entry.getKey()).getValue().getString(); } Assert.assertEquals(value, entry.getValue()); } } private void initProperties() throws Exception { this.parentProperties = new HashMap(); parentProperties.put("jcr:primaryType", "nt:nodeType"); parentProperties.put("jcr:isQueryable", "true"); parentProperties.put("jcr:isMixin", "false"); parentProperties.put("jcr:isAbstract","false"); parentProperties.put("jcr:onParentVersion", "COPY"); parentProperties.put("jcr:nodeTypeName", "qe:qeParentNode"); parentProperties.put("jcr:hasOrderableChildNodes", "false"); this.qeNodeProperties = new HashMap(); qeNodeProperties.put("jcr:isMixin", "false"); qeNodeProperties.put("jcr:isQueryable", "true"); qeNodeProperties.put("jcr:primaryType", "nt:nodeType"); qeNodeProperties.put("jcr:isAbstract", "true"); qeNodeProperties.put("jcr:nodeTypeName", "qe:qeNode"); qeNodeProperties.put("jcr:onParentVersion", "COPY"); qeNodeProperties.put("jcr:hasOrderableChildNodes", "true"); qeNodeProperties.put("jcr:supertypes", "qe:qeParentNode"); this.propertyDefinition = new HashMap(); propertyDefinition.put("jcr:name", "qe:name"); propertyDefinition.put("jcr:requiredType", "STRING"); propertyDefinition.put("jcr:protected", "false"); propertyDefinition.put("jcr:primaryType", "nt:propertyDefinition"); propertyDefinition.put("jcr:defaultValues", "default value 1"); propertyDefinition.put("jcr:isQueryOrderable", "true"); propertyDefinition.put("jcr:autoCreated", "false"); propertyDefinition.put("jcr:isFullTextSearchable", "true"); propertyDefinition.put("jcr:multiple", "false"); this.multiValues = new HashSet(); multiValues.add("jcr:mixinTypes"); multiValues.add("jcr:onParentVersion"); multiValues.add("jcr:supertypes"); multiValues.add("jcr:defaultValues"); } }