001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.search;
043:
044: import java.lang.reflect.Constructor;
045: import java.lang.reflect.Field;
046: import org.netbeans.junit.NbTestCase;
047:
048: /**
049: *
050: * @author Marian Petras
051: */
052: public class FindDialogMemoryTest extends NbTestCase {
053:
054: private FindDialogMemory memory;
055:
056: public FindDialogMemoryTest() {
057: super ("FindDialogMemoryTest");
058: }
059:
060: @Override
061: public void setUp() throws Exception {
062: Constructor<FindDialogMemory> constructor = FindDialogMemory.class
063: .getDeclaredConstructor();
064: constructor.setAccessible(true);
065: memory = constructor.newInstance();
066: }
067:
068: public void testFileNamePatternStorage() throws Exception {
069: assertNotNull(memory.getFileNamePatterns());
070: assertTrue(memory.getFileNamePatterns().isEmpty());
071:
072: final String elem0 = "abc";
073: final String elem1 = "KLM";
074: final String elem2 = "123";
075: final String fillPrefix = "fill-";
076:
077: /* check that the item has been added */
078: memory.storeFileNamePattern(elem0);
079: assertEquals(1, memory.getFileNamePatterns().size());
080: assertEquals(elem0, memory.getFileNamePatterns().get(0));
081:
082: /* check that a one-item list is unchanged after adding a duplicate: */
083: memory.storeFileNamePattern(elem0);
084: assertEquals(1, memory.getFileNamePatterns().size());
085: assertEquals(elem0, memory.getFileNamePatterns().get(0));
086:
087: /* check that a new unique item is added to the tail of the list: */
088: memory.storeFileNamePattern(elem1);
089: assertEquals(2, memory.getFileNamePatterns().size());
090: assertEquals(elem0, memory.getFileNamePatterns().get(0));
091: assertEquals(elem1, memory.getFileNamePatterns().get(1));
092:
093: /* check that a non-unique item is just moved to the end of the list: */
094: memory.storeFileNamePattern(elem0);
095: assertEquals(2, memory.getFileNamePatterns().size());
096: assertEquals(elem1, memory.getFileNamePatterns().get(0));
097: assertEquals(elem0, memory.getFileNamePatterns().get(1));
098:
099: /* check that a third unique item is actually added to the list: */
100: memory.storeFileNamePattern(elem2);
101: assertEquals(3, memory.getFileNamePatterns().size());
102: assertEquals(elem1, memory.getFileNamePatterns().get(0));
103: assertEquals(elem0, memory.getFileNamePatterns().get(1));
104: assertEquals(elem2, memory.getFileNamePatterns().get(2));
105:
106: /*
107: * check that the list is unchanged after an attempt to re-add
108: * an element that is already at the tail of the list:
109: */
110: memory.storeFileNamePattern(elem2);
111: assertEquals(3, memory.getFileNamePatterns().size());
112: assertEquals(elem1, memory.getFileNamePatterns().get(0));
113: assertEquals(elem0, memory.getFileNamePatterns().get(1));
114: assertEquals(elem2, memory.getFileNamePatterns().get(2));
115:
116: /* check that a middle element is moved to the tail if re-added: */
117: memory.storeFileNamePattern(elem0);
118: assertEquals(3, memory.getFileNamePatterns().size());
119: assertEquals(elem1, memory.getFileNamePatterns().get(0));
120: assertEquals(elem2, memory.getFileNamePatterns().get(1));
121: assertEquals(elem0, memory.getFileNamePatterns().get(2));
122:
123: /* check that the first element is moved to the tail if re-added: */
124: memory.storeFileNamePattern(elem1);
125: assertEquals(3, memory.getFileNamePatterns().size());
126: assertEquals(elem2, memory.getFileNamePatterns().get(0));
127: assertEquals(elem0, memory.getFileNamePatterns().get(1));
128: assertEquals(elem1, memory.getFileNamePatterns().get(2));
129:
130: /* check that the list can contain the specified number of elements: */
131: int currCount = memory.getFileNamePatterns().size();
132: int maxCount = getMaxFileNameCount();
133: int i = 0;
134: while (i < maxCount - currCount) {
135: memory.storeFileNamePattern(String.format("%s%03d",
136: fillPrefix, i++));
137: }
138: assertEquals(elem2, memory.getFileNamePatterns().get(0));
139: assertEquals(elem0, memory.getFileNamePatterns().get(1));
140: assertEquals(elem1, memory.getFileNamePatterns().get(2));
141: for (int j = currCount; j < maxCount; j++) {
142: assertEquals(String.format("%s%03d", fillPrefix, j
143: - currCount), memory.getFileNamePatterns().get(j));
144: }
145:
146: /*
147: * check that the oldest elements are removed if there would not be
148: * enough space for new elements:
149: */
150: while (i < maxCount) {
151: memory.storeFileNamePattern(String.format("%s%03d",
152: fillPrefix, i++));
153: }
154: for (int j = 0; j < maxCount; j++) {
155: assertEquals(String.format("%s%03d", fillPrefix, j), memory
156: .getFileNamePatterns().get(j));
157: }
158:
159: }
160:
161: private static int getMaxFileNameCount() throws Exception {
162: Field field = FindDialogMemory.class
163: .getDeclaredField("maxFileNamePatternCount");
164: field.setAccessible(true);
165: return field.getInt(null);
166: }
167:
168: }
|