ContextPathArchivePreparer implements DeploymentProcessor { @Override public void process() { WARArchive warArchive = archive.as(WARArchive.class); if (warArchive.getContextRoot() == null) { warArchive.setContextRoot(contextPath.get()); } UndertowExternalMountsAsset ut = null; if (mounts != null) { for (String mountPath : mounts) { Path staticPath = Paths.get(mountPath); if (!staticPath.isAbsolute()) { staticPath = Paths.get(System.getProperty("user.dir"), staticPath.toString()).normalize(); } if (ut == null) { ut = new UndertowExternalMountsAsset(); } ut.externalMount(staticPath.toString()); } } if (ut != null) { warArchive.add(ut, WARArchive.EXTERNAL_MOUNT_PATH); } } @Inject  ContextPathArchivePreparer(Archive archive); @Override void process();  }
@Test public void testDefaultContextRoot() throws Exception { WARArchive archive = DefaultWarDeploymentFactory.archiveFromCurrentApp(); assertThat(archive.getContextRoot()).isNull(); ContextPathArchivePreparer processor = new ContextPathArchivePreparer(archive); processor.process(); assertThat(archive.getContextRoot()).isNotNull(); assertThat(archive.getContextRoot()).isEqualTo("/"); }
@Test public void testDefaultContextRootWontOverride() throws Exception { WARArchive archive = DefaultWarDeploymentFactory.archiveFromCurrentApp(); assertThat(archive.getContextRoot()).isNull(); archive.setContextRoot("myRoot"); assertThat(archive.getContextRoot()).isNotNull(); assertThat(archive.getContextRoot()).isEqualTo("myRoot"); new ContextPathArchivePreparer(archive).process(); assertThat(archive.getContextRoot()).isNotNull(); assertThat(archive.getContextRoot()).isEqualTo("myRoot"); }
@Test public void testContextPathProperty() throws Exception { WARArchive archive = DefaultWarDeploymentFactory.archiveFromCurrentApp(); assertThat(archive.getContextRoot()).isNull(); ContextPathArchivePreparer preparer = new ContextPathArchivePreparer(archive); preparer.contextPath.set("/another-root"); preparer.process(); assertThat(archive.getContextRoot()).isNotNull(); assertThat(archive.getContextRoot()).isEqualTo("/another-root"); }
@SuppressWarnings("unchecked") @Test public void testExternalMount() throws Exception { WARArchive archive = DefaultWarDeploymentFactory.archiveFromCurrentApp(); assertThat(archive.getContextRoot()).isNull(); URL url = getClass().getClassLoader().getResource("mounts.yml"); ConfigViewFactory factory = new ConfigViewFactory(new Properties()); factory.load("test", url); factory.withProfile("test"); ConfigViewImpl view = factory.get(true); List<String> mounts = view.resolve("thorntail.context.mounts").as(List.class).getValue(); ContextPathArchivePreparer preparer = new ContextPathArchivePreparer(archive); preparer.mounts = mounts; preparer.process(); Node externalMount = archive.get(WARArchive.EXTERNAL_MOUNT_PATH); assertThat(externalMount).isNotNull(); assertThat(externalMount.getAsset()).isInstanceOf(UndertowExternalMountsAsset.class); UndertowExternalMountsAsset externalMountAsset = (UndertowExternalMountsAsset) externalMount.getAsset(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(externalMountAsset.openStream()));) { assertThat(reader.readLine()).endsWith("external1"); assertThat(reader.readLine()).endsWith("external2"); } }