Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2010 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: berkley $'
8
 *     '$Date: 2010-05-03 14:26:08 -0700 (Fri, 14 Aug 2009) $'
9
 * '$Revision: 5027 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacat.restservice;
27

    
28
import java.util.*;
29
import java.io.*;
30

    
31
import edu.ucsb.nceas.MCTestCase;
32
import edu.ucsb.nceas.metacat.dataone.CrudServiceTest;
33

    
34
import junit.framework.Test;
35
import junit.framework.TestSuite;
36

    
37
import org.apache.commons.io.IOUtils;
38

    
39
/**
40
 * @author berkley
41
 * class to test ResourceHandler
42
 */
43
public class ResourceHandlerTest extends MCTestCase
44
{
45
    public ResourceHandlerTest(String name)
46
    {
47
        super(name);
48
    }
49

    
50
    /**
51
     * Create a suite of tests to be run together
52
     */
53
    public static Test suite() 
54
    {
55
        TestSuite suite = new TestSuite();
56
        
57
        //suite.addTest(new ResourceHandlerTest("testFindBoundary"));        
58
        suite.addTest(new ResourceHandlerTest("testProcessMMP"));
59

    
60
        return suite;
61
    }
62
    
63
    public void testFindBoundary()
64
    {
65
        try
66
        {
67
            File f = new File("test/testMimeInput.txt");
68
            FileInputStream fis = new FileInputStream(f);
69
            String[] s = ResourceHandler.findBoundaryString(fis);
70
            assertEquals(s[0], "--1288304583346");
71
            //System.out.println("r: " + s[1].trim());
72
            assertTrue(s[1].trim().startsWith(s[0]));
73
        }
74
        catch(Exception e)
75
        {
76
            fail("unexpected error in testFindBoundary: " + e.getMessage());
77
        }
78
    }
79
    
80
    public void testProcessMMP()
81
    {
82
        try
83
        {
84
            runTestProccessMMP("test/testMimeInput.txt", "test/testMimeInput-object.txt");
85
            runTestProccessMMP("test/testMimeInput2.txt", "test/testMimeInput2-object.txt");
86
            runTestProccessMMP("test/testMimeInput3.txt", "test/testMimeInput3-object.txt");
87
        }
88
        catch(Exception e)
89
        {
90
            e.printStackTrace();
91
            fail("Unexpected error in testProcessMMP: " + e.getMessage());
92
        }
93
        
94
    }
95
    
96
    private void runTestProccessMMP(String filename, String answerFilename)
97
        throws Exception
98
    {
99
        System.out.println("*****************************************");
100
        System.out.println("running process test on file " + filename);
101
        File f = new File(filename);
102
        File fObject = new File(answerFilename);
103
        FileInputStream fobjectis = new FileInputStream(fObject);
104
        FileInputStream fis = new FileInputStream(f);
105
        ResourceHandler rh = new ResourceHandler(null, null, null);
106
        Hashtable<String, File> h = rh.processMMP(fis);
107
        FileInputStream smFis = new FileInputStream(h.get("sysmeta"));
108
        FileInputStream objFis = new FileInputStream(h.get("object"));
109
        String sm = IOUtils.toString(smFis);
110
        String obj = IOUtils.toString(objFis);
111
        //System.out.println("sm: " + sm);
112
        //System.out.println("obj: " + obj);
113
        String fObjectStr = IOUtils.toString(fobjectis);
114
        assertTrue(fObjectStr.trim().equals(obj.trim()));
115
        assertTrue(sm.indexOf("<d1:systemMetadata") != -1);
116
        assertTrue(sm.indexOf("</d1:systemMetadata>") != -1);
117
    }
118
}
    (1-1/1)