1 23 24 27 28 package com.sun.enterprise.admin.mbeans; 29 30 import java.io.File ; 32 33 import junit.framework.*; 35 import junit.textui.TestRunner; 36 37 import javax.management.ObjectName ; 39 40 import com.sun.enterprise.config.ConfigFactory; 42 import com.sun.enterprise.config.ConfigContext; 43 import com.sun.enterprise.config.ConfigException; 44 45 public class TargetTest extends TestCase 46 { 47 public void testTarget() throws Exception 48 { 49 testTarget(getTargetData(null, TargetType.SERVER, getConfigRef(), 50 false, "server", true)); 51 testTarget(getTargetData("server", TargetType.SERVER, getConfigRef(), 52 false, "server", true)); 53 testTarget(getTargetData("domain", TargetType.DOMAIN, null, false, 54 "domain", false)); 55 testTarget(getTargetData(getConfigRef(), TargetType.CONFIG, 56 getConfigRef(), false, getConfigRef(), true)); 57 } 58 59 public void testUnknownTarget() 60 { 61 try 62 { 63 Target target = TargetBuilder.INSTANCE.createTarget( 64 "abcd", domainContext); 65 Assert.assertTrue(false); 66 } 67 catch (Exception e) {} 68 try 69 { 70 Target target = TargetBuilder.INSTANCE.createTarget(null, null); 71 Assert.assertTrue(false); 72 } 73 catch (Exception e) {} 74 } 75 76 void testTarget(TargetData td) throws Exception 77 { 78 Target target = TargetBuilder.INSTANCE.createTarget( 79 td.name, domainContext); 80 Assert.assertTrue(target != null); 81 Assert.assertEquals(td.type, target.getType()); 82 Assert.assertEquals(getTargetObjectName(td.target, td.type), 83 target.getTargetObjectName(new String [] { getDomainName() })); 84 Assert.assertEquals(td.configRef ,target.getConfigRef()); 85 if (td.checkConfigTarget) 86 { 87 final ConfigTarget configTarget = target.getConfigTarget(); 88 Assert.assertTrue(configTarget != null); 89 Assert.assertEquals(td.is1ToN, configTarget.is1ToN(target)); 90 } 91 } 92 93 public TargetTest(String name) throws Exception 94 { 95 super(name); 96 } 97 98 protected void setUp() 99 { 100 try 101 { 102 domainContext = getConfigContext(); 103 } 104 catch (Exception e) 105 { 106 throw new RuntimeException (e.getMessage()); 107 } 108 } 109 110 protected void tearDown() 111 { 112 domainContext = null; 113 } 114 115 public static junit.framework.Test suite() 116 { 117 TestSuite suite = new TestSuite(TargetTest.class); 118 return suite; 119 } 120 121 private ConfigContext domainContext; 122 private static File domainXml; 123 124 public static void setDomainXml(File xml) 125 { 126 domainXml = xml; 127 } 128 129 public static void main(String args[]) throws Exception 130 { 131 final TestRunner runner= new TestRunner(); 132 setDomainXml(new File (args[0])); 133 final TestResult result = runner.doRun(TargetTest.suite(), false); 134 System.exit(result.errorCount() + result.failureCount()); 135 } 136 137 private ConfigContext getConfigContext() throws ConfigException 138 { 139 return ConfigFactory.createConfigContext(domainXml.getAbsolutePath()); 140 } 141 142 private String getDomainName() 143 { 144 return "testdomain"; 145 } 146 147 private String getTargetObjectName(String name, TargetType type) 148 throws Exception 149 { 150 String on = null; 151 if (type.equals(TargetType.DOMAIN)) 152 { 153 on = getDomainName() + ":type=domain,category=config"; 154 } 155 else if (type.equals(TargetType.CONFIG)) 156 { 157 on = getDomainName() + ":type=config,category=config,name=" + name; 158 } 159 else if (type.equals(TargetType.SERVER)) 160 { 161 on = getDomainName() + ":type=server,category=config,name=" + name; 162 } 163 return on; 164 } 165 166 private String getConfigRef() 167 { 168 return "server-config"; 169 } 170 171 private TargetData getTargetData(String name, 172 TargetType type, 173 String configRef, 174 boolean is1ToN, 175 String target, 176 boolean checkConfigTarget) 177 { 178 TargetData td = new TargetData(); 179 td.name = name; 180 td.type = type; 181 td.configRef = configRef; 182 td.is1ToN = is1ToN; 183 td.target = target; 184 td.checkConfigTarget = checkConfigTarget; 185 return td; 186 } 187 188 private class TargetData 189 { 190 public String name; 191 public TargetType type; 192 public String configRef; 193 public boolean is1ToN; 194 public String target; 195 public boolean checkConfigTarget; 196 } 197 } 198 | Popular Tags |