📄 abstractjcrtest.java
字号:
} } } } } if (isReadOnly) { if (testPath.length() == 0) { // test root is the root node testRootNode = superuser.getRootNode(); } else if (!superuser.getRootNode().hasNode(testPath)) { cleanUp(); fail("Workspace does not contain test data at: " + testRoot); } else { testRootNode = superuser.getRootNode().getNode(testPath); } } else if (isSupported(Repository.LEVEL_2_SUPPORTED)) { testRootNode = cleanUpTestRoot(superuser); // also clean second workspace Session s = helper.getSuperuserSession(workspaceName); try { cleanUpTestRoot(s); } finally { s.logout(); } } else { cleanUp(); fail("Test case requires level 2 support."); } } protected void cleanUp() throws Exception { if (superuser != null) { try { if (!isReadOnly && isSupported(Repository.LEVEL_2_SUPPORTED)) { cleanUpTestRoot(superuser); } } catch (Exception e) { log.println("Exception in tearDown: " + e.toString()); } finally { superuser.logout(); } } } protected void tearDown() throws Exception { cleanUp(); super.tearDown(); } /** * Runs the test cases of this test class and reports the results to * <code>testResult</code>. In contrast to the default implementation of * <code>TestCase.run()</code> this method will suppress tests errors with * a {@link NotExecutableException}. That is, test cases that throw this * exception will still result as successful. * @param testResult the test result. */ public void run(TestResult testResult) { super.run(new JCRTestResult(testResult, log)); } /** * Returns the value of the configuration property with <code>propName</code>. * The sequence how configuration properties are read is the follwoing: * <ol> * <li><code>javax.jcr.tck.<testClassName>.<testCaseName>.<propName></code></li> * <li><code>javax.jcr.tck.<testClassName>.<propName></code></li> * <li><code>javax.jcr.tck.<packageName>.<propName></code></li> * <li><code>javax.jcr.tck.<propName></code></li> * </ol> * Where: * <ul> * <li><code><testClassName></code> is the name of the test class without package prefix.</li> * <li><code><testMethodName></code> is the name of the test method</li> * <li><code><packageName></code> is the name of the package of the test class. * Example: packageName for <code>org.apache.jackrabbit.test.api.BooleanPropertyTest</code>: <code>api</code></li> * </ul> * @param propName the propName of the configration property. * @return the value of the property or <code>null</code> if the property * does not exist. * @throws RepositoryException if an error occurs while reading from * the configuration. */ public String getProperty(String propName) throws RepositoryException { String testCaseName = getName(); String testClassName = getClass().getName(); String testPackName = ""; int idx; if ((idx = testClassName.lastIndexOf('.')) > -1) { testPackName = testClassName.substring(testClassName.lastIndexOf('.', idx - 1) + 1, idx); testClassName = testClassName.substring(idx + 1); } // 1) test case specific property first String value = helper.getProperty(RepositoryStub.PROP_PREFIX + "." + testClassName + "." + testCaseName + "." + propName); if (value != null) { return value; } // 2) check test class property value = helper.getProperty(RepositoryStub.PROP_PREFIX + "." + testClassName + "." + propName); if (value != null) { return value; } // 3) check package property value = helper.getProperty(RepositoryStub.PROP_PREFIX + "." + testPackName + "." + propName); if (value != null) { return value; } // finally try global property return helper.getProperty(RepositoryStub.PROP_PREFIX + "." + propName); } /** * Returns the size of the <code>RangeIterator</code> <code>it</code>. * Note, that the <code>RangeIterator</code> might get consumed, because * {@link RangeIterator#getSize()} might return -1 (information unavailable). * @param it a <code>RangeIterator</code>. * @return the size of the iterator (number of elements). */ protected long getSize(RangeIterator it) { long size = it.getSize(); if (size != -1) { return size; } size = 0; while (it.hasNext()) { it.next(); size++; } return size; } /** * Returns the name of a workspace that is not accessible from * <code>session</code>. * @param session the session. * @return name of a non existing workspace. * @throws RepositoryException if an error occurs. */ protected String getNonExistingWorkspaceName(Session session) throws RepositoryException { List names = Arrays.asList(session.getWorkspace().getAccessibleWorkspaceNames()); String nonExisting = null; while (nonExisting == null) { String name = createRandomString(10); if (!names.contains(name)) { nonExisting = name; } } return nonExisting; } /** * Creates a <code>String</code> with a random sequence of characters * using 'a' - 'z'. * @param numChars number of characters. * @return the generated String. */ protected String createRandomString(int numChars) { Random rand = new Random(System.currentTimeMillis()); StringBuffer tmp = new StringBuffer(numChars); for (int i = 0; i < numChars; i++) { char c = (char) (rand.nextInt(('z' + 1) - 'a') + 'a'); tmp.append(c); } return tmp.toString(); } /** * Returns <code>true</code> if this repository support a certain optional * feature; otherwise <code>false</code> is returned. If there is no * such <code>descriptorKey</code> present in the repository, this method * also returns false. * * @param descriptorKey the descriptor key. * @return <code>true</code> if the option is supported. * @throws RepositoryException if an error occurs. */ protected boolean isSupported(String descriptorKey) throws RepositoryException { return "true".equals(helper.getRepository().getDescriptor(descriptorKey)); } /** * Checks that the repository supports multiple workspace, otherwise aborts with * {@link NotExecutableException}. * @throws NotExecutableException when the repository only supports a single * workspace */ protected void ensureMultipleWorkspacesSupported() throws RepositoryException, NotExecutableException { String workspacenames[] = superuser.getWorkspace().getAccessibleWorkspaceNames(); if (workspacenames == null || workspacenames.length < 2) { throw new NotExecutableException("This repository does not seem to support multiple workspaces."); } } /** * Checks that the repository can set the property to the required type, otherwise aborts with * {@link NotExecutableException}. * @throws NotExecutableException when setting the property to the required * type is not supported */ protected void ensureCanSetProperty(Node node, String propertyName, Value value) throws NotExecutableException, RepositoryException { boolean canSetIt = node.getPrimaryNodeType().canSetProperty(propertyName, value); if (! canSetIt) { // check mixins NodeType mixins[] = node.getMixinNodeTypes(); for (int i = 0; i < mixins.length && !canSetIt; i++) { canSetIt |= mixins[i].canSetProperty(propertyName, value); } } if (! canSetIt) { throw new NotExecutableException("configured property name " + propertyName + " can not be set on node " + node.getPath()); } } /** * Checks that the repository can set the property to the required type, otherwise aborts with * {@link NotExecutableException}. * @throws NotExecutableException when setting the property to the required * type is not supported */ protected void ensureCanSetProperty(Node node, String propertyName, Value[] values) throws NotExecutableException, RepositoryException { boolean canSetIt = node.getPrimaryNodeType().canSetProperty(propertyName, values); if (! canSetIt) { // check mixins NodeType mixins[] = node.getMixinNodeTypes(); for (int i = 0; i < mixins.length && !canSetIt; i++) { canSetIt |= mixins[i].canSetProperty(propertyName, values); } } if (! canSetIt) { throw new NotExecutableException("configured property name " + propertyName + " can not be set on node " + node.getPath()); } } private boolean canSetProperty(NodeType nodeType, String propertyName, int propertyType, boolean isMultiple) { PropertyDefinition propDefs[] = nodeType.getPropertyDefinitions(); for (int i = 0; i < propDefs.length; i++) { if (propDefs[i].getName().equals(propertyName) || propDefs[i].getName().equals("*")) { if ((propDefs[i].getRequiredType() == propertyType || propDefs[i].getRequiredType() == PropertyType.UNDEFINED) && propDefs[i].isMultiple() == isMultiple) { return true; } } } return false; } private boolean canSetProperty(Node node, String propertyName, int propertyType, boolean isMultiple) throws RepositoryException { if (canSetProperty(node.getPrimaryNodeType(), propertyName, propertyType, isMultiple)) { return true; } else { NodeType mixins[] = node.getMixinNodeTypes(); boolean canSetIt = false; for (int i = 0; i < mixins.length && !canSetIt; i++) { canSetIt |= canSetProperty(mixins[i], propertyName, propertyType, isMultiple); } return canSetIt; } } /** * Checks that the repository can set the property to the required type, otherwise aborts with * {@link NotExecutableException}. * @throws NotExecutableException when setting the property to the required * type is not supported */ protected void ensureCanSetProperty(Node node, String propertyName, int propertyType, boolean isMultiple) throws NotExecutableException, RepositoryException { if (! canSetProperty(node, propertyName, propertyType, isMultiple)) { throw new NotExecutableException("configured property name " + propertyName + " can not be set on node " + node.getPath()); } } /** * Checks whether the node already has the specified mixin node type */ protected boolean needsMixin(Node node, String mixin) throws RepositoryException { return ! node.getSession().getWorkspace().getNodeTypeManager().getNodeType(node.getPrimaryNodeType().getName()).isNodeType(mixin); } /** * Reverts any pending changes made by <code>s</code> and deletes any nodes * under {@link #testRoot}. If there is no node at {@link #testRoot} then * the necessary nodes are created. * * @param s the session to clean up. * @return the {@link javax.jcr.Node} that represents the test root. * @throws RepositoryException if an error occurs. */ protected Node cleanUpTestRoot(Session s) throws RepositoryException { // do a 'rollback' s.refresh(false); Node root = s.getRootNode(); Node testRootNode; if (root.hasNode(testPath)) { // clean test root testRootNode = root.getNode(testPath); for (NodeIterator children = testRootNode.getNodes(); children.hasNext();) { Node child = children.nextNode(); NodeDefinition nodeDef = child.getDefinition(); if (!nodeDef.isMandatory() && !nodeDef.isProtected()) { // try to remove child try { child.remove(); } catch (ConstraintViolationException e) { log.println("unable to remove node: " + child.getPath()); } } } } else { // create nodes to testPath StringTokenizer names = new StringTokenizer(testPath, "/"); Node currentNode = root; while (names.hasMoreTokens()) { String name = names.nextToken(); if (currentNode.hasNode(name)) { currentNode = currentNode.getNode(name); } else { currentNode = currentNode.addNode(name, testNodeType); } } testRootNode = currentNode; } s.save(); return testRootNode; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -