OakMachine { @SuppressWarnings("WeakerAccess") public <E extends Throwable> void initAndInspect(final InspectBody<E> inspectBody) throws AbortedScanException, RepositoryException, E { adminInitAndInspect(admin -> { final Session inspectSession = Util.wrapSessionReadOnly(admin); inspectBody.tryAccept(inspectSession); }); } private  OakMachine(final Packaging packagingService,
                       final List<ProgressCheck> progressChecks,
                       final ErrorListener errorListener,
                       final List<URL> preInstallUrls,
                       final List<InitStage> initStages,
                       final JcrCustomizer jcrCustomizer,
                       final InstallHookProcessorFactory installHookProcessorFactory,
                       final ClassLoader installHookClassLoader,
                       final boolean enablePreInstallHooks,
                       final InstallHookPolicy scanInstallHookPolicy,
                       final Supplier<NodeStore> nodeStoreSupplier,
                       final SubpackageSilencer subpackageSilencer,
                       final RepoInitProcessor repoInitProcessor,
                       final SlingSimulatorBackend slingSimulator,
                       final Set<String> runModes); List<ProgressCheck> getProgressChecks(); ErrorListener getErrorListener(); @Deprecated List<File> getPreInstallFiles(); List<URL> getPreInstallUrls(); @SuppressWarnings("WeakerAccess") void initAndInspect(final InspectBody<E> inspectBody); void adminInitAndInspect(final InspectBody<E> inspectBody); List<CheckReport> scanPackage(final @NotNull File... file); List<CheckReport> scanPackages(final List<File> files); static Packaging newOakpalPackagingService(); static Packaging newOakpalPackagingService(final @NotNull ClassLoader classLoader); static final String NS_URI_OAKPAL; static final String NS_PREFIX_OAKPAL; static final String LN_UNDECLARED; static final String NT_UNDECLARED; }
@Test public void testJcrSession() throws Exception { OakMachine machine = builder().build(); machine.initAndInspect(session -> { assertTrue("Root node should be same as / node", session.getRootNode().isSame(session.getNode("/"))); }); }
@Test public void testHomePaths() throws Exception { OakMachine machine = builder().build(); machine.initAndInspect(session -> { assertTrue("/home/users/system should exist", session.nodeExists("/home/users/system")); assertTrue("/home/groups should exist", session.nodeExists("/home/groups")); }); }
@Test public void testBuildWithInitStage() throws Exception { InitStage stage = new InitStage.Builder().withNs("foo", "http: builder().withInitStage(stage).withInitStage((InitStage[]) null).withInitStages(null) .build().initAndInspect(session -> { assertEquals("expect namespace uri", session.getNamespaceURI("foo"), "http: }); }
@Test public void testBuildWithErrorListener() throws Exception { final CompletableFuture<Map.Entry<String, String>> latch = new CompletableFuture<>(); final ErrorListener errorListener = mock(ErrorListener.class); doAnswer(call -> latch.complete(toEntry(call.getArgument(1), call.getArgument(2)))) .when(errorListener) .onJcrNamespaceRegistrationError(any(Throwable.class), anyString(), anyString()); builder().withErrorListener(null).withErrorListener(errorListener) .withInitStage(new InitStage.Builder().withNs("jcr", "http: .build().initAndInspect(session -> { assertEquals("jcr namespace is mapped correctly", NamespaceRegistry.NAMESPACE_JCR, session.getNamespaceURI("jcr")); }); assertEquals("latch entry equals", toEntry("jcr", "http: }
@SuppressWarnings("deprecation") @Test public void testBuildWithPreInstallPackage() throws Exception { final File testPackage = TestPackageUtil.prepareTestPackage("tmp_foo_bar.zip"); builder().withPreInstallPackage((File[]) null) .withPreInstallPackage(testPackage) .build().initAndInspect(session -> { assertTrue("path should exist", session.nodeExists("/tmp/foo/bar")); }); }
@SuppressWarnings("deprecation") @Test public void testBuildWithPreInstallPackages() throws Exception { final File testPackage = TestPackageUtil.prepareTestPackage("tmp_foo_bar.zip"); builder().withPreInstallPackages(null) .withPreInstallPackages(Collections.singletonList(testPackage)) .build().initAndInspect(session -> { assertTrue("path should exist", session.nodeExists("/tmp/foo/bar")); }); }
@Test public void testBuildWithPreInstallUrl() throws Exception { final File testPackage = TestPackageUtil.prepareTestPackage("tmp_foo_bar.zip"); builder().withPreInstallUrl(testPackage.toURI().toURL()).build().initAndInspect(session -> { assertTrue("path should exist", session.nodeExists("/tmp/foo/bar")); }); }
@Test public void testBuildWithJcrCustomizer() throws Exception { final File testPackage = TestPackageUtil.prepareTestPackage("tmp_foo_bar.zip"); final CompletableFuture<String> latch = new CompletableFuture<>(); builder().withPreInstallUrl(testPackage.toURI().toURL()).withJcrCustomizer(jcr -> { jcr.with((before, after, info) -> { if (!latch.isDone() && after.hasChildNode("tmp")) { final NodeState tmp = after.getChildNode("tmp"); if (tmp.hasChildNode("foo")) { final NodeState foo = tmp.getChildNode("foo"); if (foo.hasChildNode("bar")) { final NodeState bar = tmp.getChildNode("foo"); latch.complete("/tmp/foo/bar"); } } } return after; }); }).build().initAndInspect(session -> { assertTrue("path should exist", session.nodeExists("/tmp/foo/bar")); }); assertEquals("latch should have path", "/tmp/foo/bar", latch.getNow("")); }
@Test(expected = AbortedScanException.class) public void testBuildWithPreInstallHooks_throws() throws Exception { final File badInstallHookPackage = TestPackageUtil.prepareTestPackageFromFolder("bad-install-hook.zip", new File("src/test/resources/package_with_bad_installhook")); final File testPackage = TestPackageUtil.prepareTestPackage("tmp_foo_bar.zip"); final InstallHookProcessor processor = mock(InstallHookProcessor.class); doThrow(new PackageException("throws")).when(processor).registerHooks(any(Archive.class), any(ClassLoader.class)); builder().withEnablePreInstallHooks(true) .withPreInstallUrl(testPackage.toURI().toURL(), badInstallHookPackage.toURI().toURL()) .withInstallHookProcessorFactory(() -> processor) .build().initAndInspect(session -> { assertTrue("path should exist", session.nodeExists("/tmp/foo/bar")); }); }
@Test public void testBuildWithInstallHookProcessorFactory_nothrows() throws Exception { final File badInstallHookPackage = TestPackageUtil.prepareTestPackageFromFolder("bad-install-hook.zip", new File("src/test/resources/package_with_bad_installhook")); final File testPackage = TestPackageUtil.prepareTestPackage("tmp_foo_bar.zip"); final CompletableFuture<Boolean> latch = new CompletableFuture<>(); final InstallHookProcessor processor = mock(InstallHookProcessor.class); when(processor.execute(any(InstallContext.class))).thenReturn(true); doAnswer(call -> latch.complete(true)) .when(processor) .registerHooks(any(Archive.class), any(ClassLoader.class)); builder().withEnablePreInstallHooks(true) .withPreInstallUrl(testPackage.toURI().toURL(), badInstallHookPackage.toURI().toURL()) .withInstallHookProcessorFactory(() -> processor) .build().initAndInspect(session -> { assertTrue("path should exist", session.nodeExists("/tmp/foo/bar")); assertTrue("hook processor was called", latch.getNow(false)); }); }
@Test public void testBuildWithInstallClassLoader() throws Exception { final File testPackage = TestPackageUtil.prepareTestPackage("tmp_foo_bar.zip"); final ClassLoader hookClassLoader = new URLClassLoader(new URL[]{testPackage.toURI().toURL()}, null); final CompletableFuture<Boolean> latch = new CompletableFuture<>(); final InstallHookProcessor processor = mock(InstallHookProcessor.class); when(processor.execute(any(InstallContext.class))).thenReturn(true); doAnswer(call -> latch.complete(hookClassLoader == call.getArgument(1, ClassLoader.class))) .when(processor) .registerHooks(any(Archive.class), any(ClassLoader.class)); builder().withEnablePreInstallHooks(true) .withPreInstallUrl(testPackage.toURI().toURL()) .withInstallHookProcessorFactory(() -> processor) .withInstallHookClassLoader(hookClassLoader) .build().initAndInspect(session -> { assertTrue("path should exist", session.nodeExists("/tmp/foo/bar")); assertTrue("hook processor was called with specific class loader", latch.getNow(false)); }); }