All Downloads are FREE. Search and download functionalities are using the official Maven repository.

test.junit.fedora.test.api.TestManyDisseminations Maven / Gradle / Ivy

Go to download

The Fedora Client is a Java Library that allows API access to a Fedora Repository. The client is typically one part of a full Fedora installation.

The newest version!

package fedora.test.api;

import java.io.InputStream;

import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URL;

import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;

import fedora.client.FedoraClient;

import fedora.common.Constants;

import fedora.server.management.FedoraAPIM;

import fedora.test.FedoraServerTestCase;

/**
 * Tests a series of many dissemination requests.
 *
 * NOTE: This test requies API-A to be open (non-authenticating)
 * 
 * @author Chris Wilper
 */
public class TestManyDisseminations
        extends FedoraServerTestCase {
    
    private static final String DATA_OBJECT_PID = "test:ManyDiss";
    
    private static final String BDEF_OBJECT_PID = "test:ManyDiss-BDef";
    
    private static final String BMECH_OBJECT_PID = "test:ManyDiss-BMech";
    
    private static final String X_DS = "DC";
    
    private static final String E_DS = "DC_REF_E";
    
    private static final String R_DS = "DC_REF_R";
    
    private static final FedoraClient CLIENT;
    
    private static final FedoraAPIM APIM;
    
    private static final String BASE_URL;
    
    static {
        try {
            BASE_URL = FedoraServerTestCase.getBaseURL();
            CLIENT = FedoraServerTestCase.getFedoraClient();
            APIM = CLIENT.getAPIM();
        } catch (Exception e) {
            throw new RuntimeException("Error getting Fedora Client", e);
        }
    }

    public static Test suite() throws Exception {
        TestSuite suite = new TestSuite("TestManyDisseminations TestSuite");
        suite.addTestSuite(TestManyDisseminations.class);
        return new ManyDisseminationsTestSetup(suite);
    }

    //---
    // Tests
    //---
   
    /**
     * Tests a rapid series of requests for an inline XML datastream.
     */
    public void testManyDatastreamDisseminationsX() throws Exception {
        doDissemTest(DATA_OBJECT_PID + "/" + X_DS, false);
    }
    
    /**
     * Tests a rapid series of requests for an External datastream.
     */
    public void testManyDatastreamDisseminationsE() throws Exception {
        doDissemTest(DATA_OBJECT_PID + "/" + E_DS, false);
    }
    
    /**
     * Tests a rapid series of requests for a Redirect datastream.
     */
    public void testManyDatastreamDisseminationsR() throws Exception {
        doDissemTest(DATA_OBJECT_PID + "/" + R_DS, true);
    }
    
    /**
     * Tests a rapid series of requests for a Saxon dissemination that
     * uses an inline XML datastream.
     */
    public void testManySaxonDisseminationsX() throws Exception {
        doDissemTest(DATA_OBJECT_PID + "/" 
                     + BDEF_OBJECT_PID + "/getIDFrom" + X_DS, false);
    }
    
    /**
     * Tests a rapid series of requests for a Saxon dissemination that
     * uses an External datastream.
     */
    public void testManySaxonDisseminationsE() throws Exception {
        doDissemTest(DATA_OBJECT_PID + "/" 
                     + BDEF_OBJECT_PID + "/getIDFrom" + E_DS, false);
    }
    
    /**
     * Tests a rapid series of requests for a Saxon dissemination that
     * uses a Redirect datastream.
     */
    public void testManySaxonDisseminationsR() throws Exception {
        doDissemTest(DATA_OBJECT_PID + "/" 
                     + BDEF_OBJECT_PID + "/getIDFrom" + R_DS, false);
    }
   
    /**
     * Starts getting a dissemination, then aborts the connection
     * (forcibly closes the socket on the client side) 30 times in a row.
     */
    private void doDissemTest(String what, boolean redirectOK)
            throws Exception {
        final int num = 30;
        System.out.println("Getting " + what + " " + num + " times...");
        int i = 0;
        try {
            URL url = new URL(BASE_URL + "/get/" + what);
            for (i = 0; i < num; i++) {
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                InputStream in = conn.getInputStream();
                in.read();
                in.close();
                conn.disconnect();
            }
        } catch (Exception e) {
            fail("Dissemination of " + what + " failed on iter " + i + ": "
                 + e.getMessage());
        }
    }
    
    public static void main(String[] args) {
        junit.textui.TestRunner.run(TestAuthentication.class);
    }

    public static class ManyDisseminationsTestSetup
            extends TestSetup {
        
        private static final String CR = System.getProperty("line.separator");
        
        private static final String FOXML_FORMAT = "foxml1.0";
        
        private static final String FOXML_NAMESPACE
                = "info:fedora/fedora-system:def/foxml#";
        
        public ManyDisseminationsTestSetup(Test test) throws Exception {
            super(test);
        }

        @Override
        public void setUp() throws Exception {
            APIM.ingest(getBDefObject(), FOXML_FORMAT, "");
            APIM.ingest(getBMechObject(), FOXML_FORMAT, "");
            APIM.ingest(getDataObject(), FOXML_FORMAT, "");
        }
  
        @Override
        public void tearDown() throws Exception {
            APIM.purgeObject(DATA_OBJECT_PID, "", false);
            APIM.purgeObject(BMECH_OBJECT_PID, "", false);
            APIM.purgeObject(BDEF_OBJECT_PID, "", false);
        }
        
        private static byte[] getBDefObject() throws Exception {
            StringBuilder buf = new StringBuilder();
            openFOXML(buf, BDEF_OBJECT_PID);
            appendProperties(buf, "FedoraBDefObject");
            appendInlineDatastream(buf, "METHODMAP", getBDefMethodMap());
            closeFOXML(buf);
            return buf.toString().getBytes("UTF-8");
        }
        
        private static byte[] getBMechObject() throws Exception {
            StringBuilder buf = new StringBuilder();
            openFOXML(buf, BMECH_OBJECT_PID);
            appendProperties(buf, "FedoraBMechObject");
            appendInlineDatastream(buf, "METHODMAP", getBMechMethodMap());
            appendInlineDatastream(buf, "DSINPUTSPEC", getDSInputSpec());
            appendInlineDatastream(buf, "WSDL", getWSDL());
            closeFOXML(buf);
            return buf.toString().getBytes("UTF-8");
        }
        
        private static String getWSDL() {
            return "" + CR
                 + "  " + CR
                 + "    " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "      " + CR
                 + "      " + CR
                 + "    " + CR
                 + "    " + CR
                 + "      " + CR
                 + "      " + CR
                 + "    " + CR
                 + "    " + CR
                 + "      " + CR
                 + "      " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "      " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "    " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "    " + CR
                 + "    " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "    " + CR
                 + "    " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "      " + CR
                 + "        " + CR
                 + "      " + CR
                 + "    " + CR
                 + "  " + CR
                 + "";
        }
        
        private static String getDSInputSpec() {
            return "" + CR
                 + "  " + CR
                 + "    XSLT Binding" + CR
                 + "    text/xml" + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    DC Binding" + CR
                 + "    text/xml" + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    DC_REF_E Binding" + CR
                 + "    text/xml" + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    DC_REF_R Binding" + CR
                 + "    text/xml" + CR
                 + "    " + CR
                 + "  " + CR
                 + "";
        }
        
        private static String getBMechMethodMap() {
            return "" + CR
                 + "  " + CR
                 + "    " + CR
                 + "    " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "    " + CR
                 + "    " + CR
                 + "  " + CR
                 + "  " + CR
                 + "    " + CR
                 + "    " + CR
                 + "    " + CR
                 + "  " + CR
                 + "";
        }
        
        private static String getBDefMethodMap() {
            return "" + CR
                 + "  " + CR
                 + "  " + CR
                 + "  " + CR
                 + "" + CR;
        }
        
        private static byte[] getDataObject() throws Exception {
            StringBuilder buf = new StringBuilder();
            openFOXML(buf, DATA_OBJECT_PID);
            appendProperties(buf, "FedoraObject");
            final String mimeType = "text/xml";
            InetAddress addr = InetAddress.getLocalHost();
            final String url = "http://" + addr.getHostAddress() + ":" 
                    + FedoraServerTestCase.getPort() + "/fedora/get/"
                    + DATA_OBJECT_PID + "/" + X_DS;
            appendInlineDatastream(buf, X_DS, getDC());
            appendInlineDatastream(buf, "XSLT", getXSLT());
            appendRemoteDatastream(buf, E_DS, "E", mimeType, url);
            appendRemoteDatastream(buf, R_DS, "R", mimeType, url);
            buf.append("  " + CR
                     + "    " + CR
                     + "      " + CR
                     + "        " + CR
                     + "        " + CR
                     + "        " + CR
                     + "        " + CR
                     + "      " + CR
                     + "    " + CR
                     + "  " + CR);
            closeFOXML(buf);
            return buf.toString().getBytes("UTF-8");
        }
        
        private static String getDC() {
            return "" + CR
                 + "  test:ManyDiss" + CR
                 + "";
        }
        
        private static String getXSLT() {
            StringBuilder buf = new StringBuilder();
            buf.append("" + CR
                 + "  " + CR
                 + "  " + CR);
            for (int i = 0; i < 5000; i++) {
                buf.append("    " + CR);
                buf.append("      " + CR);
                buf.append("    " + CR);
            }
            buf.append("  " + CR
                 + "");
            return buf.toString();
        }
        
        private static void openFOXML(StringBuilder buf, String pid) {
            buf.append("" + CR);
        }
        
        private static void appendProperties(StringBuilder buf,
                                             String fType) {
            buf.append("  " + CR);
            appendProperty(buf, Constants.RDF.uri + "type", fType);
            buf.append("  " + CR);
        }
        
        private static void appendProperty(StringBuilder buf,
                                           String name,
                                           String value) {
            buf.append("    " + CR);
        }
        
        private static void appendInlineDatastream(StringBuilder buf,
                                                   String dsID,
                                                   String xml) {
            openDatastream(buf, dsID, "X");
            openDatastreamVersion(buf, dsID + ".0", "text/xml");
            buf.append("      " + CR);
            buf.append(xml);
            buf.append(CR + "      " + CR);
            closeDatastreamVersion(buf);
            closeDatastream(buf);
        }
        
        private static void appendRemoteDatastream(StringBuilder buf,
                                                   String dsID,
                                                   String controlGroup,
                                                   String mimeType,
                                                   String location) {
            openDatastream(buf, dsID, controlGroup);
            openDatastreamVersion(buf, dsID + ".0", mimeType);
            buf.append("      " + CR);
            closeDatastreamVersion(buf);
            closeDatastream(buf);
        }
        
        private static void openDatastream(StringBuilder buf,
                                           String dsID,
                                           String controlGroup) {
            buf.append("  " + CR);
        }
        
        private static void openDatastreamVersion(StringBuilder buf,
                                                  String versionID,
                                                  String mimeType) {
            buf.append("    " + CR);
        }
        
        private static void closeDatastreamVersion(StringBuilder buf) {
            buf.append("    " + CR);
        }
        
        private static void closeDatastream(StringBuilder buf) {
            buf.append("  " + CR);
        }
        
        private static void closeFOXML(StringBuilder buf) {
            buf.append("");
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy