From 7f096d30b6f0ed36471488cc7b999f0b0557399f Mon Sep 17 00:00:00 2001 From: kwilk Date: Tue, 20 Dec 2011 13:33:01 -0700 Subject: bug[ats_61B71]: Short branch names ending in period cause exceptions during editing --- .../osee/framework/jdk/core/util/StringsTest.java | 86 +++++++++--- .../osee/framework/jdk/core/util/Strings.java | 149 ++++++++++++++------- .../ui/skynet/test/renderer/RenderingUtilTest.java | 29 ++-- .../framework/ui/skynet/render/RenderingUtil.java | 3 +- 4 files changed, 193 insertions(+), 74 deletions(-) diff --git a/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/util/StringsTest.java b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/util/StringsTest.java index 9a705a74847..aff96d8aa1c 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/util/StringsTest.java +++ b/plugins/org.eclipse.osee.framework.jdk.core.test/src/org/eclipse/osee/framework/jdk/core/util/StringsTest.java @@ -1,13 +1,3 @@ -/******************************************************************************* - * Copyright (c) 2004, 2007 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 - *******************************************************************************/ /******************************************************************************* * Copyright (c) 2004, 2007 Boeing. * All rights reserved. This program and the accompanying materials @@ -20,11 +10,12 @@ *******************************************************************************/ package org.eclipse.osee.framework.jdk.core.util; -import org.eclipse.osee.framework.jdk.core.util.Strings; +import java.util.Arrays; import org.junit.Assert; -import org.junit.Test; /** + * {@link Strings} + * * @author Donald G. Dunne */ public class StringsTest { @@ -38,16 +29,16 @@ public class StringsTest { String withDots = Strings.truncate(name, 20, true); Assert.assertEquals(20, Strings.truncate(withDots, 20).length()); - Assert.assertEquals(withDots, "Now is the time f..."); + Assert.assertEquals("Now is the time f...", withDots); } - @Test + @org.junit.Test public void testUnQuote() { String actual = Strings.unquote(null); Assert.assertNull(actual); - actual = Strings.unquote(""); - Assert.assertEquals("", actual); + actual = Strings.unquote(Strings.EMPTY_STRING); + Assert.assertEquals(Strings.EMPTY_STRING, actual); actual = Strings.unquote("hello"); Assert.assertEquals("hello", actual); @@ -56,15 +47,72 @@ public class StringsTest { Assert.assertEquals("hello", actual); } - @Test + @org.junit.Test public void testQuote() { String actual = Strings.quote(null); Assert.assertNull(actual); - actual = Strings.quote(""); - Assert.assertEquals("", actual); + actual = Strings.quote(Strings.EMPTY_STRING); + Assert.assertEquals(Strings.EMPTY_STRING, actual); actual = Strings.quote("hello"); Assert.assertEquals("\"hello\"", actual); } + + @org.junit.Test + public void testSafeReplace() { + String actual = Strings.saferReplace(null, null, null); + Assert.assertNull(actual); + + actual = Strings.saferReplace(Strings.EMPTY_STRING, Strings.EMPTY_STRING, Strings.EMPTY_STRING); + Assert.assertEquals(Strings.EMPTY_STRING, actual); + + actual = Strings.saferReplace("hello", "e", "o"); + Assert.assertEquals("hollo", actual); + + actual = Strings.saferReplace(".S.t.r.i.n.g.s.T.e.s.t..", "\\.", "_"); + Assert.assertEquals("_S_t_r_i_n_g_s_T_e_s_t__", actual); + } + + @org.junit.Test + public void test_BuildStatement() { + String actual = Strings.buildStatement(null, null); + Assert.assertNull(actual); + + actual = Strings.buildStatement(Arrays.asList(Strings.EMPTY_STRING), Strings.EMPTY_STRING); + Assert.assertEquals(Strings.EMPTY_STRING, actual); + + actual = Strings.buildStatement(Arrays.asList("hello"), "e"); + Assert.assertEquals("hello", actual); + + actual = Strings.buildStatement(Arrays.asList("hello", "hello"), "e"); + Assert.assertEquals("hello e hello", actual); + + actual = Strings.buildStatement(Arrays.asList("hello", "hello", "olleh"), "e"); + Assert.assertEquals("hello, hello e olleh", actual); + + actual = Strings.buildStatment(Arrays.asList("hello", "hello", "olleh")); + Assert.assertEquals("hello, hello and olleh", actual); + } + + @org.junit.Test + public void test_minimize() { + String actual = Strings.minimize(null); + Assert.assertNull(actual); + + actual = Strings.minimize(Strings.EMPTY_STRING); + Assert.assertEquals(Strings.EMPTY_STRING, actual); + + actual = Strings.minimize("hello"); + Assert.assertEquals("hello", actual); + + actual = Strings.minimize("hello\nhello\t\thello\nhello"); + Assert.assertEquals("hellohellohellohello", actual); + + actual = Strings.minimize(System.getProperty("line.separator")); + Assert.assertEquals(Strings.EMPTY_STRING, actual); + + actual = Strings.minimize("Test\r\ning\n\tstuff"); + Assert.assertEquals("Testingstuff", actual); + } } diff --git a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/Strings.java b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/Strings.java index 38f54ad83e8..39359105bf1 100644 --- a/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/Strings.java +++ b/plugins/org.eclipse.osee.framework.jdk.core/src/org/eclipse/osee/framework/jdk/core/util/Strings.java @@ -14,12 +14,22 @@ package org.eclipse.osee.framework.jdk.core.util; import java.util.List; /** + * {@link StringsTest} + * * @author Jeff C. Phillips * @author Don Dunne * @author Karol M. Wilk */ public class Strings { - private final static String EMPTY_STRING = ""; + + private static final String AMP = "&"; + private static final String DBL_AMP = AMP + AMP; + + private static final String SPACE_COMMON = "\n|\t|\r|" + System.getProperty("line.separator"); + private static final String AND = "and"; + private static final String STR = "%s%s%s"; + private static final String QUOTE_STR = "\""; + public static final String EMPTY_STRING = ""; private Strings() { // Utility class @@ -32,6 +42,10 @@ public class Strings { return value != null && value.length() > 0; } + public static String intern(String str) { + return (str == null) ? null : str.intern(); + } + public static boolean isValid(CharSequence... values) { for (CharSequence value : values) { if (value == null || value.length() == 0) { @@ -52,58 +66,96 @@ public class Strings { * @return a string with doubled ampersands. */ public static String escapeAmpersands(String stringWithAmp) { - return isValid(stringWithAmp) ? stringWithAmp.replace("&", "&&") : null; + return saferReplace(stringWithAmp, AMP, DBL_AMP); } - public static String intern(String str) { - return (str == null) ? null : str.intern(); + /** + *

+ * Remove all \n and \t. + *

+ */ + public static String minimize(String value) { + return saferReplace(value, SPACE_COMMON, EMPTY_STRING); + } + + /** + * NOTE isValid() check is only applied to inputStr. + * + * @param inputStr string to be evaluated + * @param target + * @param replacement + * @return returns modified, new version of inputStr or inputStr if it is not valid. + */ + public static String saferReplace(String inputStr, String target, String replacement) { + return isValid(inputStr) ? inputStr.replaceAll(target, replacement) : inputStr; + } + + /** + * Truncates at length, with no ellipsis. + */ + public static String truncate(String value, int length) { + return truncate(value, length, false); + } + + /** + * Trims ASCII value from end of str iff found. + * + * @param str "Requirement." + * @param value of the character from the ASCII charset, i.e. 0x2E for '.' + * @return "Requirement" + */ + public static String truncateEndChar(String str, int value) { + if (isValid(str) && value == str.charAt(str.length() - 1)) { + str = truncate(str, str.length() - 1); + } + return str; } /** * Will truncate string if necessary and add "..." to end if addDots and truncated */ public static String truncate(String value, int length, boolean ellipsis) { - if (value == null) { + if (!isValid(value)) { return emptyString(); } + String toReturn = value; - if (Strings.isValid(value) && value.length() > length) { + if (value.length() > length) { int len = ellipsis && length - 3 > 0 ? length - 3 : length; toReturn = value.substring(0, Math.min(length, len)) + (ellipsis ? "..." : emptyString()); } return toReturn; } - public static String truncate(String value, int length) { - return truncate(value, length, false); - } - public static String unquote(String nameReference) { - String toReturn = nameReference; - if (toReturn != null) { - toReturn = toReturn.trim(); - if (Strings.isValid(toReturn) && toReturn.startsWith("\"") && toReturn.endsWith("\"")) { - toReturn = toReturn.substring(1, toReturn.length() - 1); - } - } - return toReturn; + return wrapWith(nameReference, QUOTE_STR, true); } public static String quote(String nameReference) { - String toReturn = nameReference; - if (Strings.isValid(nameReference)) { - toReturn = String.format("\"%s\"", nameReference); - } - return toReturn; + return wrapWith(nameReference, QUOTE_STR, false); } /** - *

- * Remove all \n and \t. - *

+ * Wrap value with surroundStr.
+ * NOTE value will be trimmed of whitespace. + * + * @param value A + * @param surroundStr " + * @param unWrap reverse behavior. Takes out 2 * surroundStr.lenth() from value.length() + * @return "A" */ - public static String minimize(String value) { - return isValid(value) ? value.replaceAll("\n|\t", "") : value; + public static String wrapWith(String value, String surroundStr, boolean unWrap) { + if (isValid(value)) { + value = value.trim(); + if (unWrap) { + if (value.startsWith(surroundStr) && value.endsWith(surroundStr) && 2 * surroundStr.length() < value.length()) { + value = value.substring(surroundStr.length(), value.length() - surroundStr.length()); + } + } else { + value = String.format(STR, surroundStr, value, surroundStr); + } + } + return value; } /** @@ -112,32 +164,39 @@ public class Strings { * @return A, B, C joiningWord D */ public static String buildStatment(List items) { - return buildStatment(items, " and "); + return buildStatement(items, AND); } /** - * Provides a nicer list of items with an 'and' at the end. This could be done using iterator(). + * Provides a nicer list of items with an 'and' at the end.
+ * TODO:This could be done using iterator(). * * @param items Lists of form { apple, banana, orange } or { apple, banana } * @return string of form "apple, banana and orange" or "apple and banana" depending on size of list */ - public static String buildStatment(List items, String joiningWord) { - StringBuilder niceList = new StringBuilder(); - if (items.size() >= 2) { - int andIndex = items.size() - 2; - for (int itemIndex = 0; itemIndex < items.size(); itemIndex++) { - niceList.append(items.get(itemIndex)); - if (itemIndex == andIndex) { - niceList.append(joiningWord); - } else if (itemIndex < andIndex) { - niceList.append(", "); + public static String buildStatement(List items, String joiningWord) { + String statement = null; + if (items != null) { + StringBuilder niceList = new StringBuilder(); + if (items.size() >= 2) { + int andIndex = items.size() - 2; + for (int itemIndex = 0; itemIndex < items.size(); itemIndex++) { + niceList.append(items.get(itemIndex)); + if (itemIndex == andIndex) { + niceList.append(' '); + niceList.append(joiningWord); + niceList.append(' '); + } else if (itemIndex < andIndex) { + niceList.append(", "); + } + } + } else { + if (!items.isEmpty()) { + niceList.append(items.get(0)); } } - } else { - if (!items.isEmpty()) { - niceList.append(items.get(0)); - } + statement = niceList.toString(); } - return niceList.toString(); + return statement; } } diff --git a/plugins/org.eclipse.osee.framework.ui.skynet.test/src/org/eclipse/osee/framework/ui/skynet/test/renderer/RenderingUtilTest.java b/plugins/org.eclipse.osee.framework.ui.skynet.test/src/org/eclipse/osee/framework/ui/skynet/test/renderer/RenderingUtilTest.java index fe31b934551..153e2d4df01 100644 --- a/plugins/org.eclipse.osee.framework.ui.skynet.test/src/org/eclipse/osee/framework/ui/skynet/test/renderer/RenderingUtilTest.java +++ b/plugins/org.eclipse.osee.framework.ui.skynet.test/src/org/eclipse/osee/framework/ui/skynet/test/renderer/RenderingUtilTest.java @@ -23,34 +23,45 @@ import org.eclipse.osee.framework.core.model.mocks.MockOseeDataAccessor; import org.eclipse.osee.framework.jdk.core.util.GUID; import org.eclipse.osee.framework.ui.skynet.render.RenderingUtil; import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; /** * @author Roberto E. Escobar */ public class RenderingUtilTest { + + private static AbstractOseeCache cache; + private static Branch branch; + + @BeforeClass + public static void setUpOnce() throws OseeCoreException { + cache = new BranchCache(new MockOseeDataAccessor()); + branch = createBranch(cache, GUID.create(), "Test 1", 1); + } + @Test public void testBranchToFileName() throws Exception { - AbstractOseeCache cache = new TestCache(); - Branch branch = createBranch(cache, GUID.create(), "Test 1", 1); String actual = RenderingUtil.toFileName(branch); Assert.assertEquals(encode(branch.getShortName()), actual); } + @Test + public void test_branchToFileName_notAllowedCharsInName() throws OseeCoreException { + branch.setName("0123455789012345578901234557890123.5"); + String branchShortName = RenderingUtil.toFileName(branch); + Assert.assertEquals("Not safe character found at end of branch name.", "0123455789012345578901234557890123_", + branchShortName); + } + private String encode(String guid) throws UnsupportedEncodingException { return URLEncoder.encode(guid, "UTF-8"); } - private Branch createBranch(AbstractOseeCache cache, String guid, String name, int id) throws OseeCoreException { + private static Branch createBranch(AbstractOseeCache cache, String guid, String name, int id) throws OseeCoreException { Branch branch = new BranchFactory().create(guid, name, BranchType.WORKING, BranchState.MODIFIED, false); Assert.assertNotNull(branch); branch.setId(id); return branch; } - - private final class TestCache extends BranchCache { - public TestCache() { - super(new MockOseeDataAccessor()); - } - } } \ No newline at end of file diff --git a/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/RenderingUtil.java b/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/RenderingUtil.java index 1dbbfadbe26..d280793fece 100644 --- a/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/RenderingUtil.java +++ b/plugins/org.eclipse.osee.framework.ui.skynet/src/org/eclipse/osee/framework/ui/skynet/render/RenderingUtil.java @@ -195,7 +195,8 @@ public final class RenderingUtil { } public static String toFileName(IOseeBranch branch) throws OseeCoreException { - return encode(Branch.getShortName(branch)); + String shortName = Strings.saferReplace(Branch.getShortName(branch), "\\.", "_"); + return encode(shortName); } private static String encode(String name) throws OseeCoreException { -- cgit v1.2.3