mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
15.11.2013 9ec005a5ccb24feedff497ff97075a0303af46fe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE
 * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2010 Sun Microsystems, Inc.
 *      Portions Copyright 2013 ForgeRock AS
 */
package org.opends.server.replication.common;
 
import java.util.Set;
 
import org.opends.server.replication.ReplicationTestCase;
import org.opends.server.replication.common.CSN;
import org.opends.server.replication.common.ServerState;
import org.opends.server.util.TimeThread;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
import static org.testng.Assert.*;
 
/**
 * Test the ServerState
 */
@SuppressWarnings("javadoc")
public class ServerStateTest extends ReplicationTestCase
{
 
  /**
   * Create CSN Data
   */
  @DataProvider(name = "csnData")
  public Object[][] createCSNData()
  {
    return new Object[][] {
       {new CSN(1, 0, 1)},
       {new CSN(TimeThread.getTime(),  123,  45)}
    };
  }
 
  /**
   * Create a new ServerState object
   */
  @Test(dataProvider = "csnData")
  public void serverStateTest(CSN csn) throws Exception
  {
    // Check constructor
    ServerState serverState = new ServerState() ;
 
    // Check Load
    // serverState.loadState() ;
    // TODO Check result;
 
    // Check update
    assertFalse(serverState.update((CSN)null));
    assertTrue(serverState.update(csn));
    assertFalse(serverState.update(csn));
    CSN csn1, csn2, csn3;
    csn1 = new CSN(csn.getTime() + 1, csn.getSeqnum(), csn.getServerId());
    csn2 = new CSN(csn1.getTime(), csn1.getSeqnum() + 1, csn1.getServerId());
    csn3 = new CSN(csn2.getTime(), csn2.getSeqnum(), (csn2.getServerId() + 1));
 
    assertTrue(serverState.update(csn1));
    assertTrue(serverState.update(csn2));
    assertTrue(serverState.update(csn3));
 
    // Check toStringSet
    CSN[] csns = { csn2, csn3 };
    Set<String> stringSet = serverState.toStringSet();
    assertEquals(csns.length, stringSet.size());
    // TODO Check the value
 
    // Check getMaxCSN
    assertEquals(csn2.compareTo(serverState.getCSN(csn2.getServerId())), 0);
    assertEquals(csn3.compareTo(serverState.getCSN(csn3.getServerId())), 0);
 
    // Check the toString
    String stringRep = serverState.toString();
    assertTrue(stringRep.contains(csn2.toString()));
    assertTrue(stringRep.contains(csn3.toString()));
 
    // Check getBytes
    byte[] b = serverState.getBytes();
    ServerState generatedServerState = new ServerState(b,0,b.length -1) ;
    assertEquals(b, generatedServerState.getBytes()) ;
  }
 
  /**
   * Create a new ServerState object
   */
  @Test(dataProvider = "csnData")
  public void serverStateReloadTest(CSN csn)
  throws Exception
  {
    CSN csn1, csn3;
    csn1 = new CSN(csn.getTime() + 1, csn.getSeqnum(), csn.getServerId());
    csn3 = new CSN(csn1.getTime(), csn1.getSeqnum(), (csn1.getServerId() + 1));
 
    ServerState state1 = new ServerState();
    state1.update(csn1);
    state1.update(csn3);
 
    ServerState state2 = new ServerState();
    state2.reload(state1);
 
    assertEquals(state1.toString(), state2.toString()) ;
 
  }
 
}