1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.hdfs.server.namenode;
20 
21 import static org.junit.Assert.*;
22 
23 import org.apache.hadoop.fs.permission.FsPermission;
24 import org.apache.hadoop.fs.permission.PermissionStatus;
25 
26 import org.junit.Test;
27 
28 public class TestINodeFile {
29 
30   static final short BLOCKBITS = 48;
31   static final long BLKSIZE_MAXVALUE = ~(0xffffL << BLOCKBITS);
32 
33   private String userName = "Test";
34   private short replication;
35   private long preferredBlockSize;
36 
37   /**
38    * Test for the Replication value. Sets a value and checks if it was set
39    * correct.
40    */
41   @Test
testReplication()42   public void testReplication () {
43     replication = 3;
44     preferredBlockSize = 128*1024*1024;
45     INodeFile inf = new INodeFile(new PermissionStatus(userName, null,
46                                   FsPermission.getDefault()), null, replication,
47                                   0L, 0L, preferredBlockSize);
48     assertEquals("True has to be returned in this case", replication,
49                  inf.getReplication());
50   }
51 
52   /**
53    * IllegalArgumentException is expected for setting below lower bound
54    * for Replication.
55    * @throws IllegalArgumentException as the result
56    */
57   @Test(expected=IllegalArgumentException.class)
testReplicationBelowLowerBound()58   public void testReplicationBelowLowerBound ()
59               throws IllegalArgumentException {
60     replication = -1;
61     preferredBlockSize = 128*1024*1024;
62     INodeFile inf = new INodeFile(new PermissionStatus(userName, null,
63                                   FsPermission.getDefault()), null, replication,
64                                   0L, 0L, preferredBlockSize);
65   }
66 
67   /**
68    * Test for the PreferredBlockSize value. Sets a value and checks if it was
69    * set correct.
70    */
71   @Test
testPreferredBlockSize()72   public void testPreferredBlockSize () {
73     replication = 3;
74     preferredBlockSize = 128*1024*1024;
75     INodeFile inf = new INodeFile(new PermissionStatus(userName, null,
76                                   FsPermission.getDefault()), null, replication,
77                                   0L, 0L, preferredBlockSize);
78     assertEquals("True has to be returned in this case", preferredBlockSize,
79            inf.getPreferredBlockSize());
80   }
81 
82   @Test
testPreferredBlockSizeUpperBound()83   public void testPreferredBlockSizeUpperBound () {
84     replication = 3;
85     preferredBlockSize = BLKSIZE_MAXVALUE;
86     INodeFile inf = new INodeFile(new PermissionStatus(userName, null,
87                                   FsPermission.getDefault()), null, replication,
88                                   0L, 0L, preferredBlockSize);
89     assertEquals("True has to be returned in this case", BLKSIZE_MAXVALUE,
90                  inf.getPreferredBlockSize());
91   }
92 
93   /**
94    * IllegalArgumentException is expected for setting below lower bound
95    * for PreferredBlockSize.
96    * @throws IllegalArgumentException as the result
97    */
98   @Test(expected=IllegalArgumentException.class)
testPreferredBlockSizeBelowLowerBound()99   public void testPreferredBlockSizeBelowLowerBound ()
100               throws IllegalArgumentException {
101     replication = 3;
102     preferredBlockSize = -1;
103     INodeFile inf = new INodeFile(new PermissionStatus(userName, null,
104                                   FsPermission.getDefault()), null, replication,
105                                   0L, 0L, preferredBlockSize);
106   }
107 
108   /**
109    * IllegalArgumentException is expected for setting above upper bound
110    * for PreferredBlockSize.
111    * @throws IllegalArgumentException as the result
112    */
113   @Test(expected=IllegalArgumentException.class)
testPreferredBlockSizeAboveUpperBound()114   public void testPreferredBlockSizeAboveUpperBound ()
115               throws IllegalArgumentException {
116     replication = 3;
117     preferredBlockSize = BLKSIZE_MAXVALUE+1;
118     INodeFile inf = new INodeFile(new PermissionStatus(userName, null,
119                                   FsPermission.getDefault()), null, replication,
120                                   0L, 0L, preferredBlockSize);
121   }
122 
123 }
124