This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 137069
Collapse All | Expand All

(-)a/nbjunit/src/org/netbeans/junit/NbTestCase.java (+12 lines)
Lines 125-130 Link Here
125
     * @return true if the test can run
125
     * @return true if the test can run
126
     */
126
     */
127
    public boolean canRun() {
127
    public boolean canRun() {
128
        if (NbTestSuite.ignoreRandomFailures()) {
129
            if (getClass().isAnnotationPresent(Random.class)) {
130
                return false;
131
            }
132
            try {
133
                if (getClass().getMethod(getName()).isAnnotationPresent(Random.class)) {
134
                    return false;
135
                }
136
            } catch (NoSuchMethodException x) {
137
                // Specially named methods; let it pass.
138
            }
139
        }
128
        if (null == filter) {
140
        if (null == filter) {
129
            //System.out.println("NBTestCase.canRun(): filter == null name=" + name ());
141
            //System.out.println("NBTestCase.canRun(): filter == null name=" + name ());
130
            return true; // no filter was aplied
142
            return true; // no filter was aplied
(-)a/nbjunit/src/org/netbeans/junit/NbTestSuite.java (-1 / +25 lines)
Lines 44-49 Link Here
44
import java.util.*;
44
import java.util.*;
45
import junit.framework.TestSuite;
45
import junit.framework.TestSuite;
46
import junit.framework.Test;
46
import junit.framework.Test;
47
import junit.framework.TestCase;
47
48
48
/**
49
/**
49
 * NetBeans extension to JUnit's TestSuite class.
50
 * NetBeans extension to JUnit's TestSuite class.
Lines 52-57 Link Here
52
53
53
54
54
    private Filter fFilter;
55
    private Filter fFilter;
56
57
    static boolean ignoreRandomFailures() {
58
        return Boolean.getBoolean("ignore.random.failures");
59
    }
55
60
56
    /**
61
    /**
57
     * Constructs an empty TestSuite.
62
     * Constructs an empty TestSuite.
Lines 66-72 Link Here
66
     *
71
     *
67
     */
72
     */
68
    public NbTestSuite(Class theClass) {       
73
    public NbTestSuite(Class theClass) {       
69
        super(theClass);
74
        super(testCaseClassOrDummy(theClass));
75
    }
76
    private static Class testCaseClassOrDummy(Class testClass) {
77
        if (ignoreRandomFailures() && ((Class<?>) testClass).isAnnotationPresent(Random.class)) {
78
            return APIJail.Dummy.class;
79
        } else {
80
            return testClass;
81
        }
82
    }
83
    private static class APIJail {
84
        public static class Dummy extends TestCase {
85
            public Dummy(String name) {
86
                super(name);
87
            }
88
            public void testNothing() {}
89
        }
70
    }
90
    }
71
91
72
    /**
92
    /**
Lines 95-100 Link Here
95
     * adds a test suite to this test suite
115
     * adds a test suite to this test suite
96
     */
116
     */
97
    public void addTestSuite(Class testClass) {
117
    public void addTestSuite(Class testClass) {
118
        // XXX should be expecting Class<? extends TestCase>, perhaps
119
        if (ignoreRandomFailures() && ((Class<?>) testClass).isAnnotationPresent(Random.class)) {
120
            return;
121
        }
98
        NbTest t = new NbTestSuite(testClass);
122
        NbTest t = new NbTestSuite(testClass);
99
        t.setFilter(fFilter);
123
        t.setFilter(fFilter);
100
        addTest(t);
124
        addTest(t);
(-)a/nbjunit/src/org/netbeans/junit/Random.java (+64 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.junit;
41
42
import java.lang.annotation.ElementType;
43
import java.lang.annotation.Inherited;
44
import java.lang.annotation.Retention;
45
import java.lang.annotation.RetentionPolicy;
46
import java.lang.annotation.Target;
47
48
/**
49
 * Indicates that this test can fail randomly.
50
 * <p>When used on a method in a class extending {@link NbTestCase},
51
 * the implicit suite will exclude this test in case the system property
52
 * <code>ignore.random.failures</code> is set to <code>true</code>.
53
 * When used on a class passed to {@link NbTestSuite#NbTestSuite(Class)} or {@link NbTestSuite#addTestSuite(Class)},
54
 * the suite will be empty if the system property is set;
55
 * the same if it is used on a class extending {@link NbTestCase}.
56
 * <p>Test runs which must be reliable should define the system property.
57
 * (E.g. for NetBeans modules: <code>ant -Dtest-unit-sys-prop.ignore.random.failures=true test</code>)
58
 * Developers running tests interactively should not.
59
 * @since XXX
60
 */
61
@Retention(RetentionPolicy.RUNTIME)
62
@Target({ElementType.TYPE, ElementType.METHOD})
63
@Inherited
64
public @interface Random {}
(-)a/nbjunit/test/unit/src/org/netbeans/junit/RandomTest.java (+145 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of either the GNU
7
 * General Public License Version 2 only ("GPL") or the Common
8
 * Development and Distribution License("CDDL") (collectively, the
9
 * "License"). You may not use this file except in compliance with the
10
 * License. You can obtain a copy of the License at
11
 * http://www.netbeans.org/cddl-gplv2.html
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
13
 * specific language governing permissions and limitations under the
14
 * License.  When distributing the software, include this License Header
15
 * Notice in each file and include the License file at
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
17
 * particular file as subject to the "Classpath" exception as provided
18
 * by Sun in the GPL Version 2 section of the License file that
19
 * accompanied this code. If applicable, add the following below the
20
 * License Header, with the fields enclosed by brackets [] replaced by
21
 * your own identifying information:
22
 * "Portions Copyrighted [year] [name of copyright owner]"
23
 *
24
 * If you wish your version of this file to be governed by only the CDDL
25
 * or only the GPL Version 2, indicate your decision by adding
26
 * "[Contributor] elects to include this software in this distribution
27
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
28
 * single choice of license, a recipient has the option to distribute
29
 * your version of this file under either the CDDL, the GPL Version 2 or
30
 * to extend the choice of license to its licensees as provided above.
31
 * However, if you add GPL Version 2 code and therefore, elected the GPL
32
 * Version 2 license, then the option applies only if the new code is
33
 * made subject to such option by the copyright holder.
34
 *
35
 * Contributor(s):
36
 *
37
 * Portions Copyrighted 2008 Sun Microsystems, Inc.
38
 */
39
40
package org.netbeans.junit;
41
42
import java.util.ArrayList;
43
import java.util.Arrays;
44
import java.util.Collections;
45
import java.util.Enumeration;
46
import java.util.List;
47
import junit.framework.Test;
48
import junit.framework.TestCase;
49
import junit.framework.TestResult;
50
import junit.runner.BaseTestRunner;
51
import junit.textui.TestRunner;
52
53
public class RandomTest extends TestCase {
54
55
    private static List<Integer> runs;
56
57
    public RandomTest(String name) {
58
        super(name);
59
    }
60
61
    public static class One extends NbTestCase {
62
        public One(String n) {super(n);}
63
        public void testReliable() {runs.add(10);}
64
        @Random public void testUnreliable() {runs.add(11);}
65
    }
66
67
    public static class Two extends NbTestCase {
68
        public Two(String n) {super(n);}
69
        public void testReliable() {runs.add(20);}
70
    }
71
72
    @Random public static class Three extends NbTestCase {
73
        public Three(String n) {super(n);}
74
        public void testUnreliable() {runs.add(30);}
75
    }
76
77
    public static class Four extends NbTestCase {
78
        public Four(String n) {super(n);}
79
        public void testReliable() {runs.add(40);}
80
        @Random public void testUnreliable() {runs.add(41);}
81
    }
82
83
    @Random public static class Five extends NbTestCase {
84
        public Five(String n) {super(n);}
85
        public void testUnreliable() {runs.add(50);}
86
    }
87
88
    public static class Six {
89
        public static Test suite() {return new NbTestSuite(Four.class);}
90
    }
91
92
    public static class Seven {
93
        public static Test suite() {return new NbTestSuite(Five.class);}
94
    }
95
96
    public static class Eight {
97
        public static Test suite() {
98
            NbTestSuite suite = new NbTestSuite();
99
            suite.addTestSuite(Four.class);
100
            suite.addTestSuite(Five.class);
101
            return suite;
102
        }
103
    }
104
105
    private void run(Class... tests) {
106
        runs = new ArrayList<Integer>();
107
        BaseTestRunner runner = new TestRunner();
108
        for (Class test : tests) {
109
            TestResult result = new TestResult();
110
            //result.addListener(new ResultPrinter(System.err));
111
            runner.getTest(test.getName()).run(result);
112
            assertEquals("failures in " + test, Collections.emptyList(), Collections.list((Enumeration<?>) result.failures()));
113
            assertEquals("errors in " + test, Collections.emptyList(), Collections.list((Enumeration<?>) result.errors()));
114
        }
115
    }
116
117
    private void runAll() throws Exception {
118
        run(One.class, Two.class, Three.class, Six.class, Seven.class, Eight.class);
119
    }
120
121
    public void testRegularMode() throws Exception {
122
        runAll();
123
        assertEquals(Arrays.asList(10, 11, 20, 30, 40, 41, 50, 40, 41, 50), runs);
124
    }
125
126
    public void testIgnoreRandomFailuresMode() throws Exception {
127
        System.setProperty("ignore.random.failures", "true");
128
        runAll();
129
        assertEquals(Arrays.asList(10, 20, 40, 40), runs);
130
    }
131
132
    public static class Sub extends Three {
133
        public Sub(String n) {super(n);}
134
    }
135
136
    public void testHeritability() throws Exception {
137
        System.setProperty("ignore.random.failures", "false");
138
        run(Sub.class);
139
        assertEquals(Collections.singletonList(30), runs);
140
        System.setProperty("ignore.random.failures", "true");
141
        run(Sub.class);
142
        assertEquals(Collections.emptyList(), runs);
143
    }
144
145
}
(-)a/settings/test/unit/src/org/netbeans/modules/settings/RecognizeInstanceObjectsTest.java (-19 / +4 lines)
Lines 41-55 Link Here
41
41
42
package org.netbeans.modules.settings;
42
package org.netbeans.modules.settings;
43
43
44
import junit.framework.Test;
45
import junit.framework.TestResult;
46
import junit.framework.TestSuite;
47
import org.netbeans.core.startup.layers.NamedFSServicesLookupTest;
44
import org.netbeans.core.startup.layers.NamedFSServicesLookupTest;
48
45
import org.netbeans.junit.Random;
49
46
50
/** Test finding services from manifest and .instance files using FolderLookup.
47
/** Test finding services from manifest and .instance files using FolderLookup.
51
 * @author Jaroslav Tulach
48
 * @author Jaroslav Tulach
52
 */
49
 */
50
// From time to time RecognizeInstanceObjectsTest.testOrderingAttributes fails, no idea why. -jglick
51
// And in #417 all the tests deadlocked somewhere in folder ordering, apparently at random.
52
@Random
53
public class RecognizeInstanceObjectsTest extends NamedFSServicesLookupTest{
53
public class RecognizeInstanceObjectsTest extends NamedFSServicesLookupTest{
54
    public RecognizeInstanceObjectsTest(String name) {
54
    public RecognizeInstanceObjectsTest(String name) {
55
        super(name);
55
        super(name);
Lines 60-78 Link Here
60
        return 20000;
60
        return 20000;
61
    }
61
    }
62
62
63
    public static Test suite() {
64
        // From time to time RecognizeInstanceObjectsTest.testOrderingAttributes fails, no idea why. -jglick
65
        // And in #417 all the tests deadlocked somewhere in folder ordering, apparently at random.
66
        if (Boolean.getBoolean("ignore.random.failures")) {
67
            return new Test() {
68
                public int countTestCases() {
69
                    return 0;
70
                }
71
                public void run(TestResult r) {}
72
            };
73
        } else {
74
            return new TestSuite(RecognizeInstanceObjectsTest.class);
75
        }
76
    }
77
78
}
63
}
(-)a/settings/test/unit/src/org/netbeans/modules/settings/convertors/LayersTest.java (-7 / +5 lines)
Lines 51-56 Link Here
51
import java.util.logging.Level;
51
import java.util.logging.Level;
52
import org.netbeans.junit.Log;
52
import org.netbeans.junit.Log;
53
import org.netbeans.junit.NbTestCase;
53
import org.netbeans.junit.NbTestCase;
54
import org.netbeans.junit.Random;
54
import org.openide.filesystems.FileObject;
55
import org.openide.filesystems.FileObject;
55
import org.openide.filesystems.FileSystem;
56
import org.openide.filesystems.FileSystem;
56
import org.openide.filesystems.Repository;
57
import org.openide.filesystems.Repository;
Lines 107-120 Link Here
107
        }
108
        }
108
    }
109
    }
109
    
110
    
111
    // This is supposed to be run in ide mode.
112
    // It is meaningless if run in code mode,
113
    // and it will furthermore fail if the internet connection is down.
114
    @Random
110
    public void testCorrectContentOfSettingsFiles() throws Exception {
115
    public void testCorrectContentOfSettingsFiles() throws Exception {
111
        if (Boolean.getBoolean("ignore.random.failures")) {
112
            // This is supposed to be run in ide mode.
113
            // It is meaningless if run in code mode,
114
            // and it will furthermore fail if the internet connection is down.
115
            return;
116
        }
117
118
        ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
116
        ClassLoader l = Lookup.getDefault().lookup(ClassLoader.class);
119
        assertNotNull ("In the IDE mode, there always should be a classloader", l);
117
        assertNotNull ("In the IDE mode, there always should be a classloader", l);
120
        
118
        

Return to bug 137069