    1   /*
    2    * Copyright (c) 2003
    3    * Information Desire GmbH
    4    * All rights reserved.
    5    */
    6   package com.infodesire.infobit;
    7
    8   import junit.framework.TestCase;
    9   import junit.framework.TestSuite;
    10
    11  import java.io.ByteArrayInputStream;
    12  import java.io.ByteArrayOutputStream;
    13  import java.io.InputStream;
    14
    15  import java.util.ArrayList;
    16  import java.util.Arrays;
    17  import java.util.Collection;
    18  import java.util.HashSet;
    19  import java.util.List;
    20  import java.util.Collections;
    21
    22  import com.infodesire.infobit.data.BinaryContent;
    23  import com.infodesire.infobit.data.Infobit;
    24  import com.infodesire.infobit.data.Acl;
    25  import com.infodesire.infobit.data.Version;
    26  import com.infodesire.infobit.data.Content;
    27  import com.infodesire.infobit.data.TextContent;
    28  /***
    29   * test case for infobit capabilities
    30   *
    31   * @author    konstantin
    32   * @created   July 29, 2003
    33   * @version   $Revision: 1.18 $
    34   */
    35  public class InfobitTest extends InfobitTestBase {
    36
    37      Acl             _defaultAcl;
    38
    39
    40      /***
    41       * Constructor for the InfobitTest object
    42       *
    43       * @param name  Description of Parameter
    44       */
    45      public InfobitTest(String name) {
    46          super(name);
    47      }
    48
    49
    50      /***
    51       * The JUnit setup method
    52       *
    53       * @exception Exception  Description of Exception
    54       */
    55      public void setUp() throws Exception {
    56          super.setUp();
    57          InfobitPool pool = _configuration.getPool("superuser_pool");
    58          _defaultAcl = pool.getAclManager().createAcl("default");
    59
    60      }
    61
    62
    63      /***
    64       * test creation and removal of infobits
    65       *
    66       * @exception Exception  Description of Exception
    67       */
    68      public void xxtestInfobitCreation() throws Exception {
    69
    70          InfobitPool pool = _configuration.getPool("pool_one");
    71          InfobitManager manager = pool.getInfobitManager();
    72          String iid = "DE1213434545677";
    73          Infobit ibit = manager.createInfobit(iid, _defaultAcl);
    74          // need to flush infobit pool before artifically raising exception
    75          pool.flush();
    76          //  assertTrue(pool.hasInfobit(iid));
    77          boolean bombed = false;
    78          try {
    79              manager.createInfobit(iid, _defaultAcl);
    80          } catch (InfobitException ex) {
    81              bombed = true;
    82          }
    83          assertTrue("allowed creation of infobit with the same infobit ID", bombed);
    84
    85          Infobit ibit2 = manager.getInfobit(iid);
    86          assertEquals(ibit, ibit2);
    87          manager.removeInfobit(iid);
    88          assertFalse(manager.hasInfobit(iid));
    89          assertNull(manager.getInfobit(iid));
    90
    91      }
    92
    93
    94      /***
    95       * test version creation and manipulation
    96       *
    97       * @exception Exception  Description of Exception
    98       */
    99      public void xxtestVersonCreationAndManipulation() throws Exception {
    100         InfobitPool pool = _configuration.getPool("pool_one");
    101         InfobitManager manager = pool.getInfobitManager();
    102         String iid = "DE1213434545677";
    103         Infobit ibit = manager.createInfobit(iid, _defaultAcl);
    104
    105         assertTrue(ibit.getVersions().isEmpty());
    106         assertNull(ibit.getActualVersion());
    107
    108         manager.createText(ibit, "blurge", "", new HashSet());
    109         assertEquals(ibit.getVersions().size(), 1);
    110         Version ver = (Version) ibit.getVersions().iterator().next();
    111         assertEquals(ver.getName(), "blurge");
    112         assertEquals(ver.getAuthor(), "foo");
    113         assertNull(ibit.getActualVersion());
    114
    115         manager.setActualVersion(ibit, ver);
    116
    117         assertEquals(ibit.getActualVersion(), ver);
    118     }
    119
    120
    121     /***
    122      * Tests binary content creation.
    123      *
    124      * @exception Exception  Description of Exception
    125      */
    126     public void testBinaryContentCreation() throws Exception {
    127         InfobitPool pool = _configuration.getPool("pool_one");
    128         InfobitManager manager = pool.getInfobitManager();
    129         String iid = "DE1213434545677";
    130         Infobit ibit = manager.createInfobit(iid, _defaultAcl);
    131
    132         assertTrue(ibit.getVersions().isEmpty());
    133
    134         InputStream is = getClass().getClassLoader().getResource("/maven-propaganda.png").openStream();
    135         ByteArrayOutputStream bos = new ByteArrayOutputStream();
    136         byte[] buf = new byte[512];
    137         int n;
    138         while ((n = is.read(buf)) > -1) {
    139             bos.write(buf, 0, n);
    140         }
    141
    142         byte[] data = bos.toByteArray();
    143         //   assertEquals(data[0], 0x50);
    144         //   assertEquals(data[1], 0x89);
    145         //   assertEquals(data[2], 0x47);
    146         //   assertEquals(data[3], 0x4e);
    147         //assertEquals(data[4], 0x0a);
    148         //assertEquals(data[5], 0x0d);
    149         final String mimeType = "application/octet";
    150         InputStream stream = new ByteArrayInputStream(data);
    151         manager.createBinary(ibit, "image_01", stream, mimeType,
    152             Collections.EMPTY_SET);
    153
    154         pool.flush();
    155
    156         ibit = manager.getInfobit(iid);
    157
    158         List vers = (List) ibit.getVersions();
    159         assertTrue(vers.size() == 1);
    160         assertNull(ibit.getActualVersion());
    161
    162         // assert single version has data as content
    163         Version version = (Version) vers.get(0);
    164         BinaryContent binary = (BinaryContent) version.getContent();
    165         InputStream content = manager.getContent(binary);
    166         byte[] read = new byte[data.length];
    167         int next = 0;
    168         while (next < data.length) {
    169             n = content.read(read, next, data.length - next);
    170             assertTrue(n > 0);
    171             next += n;
    172         }
    173         assertTrue(content.read() == -1);
    174         assertTrue(Arrays.equals(data, read));
    175         content.close();
    176     }
    177
    178
    179     /***
    180      * A unit test for JUnit
    181      *
    182      * @exception Exception  Description of Exception
    183      */
    184     public void testAttributeSetting() throws Exception {
    185         InfobitPool pool = _configuration.getPool("pool_one");
    186         InfobitManager manager = pool.getInfobitManager();
    187         String iid = "DE1213434545677";
    188         Infobit ibit = manager.createInfobit(iid, _defaultAcl);
    189
    190         manager.setAttribute(ibit, "blurge", "bang");
    191
    192         pool.flush();
    193
    194         ibit = manager.getInfobit(iid);
    195         assertEquals(ibit.getAttributeMap().get("blurge"), "bang");
    196         boolean bombed = false;
    197         try {
    198             ibit.getAttributeMap().put("bla", "bla");
    199         } catch (UnsupportedOperationException ex) {
    200             bombed = true;
    201         }
    202
    203         assertTrue("allowed direct modification of infobit attributes", bombed);
    204         Collection cc = manager.queryInfobits("infobit.attributes['blurge'] = 'bang'");
    205
    206         assertEquals(cc.size(), 1);
    207         assertEquals(cc.iterator().next(), ibit);
    208     }
    209
    210
    211     /***
    212      * A unit test for JUnit - disabled for a while
    213      *
    214      * @exception Exception  Description of Exception
    215      */
    216     public void xxtestBinaryStream() throws Exception {
    217         InfobitPool pool = _configuration.getPool("news_pool");
    218         InfobitManager manager = pool.getInfobitManager();
    219         Acl acl = pool.getAclManager().getAcl("news_acl");
    220
    221         Infobit ib = manager.createInfobit("maven_image", acl);
    222         InputStream is = getClass().getResourceAsStream("/maven-propaganda.png");
    223
    224         manager.setActualVersion(ib, manager.createBinary(ib, "foo-bar", is, "image/png", Collections.EMPTY_SET));
    225         pool.flush();
    226     }
    227 }
    228

This page was automatically generated by Maven