1 7 package org.jboss.test.media.entity.test; 8 9 import java.io.ByteArrayOutputStream ; 10 import java.io.InputStream ; 11 import java.io.StringWriter ; 12 13 import javax.emb.ContentAccessException; 14 import javax.naming.Context ; 15 import javax.naming.InitialContext ; 16 import javax.rmi.PortableRemoteObject ; 17 18 import junit.framework.Test; 19 import net.sourceforge.junitejb.EJBTestCase; 20 21 import org.jboss.logging.Logger; 22 import org.jboss.test.JBossTestCase; 23 import org.jboss.test.media.entity.ejb.EntityMediaBeanTester; 24 import org.jboss.test.media.entity.ejb.EntityMediaBeanTesterHome; 25 26 public class EntityMediaBeanUnitTestCase extends EJBTestCase 27 { 28 private static final Logger log = 29 Logger.getLogger(EntityMediaBeanUnitTestCase.class); 30 31 private EntityMediaBeanTesterHome home; 33 private boolean resourcesLoaded; 34 private String smallString; 35 private String bigString; 36 private byte[] smallBlob; 37 private byte[] bigBlob; 38 39 public static Test suite() throws Exception 40 { 41 return JBossTestCase.getDeploySetup( 42 EntityMediaBeanUnitTestCase.class, 43 "media-entity.jar"); 44 } 45 46 public EntityMediaBeanUnitTestCase(String name) throws Exception 47 { 48 super(name); 49 } 50 51 public void testCreate() throws Exception 52 { 53 log.debug("testCreate"); 54 EntityMediaBeanTester tester = home.create(); 55 String id = tester.createEntityMediaBean(); 56 log.debug(" Created Entity Bean with id=" + id); 57 tester.removeEntityMediaBean(id); 58 } 59 60 public void testContent() throws Exception 61 { 62 log.debug("testEmpty"); 63 EntityMediaBeanTester tester = home.create(); 64 String id = tester.createEntityMediaBean(); 65 66 try 67 { 68 byte[] nullContent = tester.getContent(id); 69 fail("Should have thrown ContentAccessException"); 70 } 71 catch (ContentAccessException e) 72 { 73 } 75 76 byte[] dummyContent = { '1', '2', '3', '4', '5' }; 77 tester.setContent(id, dummyContent); 78 byte[] content = tester.getContent(id); 79 assertEquals(dummyContent, content); 80 tester.removeEntityMediaBean(id); 81 } 82 83 87 public void setUpEJB() throws Exception 88 { 89 log.debug("setupEJB"); 90 91 if (!resourcesLoaded) 92 { 93 Context initialContext = new InitialContext (); 94 Object ref = initialContext.lookup("ejb/EntityMediaBeanTester"); 95 home = 96 (EntityMediaBeanTesterHome) PortableRemoteObject.narrow( 97 ref, 98 EntityMediaBeanTesterHome.class); 99 100 resourcesLoaded = true; 101 } 102 } 103 104 107 public void tearDownEJB() throws Exception 108 { 109 log.debug("tearDownEJB"); 110 } 111 112 114 static void assertEquals(byte[] expected, byte[] actual) 115 { 116 assertEquals(expected.length, actual.length); 117 for (int i = 0; i < expected.length; ++i) 118 assertEquals(expected[i], actual[i]); 119 } 120 121 123 128 private static final byte[] loadBinaryData(String resourceName) 129 { 130 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 131 InputStream input = classLoader.getResourceAsStream(resourceName); 132 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 133 try 134 { 135 int byteRead; 136 while ((byteRead = input.read()) != -1) 137 baos.write(byteRead); 138 return baos.toByteArray(); 139 } 140 catch (Exception e) 141 { 142 throw new IllegalStateException (e.getMessage()); 143 } 144 finally 145 { 146 try 147 { 148 baos.close(); 149 } 150 catch (Exception e) 151 { 152 } 153 try 154 { 155 input.close(); 156 } 157 catch (Exception e) 158 { 159 } 160 } 161 } 162 163 168 private static final String loadTextData(String resourceName) 169 { 170 ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 171 InputStream input = classLoader.getResourceAsStream(resourceName); 172 StringWriter stringWriter = new StringWriter (); 173 try 174 { 175 int byteRead; 176 while ((byteRead = input.read()) != -1) 177 stringWriter.write(byteRead); 178 return stringWriter.toString(); 179 } 180 catch (Exception e) 181 { 182 throw new IllegalStateException (e.getMessage()); 183 } 184 finally 185 { 186 try 187 { 188 stringWriter.close(); 189 } 190 catch (Exception e) 191 { 192 } 193 try 194 { 195 input.close(); 196 } 197 catch (Exception e) 198 { 199 } 200 } 201 } 202 } | Popular Tags |