CachingMetadataReaderLeakTests.java 2.3 KB
Newer Older
C
Chris Beams 已提交
1
/*
2
 * Copyright 2002-2014 the original author or authors.
3
 *
C
Chris Beams 已提交
4 5 6
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
7
 *
C
Chris Beams 已提交
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
C
Chris Beams 已提交
10 11 12 13 14 15 16
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

17
package org.springframework.core.type;
C
Chris Beams 已提交
18 19 20 21 22 23 24 25 26

import java.net.URL;

import org.junit.Test;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
27 28
import org.springframework.tests.Assume;
import org.springframework.tests.TestGroup;
C
Chris Beams 已提交
29

30 31 32
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

C
Chris Beams 已提交
33
/**
34 35 36
 * Unit tests for checking the behaviour of {@link CachingMetadataReaderFactory} under
 * load. If the cache is not controlled, this test should fail with an out of memory
 * exception around entry 5k.
37
 *
C
Chris Beams 已提交
38
 * @author Costin Leau
39
 * @author Sam Brannen
C
Chris Beams 已提交
40
 */
41
public class CachingMetadataReaderLeakTests {
C
Chris Beams 已提交
42

43
	private static final int ITEMS_TO_LOAD = 9999;
C
Chris Beams 已提交
44

45
	private final MetadataReaderFactory mrf = new CachingMetadataReaderFactory();
C
Chris Beams 已提交
46 47 48

	@Test
	public void testSignificantLoad() throws Exception {
49 50
		Assume.group(TestGroup.LONG_RUNNING);

C
Chris Beams 已提交
51 52 53 54 55
		// the biggest public class in the JDK (>60k)
		URL url = getClass().getResource("/java/awt/Component.class");
		assertThat(url, notNullValue());

		// look at a LOT of items
56
		for (int i = 0; i < ITEMS_TO_LOAD; i++) {
C
Chris Beams 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
			Resource resource = new UrlResource(url) {

				@Override
				public boolean equals(Object obj) {
					return (obj == this);
				}

				@Override
				public int hashCode() {
					return System.identityHashCode(this);
				}
			};

			MetadataReader reader = mrf.getMetadataReader(resource);
			assertThat(reader, notNullValue());
		}

		// useful for profiling to take snapshots
75
		// System.in.read();
C
Chris Beams 已提交
76
	}
77

C
Chris Beams 已提交
78
}