1 /*
2  * Copyright (C) 2010 The Android Open Source Project
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 package com.android.tradefed.device;
17 
18 import com.android.ddmlib.FileListingService;
19 
20 import org.easymock.EasyMock;
21 
22 import java.util.ArrayList;
23 import java.util.Collection;
24 
25 /**
26  * Helper class for mocking out device file system contents
27  */
28 public class MockFileUtil {
29 
30     /**
31      * Helper method to mock out a remote filesystem contents
32      *
33      * @param mockDevice the mock {@link ITestDevice}
34      * @param rootPath the path to the root
35      * @param childNames the child file names of directory to simulate
36      * @throws DeviceNotAvailableException
37      */
setMockDirContents(ITestDevice mockDevice, String rootPath, String... childNames)38     public static void setMockDirContents(ITestDevice mockDevice, String rootPath,
39             String... childNames) throws DeviceNotAvailableException {
40         IFileEntry rootEntry = EasyMock.createMock(IFileEntry.class);
41         EasyMock.expect(mockDevice.getFileEntry(rootPath)).andStubReturn(rootEntry);
42         boolean isDir = childNames.length != 0;
43         EasyMock.expect(rootEntry.isDirectory()).andStubReturn(isDir);
44         EasyMock.expect(rootEntry.getFullEscapedPath()).andStubReturn(rootPath);
45         EasyMock.expect(rootEntry.getName()).andStubReturn(rootPath);
46         Collection<IFileEntry> mockChildren = new ArrayList<IFileEntry>(childNames.length);
47         for (String childName : childNames) {
48             IFileEntry childMockEntry = EasyMock.createMock(IFileEntry.class);
49             EasyMock.expect(childMockEntry.getName()).andStubReturn(childName);
50             String fullPath = rootPath + FileListingService.FILE_SEPARATOR + childName;
51             EasyMock.expect(childMockEntry.getFullEscapedPath()).andStubReturn(fullPath);
52             EasyMock.expect(childMockEntry.isDirectory()).andStubReturn(Boolean.FALSE);
53             mockChildren.add(childMockEntry);
54             EasyMock.replay(childMockEntry);
55         }
56         EasyMock.expect(rootEntry.getChildren(EasyMock.anyBoolean()))
57                 .andStubReturn(mockChildren);
58         EasyMock.replay(rootEntry);
59     }
60 
61     /**
62      * Helper method to mock out a remote nested filesystem contents
63      *
64      * @param mockDevice the mock {@link ITestDevice}
65      * @param rootPath the path to the root
66      * @param pathSegments the nested file path to simulate. This method will mock out IFileEntry
67      *            objects to simulate a filesystem structure of rootPath/pathSegments
68      * @throws DeviceNotAvailableException
69      */
setMockDirPath(ITestDevice mockDevice, String rootPath, String... pathSegments)70     public static void setMockDirPath(ITestDevice mockDevice, String rootPath,
71             String... pathSegments) throws DeviceNotAvailableException {
72         IFileEntry rootEntry = EasyMock.createMock(IFileEntry.class);
73         EasyMock.expect(mockDevice.getFileEntry(rootPath)).andStubReturn(rootEntry);
74         EasyMock.expect(rootEntry.getFullEscapedPath()).andStubReturn(rootPath);
75         EasyMock.expect(rootEntry.getName()).andStubReturn(rootPath);
76         for (int i=0; i < pathSegments.length; i++) {
77             IFileEntry childMockEntry = EasyMock.createMock(IFileEntry.class);
78             EasyMock.expect(childMockEntry.getName()).andStubReturn(pathSegments[i]);
79             rootPath = rootPath + FileListingService.FILE_SEPARATOR + pathSegments[i];
80             EasyMock.expect(childMockEntry.getFullEscapedPath()).andStubReturn(rootPath);
81             Collection<IFileEntry> childrenResult = new ArrayList<IFileEntry>(1);
82             childrenResult.add(childMockEntry);
83             EasyMock.expect(rootEntry.getChildren(EasyMock.anyBoolean())).andStubReturn(
84                     childrenResult);
85             EasyMock.expect(rootEntry.isDirectory()).andStubReturn(Boolean.TRUE);
86             EasyMock.replay(rootEntry);
87             rootEntry = childMockEntry;
88         }
89         // leaf node - not a directory
90         EasyMock.expect(rootEntry.isDirectory()).andStubReturn(Boolean.FALSE);
91         EasyMock.replay(rootEntry);
92     }
93 
94 
95 }
96