1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.jpackage.test;
  25 
  26 import java.util.ArrayList;
  27 import java.util.Arrays;
  28 import java.util.List;
  29 import java.util.function.Predicate;
  30 import java.util.stream.Collectors;
  31 import java.util.stream.Stream;
  32 
  33 public abstract class RunnablePackageTest {
  34     public final void run(Action... actions) {
  35         final List<Action> actionList = new ArrayList<>();
  36         actionList.add(Action.INITIALIZE);
  37         if (actions.length == 0) {
  38             actionList.addAll(DEFAULT_ACTIONS);
  39         } else {
  40             actionList.addAll(Stream.of(actions)
  41                     .filter(Predicate.not(Action.INITIALIZE::equals))
  42                     .filter(Predicate.not(Action.FINALIZE::equals))
  43                     .collect(Collectors.toList()));
  44         }
  45         actionList.add(Action.FINALIZE);
  46 
  47         var actionGroups = groupActions(actionList);
  48         TKit.trace(String.format("Actions: " + Arrays.deepToString(
  49                 actionGroups.toArray(Action[][]::new))));
  50 
  51         runActions(actionGroups);
  52     }
  53 
  54     protected void runActions(List<Action[]> actions) {
  55         actions.forEach(this::runAction);
  56     }
  57 
  58     protected abstract void runAction(Action... action);
  59 
  60     /**
  61      * Test action.
  62      */
  63     static public enum Action {
  64         /**
  65          * Init test.
  66          */
  67         INITIALIZE,
  68         /**
  69          * Create bundle.
  70          */
  71         CREATE,
  72         /**
  73          * Verify unpacked/installed package.
  74          */
  75         VERIFY_INSTALL,
  76         /**
  77          * Verify uninstalled package.
  78          */
  79         VERIFY_UNINSTALL,
  80         /**
  81          * Unpack package bundle.
  82          */
  83         UNPACK,
  84         /**
  85          * Install package.
  86          */
  87         INSTALL,
  88         /**
  89          * Uninstall package.
  90          */
  91         UNINSTALL,
  92         /**
  93          * Finalize test.
  94          */
  95         FINALIZE;
  96 
  97         @Override
  98         public String toString() {
  99             return name().toLowerCase().replace('_', '-');
 100         }
 101 
 102         public final static Action[] CREATE_AND_UNPACK = new Action[] {
 103             CREATE, UNPACK, VERIFY_INSTALL
 104         };
 105     };
 106 
 107     private List<Action[]> groupActions(List<Action> actions) {
 108         List<Action[]> groups = new ArrayList<>();
 109         List<Action> group = null;
 110         for (var action: actions) {
 111             if (group == null) {
 112                 group = new ArrayList<>();
 113                 group.add(action);
 114             } else if (group.get(group.size() - 1) == Action.INSTALL
 115                     && action == Action.VERIFY_INSTALL) {
 116                 // Group `install` and `verify install` actions together
 117                 group.add(action);
 118             } else {
 119                 groups.add(group.toArray(Action[]::new));
 120                 group.clear();
 121                 group.add(action);
 122             }
 123         }
 124         if (group != null) {
 125             groups.add(group.toArray(Action[]::new));
 126         }
 127 
 128         return groups;
 129     }
 130 
 131     private final static List<Action> DEFAULT_ACTIONS;
 132 
 133     static {
 134         final String propertyName = "action";
 135         List<String> actions = TKit.tokenizeConfigPropertyAsList(propertyName);
 136         if (actions == null || actions.isEmpty()) {
 137             DEFAULT_ACTIONS = List.of(Action.CREATE_AND_UNPACK);
 138         } else {
 139             try {
 140                 DEFAULT_ACTIONS = actions.stream()
 141                         .map(String::toUpperCase)
 142                         .map(v -> v.replace('-', '_'))
 143                         .map(Action::valueOf)
 144                         .collect(Collectors.toUnmodifiableList());
 145             } catch (IllegalArgumentException ex) {
 146                 throw new IllegalArgumentException(String.format(
 147                         "Unrecognized value of %s property: [%s]",
 148                         TKit.getConfigPropertyName(propertyName),
 149                         TKit.getConfigProperty(propertyName)), ex);
 150             }
 151         }
 152     }
 153 }