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 package jdk.jpackage.test;
  24 
  25 import java.nio.file.Path;
  26 import java.util.HashMap;
  27 import java.util.Map;
  28 
  29 
  30 final public class FileAssociations {
  31     public FileAssociations(String faSuffixName) {
  32         suffixName = faSuffixName;
  33         setFilename("fa");
  34         setDescription("jpackage test extention");
  35     }
  36 
  37     private void createFile() {
  38         Map<String, String> entries = new HashMap<>(Map.of(
  39             "extension", suffixName,
  40             "mime-type", getMime(),
  41             "description", description
  42         ));
  43         if (icon != null) {
  44             if (TKit.isWindows()) {
  45                 entries.put("icon", icon.toString().replace("\\", "/"));
  46             } else {
  47                 entries.put("icon", icon.toString());
  48             }
  49         }
  50         TKit.createPropertiesFile(file, entries);
  51     }
  52 
  53     public FileAssociations setFilename(String v) {
  54         file = TKit.workDir().resolve(v + ".properties");
  55         return this;
  56     }
  57 
  58     public FileAssociations setDescription(String v) {
  59         description = v;
  60         return this;
  61     }
  62 
  63     public FileAssociations setIcon(Path v) {
  64         icon = v;
  65         return this;
  66     }
  67 
  68     Path getPropertiesFile() {
  69         return file;
  70     }
  71 
  72     String getSuffix() {
  73         return "." + suffixName;
  74     }
  75 
  76     String getMime() {
  77         return "application/x-jpackage-" + suffixName;
  78     }
  79 
  80     public void applyTo(PackageTest test) {
  81         test.notForTypes(PackageType.MAC_DMG, () -> {
  82             test.addInitializer(cmd -> {
  83                 createFile();
  84                 cmd.addArguments("--file-associations", getPropertiesFile());
  85             });
  86             test.addHelloAppFileAssociationsVerifier(this);
  87         });
  88     }
  89 
  90     private Path file;
  91     final private String suffixName;
  92     private String description;
  93     private Path icon;
  94 }