summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSean Kennedy2013-08-14 17:18:17 (EDT)
committer Sean Kennedy2013-08-15 13:27:43 (EDT)
commit9e842013efebd38e3c07cebe66668bf7845f5252 (patch)
treeeb9b082939efb6b4cd60dc27e0542268575c799a
parent48deb80093b8510d50487e2dfa725ef43bdf52e4 (diff)
downloadorg.eclipse.lyo.rio-9e842013efebd38e3c07cebe66668bf7845f5252.zip
org.eclipse.lyo.rio-9e842013efebd38e3c07cebe66668bf7845f5252.tar.gz
org.eclipse.lyo.rio-9e842013efebd38e3c07cebe66668bf7845f5252.tar.bz2
bug 413939: protect against NPE when generating random textrefs/changes/13/15513/1
also did some refactoring for readability Signed-off-by: Sean Kennedy <seanpk@ca.ibm.com> Change-Id: I3594cfe70a17f20f078c4c3267550aaec75cfd99
-rw-r--r--org.eclipse.lyo.rio.cm/src/main/java/org/eclipse/lyo/rio/cm/web/ChangeRequestGeneratorService.java2
-rw-r--r--org.eclipse.lyo.rio.core/src/main/java/org/eclipse/lyo/rio/util/RandomTextGenerator.java44
2 files changed, 21 insertions, 25 deletions
diff --git a/org.eclipse.lyo.rio.cm/src/main/java/org/eclipse/lyo/rio/cm/web/ChangeRequestGeneratorService.java b/org.eclipse.lyo.rio.cm/src/main/java/org/eclipse/lyo/rio/cm/web/ChangeRequestGeneratorService.java
index 30f4877..64d7c13 100644
--- a/org.eclipse.lyo.rio.cm/src/main/java/org/eclipse/lyo/rio/cm/web/ChangeRequestGeneratorService.java
+++ b/org.eclipse.lyo.rio.cm/src/main/java/org/eclipse/lyo/rio/cm/web/ChangeRequestGeneratorService.java
@@ -46,9 +46,9 @@ public class ChangeRequestGeneratorService extends RioBaseService {
RioStore store = this.getStore();
try{
+ RandomTextGenerator gen = new RandomTextGenerator();
for(int i=0;i<count;i++) {
int titleLen = rnd.nextInt(3) + 2;
- RandomTextGenerator gen = new RandomTextGenerator();
String title = gen.generateText(titleLen);
int descriptionLen = rnd.nextInt(50) + 50;
String description = gen.generateText(descriptionLen);
diff --git a/org.eclipse.lyo.rio.core/src/main/java/org/eclipse/lyo/rio/util/RandomTextGenerator.java b/org.eclipse.lyo.rio.core/src/main/java/org/eclipse/lyo/rio/util/RandomTextGenerator.java
index 068c70f..a13e785 100644
--- a/org.eclipse.lyo.rio.core/src/main/java/org/eclipse/lyo/rio/util/RandomTextGenerator.java
+++ b/org.eclipse.lyo.rio.core/src/main/java/org/eclipse/lyo/rio/util/RandomTextGenerator.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2011 IBM Corporation.
+ * Copyright (c) 2011, 2013 IBM Corporation.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@@ -27,33 +27,30 @@ import java.util.StringTokenizer;
public class RandomTextGenerator {
- private Map<String,List<String>> index = new HashMap<String,List<String>>();
+ private Map<String, List<String>> drawers = new HashMap<String,List<String>>();
+ private ArrayList<String> keys;
static private Random rnd = new Random(System.nanoTime());
public RandomTextGenerator() throws IOException{
- index();
+ fillDrawers();
}
public String generateText(int words){
StringBuilder sb = new StringBuilder();
- ArrayList<String> wordList = new ArrayList<String>();
- wordList.addAll(index.keySet());
- int start = rnd.nextInt(index.size());
- String word = wordList.get(start);
+ String word = keys.get(rnd.nextInt(keys.size()));
sb.append(word + ' ');
- int count = 0;
- while( count < words ) {
- List<String> afters = index.get(word);
- int next = rnd.nextInt(afters.size());
- word = afters.get(next);
- sb.append(word + ' ');
- count++;
+ List<String> afters = drawers.get(word);
+ if (afters != null && afters.size() > 0) {
+ for(int count = 0; count < words; count++) {
+ int next = rnd.nextInt(afters.size());
+ word = afters.get(next);
+ sb.append(word + ' ');
+ }
}
-
return sb.toString().trim();
}
- private void index() throws IOException{
+ private void fillDrawers() throws IOException{
InputStream is = this.getClass().getResourceAsStream("filler.txt"); //$NON-NLS-1$
StringBuilder sb = new StringBuilder();
BufferedReader reader = new BufferedReader( new InputStreamReader(is, "UTF-8")); //$NON-NLS-1$
@@ -67,19 +64,18 @@ public class RandomTextGenerator {
}
String str = sb.toString();
StringTokenizer st = new StringTokenizer(str," \n\t\r"); //$NON-NLS-1$
- String prevToken = null;
+ String prevToken = st.nextToken();
while( st.hasMoreTokens() ) {
String token = st.nextToken();
- if( prevToken != null ) {
- List<String> list = index.get(prevToken);
- if( list == null ) {
- list = new ArrayList<String>();
- index.put(prevToken, list);
- }
- list.add(token);
+ List<String> list = drawers.get(prevToken);
+ if( list == null ) {
+ list = new ArrayList<String>();
+ drawers.put(prevToken, list);
}
+ list.add(token);
prevToken = token;
}
+ keys = new ArrayList<String>(drawers.keySet());
}