1 // © 2016 and later: Unicode, Inc. and others. 2 // License & terms of use: http://www.unicode.org/copyright.html 3 /* 4 ******************************************************************************* 5 * Copyright (C) 1996-2011, International Business Machines Corporation and * 6 * others. All Rights Reserved. * 7 ******************************************************************************* 8 * 9 */ 10 11 package com.ibm.icu.dev.test.serializable; 12 13 import java.io.File; 14 import java.io.IOException; 15 import java.net.URL; 16 import java.util.List; 17 18 import com.ibm.icu.dev.test.serializable.SerializableTestUtility.Handler; 19 import com.ibm.icu.util.TimeZone; 20 import com.ibm.icu.util.VersionInfo; 21 22 /** 23 * This class writes the test objects for each class to a file. The work is 24 * actually done by the superclass, CoverageTest. This class just constructs 25 * a CoverageTest w/ a non-null path, which tells it to write the data. 26 * 27 */ 28 public class SerializableWriter 29 { 30 String path; 31 SerializableWriter(String path)32 public SerializableWriter(String path) 33 { 34 this.path = path; 35 } 36 folderName()37 private static String folderName() 38 { 39 int major = VersionInfo.ICU_VERSION.getMajor(); 40 int minor = VersionInfo.ICU_VERSION.getMinor(); 41 int milli = VersionInfo.ICU_VERSION.getMilli(); 42 int micro = VersionInfo.ICU_VERSION.getMicro(); 43 StringBuffer result = new StringBuffer("ICU_"); 44 45 result.append(major); 46 result.append("."); 47 result.append(minor); 48 49 if (milli != 0 || micro != 0) { 50 result.append("."); 51 result.append(milli); 52 53 if (micro != 0) { 54 result.append("."); 55 result.append(micro); 56 } 57 } 58 59 return result.toString(); 60 } 61 main(String[] args)62 public static void main(String[] args) throws IOException 63 { 64 String outDir = null; 65 if (args.length == 0) { 66 URL dataURL = SerializableWriter.class.getResource("data"); 67 outDir = dataURL.getPath() + "/" + folderName(); 68 } else { 69 outDir = args[0] + "/" + folderName(); 70 } 71 72 // Override default TimeZone, so serialized data always use 73 // the consistent zone if not specified. 74 TimeZone savedZone = TimeZone.getDefault(); 75 try { 76 TimeZone.setDefault(TimeZone.getTimeZone("America/Los_Angeles")); 77 78 SerializableWriter writer = new SerializableWriter(outDir); 79 80 writer.serialize(); 81 } finally { 82 TimeZone.setDefault(savedZone); 83 } 84 } 85 serialize()86 public void serialize() throws IOException { 87 File outDir = new File(this.path); 88 if (!outDir.exists()) { 89 outDir.mkdirs(); 90 } 91 92 List<String> classList = SerializableTestUtility.getSerializationClassList(this); 93 for (String className : classList) { 94 Handler classHandler = SerializableTestUtility.getHandler(className); 95 if (classHandler == null) { 96 System.out.println("No Handler - Skipping Class: " + className); 97 continue; 98 } 99 Object[] testObjects = classHandler.getTestObjects(); 100 File oof = new File(this.path, className + ".dat"); 101 SerializableTestUtility.serializeObjects(oof, testObjects); 102 } 103 } 104 } 105