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: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.refactoring.java;
029:
030: import java.io.File;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.util.Arrays;
034: import java.util.Collection;
035: import java.util.Comparator;
036: import javax.swing.text.BadLocationException;
037: import org.netbeans.api.java.source.ModificationResult;
038: import org.netbeans.modules.refactoring.api.AbstractRefactoring;
039: import org.netbeans.modules.refactoring.api.Problem;
040: import org.netbeans.modules.refactoring.api.RefactoringElement;
041: import org.netbeans.modules.refactoring.api.RefactoringElementImplBridge;
042: import org.netbeans.modules.refactoring.api.RefactoringSession;
043: import org.netbeans.spi.editor.highlighting.support.PositionsBag;
044: import org.openide.filesystems.FileUtil;
045: import org.openide.text.PositionBounds;
046:
047: /**
048: *
049: * @author Jiri Prox
050: */
051: public class RefactoringElementTestCase extends LogTestCase {
052:
053: public RefactoringElementTestCase(String name) {
054: super (name);
055: }
056:
057: protected void createClass(String name, String pack, String content) {
058: java.io.FileOutputStream os = null;
059: try {
060: File f = new java.io.File(getDataDir(),
061: "projects/default/src/" + pack.replace('.', '/')
062: + "/" + name + ".java");
063: f.getParentFile().mkdirs();
064: os = new java.io.FileOutputStream(f);
065: InputStream is = new java.io.ByteArrayInputStream(content
066: .getBytes("UTF-8"));
067: FileUtil.copy(is, os);
068: os.close();
069: is.close();
070: } catch (Exception ex) {
071: fail(ex.getMessage());
072: }
073: }
074:
075: private void dumpElements(Collection<RefactoringElement> elems) {
076:
077: RefactoringElement[] res = elems
078: .toArray(new RefactoringElement[] {});
079: Arrays.sort(res, new Comparator<RefactoringElement>() {
080:
081: public int compare(RefactoringElement o1,
082: RefactoringElement o2) {
083: PositionBounds p1 = o1.getPosition();
084: PositionBounds p2 = o2.getPosition();
085: int s1 = 0;
086: int s2 = 0;
087: if (p1 != null) {
088: s1 = p1.getBegin().getOffset();
089: }
090: if (p2 != null) {
091: s2 = p2.getBegin().getOffset();
092: }
093: if (s1 < s2)
094: return -1;
095: else if (s1 > s2)
096: return 1;
097: else
098: return 0;
099: }
100:
101: });
102: for (RefactoringElement refactoringElement : elems) {
103: ref("Display text: " + refactoringElement.getDisplayText());
104: ref("text: " + refactoringElement.getText());
105: ref("File: " + refactoringElement.getParentFile().getName());
106: ref("Position: "
107: + formatPositionBounds(refactoringElement
108: .getPosition()));
109: ref("Status: " + refactoringElement.getStatus());
110: ref("Class: "
111: + RefactoringElementImplBridge.getImpl(
112: refactoringElement).getClass().getName());
113: ModificationResult.Difference difference = refactoringElement
114: .getLookup().lookup(
115: ModificationResult.Difference.class);
116: if (difference != null) { //difference is only in type DiffElem
117: ref("Difference:" + difference);
118: }
119: ref("-----------------------------------");
120: }
121:
122: }
123:
124: private static String formatPositionBounds(PositionBounds pb) {
125: if (pb == null)
126: return null;
127: StringBuffer buf = new StringBuffer("Position bounds["); // NOI18N
128: try {
129: String content = pb.getText();
130: buf.append(pb.getBegin().getOffset());
131: buf.append(","); // NOI18N
132: buf.append(pb.getEnd().getOffset());
133: buf.append(",\""); // NOI18N
134: buf.append(content);
135: buf.append("\""); // NOI18N
136: } catch (IOException e) {
137: buf.append("Invalid: "); // NOI18N
138: buf.append(e.getMessage());
139: } catch (BadLocationException e) {
140: buf.append("Invalid: "); // NOI18N
141: buf.append(e.getMessage());
142: }
143: buf.append("]"); // NOI18N
144: return buf.toString();
145: }
146:
147: public boolean perform(AbstractRefactoring absRefactoring,
148: ParameterSetter parameterSetter, boolean perform) {
149: try {
150: Problem problem = absRefactoring.preCheck();
151: boolean fatal = false;
152: while (problem != null) {
153: ref.print(problem.getMessage());
154: fatal = fatal || problem.isFatal();
155: problem = problem.getNext();
156: }
157: if (fatal)
158: return false;
159: parameterSetter.setParameters();
160: problem = absRefactoring.fastCheckParameters();
161: while (problem != null) {
162: ref.print(problem.getMessage());
163: fatal = fatal || problem.isFatal();
164: problem = problem.getNext();
165: }
166: if (fatal)
167: return false;
168: problem = absRefactoring.checkParameters();
169: while (problem != null) {
170: ref.print(problem.getMessage());
171: fatal = fatal || problem.isFatal();
172: problem = problem.getNext();
173: }
174: if (fatal)
175: return false;
176: RefactoringSession rs = RefactoringSession
177: .create("Session");
178: absRefactoring.prepare(rs);
179: Collection<RefactoringElement> elems = rs
180: .getRefactoringElements();
181: dumpElements(elems);
182: if (perform)
183: rs.doRefactoring(true);
184: } catch (Exception e) {
185: e.printStackTrace(log);
186: fail();
187: }
188: return true;
189: }
190:
191: }
|