/patching/src/test/java/org/jboss/as/patching/runner/AbstractTaskTestCase.java

https://github.com/robertpanzer/jboss-as · Java · 109 lines · 71 code · 14 blank · 24 comment · 1 complexity · 6b865390c19da96f9d505c745cb07f96 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2012, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.patching.runner;
  23. import static org.jboss.as.patching.Constants.BASE;
  24. import static org.jboss.as.patching.Constants.BUNDLES;
  25. import static org.jboss.as.patching.Constants.LAYERS;
  26. import static org.jboss.as.patching.Constants.MODULES;
  27. import static org.jboss.as.patching.IoUtils.mkdir;
  28. import static org.jboss.as.patching.runner.TestUtils.randomString;
  29. import java.io.File;
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import org.jboss.as.patching.DirectoryStructure;
  34. import org.jboss.as.patching.IoUtils;
  35. import org.jboss.as.patching.PatchingException;
  36. import org.jboss.as.patching.installation.InstallationManager;
  37. import org.jboss.as.patching.installation.InstallationManagerImpl;
  38. import org.jboss.as.patching.installation.InstalledIdentity;
  39. import org.jboss.as.patching.tool.ContentVerificationPolicy;
  40. import org.jboss.as.patching.tool.PatchTool;
  41. import org.jboss.as.patching.tool.PatchingResult;
  42. import org.jboss.as.version.ProductConfig;
  43. import org.junit.After;
  44. import org.junit.Before;
  45. /**
  46. * @author <a href="http://jmesnil.net/">Jeff Mesnil</a> (c) 2012, Red Hat Inc
  47. */
  48. public abstract class AbstractTaskTestCase {
  49. protected File tempDir;
  50. protected DirectoryStructure env;
  51. protected ProductConfig productConfig;
  52. @Before
  53. public void setup() throws Exception {
  54. tempDir = mkdir(new File(System.getProperty("java.io.tmpdir")), "patching-" + randomString());
  55. File jbossHome = mkdir(tempDir, "jboss-installation");
  56. mkdir(jbossHome, MODULES, "system", LAYERS, BASE);
  57. mkdir(jbossHome, BUNDLES, "system", LAYERS, BASE);
  58. env = TestUtils.createLegacyTestStructure(jbossHome);
  59. productConfig = new ProductConfig("product", "version", "consoleSlot");
  60. }
  61. @After
  62. public void tearDown() {
  63. if (!IoUtils.recursiveDelete(tempDir)) {
  64. tempDir.deleteOnExit();
  65. }
  66. }
  67. public InstalledIdentity loadInstalledIdentity() throws IOException {
  68. List<File> moduleRoots = new ArrayList<File>();
  69. moduleRoots.add(env.getInstalledImage().getModulesDir());
  70. List<File> bundleRoots = new ArrayList<File>();
  71. bundleRoots.add(env.getInstalledImage().getBundlesDir());
  72. InstalledIdentity installedIdentity = InstalledIdentity.load(env.getInstalledImage(), productConfig, moduleRoots, bundleRoots);
  73. return installedIdentity;
  74. }
  75. protected PatchTool newPatchTool() throws IOException {
  76. final InstalledIdentity installedIdentity = loadInstalledIdentity();
  77. final InstallationManager manager = new InstallationManagerImpl(installedIdentity);
  78. return PatchTool.Factory.create(manager);
  79. }
  80. protected PatchingResult executePatch(final File file) throws IOException, PatchingException {
  81. final PatchTool tool = newPatchTool();
  82. final PatchingResult result = tool.applyPatch(file, ContentVerificationPolicy.STRICT);
  83. result.commit();
  84. return result;
  85. }
  86. protected PatchingResult rollback(String patchId) throws IOException, PatchingException {
  87. return rollback(patchId, false);
  88. }
  89. protected PatchingResult rollback(String patchId, final boolean rollbackTo) throws IOException, PatchingException {
  90. final PatchTool tool = newPatchTool();
  91. final PatchingResult result = tool.rollback(patchId, ContentVerificationPolicy.STRICT, rollbackTo, true);
  92. result.commit();
  93. return result;
  94. }
  95. }