1 16 package org.apache.catalina.storeconfig; 17 18 import java.io.PrintWriter ; 19 import java.io.StringWriter ; 20 21 import junit.framework.TestCase; 22 23 import org.apache.catalina.deploy.ContextEjb; 24 import org.apache.catalina.deploy.ContextEnvironment; 25 import org.apache.catalina.deploy.ContextLocalEjb; 26 import org.apache.catalina.deploy.ContextResource; 27 import org.apache.catalina.deploy.ContextResourceEnvRef; 28 import org.apache.catalina.deploy.ContextResourceLink; 29 import org.apache.catalina.deploy.NamingResources; 30 import org.apache.catalina.storeconfig.GlobalNamingResourcesSF; 31 import org.apache.catalina.storeconfig.StoreDescription; 32 import org.apache.catalina.storeconfig.StoreRegistry; 33 34 38 public class GlobalNamingResourcesSFTest extends TestCase { 39 StoreRegistry registry; 40 41 StringWriter writer = new StringWriter (); 42 43 PrintWriter pWriter = new PrintWriter (writer); 44 45 NamingResources reource = new NamingResources(); 46 47 GlobalNamingResourcesSF factory; 48 49 StoreDescription desc; 50 51 StoreDescription nameingDesc; 52 53 58 protected void setUp() throws Exception { 59 registry = new StoreRegistry(); 60 desc = DescriptorHelper.registerDescriptor(null, registry, 61 NamingResources.class.getName() + ".[GlobalNamingResources]", 62 "GlobalNamingResources", NamingResources.class.getName(), 63 "org.apache.catalina.storeconfig.GlobalNamingResourcesSF", 64 true, false); 65 factory = (GlobalNamingResourcesSF) desc.getStoreFactory(); 66 nameingDesc = DescriptorHelper.registerNamingDescriptor(desc, registry); 67 super.setUp(); 68 } 69 70 protected void registerDescriptor(String tag, Class aClass, 71 String factoryClass, boolean fstandard, boolean fdefault) { 72 DescriptorHelper.registerDescriptor(nameingDesc, registry, aClass 73 .getName(), tag, aClass.getName(), factoryClass, fstandard, 74 fdefault); 75 } 76 77 public void testStore() throws Exception { 78 assertNotNull(registry.findDescription(NamingResources.class)); 79 assertNotNull(registry.findDescription(ContextResourceEnvRef.class 80 .getName())); 81 assertEquals("ResourceEnvRef", registry.findDescription( 82 ContextResourceEnvRef.class.getName()).getTag()); 83 84 NamingResources resources = new NamingResources(); 85 ContextResourceEnvRef ref = new ContextResourceEnvRef(); 86 ref.setName("peter"); 87 ref.setType("type"); 88 resources.addResourceEnvRef(ref); 89 String aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR 90 + " <ResourceEnvRef" + LF.LINE_SEPARATOR 91 + " name=\"peter\"" + LF.LINE_SEPARATOR 92 + " type=\"type\"/>" + LF.LINE_SEPARATOR 93 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR; 94 check(resources, aspectedResult); 95 96 } 97 98 public void testEJBStore() throws Exception { 99 100 NamingResources resources = new NamingResources(); 101 ContextEjb ejb = new ContextEjb(); 102 ejb.setName("ejb/Service"); 103 ejb.setType("org.super.Bean"); 104 ejb.setHome("org.super.BeanHome"); 105 resources.addEjb(ejb); 106 String aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR 107 + " <EJB" + LF.LINE_SEPARATOR 108 + " home=\"org.super.BeanHome\"" + LF.LINE_SEPARATOR 109 + " name=\"ejb/Service\"" + LF.LINE_SEPARATOR 110 + " type=\"org.super.Bean\"/>" + LF.LINE_SEPARATOR 111 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR; 112 check(resources, aspectedResult); 113 } 114 115 public void testLocalEjbStore() throws Exception { 116 117 NamingResources resources = new NamingResources(); 118 ContextLocalEjb ejb = new ContextLocalEjb(); 119 ejb.setName("ejb/Service"); 120 ejb.setType("org.super.Bean"); 121 ejb.setHome("org.super.BeanHome"); 122 resources.addLocalEjb(ejb); 123 String aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR 124 + " <LocalEjb" + LF.LINE_SEPARATOR 125 + " home=\"org.super.BeanHome\"" + LF.LINE_SEPARATOR 126 + " name=\"ejb/Service\"" + LF.LINE_SEPARATOR 127 + " type=\"org.super.Bean\"/>" + LF.LINE_SEPARATOR 128 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR; 129 check(resources, aspectedResult); 130 } 131 132 public void testEnvironmentStore() throws Exception { 133 134 NamingResources resources = new NamingResources(); 135 ContextEnvironment env = new ContextEnvironment(); 136 env.setName("env/SelectEmp"); 137 env.setType("java.lang.String"); 138 env.setValue("select * from emp"); 139 resources.addEnvironment(env); 140 String aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR 141 + " <Environment" + LF.LINE_SEPARATOR 142 + " name=\"env/SelectEmp\"" + LF.LINE_SEPARATOR 143 + " type=\"java.lang.String\"" + LF.LINE_SEPARATOR 144 + " value=\"select * from emp\"/>" + LF.LINE_SEPARATOR 145 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR; 146 check(resources, aspectedResult); 147 } 148 149 public void testResourceStore() throws Exception { 150 151 NamingResources resources = new NamingResources(); 152 ContextResource res = new ContextResource(); 153 res.setName("jdbc/Emp"); 154 res.setType("javax.sql.DataSource"); 155 res.setAuth("Container"); 156 resources.addResource(res); 157 String aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR 158 + " <Resource" + LF.LINE_SEPARATOR + " auth=\"Container\"" 159 + LF.LINE_SEPARATOR + " name=\"jdbc/Emp\"" 160 + LF.LINE_SEPARATOR + " type=\"javax.sql.DataSource\"/>" 161 + LF.LINE_SEPARATOR + "</GlobalNamingResources>" 162 + LF.LINE_SEPARATOR; 163 check(resources, aspectedResult); 164 } 165 166 public void testResourceStoreProperty() throws Exception { 167 168 NamingResources resources = new NamingResources(); 169 ContextResource res = new ContextResource(); 170 res.setName("mail/MailSession"); 171 res.setType("javax.mail.Session"); 172 res.setAuth("Container"); 173 res.setProperty("mail.host", "localhost"); 174 resources.addResource(res); 175 String aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR 176 + " <Resource" + LF.LINE_SEPARATOR + " auth=\"Container\"" 177 + LF.LINE_SEPARATOR + " name=\"mail/MailSession\"" 178 + LF.LINE_SEPARATOR + " type=\"javax.mail.Session\"" 179 + LF.LINE_SEPARATOR + " mail.host=\"localhost\"/>" 180 + LF.LINE_SEPARATOR + "</GlobalNamingResources>" 181 + LF.LINE_SEPARATOR; 182 check(resources, aspectedResult); 183 } 184 185 public void testResourceLinkStore() throws Exception { 187 188 NamingResources resources = new NamingResources(); 189 ContextResourceLink res = new ContextResourceLink(); 190 res.setName("jdbc/Emp1"); 191 res.setType("javax.sql.DataSource"); 192 res.setGlobal("jdbc/Emp"); 193 resources.addResourceLink(res); 194 String aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR 195 + " <ResourceLink" + LF.LINE_SEPARATOR 196 + " global=\"jdbc/Emp\"" + LF.LINE_SEPARATOR 197 + " name=\"jdbc/Emp1\"" + LF.LINE_SEPARATOR 198 + " type=\"javax.sql.DataSource\"/>" + LF.LINE_SEPARATOR 199 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR; 200 check(resources, aspectedResult); 201 } 202 203 public void testStoreEmpty() throws Exception { 204 NamingResources resources = new NamingResources(); 205 String aspectedResult = "<GlobalNamingResources>" + LF.LINE_SEPARATOR 206 + "</GlobalNamingResources>" + LF.LINE_SEPARATOR; 207 check(resources, aspectedResult); 208 } 209 210 protected void check(NamingResources resources, String aspectedResult) 211 throws Exception { 212 factory.store(pWriter, -2, resources); 213 assertEquals(aspectedResult, writer.toString()); 214 } 215 216 } | Popular Tags |