protected ModelNode testWrite( final ModelNode address, final String attribute, final ModelNode value) throws IOException { final CompositeOperationBuilder builder = CompositeOperationBuilder.create(); builder.addStep(Operations.createWriteAttributeOperation(address, attribute, value)); // Create the read operation builder.addStep(Operations.createReadAttributeOperation(address, attribute)); final ModelNode result = executeOperation(builder.build()); assertEquals(value, Operations.readResult(Operations.readResult(result).get("step-2"))); return result; }
static boolean isStandaloneRunning(final ModelControllerClient client) { try { final ModelNode response = client.execute(Operations.createReadAttributeOperation(EMPTY_ADDRESS, "server-state")); if (Operations.isSuccessfulOutcome(response)) { final String state = Operations.readResult(response).asString(); return !CONTROLLER_PROCESS_STATE_STARTING.equals(state) && !CONTROLLER_PROCESS_STATE_STOPPING.equals(state); } } catch (RuntimeException | IOException e) { LOGGER.debug("Interrupted determining if standalone is running", e); } return false; }
static File getAbsoluteLogFilePath(final ManagementClient client, final String filename) throws IOException, MgmtOperationException { final ModelNode address = PathAddress.pathAddress( PathElement.pathElement(ModelDescriptionConstants.PATH, "jboss.server.log.dir")) .toModelNode(); final ModelNode op = Operations.createReadAttributeOperation(address, ModelDescriptionConstants.PATH); final ModelNode result = client.getControllerClient().execute(op); if (Operations.isSuccessfulOutcome(result)) { return new File(Operations.readResult(result).asString(), filename); } throw new MgmtOperationException("Failed to read the path resource", op, result); }
protected ModelNode testUndefine( final ModelNode address, final String attribute, final boolean expectFailure) throws IOException { final CompositeOperationBuilder builder = CompositeOperationBuilder.create(); builder.addStep(Operations.createUndefineAttributeOperation(address, attribute)); // Create the read operation final ModelNode readOp = Operations.createReadAttributeOperation(address, attribute); readOp.get("include-defaults").set(false); builder.addStep(readOp); final ModelNode result = client.getControllerClient().execute(builder.build()); if (expectFailure) { assertFalse( "Undefining attribute " + attribute + " should have failed.", Operations.isSuccessfulOutcome(result)); } else { if (!Operations.isSuccessfulOutcome(result)) { Assert.fail(Operations.getFailureDescription(result).toString()); } assertFalse( "Attribute '" + attribute + "' was not undefined.", Operations.readResult(Operations.readResult(result).get("step-2")).isDefined()); } return result; }
@Test public void testUsage(@ArquillianResource URL url) throws Exception { final ManagementClient managementClient = createManagementClient(); final ModelControllerClient client = managementClient.getControllerClient(); try { // Create the custom formatter ModelNode op = Operations.createAddOperation(CUSTOM_FORMATTER_ADDRESS); op.get("class").set("java.util.logging.XMLFormatter"); // the module doesn't really matter since it's a JDK, so we'll just use the jboss-logmanager. op.get("module").set("org.jboss.logmanager"); executeOperation(client, op); // Create the handler op = Operations.createAddOperation(HANDLER_ADDRESS); final ModelNode file = op.get("file"); file.get("relative-to").set("jboss.server.log.dir"); file.get("path").set(FILE_NAME); op.get("append").set(false); op.get("autoflush").set(true); op.get("named-formatter").set(CUSTOM_FORMATTER_NAME); executeOperation(client, op); // Add the handler to the root logger op = Operations.createOperation("add-handler", ROOT_LOGGER_ADDRESS); op.get(ModelDescriptionConstants.NAME).set(HANDLER_NAME); executeOperation(client, op); // Get the log file op = Operations.createOperation("resolve-path", HANDLER_ADDRESS); ModelNode result = executeOperation(client, op); final Path logFile = Paths.get(readResultAsString(result)); // The file should exist Assert.assertTrue("The log file was not created.", Files.exists(logFile)); // Log 5 records doLog(url, "Test message: ", 5); // Read the log file try (BufferedReader reader = Files.newBufferedReader(logFile, StandardCharsets.UTF_8)) { final Pattern pattern = Pattern.compile("^(<message>)+(Test message: \\d)+(</message>)$"); final List<String> messages = new ArrayList<>(5); String line; while ((line = reader.readLine()) != null) { final String trimmedLine = line.trim(); final Matcher m = pattern.matcher(trimmedLine); // Very simple xml parsing if (m.matches()) { messages.add(m.group(2)); } } // Should be 5 messages Assert.assertEquals(5, messages.size()); // Check each message int count = 0; for (String msg : messages) { Assert.assertEquals("Test message: " + count++, msg); } } // Remove the handler from the root-logger op = Operations.createOperation("remove-handler", ROOT_LOGGER_ADDRESS); op.get(ModelDescriptionConstants.NAME).set(HANDLER_NAME); executeOperation(client, op); // Remove the custom formatter op = Operations.createRemoveOperation(CUSTOM_FORMATTER_ADDRESS); executeOperation(client, op); // Remove the handler op = Operations.createRemoveOperation(HANDLER_ADDRESS); executeOperation(client, op); // So we don't pollute other, verify the formatter and handler have been removed op = Operations.createReadAttributeOperation(ROOT_LOGGER_ADDRESS, "handlers"); result = executeOperation(client, op); // Should be a list type final List<ModelNode> handlers = Operations.readResult(result).asList(); for (ModelNode handler : handlers) { Assert.assertNotEquals(CUSTOM_FORMATTER_NAME, handler.asString()); } verifyRemoved(client, CUSTOM_FORMATTER_ADDRESS); verifyRemoved(client, HANDLER_ADDRESS); // Delete the log file Files.delete(logFile); // Ensure it's been deleted Assert.assertFalse(Files.exists(logFile)); } finally { safeClose(managementClient); safeClose(client); } }