• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.jimfs;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static org.junit.Assert.assertNotNull;
21 import static org.junit.Assert.fail;
22 
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableSet;
25 import java.io.IOException;
26 import java.nio.ByteBuffer;
27 import java.nio.file.attribute.UserDefinedFileAttributeView;
28 import java.util.Arrays;
29 import java.util.Set;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.junit.runners.JUnit4;
33 
34 /**
35  * Tests for {@link UserDefinedAttributeProvider}.
36  *
37  * @author Colin Decker
38  */
39 @RunWith(JUnit4.class)
40 public class UserDefinedAttributeProviderTest
41     extends AbstractAttributeProviderTest<UserDefinedAttributeProvider> {
42 
43   @Override
createProvider()44   protected UserDefinedAttributeProvider createProvider() {
45     return new UserDefinedAttributeProvider();
46   }
47 
48   @Override
createInheritedProviders()49   protected Set<? extends AttributeProvider> createInheritedProviders() {
50     return ImmutableSet.of();
51   }
52 
53   @Test
testInitialAttributes()54   public void testInitialAttributes() {
55     // no initial attributes
56     assertThat(ImmutableList.copyOf(file.getAttributeKeys())).isEmpty();
57     assertThat(provider.attributes(file)).isEmpty();
58   }
59 
60   @Test
testGettingAndSetting()61   public void testGettingAndSetting() {
62     byte[] bytes = {0, 1, 2, 3};
63     provider.set(file, "user", "one", bytes, false);
64     provider.set(file, "user", "two", ByteBuffer.wrap(bytes), false);
65 
66     byte[] one = (byte[]) provider.get(file, "one");
67     byte[] two = (byte[]) provider.get(file, "two");
68     assertThat(Arrays.equals(one, bytes)).isTrue();
69     assertThat(Arrays.equals(two, bytes)).isTrue();
70 
71     assertSetFails("foo", "hello");
72 
73     assertThat(provider.attributes(file)).containsExactly("one", "two");
74   }
75 
76   @Test
testSetOnCreate()77   public void testSetOnCreate() {
78     assertSetFailsOnCreate("anything", new byte[0]);
79   }
80 
81   @Test
testView()82   public void testView() throws IOException {
83     UserDefinedFileAttributeView view = provider.view(fileLookup(), NO_INHERITED_VIEWS);
84     assertNotNull(view);
85 
86     assertThat(view.name()).isEqualTo("user");
87     assertThat(view.list()).isEmpty();
88 
89     byte[] b1 = {0, 1, 2};
90     byte[] b2 = {0, 1, 2, 3, 4};
91 
92     view.write("b1", ByteBuffer.wrap(b1));
93     view.write("b2", ByteBuffer.wrap(b2));
94 
95     assertThat(view.list()).containsAtLeast("b1", "b2");
96     assertThat(file.getAttributeKeys()).containsExactly("user:b1", "user:b2");
97 
98     assertThat(view.size("b1")).isEqualTo(3);
99     assertThat(view.size("b2")).isEqualTo(5);
100 
101     ByteBuffer buf1 = ByteBuffer.allocate(view.size("b1"));
102     ByteBuffer buf2 = ByteBuffer.allocate(view.size("b2"));
103 
104     view.read("b1", buf1);
105     view.read("b2", buf2);
106 
107     assertThat(Arrays.equals(b1, buf1.array())).isTrue();
108     assertThat(Arrays.equals(b2, buf2.array())).isTrue();
109 
110     view.delete("b2");
111 
112     assertThat(view.list()).containsExactly("b1");
113     assertThat(file.getAttributeKeys()).containsExactly("user:b1");
114 
115     try {
116       view.size("b2");
117       fail();
118     } catch (IllegalArgumentException expected) {
119       assertThat(expected.getMessage()).contains("not set");
120     }
121 
122     try {
123       view.read("b2", ByteBuffer.allocate(10));
124       fail();
125     } catch (IllegalArgumentException expected) {
126       assertThat(expected.getMessage()).contains("not set");
127     }
128 
129     view.write("b1", ByteBuffer.wrap(b2));
130     assertThat(view.size("b1")).isEqualTo(5);
131 
132     view.delete("b2"); // succeeds
133   }
134 }
135