package au.com.infomedix.harvey.documentstore.jcr; import static org.junit.Assert.*; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import javax.jcr.Node; import javax.jcr.Repository; import javax.jcr.Session; import javax.jcr.SimpleCredentials; import javax.jcr.nodetype.NodeType; import junitx.util.PrivateAccessor; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.Mockito; import org.modeshape.common.collection.Problems; import org.modeshape.jcr.JcrEngine; import org.modeshape.jcr.RepositoryConfiguration; import au.com.infomedix.harvey.config.WFAttrTypesConfig; public class MixinTest { private Repository repository; private Session session; // should keep this session alive until tearDown(), private JcrEngine engine; // otherwise the repository will be closed and // we will lose our changes (since we are using // in memory repository). @Before public void setUp() throws Exception { Repository repository = null; // This is how to connect when in a appserver // we need to create a engine here and configure it from // a testcase configuration file. // new CndImporter().registerNodeTypes(new // InputStreamReader(DocumentStoreJcrBeanTest.class.getResourceAsStream("/schema.cnd")), // session); // Create and start the engine ... engine = new JcrEngine(); engine.start(); RepositoryConfiguration config = RepositoryConfiguration.read("src/test/resources/repository-config.json"); // Verify the configuration for the repository ... Problems problems = config.validate(); if (problems.hasErrors()) { System.err.println("Problems starting the engine."); System.err.println(problems); throw new Exception("Problems starting the JCR engine, " + problems); } repository = engine.deploy(config); session = repository.login(new SimpleCredentials("username", "password".toCharArray()), "default"); org.modeshape.jcr.api.nodetype.NodeTypeManager ntm = (org.modeshape.jcr.api.nodetype.NodeTypeManager) session .getWorkspace().getNodeTypeManager(); ntm.registerNodeTypes(this.getClass().getResourceAsStream("/simple-schema.cnd"), true); } @Test public void addTwoNodesWithTypes() throws Exception { Node docGroup = session.getRootNode().addNode("inf:documentGroup", "inf:documentGroup"); assertEquals("inf:documentGroup",docGroup.getPrimaryNodeType().getName()); docGroup.addMixin("er:eReferral"); Node ergp = docGroup.addNode("er:gp", "inf:doctor"); assertEquals("inf:doctor",ergp.getPrimaryNodeType().getName()); } @Test public void addTwoNodesWithoutSubnodeType() throws Exception { Node docGroup = session.getRootNode().addNode("inf:documentGroup", "inf:documentGroup"); assertEquals("inf:documentGroup",docGroup.getPrimaryNodeType().getName()); docGroup.addMixin("er:eReferral"); Node ergp = docGroup.addNode("er:gp"); assertEquals("inf:doctor",ergp.getPrimaryNodeType().getName()); } @After public void tearDown() throws Exception { // shutdown the last session if (session != null && session.isLive()) { session.logout(); } if (engine != null) { Future termination = engine.shutdown(); boolean terminated = termination.get(2, TimeUnit.SECONDS); if (!terminated) { System.err.println("JCR Engine didn't terminate normally on shutdown"); } } } }