1 /* 2 * Copyright (C) 2009 Marc Blank 3 * Licensed to The Android Open Source Project. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.exchange; 19 20 import android.content.Context; 21 import android.test.AndroidTestCase; 22 23 import java.io.File; 24 import java.io.IOException; 25 26 public class EasSyncServiceTests extends AndroidTestCase { 27 Context mMockContext; 28 29 @Override setUp()30 public void setUp() throws Exception { 31 super.setUp(); 32 mMockContext = getContext(); 33 } 34 35 /** 36 * Test that our unique file name algorithm works as expected. 37 * @throws IOException 38 */ testCreateUniqueFile()39 public void testCreateUniqueFile() throws IOException { 40 // Delete existing files, if they exist 41 EasSyncService svc = new EasSyncService(); 42 svc.mContext = mMockContext; 43 try { 44 String fileName = "A11achm3n1.doc"; 45 File uniqueFile = svc.createUniqueFileInternal(null, fileName); 46 assertEquals(fileName, uniqueFile.getName()); 47 if (uniqueFile.createNewFile()) { 48 uniqueFile = svc.createUniqueFileInternal(null, fileName); 49 assertEquals("A11achm3n1-2.doc", uniqueFile.getName()); 50 if (uniqueFile.createNewFile()) { 51 uniqueFile = svc.createUniqueFileInternal(null, fileName); 52 assertEquals("A11achm3n1-3.doc", uniqueFile.getName()); 53 } 54 } 55 fileName = "A11achm3n1"; 56 uniqueFile = svc.createUniqueFileInternal(null, fileName); 57 assertEquals(fileName, uniqueFile.getName()); 58 if (uniqueFile.createNewFile()) { 59 uniqueFile = svc.createUniqueFileInternal(null, fileName); 60 assertEquals("A11achm3n1-2", uniqueFile.getName()); 61 } 62 } finally { 63 // These are the files that should be created earlier in the test. Make sure 64 // they are deleted for the next go-around 65 File directory = getContext().getFilesDir(); 66 String[] fileNames = new String[] {"A11achm3n1.doc", "A11achm3n1-2.doc", "A11achm3n1"}; 67 int length = fileNames.length; 68 for (int i = 0; i < length; i++) { 69 File file = new File(directory, fileNames[i]); 70 if (file.exists()) { 71 file.delete(); 72 } 73 } 74 } 75 } 76 77 78 } 79