/* * Copyright (C) 2013 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.tools.idea.gradle.util; import com.android.SdkConstants; import com.google.common.base.Charsets; import com.google.common.io.Files; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.util.io.FileUtil; import com.intellij.testFramework.IdeaTestCase; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Properties; import static org.easymock.EasyMock.*; /** * Tests for {@link LocalProperties}. */ public class LocalPropertiesTest extends IdeaTestCase { private LocalProperties myLocalProperties; @Override protected void setUp() throws Exception { super.setUp(); myLocalProperties = new LocalProperties(myProject); } public void testCreateFileOnSave() throws Exception { myLocalProperties.save(); File localPropertiesFile = new File(myProject.getBasePath(), SdkConstants.FN_LOCAL_PROPERTIES); assertTrue(localPropertiesFile.isFile()); } public void testSetAndroidSdkPathWithString() throws Exception { String androidSdkPath = FileUtil.toSystemDependentName("/home/sdk2"); myLocalProperties.setAndroidSdkPath(androidSdkPath); myLocalProperties.save(); File actual = myLocalProperties.getAndroidSdkPath(); assertNotNull(actual); assertEquals(FileUtil.toCanonicalPath(androidSdkPath), FileUtil.toCanonicalPath(actual.getPath())); } public void testSetAndroidSdkPathWithSdk() throws Exception { String androidSdkPath = FileUtil.toSystemDependentName("/home/sdk2"); Sdk sdk = createMock(Sdk.class); expect(sdk.getHomePath()).andReturn(androidSdkPath); replay(sdk); myLocalProperties.setAndroidSdkPath(sdk); verify(sdk); myLocalProperties.save(); File actual = myLocalProperties.getAndroidSdkPath(); assertNotNull(actual); assertEquals(FileUtil.toCanonicalPath(androidSdkPath), FileUtil.toCanonicalPath(actual.getPath())); } @SuppressWarnings("ResultOfMethodCallIgnored") public void testUnicodeLoad() throws Exception { File localPropertiesFile = new File(myProject.getBasePath(), SdkConstants.FN_LOCAL_PROPERTIES); File tempDir = Files.createTempDir(); File sdk = new File(tempDir, "\u00C6\u0424"); sdk.mkdirs(); Properties outProperties = new Properties(); outProperties.setProperty(SdkConstants.SDK_DIR_PROPERTY, sdk.getPath()); // First write properties using the default encoding (which will \\u escape all non-iso-8859 chars) PropertiesUtil.savePropertiesToFile(outProperties, localPropertiesFile, null); // Read back platform default version of string; confirm that it gets converted properly LocalProperties properties1 = new LocalProperties(myProject); File sdkPath1 = properties1.getAndroidSdkPath(); assertNotNull(sdkPath1); assertTrue(sdkPath1.exists()); assertTrue(FileUtil.filesEqual(sdk, sdkPath1)); // Next write properties using the UTF-8 encoding. Chars will no longer be escaped. // Confirm that we read these in properly too. Writer writer = new OutputStreamWriter(new FileOutputStream(localPropertiesFile), Charsets.UTF_8); outProperties.store(writer, null); // Read back platform default version of string; confirm that it gets converted properly LocalProperties properties2 = new LocalProperties(myProject); File sdkPath2 = properties2.getAndroidSdkPath(); assertNotNull(sdkPath2); assertTrue(sdkPath2.exists()); assertTrue(FileUtil.filesEqual(sdk, sdkPath2)); sdk.delete(); tempDir.delete(); } }