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 /plugins/org.eclipse.osee.coverage | |
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
Diffstat (limited to 'plugins/org.eclipse.osee.coverage')
11 files changed, 429 insertions, 270 deletions
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; - } - -} |