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.fail; 21 22 import com.google.common.collect.ImmutableMap; 23 import java.io.IOException; 24 import java.nio.file.attribute.FileAttributeView; 25 import java.util.Map; 26 import java.util.Set; 27 import org.junit.Before; 28 29 /** 30 * Abstract base class for tests of individual {@link AttributeProvider} implementations. 31 * 32 * @author Colin Decker 33 */ 34 public abstract class AbstractAttributeProviderTest<P extends AttributeProvider> { 35 36 protected static final ImmutableMap<String, FileAttributeView> NO_INHERITED_VIEWS = 37 ImmutableMap.of(); 38 39 protected P provider; 40 protected File file; 41 42 /** Create the provider being tested. */ createProvider()43 protected abstract P createProvider(); 44 45 /** Creates the set of providers the provider being tested depends on. */ createInheritedProviders()46 protected abstract Set<? extends AttributeProvider> createInheritedProviders(); 47 fileLookup()48 protected FileLookup fileLookup() { 49 return new FileLookup() { 50 @Override 51 public File lookup() throws IOException { 52 return file; 53 } 54 }; 55 } 56 57 @Before 58 public void setUp() { 59 this.provider = createProvider(); 60 this.file = Directory.create(0); 61 62 Map<String, ?> defaultValues = createDefaultValues(); 63 setDefaultValues(file, provider, defaultValues); 64 65 Set<? extends AttributeProvider> inheritedProviders = createInheritedProviders(); 66 for (AttributeProvider inherited : inheritedProviders) { 67 setDefaultValues(file, inherited, defaultValues); 68 } 69 } 70 71 private static void setDefaultValues( 72 File file, AttributeProvider provider, Map<String, ?> defaultValues) { 73 Map<String, ?> defaults = provider.defaultValues(defaultValues); 74 for (Map.Entry<String, ?> entry : defaults.entrySet()) { 75 int separatorIndex = entry.getKey().indexOf(':'); 76 String view = entry.getKey().substring(0, separatorIndex); 77 String attr = entry.getKey().substring(separatorIndex + 1); 78 file.setAttribute(view, attr, entry.getValue()); 79 } 80 } 81 82 protected Map<String, ?> createDefaultValues() { 83 return ImmutableMap.of(); 84 } 85 86 // assertions 87 88 protected void assertSupportsAll(String... attributes) { 89 for (String attribute : attributes) { 90 assertThat(provider.supports(attribute)).isTrue(); 91 } 92 } 93 94 protected void assertContainsAll(File file, ImmutableMap<String, Object> expectedAttributes) { 95 for (Map.Entry<String, Object> entry : expectedAttributes.entrySet()) { 96 String attribute = entry.getKey(); 97 Object value = entry.getValue(); 98 99 assertThat(provider.get(file, attribute)).isEqualTo(value); 100 } 101 } 102 103 protected void assertSetAndGetSucceeds(String attribute, Object value) { 104 assertSetAndGetSucceeds(attribute, value, false); 105 } 106 107 protected void assertSetAndGetSucceeds(String attribute, Object value, boolean create) { 108 provider.set(file, provider.name(), attribute, value, create); 109 assertThat(provider.get(file, attribute)).isEqualTo(value); 110 } 111 112 protected void assertSetAndGetSucceedsOnCreate(String attribute, Object value) { 113 assertSetAndGetSucceeds(attribute, value, true); 114 } 115 116 @SuppressWarnings("EmptyCatchBlock") 117 protected void assertSetFails(String attribute, Object value) { 118 try { 119 provider.set(file, provider.name(), attribute, value, false); 120 fail(); 121 } catch (IllegalArgumentException expected) { 122 } 123 } 124 125 @SuppressWarnings("EmptyCatchBlock") 126 protected void assertSetFailsOnCreate(String attribute, Object value) { 127 try { 128 provider.set(file, provider.name(), attribute, value, true); 129 fail(); 130 } catch (UnsupportedOperationException expected) { 131 } 132 } 133 } 134