@Test public void testOperations() 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("org.jboss.logmanager.formatters.PatternFormatter"); op.get("module").set("org.jboss.logmanager"); executeOperation(client, op); // Write some properties final ModelNode properties = new ModelNode().setEmptyList(); properties.add("pattern", "%s%E%n"); testWrite(client, CUSTOM_FORMATTER_ADDRESS, "properties", properties); // Undefine the properties testUndefine(client, CUSTOM_FORMATTER_ADDRESS, "properties"); // Write a new class attribute, should leave in restart state ModelNode result = testWrite(client, CUSTOM_FORMATTER_ADDRESS, "class", "java.util.logging.XMLFormatter"); // Check the state Assert.assertTrue( result.get("response-headers").get("operation-requires-reload").asBoolean()); // Undefining the class should fail testUndefine(client, CUSTOM_FORMATTER_ADDRESS, "class", true); // Restart the server restart(managementClient); // Change the module which should require a restart result = testWrite(client, CUSTOM_FORMATTER_ADDRESS, "module", "sun.jdk"); // Check the state Assert.assertTrue( result.get("response-headers").get("operation-requires-reload").asBoolean()); // Undefining the module should fail testUndefine(client, CUSTOM_FORMATTER_ADDRESS, "module", true); // Restart the server restart(managementClient); // Remove the custom formatter op = Operations.createRemoveOperation(CUSTOM_FORMATTER_ADDRESS); executeOperation(client, op); // Verify it's been removed verifyRemoved(client, CUSTOM_FORMATTER_ADDRESS); } finally { safeClose(managementClient); safeClose(client); } }
@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); } }