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
|
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.zookeeper.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Test;
/**
* Test suite for validating the Create API.
*/
public class CreateTest extends ClientBase {
private ZooKeeper zk;
@Override
public void setUp() throws Exception {
super.setUp();
zk = createClient();
}
@Override
public void tearDown() throws Exception {
super.tearDown();
zk.close();
}
@Test
public void testCreate() throws IOException, KeeperException, InterruptedException {
createNoStatVerifyResult("/foo");
createNoStatVerifyResult("/foo/child");
}
@Test
public void testCreateWithStat() throws IOException, KeeperException, InterruptedException {
String name = "/foo";
Stat stat = createWithStatVerifyResult("/foo");
Stat childStat = createWithStatVerifyResult("/foo/child");
// Don't expect to get the same stats for different creates.
assertFalse(stat.equals(childStat));
}
@Test
public void testCreateWithNullStat() throws IOException, KeeperException, InterruptedException {
String name = "/foo";
assertNull(zk.exists(name, false));
Stat stat = null;
// If a null Stat object is passed the create should still
// succeed, but no Stat info will be returned.
String path = zk.create(name, name.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat);
assertNull(stat);
assertNotNull(zk.exists(name, false));
}
private void createNoStatVerifyResult(String newName) throws KeeperException, InterruptedException {
assertNull("Node existed before created", zk.exists(newName, false));
String path = zk.create(newName, newName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
assertEquals(path, newName);
assertNotNull("Node was not created as expected", zk.exists(newName, false));
}
private Stat createWithStatVerifyResult(String newName) throws KeeperException, InterruptedException {
assertNull("Node existed before created", zk.exists(newName, false));
Stat stat = new Stat();
String path = zk.create(newName, newName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, stat);
assertEquals(path, newName);
validateCreateStat(stat, newName);
Stat referenceStat = zk.exists(newName, false);
assertNotNull("Node was not created as expected", referenceStat);
assertEquals(referenceStat, stat);
return stat;
}
private void validateCreateStat(Stat stat, String name) {
assertEquals(stat.getCzxid(), stat.getMzxid());
assertEquals(stat.getCzxid(), stat.getPzxid());
assertEquals(stat.getCtime(), stat.getMtime());
assertEquals(0, stat.getCversion());
assertEquals(0, stat.getVersion());
assertEquals(0, stat.getAversion());
assertEquals(0, stat.getEphemeralOwner());
assertEquals(name.length(), stat.getDataLength());
assertEquals(0, stat.getNumChildren());
}
}
|