View Javadoc

1   package org.apache.helix;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.helix.ZNRecord;
23  import org.apache.helix.controller.HierarchicalDataHolder;
24  import org.apache.helix.manager.zk.ZkClient;
25  import org.testng.annotations.Test;
26  import org.testng.AssertJUnit;
27  import java.io.FileFilter;
28  
29  import org.testng.Assert;
30  import org.testng.annotations.Test;
31  
32  
33  public class TestHierarchicalDataStore extends ZkUnitTestBase
34  {
35    protected static ZkClient _zkClientString = null;
36  
37    @Test (groups = {"unitTest"})
38    public void testHierarchicalDataStore()
39    {
40      _zkClientString = new ZkClient(ZK_ADDR, 1000, 3000);
41  
42      String path = "/tmp/testHierarchicalDataStore";
43      FileFilter filter = null;
44      // _zkClient.setZkSerializer(new ZNRecordSerializer());
45  
46      _zkClientString.deleteRecursive(path);
47      HierarchicalDataHolder<ZNRecord> dataHolder = new HierarchicalDataHolder<ZNRecord>(
48          _zkClientString, path, filter);
49      dataHolder.print();
50      AssertJUnit.assertFalse(dataHolder.refreshData());
51  
52      // write data
53      add(path, "root data");
54      AssertJUnit.assertTrue(dataHolder.refreshData());
55      dataHolder.print();
56  
57      // add some children
58      add(path + "/child1", "child 1 data");
59      add(path + "/child2", "child 2 data");
60      AssertJUnit.assertTrue(dataHolder.refreshData());
61      dataHolder.print();
62  
63      // add some grandchildren
64      add(path + "/child1" + "/grandchild1", "grand child 1 data");
65      add(path + "/child1" + "/grandchild2", "grand child 2 data");
66      AssertJUnit.assertTrue(dataHolder.refreshData());
67      dataHolder.print();
68      
69      AssertJUnit.assertFalse(dataHolder.refreshData());
70      
71      set(path + "/child1", "new child 1 data");
72      AssertJUnit.assertTrue(dataHolder.refreshData());
73      dataHolder.print();
74    }
75  
76    private void set(String path, String data)
77    {
78      _zkClientString.writeData(path, data);
79    }
80  
81    private void add(String path, String data)
82    {
83      _zkClientString.createPersistent(path, true);
84      _zkClientString.writeData(path, data);
85    }
86  
87  }