コード例 #1
0
  public byte[] getBytes(RenderedImage renderedImage, String contentType) throws IOException {

    UnsyncByteArrayOutputStream baos = new UnsyncByteArrayOutputStream();

    write(renderedImage, contentType, baos);

    return baos.toByteArray();
  }
  @Test
  public void testURLTemplateResourceExternalization() throws Exception {
    String templateId = "testId";

    Class<?> clazz = getClass();

    ClassLoader classLoader = clazz.getClassLoader();

    String resourcePath = clazz.getName();

    resourcePath = resourcePath.replace('.', '/') + ".class";

    URL url = classLoader.getResource(resourcePath);

    URLTemplateResource urlTemplateResource = new URLTemplateResource(templateId, url);

    // writeExternal

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    ObjectOutput objectOutput = new MockObjectOutput(unsyncByteArrayOutputStream);

    urlTemplateResource.writeExternal(objectOutput);

    objectOutput.close();

    byte[] externalizedData = unsyncByteArrayOutputStream.toByteArray();

    DataInputStream dataInputStream =
        new DataInputStream(new UnsyncByteArrayInputStream(externalizedData));

    Assert.assertEquals(templateId, dataInputStream.readUTF());
    Assert.assertEquals(url.toExternalForm(), dataInputStream.readUTF());

    // readExternal

    URLTemplateResource newURLTemplateResource = new URLTemplateResource();

    MockObjectInput mockObjectInput =
        new MockObjectInput(new DataInputStream(new UnsyncByteArrayInputStream(externalizedData)));

    newURLTemplateResource.readExternal(mockObjectInput);

    Assert.assertEquals(templateId, newURLTemplateResource.getTemplateId());

    Field templateURLField =
        ReflectionUtil.getDeclaredField(URLTemplateResource.class, "_templateURL");

    Assert.assertEquals(url, templateURLField.get(newURLTemplateResource));
  }
  @Test
  public void testCacheTemplateResourceExternalization() throws Exception {
    StringTemplateResource stringTemplateResource =
        new StringTemplateResource("testId", "testContent");

    CacheTemplateResource cacheTemplateResource = new CacheTemplateResource(stringTemplateResource);

    // writeExternal

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    ObjectOutput objectOutput = new ObjectOutputStream(unsyncByteArrayOutputStream);

    cacheTemplateResource.writeExternal(objectOutput);

    objectOutput.close();

    byte[] externalizedData = unsyncByteArrayOutputStream.toByteArray();

    ObjectInput objectInput =
        new ObjectInputStream(new UnsyncByteArrayInputStream(externalizedData));

    Assert.assertEquals(cacheTemplateResource.getLastModified(), objectInput.readLong());
    Assert.assertEquals(stringTemplateResource, objectInput.readObject());

    // readExternal

    CacheTemplateResource newCacheTemplateResource = new CacheTemplateResource();

    objectInput = new ObjectInputStream(new UnsyncByteArrayInputStream(externalizedData));

    newCacheTemplateResource.readExternal(objectInput);

    Field templateResourceField =
        ReflectionUtil.getDeclaredField(CacheTemplateResource.class, "_templateResource");

    Assert.assertEquals(
        stringTemplateResource, templateResourceField.get(newCacheTemplateResource));

    Assert.assertEquals(
        cacheTemplateResource.getLastModified(), newCacheTemplateResource.getLastModified());
  }
  @Test
  public void testStringTemplateResourceExternalization() throws Exception {
    String templateId = "testId";
    String templateContent = "testContent";

    StringTemplateResource stringTemplateResource =
        new StringTemplateResource(templateId, templateContent);

    // writeExternal

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    ObjectOutput objectOutput = new MockObjectOutput(unsyncByteArrayOutputStream);

    stringTemplateResource.writeExternal(objectOutput);

    objectOutput.close();

    byte[] externalizedData = unsyncByteArrayOutputStream.toByteArray();

    DataInputStream dataInputStream =
        new DataInputStream(new UnsyncByteArrayInputStream(externalizedData));

    Assert.assertEquals(stringTemplateResource.getLastModified(), dataInputStream.readLong());
    Assert.assertEquals(templateContent, dataInputStream.readUTF());
    Assert.assertEquals(templateId, dataInputStream.readUTF());

    // readExternal

    StringTemplateResource newStringTemplateResource = new StringTemplateResource();

    MockObjectInput mockObjectInput =
        new MockObjectInput(new DataInputStream(new UnsyncByteArrayInputStream(externalizedData)));

    newStringTemplateResource.readExternal(mockObjectInput);

    Assert.assertEquals(
        stringTemplateResource.getLastModified(), newStringTemplateResource.getLastModified());
    Assert.assertEquals(templateContent, newStringTemplateResource.getContent());
    Assert.assertEquals(templateId, newStringTemplateResource.getTemplateId());
  }
  @Test
  public void testJournalTemplateResourceExternalization() throws Exception {
    String templateId = "testId";
    final long journalTemplateId = 100;

    JournalTemplate journalTemplate =
        (JournalTemplate)
            ProxyUtil.newProxyInstance(
                getClass().getClassLoader(),
                new Class<?>[] {JournalTemplate.class},
                new InvocationHandler() {

                  public Object invoke(Object proxy, Method method, Object[] arguments)
                      throws Throwable {

                    String methodName = method.getName();

                    if (methodName.equals("getId")) {
                      return journalTemplateId;
                    }

                    throw new UnsupportedOperationException();
                  }
                });

    JournalTemplateResource journalTemplateResource =
        new JournalTemplateResource(templateId, journalTemplate);

    // writeExternal

    UnsyncByteArrayOutputStream unsyncByteArrayOutputStream = new UnsyncByteArrayOutputStream();

    ObjectOutput objectOutput = new MockObjectOutput(unsyncByteArrayOutputStream);

    journalTemplateResource.writeExternal(objectOutput);

    objectOutput.close();

    byte[] externalizedData = unsyncByteArrayOutputStream.toByteArray();

    DataInputStream dataInputStream =
        new DataInputStream(new UnsyncByteArrayInputStream(externalizedData));

    Assert.assertEquals(journalTemplateId, dataInputStream.readLong());
    Assert.assertEquals(templateId, dataInputStream.readUTF());

    // readExternal

    JournalTemplateResource newJournalTemplateResource = new JournalTemplateResource();

    MockObjectInput mockObjectInput =
        new MockObjectInput(new DataInputStream(new UnsyncByteArrayInputStream(externalizedData)));

    UnsyncByteArrayOutputStream hijackedUnsyncByteArrayOutputStream =
        ConsoleTestUtil.hijackStdErr();

    try {
      newJournalTemplateResource.readExternal(mockObjectInput);

      Assert.fail();
    } catch (IOException ioe) {
      Assert.assertEquals(
          "Unable to retrieve journal template with ID " + journalTemplateId, ioe.getMessage());
    } finally {
      ConsoleTestUtil.restoreStdErr(hijackedUnsyncByteArrayOutputStream);
    }

    Assert.assertEquals(null, newJournalTemplateResource.getTemplateId());
  }