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 java.io.IOException; 23 import java.nio.file.attribute.GroupPrincipal; 24 import java.nio.file.attribute.UserPrincipal; 25 import java.nio.file.attribute.UserPrincipalLookupService; 26 import java.nio.file.attribute.UserPrincipalNotFoundException; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 /** 32 * Tests for {@link UserLookupService}. 33 * 34 * @author Colin Decker 35 */ 36 @RunWith(JUnit4.class) 37 public class UserLookupServiceTest { 38 39 @Test testUserLookupService()40 public void testUserLookupService() throws IOException { 41 UserPrincipalLookupService service = new UserLookupService(true); 42 UserPrincipal bob1 = service.lookupPrincipalByName("bob"); 43 UserPrincipal bob2 = service.lookupPrincipalByName("bob"); 44 UserPrincipal alice = service.lookupPrincipalByName("alice"); 45 46 assertThat(bob1).isEqualTo(bob2); 47 assertThat(bob1).isNotEqualTo(alice); 48 49 GroupPrincipal group1 = service.lookupPrincipalByGroupName("group"); 50 GroupPrincipal group2 = service.lookupPrincipalByGroupName("group"); 51 GroupPrincipal foo = service.lookupPrincipalByGroupName("foo"); 52 53 assertThat(group1).isEqualTo(group2); 54 assertThat(group1).isNotEqualTo(foo); 55 } 56 57 @Test testServiceNotSupportingGroups()58 public void testServiceNotSupportingGroups() throws IOException { 59 UserPrincipalLookupService service = new UserLookupService(false); 60 61 try { 62 service.lookupPrincipalByGroupName("group"); 63 fail(); 64 } catch (UserPrincipalNotFoundException expected) { 65 assertThat(expected.getName()).isEqualTo("group"); 66 } 67 } 68 } 69