提交 ec8847a5 编写于 作者: M mchung

6962815: support enable and disable of the servicetag's system registry for testing purpose

Summary: Allow the system registry to be disabled/enabled at runtime
Reviewed-by: ksrini
上级 585236c9
...@@ -67,7 +67,6 @@ public class Registry { ...@@ -67,7 +67,6 @@ public class Registry {
// The stclient output has to be an exported interface // The stclient output has to be an exported interface
private static final String INSTANCE_URN_DESC = "Product instance URN="; private static final String INSTANCE_URN_DESC = "Product instance URN=";
private static boolean initialized = false; private static boolean initialized = false;
private static boolean supportsHelperClass = true; // default
private static File stclient = null; private static File stclient = null;
private static String stclientPath = null; private static String stclientPath = null;
private static Registry registry = new Registry(); private static Registry registry = new Registry();
...@@ -81,17 +80,6 @@ public class Registry { ...@@ -81,17 +80,6 @@ public class Registry {
private synchronized static String getSTclient() { private synchronized static String getSTclient() {
if (!initialized) { if (!initialized) {
// the system property always overrides the default setting
if (System.getProperty(SVCTAG_STHELPER_SUPPORTED) != null) {
supportsHelperClass = Boolean.getBoolean(SVCTAG_STHELPER_SUPPORTED);
}
// This is only used for testing
stclientPath = System.getProperty(SVCTAG_STCLIENT_CMD);
if (stclientPath != null) {
return stclientPath;
}
// Initialization to determine the platform's stclient pathname // Initialization to determine the platform's stclient pathname
String os = System.getProperty("os.name"); String os = System.getProperty("os.name");
if (os.equals("SunOS")) { if (os.equals("SunOS")) {
...@@ -108,10 +96,26 @@ public class Registry { ...@@ -108,10 +96,26 @@ public class Registry {
initialized = true; initialized = true;
} }
boolean supportsHelperClass = true; // default
if (System.getProperty(SVCTAG_STHELPER_SUPPORTED) != null) {
// the system property always overrides the default setting
supportsHelperClass = Boolean.getBoolean(SVCTAG_STHELPER_SUPPORTED);
}
if (!supportsHelperClass) {
// disable system registry
return null;
}
// This is only used for testing
String path = System.getProperty(SVCTAG_STCLIENT_CMD);
if (path != null) {
return path;
}
// com.sun.servicetag package has to be compiled with JDK 5 as well // com.sun.servicetag package has to be compiled with JDK 5 as well
// JDK 5 doesn't support the File.canExecute() method. // JDK 5 doesn't support the File.canExecute() method.
// Risk not checking isExecute() for the stclient command is very low. // Risk not checking isExecute() for the stclient command is very low.
if (stclientPath == null && stclient != null && stclient.exists()) { if (stclientPath == null && stclient != null && stclient.exists()) {
stclientPath = stclient.getAbsolutePath(); stclientPath = stclient.getAbsolutePath();
} }
...@@ -142,8 +146,8 @@ public class Registry { ...@@ -142,8 +146,8 @@ public class Registry {
* @return {@code true} if the {@code Registry} class is supported; * @return {@code true} if the {@code Registry} class is supported;
* otherwise, return {@code false}. * otherwise, return {@code false}.
*/ */
public static boolean isSupported() { public static synchronized boolean isSupported() {
return (getSTclient() != null && supportsHelperClass); return getSTclient() != null;
} }
private static List<String> getCommandList() { private static List<String> getCommandList() {
......
...@@ -56,8 +56,17 @@ public class FindServiceTags { ...@@ -56,8 +56,17 @@ public class FindServiceTags {
private static int expectedUrnCount = 3; private static int expectedUrnCount = 3;
public static void main(String[] argv) throws Exception { public static void main(String[] argv) throws Exception {
try {
registry = Util.getSvcTagClientRegistry(); registry = Util.getSvcTagClientRegistry();
runTest();
} finally {
// restore empty registry file
Util.emptyRegistryFile();
}
System.out.println("Test passed.");
}
public static void runTest() throws Exception {
for (String filename : files) { for (String filename : files) {
File f = new File(servicetagDir, filename); File f = new File(servicetagDir, filename);
ServiceTag svcTag = Util.newServiceTag(f); ServiceTag svcTag = Util.newServiceTag(f);
...@@ -95,7 +104,6 @@ public class FindServiceTags { ...@@ -95,7 +104,6 @@ public class FindServiceTags {
tags.size()); tags.size());
} }
System.out.println("Test passed.");
} }
private static void findServiceTags(String productUrn) throws Exception { private static void findServiceTags(String productUrn) throws Exception {
......
...@@ -31,8 +31,8 @@ ...@@ -31,8 +31,8 @@
* are both created correctly. * are both created correctly.
* @author Mandy Chung * @author Mandy Chung
* *
* @run build JavaServiceTagTest1 * @run build JavaServiceTagTest1 SvcTagClient Util
* @run main/othervm JavaServiceTagTest1 * @run main JavaServiceTagTest1
*/ */
import com.sun.servicetag.*; import com.sun.servicetag.*;
...@@ -46,6 +46,16 @@ public class JavaServiceTagTest1 { ...@@ -46,6 +46,16 @@ public class JavaServiceTagTest1 {
private static File svcTagFile; private static File svcTagFile;
private static Registry registry; private static Registry registry;
public static void main(String[] argv) throws Exception { public static void main(String[] argv) throws Exception {
try {
registry = Util.getSvcTagClientRegistry();
runTest();
} finally {
// restore empty registry file
Util.emptyRegistryFile();
}
}
private static void runTest() throws Exception {
// cleanup the registration.xml and servicetag file in the test directory // cleanup the registration.xml and servicetag file in the test directory
System.setProperty("servicetag.dir.path", registrationDir); System.setProperty("servicetag.dir.path", registrationDir);
regFile = new File(registrationDir, "registration.xml"); regFile = new File(registrationDir, "registration.xml");
...@@ -54,8 +64,6 @@ public class JavaServiceTagTest1 { ...@@ -54,8 +64,6 @@ public class JavaServiceTagTest1 {
svcTagFile = new File(registrationDir, "servicetag"); svcTagFile = new File(registrationDir, "servicetag");
svcTagFile.delete(); svcTagFile.delete();
registry = Util.getSvcTagClientRegistry();
// verify that only one service tag is created // verify that only one service tag is created
ServiceTag st1 = testJavaServiceTag("Test1"); ServiceTag st1 = testJavaServiceTag("Test1");
......
...@@ -31,7 +31,7 @@ ...@@ -31,7 +31,7 @@
* @author Mandy Chung * @author Mandy Chung
* *
* @run build SvcTagClient SystemRegistryTest Util * @run build SvcTagClient SystemRegistryTest Util
* @run main/othervm SystemRegistryTest * @run main SystemRegistryTest
*/ */
import com.sun.servicetag.*; import com.sun.servicetag.*;
...@@ -50,8 +50,16 @@ public class SystemRegistryTest { ...@@ -50,8 +50,16 @@ public class SystemRegistryTest {
private static Registry registry; private static Registry registry;
public static void main(String[] argv) throws Exception { public static void main(String[] argv) throws Exception {
try {
registry = Util.getSvcTagClientRegistry(); registry = Util.getSvcTagClientRegistry();
runTest();
} finally {
// restore empty registry file
Util.emptyRegistryFile();
}
}
private static void runTest() throws Exception {
for (String filename : files) { for (String filename : files) {
File f = new File(servicetagDir, filename); File f = new File(servicetagDir, filename);
ServiceTag svcTag = Util.newServiceTag(f); ServiceTag svcTag = Util.newServiceTag(f);
......
...@@ -219,25 +219,25 @@ public class Util { ...@@ -219,25 +219,25 @@ public class Util {
} }
private static Registry registry = null; private static Registry registry = null;
private static File registryFile = null;
/** /**
* Returns the Registry processed by SvcTagClient that simulates * Returns the Registry processed by SvcTagClient that simulates
* stclient. * stclient.
*/ */
static synchronized Registry getSvcTagClientRegistry() throws IOException { static synchronized Registry getSvcTagClientRegistry() throws IOException {
String regDir = System.getProperty("test.classes");
File f = new File(regDir, "registry.xml");
if (registry != null) { if (registry != null) {
if (!f.equals(registryFile) && f.length() != 0) {
throw new AssertionError("Has to be empty registry.xml to run in samevm");
}
return registry; return registry;
} }
// System.setProperty("servicetag.verbose", "true"); // System.setProperty("servicetag.verbose", "true");
// enable the helper class // enable the helper class
System.setProperty("servicetag.sthelper.supported", "true"); System.setProperty("servicetag.sthelper.supported", "true");
registryFile = f;
// clean up registry.xml
String regDir = System.getProperty("test.classes");
File registryFile = new File(regDir, "registry.xml");
if (registryFile.exists()) {
registryFile.delete();
}
String stclientCmd = Util.getSvcClientCommand(registryFile.getCanonicalPath()); String stclientCmd = Util.getSvcClientCommand(registryFile.getCanonicalPath());
System.out.println("stclient cmd: " + stclientCmd); System.out.println("stclient cmd: " + stclientCmd);
...@@ -247,4 +247,17 @@ public class Util { ...@@ -247,4 +247,17 @@ public class Util {
registry = Registry.getSystemRegistry(); registry = Registry.getSystemRegistry();
return registry; return registry;
} }
static void emptyRegistryFile() throws IOException {
if (registryFile.exists()) {
BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(registryFile));
try {
RegistrationData data = new RegistrationData();
data.storeToXML(out);
} finally {
out.close();
}
}
}
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册