Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
rhbz1002774 - change HTextFlow.setContents logic to ensure correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Huang committed Sep 6, 2013
1 parent ce5e911 commit cf9c5fd
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 10 deletions.
24 changes: 14 additions & 10 deletions zanata-model/src/main/java/org/zanata/model/HTextFlow.java
Expand Up @@ -282,13 +282,22 @@ public List<String> getContents()
return contents;
}

public void setContents(List<String> contents)
public void setContents(List<String> newContents)
{
if(!Objects.equal(contents, this.getContents()))
if (!newContents.equals(this.getContents()))
{
for( int i=0; i<contents.size(); i++ )
int newContentSize = newContents.size();
for (int i = 0; i < newContentSize; i++)
{
this.setContent(i, contents.get(i));
this.setContent(i, newContents.get(i));
}
int difference = getContents().size() - newContentSize;
if (difference > 0)
{
for (int i = newContentSize; i < newContentSize + difference; i++)
{
setContent(i, null);
}
}
updateContentHash();
updateWordCount();
Expand Down Expand Up @@ -510,12 +519,7 @@ private void preUpdate()
if (!isPlural())
{
// if plural form has changed, we need to clear out obsolete contents
List<String> newContents = Lists.newArrayList(content0);
for (int i = 1; i < MAX_PLURALS; i++)
{
newContents.add(null);
}
setContents(newContents);
setContents(content0);
}
}
}
Expand Down
53 changes: 53 additions & 0 deletions zanata-model/src/test/java/org/zanata/model/HTextFlowTest.java
@@ -0,0 +1,53 @@
/*
* Copyright 2013, Red Hat, Inc. and individual contributors as indicated by the
* @author tags. See the copyright.txt file in the distribution for a full
* listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this software; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
* site: http://www.fsf.org.
*/

package org.zanata.model;

import org.hamcrest.Matchers;
import org.testng.annotations.Test;

import static org.hamcrest.MatcherAssert.assertThat;

/**
* @author Patrick Huang <a href="mailto:pahuang@redhat.com">pahuang@redhat.com</a>
*/
@Test(groups = "unit-tests")
public class HTextFlowTest
{
@Test
public void testSetContents() throws Exception
{
HTextFlow textFlow = new HTextFlow();

textFlow.setContents("a", "b");

assertThat(textFlow.getContents(), Matchers.contains("a", "b"));

textFlow.setContents("a");

assertThat(textFlow.getContents(), Matchers.contains("a"));
assertThat(textFlow.getContent1(), Matchers.nullValue());

textFlow.setContents("a", "b");
assertThat(textFlow.getContents(), Matchers.contains("a", "b"));

}
}

0 comments on commit cf9c5fd

Please sign in to comment.