KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > media > entity > test > EntityMediaBeanUnitTestCase


1 /*
2 * JBoss, the OpenSource J2EE webOS
3 *
4 * Distributable under LGPL license.
5 * See terms of license at gnu.org.
6 */

7 package org.jboss.test.media.entity.test;
8
9 import java.io.ByteArrayOutputStream JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.StringWriter JavaDoc;
12
13 import javax.emb.ContentAccessException;
14 import javax.naming.Context JavaDoc;
15 import javax.naming.InitialContext JavaDoc;
16 import javax.rmi.PortableRemoteObject JavaDoc;
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    // Attributes ----------------------------------------------------
32
private EntityMediaBeanTesterHome home;
33    private boolean resourcesLoaded;
34    private String JavaDoc smallString;
35    private String JavaDoc bigString;
36    private byte[] smallBlob;
37    private byte[] bigBlob;
38
39    public static Test suite() throws Exception JavaDoc
40    {
41       return JBossTestCase.getDeploySetup(
42          EntityMediaBeanUnitTestCase.class,
43          "media-entity.jar");
44    }
45
46    public EntityMediaBeanUnitTestCase(String JavaDoc name) throws Exception JavaDoc
47    {
48       super(name);
49    }
50
51    public void testCreate() throws Exception JavaDoc
52    {
53       log.debug("testCreate");
54       EntityMediaBeanTester tester = home.create();
55       String JavaDoc id = tester.createEntityMediaBean();
56       log.debug(" Created Entity Bean with id=" + id);
57       tester.removeEntityMediaBean(id);
58    }
59
60    public void testContent() throws Exception JavaDoc
61    {
62       log.debug("testEmpty");
63       EntityMediaBeanTester tester = home.create();
64       String JavaDoc 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          // expected
74
}
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    /**
84     * Lookup the LOB lobHome and cache it.
85     * Load the test data.
86     */

87    public void setUpEJB() throws Exception JavaDoc
88    {
89       log.debug("setupEJB");
90
91       if (!resourcesLoaded)
92       {
93          Context JavaDoc initialContext = new InitialContext JavaDoc();
94          Object JavaDoc ref = initialContext.lookup("ejb/EntityMediaBeanTester");
95          home =
96             (EntityMediaBeanTesterHome) PortableRemoteObject.narrow(
97                ref,
98                EntityMediaBeanTesterHome.class);
99
100          resourcesLoaded = true;
101       }
102    }
103
104    /**
105     * Remove data references so that they can be garbage collected if needed.
106     */

107    public void tearDownEJB() throws Exception JavaDoc
108    {
109       log.debug("tearDownEJB");
110    }
111
112    // Protected -------------------------------------------------------
113

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    // Private -------------------------------------------------------
122

123    /**
124     * Return the content of the input stream provided as a byte array.
125     * @param resourceName resource to read
126     * @return content as a byte array
127     */

128    private static final byte[] loadBinaryData(String JavaDoc resourceName)
129    {
130       ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
131       InputStream JavaDoc input = classLoader.getResourceAsStream(resourceName);
132       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
133       try
134       {
135          int byteRead;
136          while ((byteRead = input.read()) != -1)
137             baos.write(byteRead);
138          return baos.toByteArray();
139       }
140       catch (Exception JavaDoc e)
141       {
142          throw new IllegalStateException JavaDoc(e.getMessage());
143       }
144       finally
145       {
146          try
147          {
148             baos.close();
149          }
150          catch (Exception JavaDoc e)
151          {
152          }
153          try
154          {
155             input.close();
156          }
157          catch (Exception JavaDoc e)
158          {
159          }
160       }
161    }
162
163    /**
164     * Return the content of the input stream provided as a String.
165     * @param resourceName resource to read
166     * @return content as a string
167     */

168    private static final String JavaDoc loadTextData(String JavaDoc resourceName)
169    {
170       ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
171       InputStream JavaDoc input = classLoader.getResourceAsStream(resourceName);
172       StringWriter JavaDoc stringWriter = new StringWriter JavaDoc();
173       try
174       {
175          int byteRead;
176          while ((byteRead = input.read()) != -1)
177             stringWriter.write(byteRead);
178          return stringWriter.toString();
179       }
180       catch (Exception JavaDoc e)
181       {
182          throw new IllegalStateException JavaDoc(e.getMessage());
183       }
184       finally
185       {
186          try
187          {
188             stringWriter.close();
189          }
190          catch (Exception JavaDoc e)
191          {
192          }
193          try
194          {
195             input.close();
196          }
197          catch (Exception JavaDoc e)
198          {
199          }
200       }
201    }
202 }
Popular Tags