Add kernel BuildInfo classes

Change-Id: I6d37abc67dfea4e716247fe2f94ce25d50088b15
diff --git a/src/com/android/tradefed/build/IKernelBuildInfo.java b/src/com/android/tradefed/build/IKernelBuildInfo.java
new file mode 100644
index 0000000..eb1ca4b
--- /dev/null
+++ b/src/com/android/tradefed/build/IKernelBuildInfo.java
@@ -0,0 +1,87 @@
+/*
+ * Copyright (C) 2012 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.tradefed.build;
+
+import java.io.File;
+
+/**
+ * A {@link IBuildInfo} that represents a kernel build.
+ */
+public interface IKernelBuildInfo extends IBuildInfo {
+
+    /**
+     * Get the kernel file.
+     *
+     * @return the kernel file
+     */
+    public File getKernelFile();
+
+    /**
+     * Get the kernel file version.
+     *
+     * @return the kernel file version
+     */
+    public String getKernelVersion();
+
+    /**
+     * Set the kernel file
+     *
+     * @param kernelFile
+     */
+    public void setKernelFile(File kernelFile, String version);
+
+    /**
+     * Get the git commit time.
+     *
+     * @return the git commit time in seconds since the Unix epoch.
+     */
+    public long getCommitTime();
+
+    /**
+     * Sets the git commit time for the change.
+     *
+     * @param time the time in seconds since the Unix epoch.
+     */
+    public void setCommitTime(long time);
+
+    /**
+     * Gets the git sha1.
+     *
+     * @return the git sha1
+     */
+    public String getSha1();
+
+    /**
+     * Sets the git sha1.
+     *
+     * @param sha1 the git sha1
+     */
+    public void setSha1(String sha1);
+
+    /**
+     * Gets the git short sha1.
+     *
+     * @return the git short sha1
+     */
+    public String getShortSha1();
+
+    /**
+     * Sets the git short sha1.
+     *
+     * @param shortSha1 the git short sha1
+     */
+    public void setShortSha1(String shortSha1);
+}
diff --git a/src/com/android/tradefed/build/KernelBuildInfo.java b/src/com/android/tradefed/build/KernelBuildInfo.java
new file mode 100644
index 0000000..de24c83
--- /dev/null
+++ b/src/com/android/tradefed/build/KernelBuildInfo.java
@@ -0,0 +1,145 @@
+/*
+ * Copyright (C) 2012 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.tradefed.build;
+
+import java.io.File;
+import java.io.IOException;
+
+/**
+ * A {@link IBuildInfo} that represents a kernel build.
+ */
+public class KernelBuildInfo extends BuildInfo implements IKernelBuildInfo {
+    private final static String KERNEL_FILE = "kernel";
+
+    private String mSha1 = null;
+    private String mShortSha1 = null;
+    private long mCommitTime = 0;
+
+    /**
+     * Creates a {@link KernelBuildInfo}.
+     */
+    public KernelBuildInfo() {
+        super();
+    }
+
+    /**
+     * Creates a {@link KernelBuildInfo}.
+     *
+     * @param sha1 the git sha1, used as the build id
+     * @param shortSha1 the git short sha1
+     * @param commitTime the git commit time
+     * @param testTarget the test target
+     * @param buildName the build name
+     */
+    public KernelBuildInfo(String sha1, String shortSha1, long commitTime, String testTarget,
+            String buildName) {
+        super(sha1, testTarget, buildName);
+        mSha1 = sha1;
+        mShortSha1 = shortSha1;
+        mCommitTime = commitTime;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getKernelFile() {
+        return getFile(KERNEL_FILE);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getKernelVersion() {
+        return getVersion(KERNEL_FILE);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setKernelFile(File kernelFile, String version) {
+        setFile(KERNEL_FILE, kernelFile, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getSha1() {
+        return mSha1;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setSha1(String sha1) {
+        mSha1 = sha1;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getShortSha1() {
+        return mShortSha1;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setShortSha1(String shortSha1) {
+        mShortSha1 = shortSha1;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public long getCommitTime() {
+        return mCommitTime;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setCommitTime(long time) {
+        mCommitTime = time;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public IBuildInfo clone() {
+        KernelBuildInfo copy = new KernelBuildInfo(getSha1(), getShortSha1(),
+                getCommitTime(), getTestTag(), getBuildTargetName());
+        copy.addAllBuildAttributes(this);
+        try {
+            copy.addAllFiles(this);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+        copy.setBuildBranch(getBuildBranch());
+        copy.setBuildFlavor(getBuildFlavor());
+
+        return copy;
+    }
+}
diff --git a/src/com/android/tradefed/build/KernelDeviceBuildInfo.java b/src/com/android/tradefed/build/KernelDeviceBuildInfo.java
new file mode 100644
index 0000000..8d2dcfa
--- /dev/null
+++ b/src/com/android/tradefed/build/KernelDeviceBuildInfo.java
@@ -0,0 +1,359 @@
+/*
+ * Copyright (C) 2012 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.tradefed.build;
+
+import java.io.File;
+
+/**
+ * A {@link IBuildInfo} that represents a kernel build paired with a complete Android build.
+ */
+public class KernelDeviceBuildInfo extends BuildInfo implements IDeviceBuildInfo,
+        IKernelBuildInfo {
+    private IDeviceBuildInfo mDeviceBuild = new DeviceBuildInfo();
+    private IKernelBuildInfo mKernelBuild = new KernelBuildInfo();
+
+    /**
+     * Creates a {@link KernelBuildInfo}.
+     */
+    public KernelDeviceBuildInfo() {
+        super();
+    }
+
+    /**
+     * Creates a {@link KernelBuildInfo}.
+     *
+     * @param buildId the build id as a combination of the kernel build id and the device build id
+     * @param testTarget the test target
+     * @param buildName the build name
+     */
+    public KernelDeviceBuildInfo(String buildId, String testTarget, String buildName) {
+        super(buildId, testTarget, buildName);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getDeviceBuildId() {
+        return mDeviceBuild.getDeviceBuildId();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getDeviceImageFile() {
+        return mDeviceBuild.getDeviceImageFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getDeviceImageVersion() {
+        return mDeviceBuild.getDeviceImageVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setDeviceImageFile(File deviceImageFile, String version) {
+        mDeviceBuild.setDeviceImageFile(deviceImageFile, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getUserDataImageFile() {
+        return mDeviceBuild.getUserDataImageFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getUserDataImageVersion() {
+        return mDeviceBuild.getUserDataImageVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setUserDataImageFile(File userDataFile, String version) {
+        mDeviceBuild.setUserDataImageFile(userDataFile, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getTestsDir() {
+        return mDeviceBuild.getTestsDir();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getTestsDirVersion() {
+        return mDeviceBuild.getTestsDirVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setTestsDir(File testsDir, String version) {
+        mDeviceBuild.setTestsDir(testsDir, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getBasebandImageFile() {
+        return mDeviceBuild.getBasebandImageFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getBasebandVersion() {
+        return mDeviceBuild.getBasebandVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setBasebandImage(File basebandFile, String version) {
+        mDeviceBuild.setBasebandImage(basebandFile, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getBootloaderImageFile() {
+        return mDeviceBuild.getBootloaderImageFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getBootloaderVersion() {
+        return mDeviceBuild.getBootloaderVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setBootloaderImageFile(File bootloaderImgFile, String version) {
+        mDeviceBuild.setBootloaderImageFile(bootloaderImgFile, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getOtaPackageFile() {
+        return mDeviceBuild.getOtaPackageFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getOtaPackageVersion() {
+        return mDeviceBuild.getOtaPackageVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setOtaPackageFile(File otaFile, String version) {
+        mDeviceBuild.setOtaPackageFile(otaFile, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getMkbootimgFile() {
+        return mDeviceBuild.getMkbootimgFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getMkbootimgVersion() {
+        return mDeviceBuild.getMkbootimgVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setMkbootimgFile(File mkbootimg, String version) {
+        mDeviceBuild.setMkbootimgFile(mkbootimg, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getRamdiskFile() {
+        return mDeviceBuild.getRamdiskFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getRamdiskVersion() {
+        return mDeviceBuild.getRamdiskVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setRamdiskFile(File ramdisk, String version) {
+        mDeviceBuild.setRamdiskFile(ramdisk, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public File getKernelFile() {
+        return mKernelBuild.getKernelFile();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getKernelVersion() {
+        return mKernelBuild.getKernelVersion();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setKernelFile(File kernelFile, String version) {
+        mKernelBuild.setKernelFile(kernelFile, version);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getSha1() {
+        return mKernelBuild.getSha1();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setSha1(String sha1) {
+        mKernelBuild.setSha1(sha1);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String getShortSha1() {
+        return mKernelBuild.getShortSha1();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setShortSha1(String shortSha1) {
+        mKernelBuild.setShortSha1(shortSha1);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public long getCommitTime() {
+        return mKernelBuild.getCommitTime();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void setCommitTime(long time) {
+        mKernelBuild.getCommitTime();
+    }
+
+    /**
+     * Sets the device build.
+     */
+    public void setDeviceBuild(IDeviceBuildInfo deviceBuild) {
+        mDeviceBuild = deviceBuild;
+    }
+
+    /**
+     * Sets the kernel build.
+     */
+    public void setKernelBuild(IKernelBuildInfo kernelBuild) {
+        mKernelBuild = kernelBuild;
+    }
+
+    private void generateBuildId() {
+
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void cleanUp() {
+        mDeviceBuild.cleanUp();
+        mKernelBuild.cleanUp();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public IBuildInfo clone() {
+        KernelDeviceBuildInfo copy = new KernelDeviceBuildInfo(getBuildId(), getTestTag(),
+                getBuildTargetName());
+        copy.addAllBuildAttributes(this);
+        IDeviceBuildInfo deviceBuildClone = (IDeviceBuildInfo) mDeviceBuild.clone();
+        copy.setDeviceBuild(deviceBuildClone);
+        IKernelBuildInfo kernelBuildClone = (IKernelBuildInfo) mKernelBuild.clone();
+        copy.setKernelBuild(kernelBuildClone);
+        return copy;
+    }
+}
diff --git a/tests/src/com/android/tradefed/build/KernelBuildInfoTest.java b/tests/src/com/android/tradefed/build/KernelBuildInfoTest.java
new file mode 100644
index 0000000..7bc81cc
--- /dev/null
+++ b/tests/src/com/android/tradefed/build/KernelBuildInfoTest.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2012 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.tradefed.build;
+
+import com.android.tradefed.util.FileUtil;
+
+import junit.framework.TestCase;
+
+import java.io.File;
+
+/**
+ * Unit tests for {@link KernelBuildInfo}.
+ */
+public class KernelBuildInfoTest extends TestCase {
+
+    private IKernelBuildInfo mBuildInfo;
+    private File mKernelFile;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mBuildInfo = new KernelBuildInfo("kernel", "ker", 1, "build", "target");
+        mKernelFile = FileUtil.createTempFile("kernel", null);
+        FileUtil.writeToFile("filedata", mKernelFile);
+        mBuildInfo.setKernelFile(mKernelFile, "1");
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        FileUtil.deleteFile(mKernelFile);
+    }
+
+    /**
+     * Test method for {@link KernelBuildInfo#clone()}.
+     */
+    public void testClone() throws Exception {
+        IKernelBuildInfo copy = (KernelBuildInfo) mBuildInfo.clone();
+
+        assertEquals(mBuildInfo.getBuildBranch(), copy.getBuildBranch());
+        assertEquals(mBuildInfo.getBuildFlavor(), copy.getBuildFlavor());
+        assertEquals(mBuildInfo.getBuildId(), copy.getBuildId());
+        assertEquals(mBuildInfo.getBuildTargetName(), copy.getBuildTargetName());
+        assertEquals(mBuildInfo.getCommitTime(), copy.getCommitTime());
+        assertEquals(mBuildInfo.getShortSha1(), copy.getShortSha1());
+        assertEquals(mBuildInfo.getTestTag(), copy.getTestTag());
+
+        assertFalse(mKernelFile.getAbsolutePath().equals(copy.getKernelFile()));
+        assertTrue(FileUtil.compareFileContents(mKernelFile, copy.getKernelFile()));
+    }
+
+    /**
+     * Test method for {@link KernelBuildInfo#cleanUp()}.
+     */
+    public void testCleanUp() {
+        assertTrue(mBuildInfo.getKernelFile().exists());
+        mBuildInfo.cleanUp();
+        assertNull(mBuildInfo.getKernelFile());
+        assertFalse(mKernelFile.exists());
+    }
+}
diff --git a/tests/src/com/android/tradefed/build/KernelDeviceBuildInfoTest.java b/tests/src/com/android/tradefed/build/KernelDeviceBuildInfoTest.java
new file mode 100644
index 0000000..bb630b5
--- /dev/null
+++ b/tests/src/com/android/tradefed/build/KernelDeviceBuildInfoTest.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright (C) 2012 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.tradefed.build;
+
+import com.android.tradefed.util.FileUtil;
+
+import junit.framework.TestCase;
+
+import java.io.File;
+
+/**
+ * Unit tests for {@link KernelBuildInfo}.
+ */
+public class KernelDeviceBuildInfoTest extends TestCase {
+    private static final String DEVICE_VERSION = "device";
+    private static final String KERNEL_VERSION = "kernel";
+
+    private KernelDeviceBuildInfo mBuildInfo;
+    private File mDeviceFile;
+    private File mKernelFile;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+
+        mBuildInfo = new KernelDeviceBuildInfo("kernel_device", "build", "target");
+        mBuildInfo.setDeviceBuild(new DeviceBuildInfo("device", "build", "target"));
+        mBuildInfo.setKernelBuild(new KernelBuildInfo("kernel", "ker", 0, "build", "target"));
+
+        mDeviceFile = FileUtil.createTempFile("device", "tmp");
+        FileUtil.writeToFile("device", mDeviceFile);
+        mBuildInfo.setDeviceImageFile(mDeviceFile, DEVICE_VERSION);
+
+        mKernelFile = FileUtil.createTempFile("kernel", "tmp");
+        FileUtil.writeToFile("kernel", mKernelFile);
+        mBuildInfo.setKernelFile(mKernelFile, KERNEL_VERSION);
+    }
+
+    /**
+     * Test method for {@link KernelDeviceBuildInfo#clone()}.
+     */
+    public void testClone() throws Exception {
+        KernelDeviceBuildInfo copy = (KernelDeviceBuildInfo) mBuildInfo.clone();
+
+        assertEquals(mBuildInfo.getBuildBranch(), copy.getBuildBranch());
+        assertEquals(mBuildInfo.getBuildFlavor(), copy.getBuildFlavor());
+        assertEquals(mBuildInfo.getBuildId(), copy.getBuildId());
+        assertEquals(mBuildInfo.getBuildTargetName(), copy.getBuildTargetName());
+        assertEquals(mBuildInfo.getCommitTime(), copy.getCommitTime());
+        assertEquals(mBuildInfo.getShortSha1(), copy.getShortSha1());
+        assertEquals(mBuildInfo.getTestTag(), copy.getTestTag());
+
+        assertFalse(mDeviceFile.getAbsolutePath().equals(copy.getDeviceImageFile()));
+        assertTrue(FileUtil.compareFileContents(mDeviceFile, copy.getDeviceImageFile()));
+
+        assertFalse(mKernelFile.getAbsolutePath().equals(copy.getKernelFile()));
+        assertTrue(FileUtil.compareFileContents(mKernelFile, copy.getKernelFile()));
+    }
+
+    /**
+     * Test method for {@link KernelDeviceBuildInfo#cleanUp()}.
+     */
+    public void testCleanUp() {
+        assertTrue(mBuildInfo.getDeviceImageFile().exists());
+        assertTrue(mBuildInfo.getKernelFile().exists());
+        mBuildInfo.cleanUp();
+        assertNull(mBuildInfo.getDeviceImageFile());
+        assertNull(mBuildInfo.getKernelFile());
+        assertFalse(mDeviceFile.exists());
+        assertFalse(mKernelFile.exists());
+    }
+}