Project

General

Profile

« Previous | Next » 

Revision 10047

Added by Matt Jones about 8 years ago

Fix issue in getDefaultReplicationPolicy.

Previously, under some circumstances, a ReplicationPolicy was created but was missing the optional replicationAllowed and numReplicas attributes. These are critical to the policy, so ensure they are set to sensible defaults even when property parsing errors and other issues might skip them. Added a test of this method, but I note that other methods in SystemMetadataFactory do not have unit tests and should be added if they are modified.

View differences:

src/edu/ucsb/nceas/metacat/dataone/SystemMetadataFactory.java
793 793
	 * than or equal to zero, no policy needs to be set, so return null.
794 794
	 * @return ReplicationPolicy, or null if no replication policy is needed
795 795
	 */
796
    private static ReplicationPolicy getDefaultReplicationPolicy() {
796
    protected static ReplicationPolicy getDefaultReplicationPolicy() {
797 797
        ReplicationPolicy rp = null;
798 798
        int numReplicas = -1;
799 799
        try {
800 800
            numReplicas = new Integer(PropertyService.getProperty("dataone.replicationpolicy.default.numreplicas"));
801 801
        } catch (NumberFormatException e) {
802
            // The property is not a valid integer, so return a null policy
803
            return null;
802
            // The property is not a valid integer, so set it to 0
803
            numReplicas = 0;
804 804
        } catch (PropertyNotFoundException e) {
805
            // The property is not found, so return a null policy
806
            return null;
805
            // The property is not found, so set it to 0
806
            numReplicas = 0;
807 807
        }
808 808
        
809
        rp = new ReplicationPolicy();
809 810
        if (numReplicas > 0) {
810
            rp = new ReplicationPolicy();
811 811
            rp.setReplicationAllowed(true);
812 812
            rp.setNumberReplicas(numReplicas);
813 813
            try {
......
832 832
            } catch (PropertyNotFoundException e) {
833 833
                // No blocked list found in properties, so just ignore it; no action needed
834 834
            }
835
        } else {
836
            rp.setReplicationAllowed(false);
837
            rp.setNumberReplicas(0);
835 838
        }
836 839
        return rp;
837 840
    }
test/edu/ucsb/nceas/metacat/dataone/SystemMetadataFactoryTest.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2016 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *
6
 *   '$Author: jones $'
7
 *     '$Date: 2014-08-07 14:28:35 -0700 (Thu, 07 Aug 2014) $'
8
 * '$Revision: 8834 $'
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 2 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program; if not, write to the Free Software
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23
 */
24

  
25
package edu.ucsb.nceas.metacat.dataone;
26

  
27
import junit.framework.Test;
28
import junit.framework.TestSuite;
29

  
30
import org.dataone.service.types.v1.ReplicationPolicy;
31

  
32
import edu.ucsb.nceas.MCTestCase;
33

  
34
/**
35
 * A class for testing the generation of SystemMetadata from defaults
36
 */
37
public class SystemMetadataFactoryTest extends MCTestCase {   
38
    
39
	/**
40
    * constructor for the test
41
    */
42
    public SystemMetadataFactoryTest(String name) {
43
        super(name);
44
    }
45
  
46
    /**
47
	 * Establish a testing framework by initializing appropriate objects
48
	 */
49
    public void setUp() throws Exception {
50
    	
51
    }
52

  
53
	/**
54
	 * Release any objects after tests are complete
55
	 */
56
	public void tearDown() {
57
		
58
	}
59
	
60
	/**
61
     * Create a suite of tests to be run together
62
     */
63
    public static Test suite() 
64
    {
65
        TestSuite suite = new TestSuite();
66
        suite.addTest(new SystemMetadataFactoryTest("initialize"));
67
        suite.addTest(new SystemMetadataFactoryTest("getDefaultReplicationPolicy"));
68
        return suite;
69
    }
70
	
71
	/**
72
	 * Run an initial test that always passes to check that the test harness is
73
	 * working.
74
	 */
75
	public void initialize() 
76
	{
77
		assertTrue(1 == 1);
78
	}
79
	
80
	public void getDefaultReplicationPolicy() throws Exception {
81
        ReplicationPolicy rp = SystemMetadataFactory.getDefaultReplicationPolicy();
82
        assertNotNull(rp);
83
        assertTrue(!rp.getReplicationAllowed());
84
        assertTrue(rp.getNumberReplicas() >= 0);
85
	}
86
}

Also available in: Unified diff