1 /* 2 * Copyright (c) 2008, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * @test 28 * @bug 6622366 29 * @summary Basic Test for deleting a service tag in a product registration 30 * @author Mandy Chung 31 * 32 * @run build DeleteServiceTag Util 33 * @run main DeleteServiceTag 34 */ 35 36 import com.sun.servicetag.*; 37 import java.io.*; 38 import java.util.*; 39 40 public class DeleteServiceTag { 41 private static RegistrationData registration; 42 private static File regFile; 43 private static Map<String, ServiceTag> stMap = 44 new LinkedHashMap<String, ServiceTag>(); 45 private static String[] files = new String[] { 46 "servicetag1.properties", 47 "servicetag2.properties", 48 "servicetag3.properties" 49 }; 50 51 public static void main(String[] argv) throws Exception { 52 String registrationDir = System.getProperty("test.classes"); 53 String servicetagDir = System.getProperty("test.src"); 54 55 File original = new File(servicetagDir, "registration.xml"); 56 regFile = new File(registrationDir, "registration.xml"); 57 copyRegistrationXML(original, regFile); 58 59 // loads all the service tags 60 for (String f : files) { 61 File stfile = new File(servicetagDir, f); 62 ServiceTag svcTag = Util.newServiceTag(stfile); 63 stMap.put(svcTag.getInstanceURN(), svcTag); 64 } 65 66 // load the registration data with all service tags 67 BufferedInputStream in = new BufferedInputStream(new FileInputStream(regFile)); 68 registration = RegistrationData.loadFromXML(in); 69 70 if (stMap.size() != files.length) { 71 throw new RuntimeException("Invalid service tag count= " + 72 stMap.size() + " expected=" + files.length); 73 } 74 // check the service tags 75 Util.checkRegistrationData(regFile.getCanonicalPath(), stMap); 76 77 // delete a service tag 78 deleteServiceTag(servicetagDir, files[0]); 79 80 System.out.println("Test passed: service tags deleted."); 81 } 82 83 private static void copyRegistrationXML(File from, File to) throws IOException { 84 85 to.delete(); 86 BufferedReader reader = new BufferedReader(new FileReader(from)); 87 PrintWriter writer = new PrintWriter(to); 88 try { 89 String line = null; 90 while ((line = reader.readLine()) != null) { 91 writer.println(line); 92 } 93 writer.flush(); 94 } finally { 95 writer.close(); 96 } 97 } 98 99 private static void deleteServiceTag(String parent, String filename) throws Exception { 100 File f = new File(parent, filename); 101 ServiceTag svcTag = Util.newServiceTag(f); 102 103 ServiceTag st = registration.removeServiceTag(svcTag.getInstanceURN()); 104 if (st == null) { 105 throw new RuntimeException("RegistrationData.remove method" + 106 " returns null"); 107 } 108 if (!Util.matches(st, svcTag)) { 109 throw new RuntimeException("ServiceTag added in the registration " + 110 " doesn't match."); 111 } 112 // check the service tags before storing the updated data 113 Util.checkRegistrationData(regFile.getCanonicalPath(), stMap); 114 115 ServiceTag st1 = registration.getServiceTag(svcTag.getInstanceURN()); 116 if (st1 != null) { 117 throw new RuntimeException("RegistrationData.get method returns " + 118 "non-null."); 119 } 120 // Now remove the service tag from the map and store to the XML file 121 stMap.remove(svcTag.getInstanceURN()); 122 BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(regFile)); 123 try { 124 registration.storeToXML(out); 125 } finally { 126 out.close(); 127 } 128 } 129 }