diff options
author | john.r.misinco | 2011-06-02 21:45:55 +0000 |
---|---|---|
committer | Ryan D. Brooks | 2011-06-02 21:45:55 +0000 |
commit | 454d214bdc54d64c553b890bf6d0f8fc948fccb6 (patch) | |
tree | 56cf35e57f9dcb1507f33bed8633c974ea4e5ec8 | |
parent | 5cf3844a4f6260e01705735751191b4422f2a180 (diff) | |
download | org.eclipse.osee-454d214bdc54d64c553b890bf6d0f8fc948fccb6.tar.gz org.eclipse.osee-454d214bdc54d64c553b890bf6d0f8fc948fccb6.tar.xz org.eclipse.osee-454d214bdc54d64c553b890bf6d0f8fc948fccb6.zip |
refactor[bgz_348142]: Remove osee_cvg_testunits table dependency
19 files changed, 779 insertions, 481 deletions
diff --git a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/Coverage_Db_Suite.java b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/Coverage_Db_Suite.java index f9ed33849df..c51cac012f9 100644 --- a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/Coverage_Db_Suite.java +++ b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/Coverage_Db_Suite.java @@ -15,8 +15,8 @@ import org.eclipse.osee.coverage.test.model.CoverageItemPersistTest; import org.eclipse.osee.coverage.test.model.CoverageOptionManagerStoreTest; import org.eclipse.osee.coverage.test.model.CoveragePreferencesTest; import org.eclipse.osee.coverage.test.model.CoverageUnitPersistTest; -import org.eclipse.osee.coverage.test.model.DbTestUnitProviderTest; -import org.eclipse.osee.coverage.test.model.TestUnitStoreTest; +import org.eclipse.osee.coverage.test.store.ArtifactTestUnitStoreTest; +import org.eclipse.osee.coverage.test.store.TestUnitCacheTest; import org.eclipse.osee.framework.core.client.ClientSessionManager; import org.eclipse.osee.framework.ui.skynet.render.RenderingUtil; import org.eclipse.osee.support.test.util.TestUtil; @@ -28,8 +28,8 @@ import org.junit.runners.Suite; @Suite.SuiteClasses({ CoveragePreferencesTest.class, CoverageOptionManagerStoreTest.class, - TestUnitStoreTest.class, - DbTestUnitProviderTest.class, + TestUnitCacheTest.class, + ArtifactTestUnitStoreTest.class, Coverage_Suite.class, CoverageParametersTest.class, CoverageItemPersistTest.class, diff --git a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/CoverageItemPersistTest.java b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/CoverageItemPersistTest.java index 882c8a400ec..2b0fb32b036 100644 --- a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/CoverageItemPersistTest.java +++ b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/CoverageItemPersistTest.java @@ -16,8 +16,9 @@ import org.eclipse.osee.coverage.model.CoverageItem; import org.eclipse.osee.coverage.model.CoverageOptionManager; import org.eclipse.osee.coverage.model.CoverageOptionManagerDefault; import org.eclipse.osee.coverage.model.CoverageUnit; -import org.eclipse.osee.coverage.store.DbTestUnitProvider; import org.eclipse.osee.coverage.store.OseeCoverageUnitStore; +import org.eclipse.osee.coverage.store.TestUnitCache; +import org.eclipse.osee.coverage.test.store.MockTestUnitStore; import org.eclipse.osee.coverage.test.util.CoverageTestUtil; import org.eclipse.osee.coverage.util.CoverageUtil; import org.eclipse.osee.framework.core.exception.ArtifactDoesNotExist; @@ -40,6 +41,7 @@ public class CoverageItemPersistTest { public static CoverageItem ci = null; public static String parentGuid = null; public static String guid = null; + public static TestUnitCache testUnitCache; @AfterClass public static void testCleanup() throws OseeCoreException { @@ -55,8 +57,9 @@ public class CoverageItemPersistTest { parentCu = new CoverageUnit(null, "Top", "C:/UserData/", null); parentGuid = parentCu.getGuid(); + testUnitCache = new TestUnitCache(new MockTestUnitStore()); ci = new CoverageItem(parentCu, CoverageOptionManager.Deactivated_Code, "1"); - ci.setTestUnitProvider(DbTestUnitProvider.instance()); + ci.setTestUnitProvider(testUnitCache); for (int x = 0; x < 10; x++) { ci.addTestUnitName("Test Unit " + x); } @@ -122,7 +125,7 @@ public class CoverageItemPersistTest { OseeCoverageUnitStore.get(parentCu, CoverageTestUtil.getTestBranch()).load( CoverageOptionManagerDefault.instance()); CoverageItem ci = parentCu.getCoverageItems().iterator().next(); - ci.setTestUnitProvider(DbTestUnitProvider.instance()); + ci.setTestUnitProvider(testUnitCache); Assert.assertEquals(guid, ci.getGuid()); Assert.assertEquals("1", ci.getOrderNumber()); Assert.assertEquals(CoverageOptionManager.Deactivated_Code, ci.getCoverageMethod()); diff --git a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/CoverageUnitPersistTest.java b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/CoverageUnitPersistTest.java index 1263d4446ea..912a7249e85 100644 --- a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/CoverageUnitPersistTest.java +++ b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/CoverageUnitPersistTest.java @@ -15,27 +15,20 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; -import org.eclipse.osee.coverage.event.CoverageEventType; -import org.eclipse.osee.coverage.event.CoveragePackageEvent; import org.eclipse.osee.coverage.internal.Activator; import org.eclipse.osee.coverage.merge.IMergeItem; import org.eclipse.osee.coverage.merge.MergeImportManager; import org.eclipse.osee.coverage.merge.MergeManager; import org.eclipse.osee.coverage.model.CoverageImport; import org.eclipse.osee.coverage.model.CoverageItem; -import org.eclipse.osee.coverage.model.CoverageOptionManager; import org.eclipse.osee.coverage.model.CoverageOptionManagerDefault; import org.eclipse.osee.coverage.model.CoveragePackage; import org.eclipse.osee.coverage.model.CoverageUnit; import org.eclipse.osee.coverage.model.ICoverage; import org.eclipse.osee.coverage.model.SimpleCoverageUnitFileContentsProvider; -import org.eclipse.osee.coverage.model.SimpleTestUnitProvider; import org.eclipse.osee.coverage.model.SimpleWorkProductTaskProvider; import org.eclipse.osee.coverage.store.CoverageArtifactTypes; -import org.eclipse.osee.coverage.store.DbTestUnitProvider; import org.eclipse.osee.coverage.store.OseeCoveragePackageStore; -import org.eclipse.osee.coverage.store.OseeCoverageUnitStore; -import org.eclipse.osee.coverage.store.TestUnitStore; import org.eclipse.osee.coverage.test.SampleJavaFileParser; import org.eclipse.osee.coverage.test.import1.CoverageImport1TestBlam; import org.eclipse.osee.coverage.test.util.CoverageTestUtil; @@ -44,8 +37,6 @@ import org.eclipse.osee.coverage.util.ISaveable; import org.eclipse.osee.framework.core.exception.ArtifactDoesNotExist; import org.eclipse.osee.framework.core.exception.OseeCoreException; import org.eclipse.osee.framework.core.model.Branch; -import org.eclipse.osee.framework.jdk.core.util.GUID; -import org.eclipse.osee.framework.jdk.core.util.Strings; import org.eclipse.osee.framework.logging.OseeLevel; import org.eclipse.osee.framework.logging.OseeLog; import org.eclipse.osee.framework.skynet.core.artifact.Artifact; @@ -184,46 +175,4 @@ public class CoverageUnitPersistTest { } } - /** - * Test that a coverage item that has a simpletestunitprovider, as imports will, will covert over and use the - * DbTestUnitProvider when the item is persisted. Then, when re-loaded, will load back properly using - * DbTestUnitProvider - */ - @Test - public void testSimpleToDbTestUnitProvider() throws OseeCoreException { - TestUnitStore.clearStore(); - String cuName = DbTestUnitProviderTest.class.getSimpleName() + "-" + GUID.create(); - CoverageUnit unit = new CoverageUnit(null, cuName, "location", new SimpleCoverageUnitFileContentsProvider()); - unit.setWorkProductTaskGuid(GUID.create()); - CoverageItem item = new CoverageItem(unit, CoverageOptionManager.Test_Unit, "1"); - item.setTestUnitProvider(new SimpleTestUnitProvider()); - for (int x = 0; x < 10; x++) { - item.addTestUnitName("Test Unit " + x); - } - Assert.assertEquals(10, item.getTestUnits().size()); - OseeCoverageUnitStore store = new OseeCoverageUnitStore(unit, CoverageTestUtil.getTestBranch()); - CoveragePackageEvent coverageEvent = - new CoveragePackageEvent("Test CP", GUID.create(), CoverageEventType.Deleted, GUID.create()); - SkynetTransaction transaction = new SkynetTransaction(CoverageTestUtil.getTestBranch(), "Coverage Unit Commit"); - Result result = store.save(transaction, coverageEvent); - transaction.execute(); - Assert.assertTrue(result.isTrue()); - - Artifact artifact = - ArtifactQuery.getArtifactFromTypeAndName(CoverageArtifactTypes.CoverageUnit, cuName, - CoverageTestUtil.getTestBranch()); - Assert.assertNotNull(artifact); - OseeCoverageUnitStore dbStore = - new OseeCoverageUnitStore(null, artifact, CoverageOptionManagerDefault.instance()); - CoverageUnit dbUnit = dbStore.getCoverageUnit(); - Assert.assertEquals(1, dbUnit.getCoverageItems().size()); - CoverageItem dbItem = dbUnit.getCoverageItems().iterator().next(); - Assert.assertTrue(dbItem.getTestUnitProvider() instanceof DbTestUnitProvider); - Assert.assertEquals(10, dbItem.getTestUnits().size()); - Assert.assertTrue(dbItem.getTestUnits().iterator().next().startsWith("Test Unit ")); - Assert.assertEquals(10, TestUnitStore.getTestUnitCount()); - Assert.assertTrue(Strings.isValid(dbUnit.getWorkProductTaskGuid())); - Assert.assertEquals(unit.getWorkProductTaskGuid(), dbUnit.getWorkProductTaskGuid()); - TestUnitStore.clearStore(); - } } diff --git a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/DbTestUnitProviderTest.java b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/DbTestUnitProviderTest.java deleted file mode 100644 index 372698b3090..00000000000 --- a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/DbTestUnitProviderTest.java +++ /dev/null @@ -1,96 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010 Boeing. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.coverage.test.model; - -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import junit.framework.Assert; -import org.eclipse.osee.coverage.model.CoverageItem; -import org.eclipse.osee.coverage.model.CoverageOptionManager; -import org.eclipse.osee.coverage.store.DbTestUnitProvider; -import org.eclipse.osee.coverage.store.TestUnitStore; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * @author Donald G. Dunne - */ -public class DbTestUnitProviderTest { - - public static CoverageItem item, item2; - public static DbTestUnitProvider dbTestUnitProvider; - - @BeforeClass - public static void setup() throws OseeCoreException { - item = new CoverageItem(null, CoverageOptionManager.Test_Unit, "1"); - item2 = new CoverageItem(null, CoverageOptionManager.Test_Unit, "2"); - dbTestUnitProvider = DbTestUnitProvider.instance(); - TestUnitStore.clearStore(); - } - - @AfterClass - public static void cleanup() throws OseeCoreException { - TestUnitStore.clearStore(); - } - - /** - * Test method for - * {@link org.eclipse.osee.coverage.store.TestUnitStore#setTestUnits(org.eclipse.osee.coverage.model.CoverageUnit, java.util.Collection)} - * . - */ - @Test - public void testSetTestUnits() throws OseeCoreException { - List<String> names = Arrays.asList("Now.java", "Is.java", "The.java", "Time.java"); - dbTestUnitProvider.setTestUnits(item, names); - Integer id = TestUnitStore.getTestUnitId("Now.java", false); - Assert.assertNotNull(id); - Assert.assertEquals(1, id.intValue()); - } - - /** - * Test method for - * {@link org.eclipse.osee.coverage.store.TestUnitStore#getTestUnits(org.eclipse.osee.coverage.model.CoverageUnit)}. - */ - @Test - public void testGetTestUnits() throws OseeCoreException { - Collection<String> names = dbTestUnitProvider.getTestUnits(item); - Assert.assertEquals(4, names.size()); - - dbTestUnitProvider.setTestUnits(item2, Arrays.asList("Time.java", "The.java", "NewOne.java")); - - // ensure that only unique name entries exist - Assert.assertEquals(5, TestUnitStore.getTestUnitCount()); - - // Ensure that can retrieve items specific to coverageItem - names = dbTestUnitProvider.getTestUnits(item); - Assert.assertEquals(4, names.size()); - names = dbTestUnitProvider.getTestUnits(item2); - Assert.assertEquals(3, names.size()); - - } - - /** - * Test method for - * {@link org.eclipse.osee.coverage.store.TestUnitStore#removeTestUnitsFromDb(java.lang.String, java.util.List)}. - */ - @Test - public void testRemoveTestUnitsFromDb() throws OseeCoreException { - Assert.assertEquals(3, dbTestUnitProvider.getTestUnits(item2).size()); - dbTestUnitProvider.removeTestUnits(item2, Arrays.asList("Time.java")); - Assert.assertEquals(2, dbTestUnitProvider.getTestUnits(item2).size()); - dbTestUnitProvider.removeTestUnits(item2); - Assert.assertEquals(0, dbTestUnitProvider.getTestUnits(item2).size()); - } - -} diff --git a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/TestUnitStoreTest.java b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/TestUnitStoreTest.java deleted file mode 100644 index 77983206066..00000000000 --- a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/model/TestUnitStoreTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010 Boeing. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.coverage.test.model; - -import junit.framework.Assert; -import org.eclipse.osee.coverage.store.TestUnitStore; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.junit.AfterClass; -import org.junit.BeforeClass; -import org.junit.Test; - -/** - * @author Donald G. Dunne - */ -public class TestUnitStoreTest { - - @BeforeClass - public static void setup() throws OseeCoreException { - TestUnitStore.clearStore(); - } - - @AfterClass - public static void cleanup() throws OseeCoreException { - TestUnitStore.clearStore(); - } - - /** - * Test method for - * {@link org.eclipse.osee.coverage.store.TestUnitStore#addTestUnitNameToDb(java.lang.String, java.lang.String)}. - */ - @Test - public void testAddTestUnitNameToDb() throws OseeCoreException { - int count = TestUnitStore.getTestUnitCount(); - Assert.assertEquals(0, count); - Integer thisJavaId = TestUnitStore.getTestUnitId("This.java", true); - int newCount = TestUnitStore.getTestUnitCount(); - Assert.assertEquals(1, newCount); - - Integer nameId = TestUnitStore.getTestUnitId("NotThis.java", false); - Assert.assertNull(nameId); - // Count should not have changed - newCount = TestUnitStore.getTestUnitCount(); - Assert.assertEquals(1, newCount); - - String name = TestUnitStore.getTestUnitName(thisJavaId); - Assert.assertEquals("This.java", name); - } - -} diff --git a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/ArtifactTestUnitStoreTest.java b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/ArtifactTestUnitStoreTest.java new file mode 100644 index 00000000000..f11afe70262 --- /dev/null +++ b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/ArtifactTestUnitStoreTest.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.coverage.test.store; + +import java.util.Map.Entry; +import org.eclipse.osee.coverage.model.CoverageItem; +import org.eclipse.osee.coverage.model.CoverageOptionManager; +import org.eclipse.osee.coverage.model.CoverageOptionManagerDefault; +import org.eclipse.osee.coverage.model.CoverageUnit; +import org.eclipse.osee.coverage.store.ArtifactTestUnitStore; +import org.eclipse.osee.coverage.store.TestUnitCache; +import org.eclipse.osee.framework.core.enums.CoreArtifactTypes; +import org.eclipse.osee.framework.core.enums.CoreAttributeTypes; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.core.model.Branch; +import org.eclipse.osee.framework.skynet.core.artifact.Artifact; +import org.eclipse.osee.framework.skynet.core.artifact.BranchManager; +import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery; +import org.eclipse.osee.support.test.util.TestUtil; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +/** + * @author John Misinco + */ +public class ArtifactTestUnitStoreTest { + + private static final String testInputData = "1|test1\n2|test2\n3|test3"; + Branch testBranch; + private static final String testArtifactGuid = "AEas7qndRl+k+g6YpkwA"; + + private CoverageItem createCoverageItem(TestUnitCache tc) throws OseeCoreException { + CoverageUnit parent = new CoverageUnit(null, "Top", "C:/UserData/", null); + CoverageItem ci1 = new CoverageItem(parent, CoverageOptionManager.Deactivated_Code, "1"); + ci1.setName("this is text"); + return CoverageItem.createCoverageItem(parent, ci1.toXml(), CoverageOptionManagerDefault.instance(), tc); + } + + @Before + public void createTestArtifact() throws OseeCoreException { + Assert.assertTrue(TestUtil.isTestDb()); + testBranch = BranchManager.createTopLevelBranch("TestBranch"); + Artifact testArtifact = + ArtifactQuery.getOrCreate(testArtifactGuid, null, CoreArtifactTypes.GeneralData, testBranch); + testArtifact.setSoleAttributeFromString(CoreAttributeTypes.GeneralStringData, testInputData); + } + + @After + public void cleanUpTestArtifact() { + BranchManager.deleteBranch(testBranch); + } + + @Test + public void testLoad() throws OseeCoreException { + ArtifactTestUnitStore store = new ArtifactTestUnitStore(testBranch); + TestUnitCache tc = new TestUnitCache(store); + store.load(tc); + StringBuilder actual = new StringBuilder(); + boolean firstTime = true; + for (Entry<Integer, String> entry : tc.getAllCachedTestUnitEntries()) { + if (!firstTime) { + actual.append("\n"); + } + actual.append(Integer.toString(entry.getKey())); + actual.append("|"); + actual.append(entry.getValue()); + firstTime = false; + } + Assert.assertEquals(testInputData, actual.toString()); + } + + @Test + public void testStore() throws OseeCoreException { + ArtifactTestUnitStore store = new ArtifactTestUnitStore(testBranch); + TestUnitCache tc = new TestUnitCache(store); + CoverageItem ci = createCoverageItem(tc); + ci.addTestUnitName("test1"); + ci.addTestUnitName("test10"); + + store.store(tc); + Artifact testArtifact = + ArtifactQuery.getOrCreate(testArtifactGuid, null, CoreArtifactTypes.GeneralData, testBranch); + String actual = testArtifact.getSoleAttributeValueAsString(CoreAttributeTypes.GeneralStringData, ""); + String expected = testInputData + "\n4|test10"; + Assert.assertEquals(expected, actual); + + } + +} diff --git a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/MockTestUnitStore.java b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/MockTestUnitStore.java new file mode 100644 index 00000000000..84d8adf5e84 --- /dev/null +++ b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/MockTestUnitStore.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.coverage.test.store; + +import org.eclipse.osee.coverage.store.ITestUnitStore; +import org.eclipse.osee.coverage.store.TestUnitCache; +import org.eclipse.osee.framework.core.exception.OseeCoreException; + +/** + * @author John Misinco + */ +public class MockTestUnitStore implements ITestUnitStore { + + @SuppressWarnings("unused") + @Override + public void load(TestUnitCache cache) throws OseeCoreException { + // do nothing + } + + @SuppressWarnings("unused") + @Override + public void store(TestUnitCache cache) throws OseeCoreException { + // do nothing + } + +} diff --git a/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/TestUnitCacheTest.java b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/TestUnitCacheTest.java new file mode 100644 index 00000000000..9909e92c9a1 --- /dev/null +++ b/plugins/org.eclipse.osee.coverage.test/src/org/eclipse/osee/coverage/test/store/TestUnitCacheTest.java @@ -0,0 +1,207 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.coverage.test.store; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import junit.framework.Assert; +import org.eclipse.osee.coverage.model.CoverageItem; +import org.eclipse.osee.coverage.model.CoverageOptionManager; +import org.eclipse.osee.coverage.model.CoverageOptionManagerDefault; +import org.eclipse.osee.coverage.model.CoverageUnit; +import org.eclipse.osee.coverage.store.ITestUnitStore; +import org.eclipse.osee.coverage.store.TestUnitCache; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.jdk.core.util.Collections; +import org.junit.Test; + +/** + * @author John Misinco + */ +public class TestUnitCacheTest { + + private CoverageItem createCoverageItem(TestUnitCache tc) throws OseeCoreException { + CoverageUnit parent = new CoverageUnit(null, "Top", "C:/UserData/", null); + CoverageItem ci1 = new CoverageItem(parent, CoverageOptionManager.Deactivated_Code, "1"); + ci1.setName("this is text"); + return CoverageItem.createCoverageItem(parent, ci1.toXml(), CoverageOptionManagerDefault.instance(), tc); + } + + @Test + public void testPut() throws OseeCoreException { + ITestUnitStore testUnitStore = new MockTestUnitStore(); + TestUnitCache tc = new TestUnitCache(testUnitStore); + tc.put("test1"); + tc.put("test2"); + Assert.assertEquals(2, tc.getAllCachedTestUnitNames().size()); + + tc.put(1, "test3"); + tc.put("test1"); + Assert.assertEquals(2, tc.getAllCachedTestUnitNames().size()); + + tc.put(4, "test4"); + Assert.assertEquals(3, tc.getAllCachedTestUnitNames().size()); + } + + @Test + public void testGetAllCachedTestUnitEntries() throws OseeCoreException { + ITestUnitStore testUnitStore = new MockTestUnitStore(); + TestUnitCache tc = new TestUnitCache(testUnitStore); + HashMap<Integer, String> entries = new HashMap<Integer, String>(); + for (int i = 0; i < 10; i++) { + entries.put(i, "test" + Integer.toString(i)); + tc.put(i, "test" + Integer.toString(i)); + } + + Set<Entry<Integer, String>> cacheEntries = tc.getAllCachedTestUnitEntries(); + int numFound = 0; + for (Entry<Integer, String> cacheEntry : cacheEntries) { + if (entries.containsKey(cacheEntry.getKey()) && entries.get(cacheEntry.getKey()).equals(cacheEntry.getValue())) { + numFound++; + } + } + + Assert.assertTrue(numFound == entries.size()); + } + + @Test + public void testGetAllCachedTestUnitNames() throws OseeCoreException { + ITestUnitStore testUnitStore = new MockTestUnitStore(); + TestUnitCache tc = new TestUnitCache(testUnitStore); + String[] entries = {"test1", "test2", "test3", "test4"}; + for (String entry : entries) { + tc.put(entry); + } + + Set<Entry<Integer, String>> cacheEntries = tc.getAllCachedTestUnitEntries(); + List<String> foundNames = new ArrayList<String>(); + for (Entry<Integer, String> cacheEntry : cacheEntries) { + foundNames.add(cacheEntry.getValue()); + } + + Assert.assertTrue(foundNames.size() == entries.length); + Assert.assertTrue(Collections.setComplement(foundNames, Arrays.asList(entries)).size() == 0); + } + + @Test + public void testAddAndGetTestUnits() throws OseeCoreException { + ITestUnitStore testUnitStore = new MockTestUnitStore(); + TestUnitCache tc = new TestUnitCache(testUnitStore); + Collection<String> expected = new ArrayList<String>(); + CoverageItem ci = createCoverageItem(tc); + for (int i = 0; i < 10; i++) { + String testUnitName = "test" + Integer.toString(i); + expected.add(testUnitName); + tc.put(i, testUnitName); + tc.addTestUnit(ci, testUnitName); + } + + Collection<String> actual = tc.getTestUnits(ci); + Assert.assertTrue(Collections.setComplement(expected, actual).size() == 0); + + } + + @Test + public void testRemoveTestUnit() throws OseeCoreException { + ITestUnitStore testUnitStore = new MockTestUnitStore(); + TestUnitCache tc = new TestUnitCache(testUnitStore); + Collection<String> expected = new ArrayList<String>(); + CoverageItem ci = createCoverageItem(tc); + for (int i = 0; i < 10; i++) { + String testUnitName = "test" + Integer.toString(i); + expected.add(testUnitName); + tc.put(i, testUnitName); + tc.addTestUnit(ci, testUnitName); + } + + Collection<String> actual = tc.getTestUnits(ci); + Assert.assertTrue(Collections.setComplement(expected, actual).size() == 0); + + tc.removeTestUnit(ci, "test1"); + tc.removeTestUnit(ci, "test11"); + tc.removeTestUnit(ci, "test5"); + + expected.remove("test1"); + expected.remove("test5"); + actual = tc.getTestUnits(ci); + + Assert.assertTrue(Collections.setComplement(expected, actual).size() == 0); + + } + + @Test + public void testSetTestUnits() throws OseeCoreException { + ITestUnitStore testUnitStore = new MockTestUnitStore(); + TestUnitCache tc = new TestUnitCache(testUnitStore); + Collection<String> expected = new ArrayList<String>(); + CoverageItem ci = createCoverageItem(tc); + for (int i = 0; i < 10; i++) { + String testUnitName = "test" + Integer.toString(i); + expected.add(testUnitName); + tc.put(i, testUnitName); + } + + tc.setTestUnits(ci, expected); + Collection<String> actual = tc.getTestUnits(ci); + Assert.assertTrue(Collections.setComplement(expected, actual).size() == 0); + + expected.add("test12"); + tc.setTestUnits(ci, expected); + actual = tc.getTestUnits(ci); + Assert.assertTrue(Collections.setComplement(expected, actual).size() == 0); + + } + + @Test + public void testToXml() throws OseeCoreException { + ITestUnitStore testUnitStore = new MockTestUnitStore(); + TestUnitCache tc = new TestUnitCache(testUnitStore); + HashMap<Integer, String> entries = new HashMap<Integer, String>(); + for (int i = 0; i < 10; i++) { + entries.put(i, "test" + Integer.toString(i)); + tc.put(i, "test" + Integer.toString(i)); + } + + CoverageItem ci = createCoverageItem(tc); + tc.addTestUnit(ci, "test2"); + tc.addTestUnit(ci, "test1"); + tc.addTestUnit(ci, "test10"); + + String expected = "1;2;10"; + Assert.assertTrue(expected.equals(tc.toXml(ci))); + } + + @Test + public void testFromXml() throws OseeCoreException { + ITestUnitStore testUnitStore = new MockTestUnitStore(); + TestUnitCache tc = new TestUnitCache(testUnitStore); + for (int i = 0; i < 10; i++) { + tc.put(i, "test" + Integer.toString(i)); + } + CoverageItem ci = createCoverageItem(tc); + + String fromXml = "1;8;2;10"; + tc.fromXml(ci, fromXml); + Collection<String> units = tc.getTestUnits(ci); + Collection<String> expected = new ArrayList<String>(); + expected.add("test1"); + expected.add("test2"); + expected.add("test8"); + + Assert.assertTrue(Collections.setComplement(expected, units).size() == 0); + } + +} diff --git a/plugins/org.eclipse.osee.coverage/META-INF/MANIFEST.MF b/plugins/org.eclipse.osee.coverage/META-INF/MANIFEST.MF index 39f5db2dab0..e427a583518 100644 --- a/plugins/org.eclipse.osee.coverage/META-INF/MANIFEST.MF +++ b/plugins/org.eclipse.osee.coverage/META-INF/MANIFEST.MF @@ -29,7 +29,8 @@ Require-Bundle: org.eclipse.ui, org.eclipse.osee.coverage.help.ui;bundle-version="0.9.8", org.eclipse.osee.framework.help.ui;bundle-version="0.9.8" Bundle-ActivationPolicy: lazy -Import-Package: com.lowagie.text;version="2.1.7", +Import-Package: com.google.common.collect, + com.lowagie.text;version="2.1.7", com.lowagie.text.pdf;version="2.1.7", org.eclipse.osee.framework.access, org.eclipse.osee.framework.core.client, diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/model/ITestUnitProvider.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/model/ITestUnitProvider.java index 2083156439e..9293c236986 100644 --- a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/model/ITestUnitProvider.java +++ b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/model/ITestUnitProvider.java @@ -31,4 +31,5 @@ public interface ITestUnitProvider { public String toXml(CoverageItem coverageItem) throws OseeCoreException; public void fromXml(CoverageItem coverageItem, String xml) throws OseeCoreException; + } diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CoverageNavigateViewItems.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CoverageNavigateViewItems.java index eb21926f8ff..ac20ac5128d 100644 --- a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CoverageNavigateViewItems.java +++ b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CoverageNavigateViewItems.java @@ -63,6 +63,7 @@ public class CoverageNavigateViewItems implements XNavigateViewItems { try { if (SystemGroup.OseeAdmin.isCurrentUserMember()) { + items.add(new CreateCoverageTestUnitArtifact(null)); for (ICoverageNavigateItem navigateItem : getExtensionPointNavigateItems()) { try { items.addAll(navigateItem.getNavigateItems()); diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CreateCoverageTestUnitArtifact.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CreateCoverageTestUnitArtifact.java new file mode 100644 index 00000000000..5a0a2cc6e90 --- /dev/null +++ b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CreateCoverageTestUnitArtifact.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.coverage.navigate; + +import org.eclipse.osee.coverage.store.ArtifactTestUnitStore; +import org.eclipse.osee.coverage.util.CoverageUtil; +import org.eclipse.osee.framework.core.enums.CoreArtifactTypes; +import org.eclipse.osee.framework.core.enums.CoreAttributeTypes; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.database.core.ConnectionHandler; +import org.eclipse.osee.framework.database.core.IOseeStatement; +import org.eclipse.osee.framework.skynet.core.artifact.Artifact; +import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery; +import org.eclipse.osee.framework.ui.plugin.util.AWorkbench; +import org.eclipse.osee.framework.ui.plugin.xnavigate.XNavigateComposite.TableLoadOption; +import org.eclipse.osee.framework.ui.plugin.xnavigate.XNavigateItem; +import org.eclipse.osee.framework.ui.skynet.FrameworkImage; + +/** + * @author John Misinco + */ +public class CreateCoverageTestUnitArtifact extends XNavigateItem { + + public CreateCoverageTestUnitArtifact(XNavigateItem parent) { + super(parent, "Create Coverage Test Unit Artifact", FrameworkImage.GEAR); + } + + @Override + public void run(TableLoadOption... tableLoadOptions) throws OseeCoreException { + + if (CoverageUtil.getBranch() == null) { + if (!CoverageUtil.getBranchFromUser(false)) { + return; + } + } + + IOseeStatement chStmt = ConnectionHandler.getStatement(); + StringBuilder sb = new StringBuilder(); + try { + String sql = "select * from osee_cvg_testunits order by name_id asc"; + chStmt.runPreparedQuery(sql); + boolean firstTime = true; + while (chStmt.next()) { + if (!firstTime) { + sb.append("\n"); + } + sb.append(chStmt.getInt("name_id")); + sb.append("|"); + sb.append(chStmt.getString("name")); + firstTime = false; + } + } finally { + chStmt.close(); + } + Artifact art = + ArtifactQuery.getOrCreate(ArtifactTestUnitStore.COVERAGE_GUID, null, CoreArtifactTypes.GeneralData, + CoverageUtil.getBranch()); + art.setSoleAttributeFromString(CoreAttributeTypes.GeneralStringData, sb.toString()); + art.persist(); + AWorkbench.popup("Completed", "Complete"); + } +} diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CreateTestCoverageUnits.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CreateTestCoverageUnits.java index 9da9b6cb3ed..10a1cac0801 100644 --- a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CreateTestCoverageUnits.java +++ b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/navigate/CreateTestCoverageUnits.java @@ -84,7 +84,7 @@ public class CreateTestCoverageUnits extends XNavigateItemAction { store.save(); } - } catch (Exception ex) { + } catch (Throwable ex) { OseeLog.log(Activator.class, OseeLevel.SEVERE_POPUP, ex); } diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/ArtifactTestUnitStore.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/ArtifactTestUnitStore.java new file mode 100644 index 00000000000..90202723337 --- /dev/null +++ b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/ArtifactTestUnitStore.java @@ -0,0 +1,109 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.coverage.store; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import java.util.StringTokenizer; +import org.eclipse.osee.framework.core.data.IOseeBranch; +import org.eclipse.osee.framework.core.enums.CoreArtifactTypes; +import org.eclipse.osee.framework.core.enums.CoreAttributeTypes; +import org.eclipse.osee.framework.core.exception.OseeArgumentException; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.jdk.core.util.Strings; +import org.eclipse.osee.framework.skynet.core.artifact.Artifact; +import org.eclipse.osee.framework.skynet.core.artifact.search.ArtifactQuery; + +/** + * @author John Misinco + */ +public class ArtifactTestUnitStore implements ITestUnitStore { + + public static final String COVERAGE_GUID = "AEas7qndRl+k+g6YpkwA"; + + private final IOseeBranch branch; + + public ArtifactTestUnitStore(IOseeBranch branch) { + this.branch = branch; + } + + @Override + public void load(TestUnitCache cache) throws OseeCoreException { + String data = getAttributeData(); + parse(data, cache); + } + + private String getAttributeData() throws OseeCoreException { + Artifact artifact = getCoverageTestUnitArtifact(); + return artifact.getSoleAttributeValueAsString(CoreAttributeTypes.GeneralStringData, ""); + } + + @Override + public void store(TestUnitCache cache) throws OseeCoreException { + Artifact artifact = getCoverageTestUnitArtifact(); + + Set<Entry<Integer, String>> entries = cache.getAllCachedTestUnitEntries(); + List<Entry<Integer, String>> entriesList = new ArrayList<Entry<Integer, String>>(entries); + Collections.sort(entriesList, new Comparator<Entry<Integer, String>>() { + + @Override + public int compare(Entry<Integer, String> o1, Entry<Integer, String> o2) { + return o1.getKey().compareTo(o2.getKey()); + } + }); + + String storage = asStorage(entriesList); + artifact.setSoleAttributeFromString(CoreAttributeTypes.GeneralStringData, storage); + artifact.persist(); + } + + protected String asStorage(List<Entry<Integer, String>> entries) { + StringBuilder sb = new StringBuilder(); + boolean firstTime = true; + for (Entry<Integer, String> entry : entries) { + if (!firstTime) { + sb.append("\n"); + } + sb.append(entry.getKey()); + sb.append("|"); + sb.append(entry.getValue()); + firstTime = false; + } + return sb.toString(); + } + + protected void parse(String data, TestUnitCache cache) throws OseeCoreException { + StringTokenizer entries = new StringTokenizer(data, "\n"); + while (entries.hasMoreElements()) { + StringTokenizer idName = new StringTokenizer(entries.nextToken(), "|"); + if (idName.countTokens() == 2) { + String id = idName.nextToken(); + String testUnitName = idName.nextToken(); + if (Strings.isValid(id, testUnitName)) { + int key = Integer.parseInt(id); + cache.put(key, testUnitName); + } else { + throw new OseeArgumentException("Invalid Test Unit Name"); + } + } else { + throw new OseeArgumentException("Invalid Test Unit Name"); + } + } + } + + private Artifact getCoverageTestUnitArtifact() throws OseeCoreException { + return ArtifactQuery.getOrCreate(COVERAGE_GUID, null, CoreArtifactTypes.GeneralData, branch); + } +} diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/DbTestUnitProvider.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/DbTestUnitProvider.java deleted file mode 100644 index 9ea8c5c938a..00000000000 --- a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/DbTestUnitProvider.java +++ /dev/null @@ -1,136 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010 Boeing. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.coverage.store; - -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; -import java.util.logging.Level; -import org.eclipse.osee.coverage.internal.Activator; -import org.eclipse.osee.coverage.model.CoverageItem; -import org.eclipse.osee.coverage.model.ITestUnitProvider; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.eclipse.osee.framework.jdk.core.type.HashCollection; -import org.eclipse.osee.framework.jdk.core.util.Strings; -import org.eclipse.osee.framework.logging.OseeLog; - -/** - * This provider takes test unit names and converts them to ids and adds name/id entry to db table. It uses the - * SimpleTestUnitProvider implementation to store off the nameIds instead of the names.<br> - * <br> - * This saves space and allows CoverageItems meta-data to be stored in DB versus slow/non-bulk-loading binary attribute. - * - * @author Donald G. Dunne - */ -public class DbTestUnitProvider implements ITestUnitProvider { - private static DbTestUnitProvider instance = new DbTestUnitProvider(); - // This structure will store the nameIds that map to DB name table - final HashCollection<CoverageItem, Integer> coverageItemToTestUnits = - new HashCollection<CoverageItem, Integer>(1000); - - private DbTestUnitProvider() { - // private constructor - } - - public static DbTestUnitProvider instance() { - return instance; - } - - @Override - public void setTestUnits(CoverageItem coverageItem, Collection<String> testUnitNames) throws OseeCoreException { - coverageItemToTestUnits.removeValues(coverageItem); - for (String testUnitName : testUnitNames) { - addTestUnit(coverageItem, testUnitName); - } - } - - public void removeTestUnits(CoverageItem coverageItem) throws OseeCoreException { - removeTestUnits(coverageItem, null); - } - - /** - * @param testUnitNames if null, removes all names - */ - public void removeTestUnits(CoverageItem coverageItem, Collection<String> testUnitNames) throws OseeCoreException { - if (testUnitNames == null) { - for (String name : getTestUnits(coverageItem)) { - Integer nameId = TestUnitStore.getTestUnitId(name, false); - if (nameId != null) { - coverageItemToTestUnits.removeValue(coverageItem, nameId); - } - } - } else { - for (String testUnitName : testUnitNames) { - Integer nameId = TestUnitStore.getTestUnitId(testUnitName, false); - if (nameId != null) { - coverageItemToTestUnits.removeValue(coverageItem, nameId); - } - } - } - } - - @Override - public void addTestUnit(CoverageItem coverageItem, String testUnitName) throws OseeCoreException { - Collection<String> testUnitNames = getTestUnits(coverageItem); - if (!testUnitNames.contains(testUnitName)) { - Integer nameId = TestUnitStore.getTestUnitId(testUnitName, true); - coverageItemToTestUnits.put(coverageItem, nameId); - } - } - - @Override - public Collection<String> getTestUnits(CoverageItem coverageItem) { - try { - Collection<Integer> testUnitNameIds = coverageItemToTestUnits.getValues(coverageItem); - if (testUnitNameIds != null) { - Set<String> names = new HashSet<String>(); - for (Integer nameId : testUnitNameIds) { - String name = TestUnitStore.getTestUnitName(nameId); - if (Strings.isValid(name)) { - names.add(name); - } - } - return names; - } - } catch (OseeCoreException ex) { - OseeLog.log(Activator.class, Level.SEVERE, ex); - } - return Collections.emptyList(); - } - - @Override - public void fromXml(CoverageItem coverageItem, String xml) throws OseeCoreException { - removeTestUnits(coverageItem); - for (String nameIdStr : xml.split(";")) { - try { - coverageItemToTestUnits.put(coverageItem, new Integer(nameIdStr)); - } catch (NumberFormatException ex) { - OseeLog.log(Activator.class, Level.SEVERE, ex); - } - } - } - - @Override - public String toXml(CoverageItem coverageItem) { - Collection<Integer> testUnitNameIds = coverageItemToTestUnits.getValues(coverageItem); - if (testUnitNameIds == null) { - return ""; - } - return org.eclipse.osee.framework.jdk.core.util.Collections.toString(";", testUnitNameIds); - } - - @Override - public void removeTestUnit(CoverageItem coverageItem, String testUnitName) throws OseeCoreException { - removeTestUnits(coverageItem, Arrays.asList(testUnitName)); - } -} diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/ITestUnitStore.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/ITestUnitStore.java new file mode 100644 index 00000000000..e3d3b33571c --- /dev/null +++ b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/ITestUnitStore.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.coverage.store; + +import org.eclipse.osee.framework.core.exception.OseeCoreException; + +/** + * @author John Misinco + */ +public interface ITestUnitStore { + + public void load(TestUnitCache cache) throws OseeCoreException; + + public void store(TestUnitCache cache) throws OseeCoreException; + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/OseeCoverageUnitStore.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/OseeCoverageUnitStore.java index 1c63c95907e..8ea0d2744f4 100644 --- a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/OseeCoverageUnitStore.java +++ b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/OseeCoverageUnitStore.java @@ -13,7 +13,9 @@ package org.eclipse.osee.coverage.store; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.logging.Level; import org.eclipse.osee.coverage.event.CoverageChange; import org.eclipse.osee.coverage.event.CoverageEventType; @@ -24,6 +26,8 @@ import org.eclipse.osee.coverage.model.CoverageOptionManager; import org.eclipse.osee.coverage.model.CoverageOptionManagerDefault; import org.eclipse.osee.coverage.model.CoverageUnit; import org.eclipse.osee.coverage.model.ICoverage; +import org.eclipse.osee.coverage.model.ITestUnitProvider; +import org.eclipse.osee.framework.core.data.IOseeBranch; import org.eclipse.osee.framework.core.enums.ModificationType; import org.eclipse.osee.framework.core.exception.OseeCoreException; import org.eclipse.osee.framework.core.model.Branch; @@ -44,6 +48,8 @@ import org.eclipse.osee.framework.ui.plugin.util.Result; public class OseeCoverageUnitStore extends OseeCoverageStore { private final CoverageUnit coverageUnit; + private static final Map<IOseeBranch, ITestUnitProvider> testUnitProviderCache = + new HashMap<IOseeBranch, ITestUnitProvider>(); public OseeCoverageUnitStore(ICoverage parent, Artifact artifact, CoverageOptionManager coverageOptionManager) throws OseeCoreException { super(null, artifact.getArtifactType(), artifact.getBranch()); @@ -91,8 +97,7 @@ public class OseeCoverageUnitStore extends OseeCoverageStore { if (artifact != null) { for (String value : artifact.getAttributesToStringList(CoverageAttributeTypes.Item)) { CoverageItem item = - CoverageItem.createCoverageItem(coverageUnit, value, coverageOptionManager, - DbTestUnitProvider.instance()); + CoverageItem.createCoverageItem(coverageUnit, value, coverageOptionManager, getTestUnitProvider()); coverageUnit.addCoverageItem(item); } // Don't load file contents until needed @@ -113,6 +118,13 @@ public class OseeCoverageUnitStore extends OseeCoverageStore { } } + private ITestUnitProvider getTestUnitProvider() { + if (!testUnitProviderCache.containsKey(branch)) { + testUnitProviderCache.put(branch, new TestUnitCache(new ArtifactTestUnitStore(branch))); + } + return testUnitProviderCache.get(branch); + } + public void reloadItem(CoverageEventType eventType, CoverageItem currentCoverageItem, CoverageChange change, CoverageOptionManager coverageOptionManager) throws OseeCoreException { Artifact artifact = getArtifact(false); @@ -122,8 +134,7 @@ public class OseeCoverageUnitStore extends OseeCoverageStore { if (eventType == CoverageEventType.Modified) { for (String value : artifact.getAttributesToStringList(CoverageAttributeTypes.Item)) { CoverageItem dbChangedItem = - CoverageItem.createCoverageItem(coverageUnit, value, coverageOptionManager, - DbTestUnitProvider.instance()); + CoverageItem.createCoverageItem(coverageUnit, value, coverageOptionManager, getTestUnitProvider()); if (currentCoverageItem.getGuid().equals(dbChangedItem.getGuid())) { currentCoverageItem.copy(currentCoverageItem, dbChangedItem); } @@ -142,14 +153,12 @@ public class OseeCoverageUnitStore extends OseeCoverageStore { List<String> items = new ArrayList<String>(); for (CoverageItem coverageItem : coverageUnit.getCoverageItems()) { - if (!(coverageItem.getTestUnitProvider() instanceof DbTestUnitProvider)) { - // Get test names from coverageItem - Collection<String> testUnitNames = coverageItem.getTestUnits(); - // Set provider to db provider - coverageItem.setTestUnitProvider(DbTestUnitProvider.instance()); - // store off testUnitNames; this will add to db and replace names with db nameId - coverageItem.setTestUnits(testUnitNames); - } + // Get test names from coverageItem + Collection<String> testUnitNames = coverageItem.getTestUnits(); + // Set provider to db provider + coverageItem.setTestUnitProvider(getTestUnitProvider()); + // store off testUnitNames; this will add to db and replace names with db nameId + coverageItem.setTestUnits(testUnitNames); items.add(coverageItem.toXml()); } artifact.setAttributeValues(CoverageAttributeTypes.Item, items); diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/TestUnitCache.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/TestUnitCache.java new file mode 100644 index 00000000000..380b6a49e69 --- /dev/null +++ b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/TestUnitCache.java @@ -0,0 +1,200 @@ +/******************************************************************************* + * Copyright (c) 2011 Boeing. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Boeing - initial API and implementation + *******************************************************************************/ +package org.eclipse.osee.coverage.store; + +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map.Entry; +import java.util.Set; +import java.util.TreeSet; +import java.util.logging.Level; +import org.eclipse.osee.coverage.internal.Activator; +import org.eclipse.osee.coverage.model.CoverageItem; +import org.eclipse.osee.coverage.model.ITestUnitProvider; +import org.eclipse.osee.framework.core.exception.OseeCoreException; +import org.eclipse.osee.framework.jdk.core.type.HashCollection; +import org.eclipse.osee.framework.jdk.core.util.Collections; +import org.eclipse.osee.framework.logging.OseeLog; +import com.google.common.collect.HashBiMap; + +/** + * @author John Misinco + */ +public class TestUnitCache implements ITestUnitProvider { + + private final static int HASHMAP_SIZE = 3000; + private final HashCollection<CoverageItem, Integer> itemsToTestUnit = new HashCollection<CoverageItem, Integer>( + HASHMAP_SIZE); + + private int lastId; + private final HashBiMap<Integer, String> idToNameCache = new HashBiMap<Integer, String>(HASHMAP_SIZE); + + private final ITestUnitStore testUnitStore; + private boolean ensurePopulatedRanOnce; + private boolean cacheIsDirty; + + public TestUnitCache(ITestUnitStore testUnitStore) { + super(); + this.lastId = 0; + this.ensurePopulatedRanOnce = false; + this.cacheIsDirty = false; + this.testUnitStore = testUnitStore; + } + + private Integer getKey(String testUnitName) throws OseeCoreException { + ensurePopulated(); + Integer key = idToNameCache.inverse().get(testUnitName); + if (key == null) { + return ++lastId; + } else { + return key; + } + } + + public void put(String testUnitName) throws OseeCoreException { + ensurePopulated(); + int key = getKey(testUnitName); + put(key, testUnitName); + } + + public void put(Integer key, String testUnitName) throws OseeCoreException { + ensurePopulated(); + if (idToNameCache.containsKey(key)) { + OseeLog.format(Activator.class, Level.WARNING, "TestUnit key: [%s] has already been used", key); + } else if (idToNameCache.containsValue(testUnitName)) { + OseeLog.format(Activator.class, Level.WARNING, "TestUnit: [%s] has already been cached", testUnitName); + } else { + idToNameCache.put(key, testUnitName); + cacheIsDirty = true; + lastId = Math.max(lastId, key); + } + } + + public Set<Entry<Integer, String>> getAllCachedTestUnitEntries() throws OseeCoreException { + ensurePopulated(); + return idToNameCache.entrySet(); + } + + public Collection<String> getAllCachedTestUnitNames() throws OseeCoreException { + ensurePopulated(); + return idToNameCache.values(); + } + + private Collection<String> getTestUnitsHelper(CoverageItem coverageItem, String searchName) throws OseeCoreException { + ensurePopulated(); + Collection<Integer> entries = itemsToTestUnit.getValues(coverageItem); + Set<String> names = new LinkedHashSet<String>(); + if (entries != null) { + for (Integer entry : entries) { + if (idToNameCache.containsKey(entry)) { + String name = idToNameCache.get(entry); + names.add(name); + if (searchName != null && searchName.equals(name)) { + break; + } + } else { + names.add("UNRESOLVED_ID"); + } + } + return names; + } else { + return java.util.Collections.emptyList(); + } + } + + @Override + public Collection<String> getTestUnits(CoverageItem coverageItem) throws OseeCoreException { + return getTestUnitsHelper(coverageItem, null); + } + + @Override + public void addTestUnit(CoverageItem coverageItem, String testUnitName) throws OseeCoreException { + ensurePopulated(); + Collection<String> testUnitNames = getTestUnitsHelper(coverageItem, testUnitName); + if (!testUnitNames.contains(testUnitName)) { + if (idToNameCache.inverse().get(testUnitName) == null) { + put(testUnitName); + } + int key = idToNameCache.inverse().get(testUnitName); + itemsToTestUnit.put(coverageItem, key); + } + } + + @Override + public void removeTestUnit(CoverageItem coverageItem, String testUnitName) throws OseeCoreException { + ensurePopulated(); + Integer value = idToNameCache.inverse().get(testUnitName); + if (value != null) { + itemsToTestUnit.removeValue(coverageItem, value); + } else { + OseeLog.format(Activator.class, Level.WARNING, "TestUnitName: [%s] is not associated with CoverageItem: [%s]", + testUnitName, coverageItem.toString()); + } + } + + @Override + public void setTestUnits(CoverageItem coverageItem, Collection<String> testUnitNames) throws OseeCoreException { + ensurePopulated(); + Collection<String> entries = getTestUnits(coverageItem); + List<String> notAdded = Collections.setComplement(testUnitNames, entries); + for (String testUnitName : notAdded) { + Integer key = getKey(testUnitName); + put(key, testUnitName); + itemsToTestUnit.put(coverageItem, key); + } + } + + @Override + public String toXml(CoverageItem coverageItem) throws OseeCoreException { + String toReturn = ""; + Collection<Integer> values = itemsToTestUnit.getValues(coverageItem); + if (values != null) { + List<Integer> testIdEntries = (List<Integer>) values; + java.util.Collections.sort(testIdEntries); + toReturn = Collections.toString(";", testIdEntries); + persist(); + } + return toReturn; + } + + @Override + public void fromXml(CoverageItem coverageItem, String xml) { + itemsToTestUnit.removeValues(coverageItem); + Set<Integer> entries = new TreeSet<Integer>(); + String[] testUnitIds = xml.split(";"); + for (String key : testUnitIds) { + Integer iKey = new Integer(key); + entries.add(iKey); + } + itemsToTestUnit.put(coverageItem, entries); + } + + public void persist() throws OseeCoreException { + ensurePopulated(); + if (cacheIsDirty) { + testUnitStore.store(this); + cacheIsDirty = false; + } + } + + private synchronized void ensurePopulated() throws OseeCoreException { + if (!ensurePopulatedRanOnce) { + ensurePopulatedRanOnce = true; + try { + testUnitStore.load(this); + } catch (OseeCoreException ex) { + throw ex; + } + } + } + +}
\ No newline at end of file diff --git a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/TestUnitStore.java b/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/TestUnitStore.java deleted file mode 100644 index 26f3f70fb19..00000000000 --- a/plugins/org.eclipse.osee.coverage/src/org/eclipse/osee/coverage/store/TestUnitStore.java +++ /dev/null @@ -1,120 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2010 Boeing. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Boeing - initial API and implementation - *******************************************************************************/ -package org.eclipse.osee.coverage.store; - -import java.util.HashMap; -import java.util.Map; -import org.eclipse.osee.framework.core.exception.OseeCoreException; -import org.eclipse.osee.framework.database.core.ConnectionHandler; -import org.eclipse.osee.framework.database.core.IOseeStatement; - -/** - * @author Donald G. Dunne - */ -public class TestUnitStore { - private static String TEST_UNIT_NAME_QUERY = "select name from osee_cvg_testunits where name_id = ?"; - private static String TEST_UNIT_ID_QUERY = "select name_id from osee_cvg_testunits where name = ?"; - private static String INSERT_TEST_UNIT_NAME = "insert into osee_cvg_testunits (name_id, name) values (?,?)"; - private static String DELETE_ALL_TEST_NAMES = "delete from osee_cvg_testunits"; - private static String TEST_UNIT_COUNT_QUERY = "SELECT count(name) from osee_cvg_testunits"; - private static TestUnitStore instance = new TestUnitStore(); - final Map<String, Integer> nameToId = new HashMap<String, Integer>(1000); - final Map<Integer, String> idToName = new HashMap<Integer, String>(1000); - - private TestUnitStore() { - // private constructor - } - - private int addTestUnitNameToDb(String name) throws OseeCoreException { - IOseeStatement chStmt = ConnectionHandler.getStatement(); - int nextId = getNextTestUnitNameId(); - try { - chStmt.runCallableStatement(INSERT_TEST_UNIT_NAME, nextId, name); - } finally { - chStmt.close(); - } - return nextId; - } - - public static void clearStore() throws OseeCoreException { - instance.nameToId.clear(); - instance.idToName.clear(); - IOseeStatement chStmt = ConnectionHandler.getStatement(); - try { - chStmt.runCallableStatement(DELETE_ALL_TEST_NAMES); - } finally { - chStmt.close(); - } - } - - private int getNextTestUnitNameId() throws OseeCoreException { - return getTestUnitCount() + 1; - } - - public static int getTestUnitCount() throws OseeCoreException { - IOseeStatement chStmt = ConnectionHandler.getStatement(); - try { - chStmt.runPreparedQuery(TEST_UNIT_COUNT_QUERY); - chStmt.next(); - return chStmt.getInt(1); - } finally { - chStmt.close(); - } - } - - private void cacheName(String name, Integer idInt) { - nameToId.put(name, idInt); - idToName.put(idInt, name); - } - - public static Integer getTestUnitId(String name, boolean add) throws OseeCoreException { - if (instance.nameToId.containsKey(name)) { - return instance.nameToId.get(name); - } - IOseeStatement chStmt = ConnectionHandler.getStatement(); - try { - chStmt.runPreparedQuery(TEST_UNIT_ID_QUERY, name); - if (chStmt.next()) { - Integer nameId = chStmt.getInt(1); - instance.cacheName(name, nameId); - return nameId; - } - if (add) { - int nameId = instance.addTestUnitNameToDb(name); - instance.cacheName(name, nameId); - return nameId; - } else { - return null; - } - } finally { - chStmt.close(); - } - } - - public static String getTestUnitName(Integer nameId) throws OseeCoreException { - if (instance.idToName.containsKey(nameId)) { - return instance.idToName.get(nameId); - } - IOseeStatement chStmt = ConnectionHandler.getStatement(); - try { - chStmt.runPreparedQuery(TEST_UNIT_NAME_QUERY, nameId); - if (chStmt.next()) { - String name = chStmt.getString(1); - instance.cacheName(name, nameId); - return name; - } - } finally { - chStmt.close(); - } - return null; - } - -} |